ChangeLog++
[quassel.git] / src / common / buffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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 BufferSyncer::BufferSyncer(QObject *parent)
24   : SyncableObject(parent)
25 {
26 }
27
28
29 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const {
30   if(_lastSeenMsg.contains(buffer))
31     return _lastSeenMsg[buffer];
32   return MsgId();
33 }
34
35 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId) {
36   if(!msgId.isValid())
37     return false;
38
39   const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
40   if(!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
41     _lastSeenMsg[buffer] = msgId;
42     emit lastSeenMsgSet(buffer, msgId);
43     return true;
44   }
45   return false;
46 }
47
48 QVariantList BufferSyncer::initLastSeenMsg() const {
49   QVariantList list;
50   QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
51   while(iter != _lastSeenMsg.constEnd()) {
52     list << QVariant::fromValue<BufferId>(iter.key())
53          << QVariant::fromValue<MsgId>(iter.value());
54     iter++;
55   }
56   return list;
57 }
58
59 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list) {
60   _lastSeenMsg.clear();
61   Q_ASSERT(list.count() % 2 == 0);
62   for(int i = 0; i < list.count(); i += 2) {
63     setLastSeenMsg(list[i].value<BufferId>(), list[i+1].value<MsgId>());
64   }
65 }
66
67 void BufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId) {
68   if(setLastSeenMsg(buffer, msgId))
69     emit setLastSeenMsgRequested(buffer, msgId);
70 }
71
72
73 void BufferSyncer::requestRemoveBuffer(BufferId buffer) {
74   emit removeBufferRequested(buffer);
75 }
76
77 void BufferSyncer::removeBuffer(BufferId buffer) {
78   if(_lastSeenMsg.contains(buffer))
79     _lastSeenMsg.remove(buffer);
80   emit bufferRemoved(buffer);
81 }
82
83 void BufferSyncer::renameBuffer(BufferId buffer, QString newName) {
84   emit bufferRenamed(buffer, newName);
85 }