Checking whether a initSetMethod exists before bluntly invoking it. (fixes a new...
[quassel.git] / src / common / buffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "buffersyncer.h"
22
23 INIT_SYNCABLE_OBJECT(BufferSyncer)
24 BufferSyncer::BufferSyncer(QObject *parent)
25   : SyncableObject(parent)
26 {
27 }
28
29 BufferSyncer::BufferSyncer(const QHash<BufferId, MsgId> &lastSeenMsg, const QHash<BufferId, MsgId> &markerLines, QObject *parent)
30   : SyncableObject(parent),
31     _lastSeenMsg(lastSeenMsg),
32     _markerLines(markerLines)
33 {
34 }
35
36 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const {
37   return _lastSeenMsg.value(buffer, MsgId());
38 }
39
40 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId) {
41   if(!msgId.isValid())
42     return false;
43
44   const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
45   if(!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
46     _lastSeenMsg[buffer] = msgId;
47     SYNC(ARG(buffer), ARG(msgId))
48     emit lastSeenMsgSet(buffer, msgId);
49     return true;
50   }
51   return false;
52 }
53
54 MsgId BufferSyncer::markerLine(BufferId buffer) const {
55   return _markerLines.value(buffer, MsgId());
56 }
57
58 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId &msgId) {
59   if(!msgId.isValid())
60     return false;
61
62   _markerLines[buffer] = msgId;
63   SYNC(ARG(buffer), ARG(msgId))
64   emit markerLineSet(buffer, msgId);
65   return true;
66 }
67
68 QVariantList BufferSyncer::initLastSeenMsg() const {
69   QVariantList list;
70   QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
71   while(iter != _lastSeenMsg.constEnd()) {
72     list << QVariant::fromValue<BufferId>(iter.key())
73          << QVariant::fromValue<MsgId>(iter.value());
74     ++iter;
75   }
76   return list;
77 }
78
79 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list) {
80   _lastSeenMsg.clear();
81   Q_ASSERT(list.count() % 2 == 0);
82   for(int i = 0; i < list.count(); i += 2) {
83     setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
84   }
85 }
86
87 QVariantList BufferSyncer::initMarkerLines() const {
88   QVariantList list;
89   QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
90   while(iter != _markerLines.constEnd()) {
91     list << QVariant::fromValue<BufferId>(iter.key())
92          << QVariant::fromValue<MsgId>(iter.value());
93     ++iter;
94   }
95   return list;
96 }
97
98 void BufferSyncer::initSetMarkerLines(const QVariantList &list) {
99   _markerLines.clear();
100   Q_ASSERT(list.count() % 2 == 0);
101   for(int i = 0; i < list.count(); i += 2) {
102     setMarkerLine(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
103   }
104 }
105
106 void BufferSyncer::removeBuffer(BufferId buffer) {
107   if(_lastSeenMsg.contains(buffer))
108     _lastSeenMsg.remove(buffer);
109   if(_markerLines.contains(buffer))
110     _markerLines.remove(buffer);
111   SYNC(ARG(buffer))
112   emit bufferRemoved(buffer);
113 }
114
115 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2) {
116   if(_lastSeenMsg.contains(buffer2))
117     _lastSeenMsg.remove(buffer2);
118   if(_markerLines.contains(buffer2))
119     _markerLines.remove(buffer2);
120   SYNC(ARG(buffer1), ARG(buffer2))
121   emit buffersPermanentlyMerged(buffer1, buffer2);
122 }