Fitting the SyncableObjects to the new Style
[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, QObject *parent)
30   : SyncableObject(parent),
31     _lastSeenMsg(lastSeenMsg)
32 {
33 }
34
35 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const {
36   if(_lastSeenMsg.contains(buffer))
37     return _lastSeenMsg[buffer];
38   return MsgId();
39 }
40
41 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId) {
42   if(!msgId.isValid())
43     return false;
44
45   const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
46   if(!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
47     _lastSeenMsg[buffer] = msgId;
48     SYNC(ARG(buffer), ARG(msgId))
49     emit lastSeenMsgSet(buffer, msgId);
50     return true;
51   }
52   return false;
53 }
54
55 QVariantList BufferSyncer::initLastSeenMsg() const {
56   QVariantList list;
57   QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
58   while(iter != _lastSeenMsg.constEnd()) {
59     list << QVariant::fromValue<BufferId>(iter.key())
60          << QVariant::fromValue<MsgId>(iter.value());
61     iter++;
62   }
63   return list;
64 }
65
66 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list) {
67   _lastSeenMsg.clear();
68   Q_ASSERT(list.count() % 2 == 0);
69   for(int i = 0; i < list.count(); i += 2) {
70     setLastSeenMsg(list[i].value<BufferId>(), list[i+1].value<MsgId>());
71   }
72 }
73
74 void BufferSyncer::removeBuffer(BufferId buffer) {
75   if(_lastSeenMsg.contains(buffer))
76     _lastSeenMsg.remove(buffer);
77   SYNC(ARG(buffer))
78   emit bufferRemoved(buffer);
79 }
80
81 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2) {
82   if(_lastSeenMsg.contains(buffer2))
83     _lastSeenMsg.remove(buffer2);
84   SYNC(ARG(buffer1), ARG(buffer2))
85   emit buffersPermanentlyMerged(buffer1, buffer2);
86 }