messagemodel.[h|cpp]++
[quassel.git] / src / client / buffer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 #include <QDebug>
21
22 #include "buffer.h"
23
24 #include "buffersyncer.h"
25 #include "client.h"
26 #include "networkmodel.h"
27 #include "quasselui.h"
28 #include "util.h"
29
30
31 Buffer::Buffer(BufferInfo bufferid, QObject *parent)
32   : QObject(parent),
33     _bufferInfo(bufferid),
34     _isVisible(false),
35     _activityLevel(NoActivity)
36 {
37
38 }
39
40 BufferInfo Buffer::bufferInfo() const {
41   // still needed by the gui *sigh* to request the backlogs *sigh*
42   return _bufferInfo;
43 }
44
45 QList<AbstractUiMsg *> Buffer::contents() const {
46   return layoutedMsgs;
47 }
48
49 void Buffer::appendMsg(const Message &msg) {
50   updateActivityLevel(msg);
51   AbstractUiMsg *m = Client::layoutMsg(msg);
52   layoutedMsgs.append(m);
53   emit msgAppended(m);
54 }
55
56 void Buffer::prependMsg(const Message &msg) {
57   // check for duplicate first
58   if(contents().count() > 0 && msg.msgId() >= contents().first()->msgId()) {
59     return;
60   }
61   updateActivityLevel(msg);
62   layoutQueue.append(msg);
63 }
64
65 bool Buffer::layoutMsg() {
66   if(layoutQueue.count()) {
67     AbstractUiMsg *m = Client::layoutMsg(layoutQueue.takeFirst());
68     layoutedMsgs.prepend(m);
69     emit msgPrepended(m);
70   }
71   return layoutQueue.count();
72 }
73
74 void Buffer::setVisible(bool visible) {
75   _isVisible = visible;
76   setActivityLevel(NoActivity);
77   if(!layoutedMsgs.count()) return;
78   setLastSeenMsg(layoutedMsgs.last()->msgId());
79 }
80
81 void Buffer::setLastSeenMsg(const MsgId &msgId) {
82   // qDebug() << "want to set lastSeen:" << bufferInfo() << seen << lastSeen();
83   const MsgId oldLastSeen = lastSeenMsg();
84   if(!oldLastSeen.isValid() || msgId.isValid() && msgId > oldLastSeen) {
85     //qDebug() << "setting:" << bufferInfo().bufferName() << seen;
86     _lastSeenMsg = msgId;
87     Client::setBufferLastSeenMsg(bufferInfo().bufferId(), msgId);
88     //qDebug() << "setting lastSeen:" << bufferInfo() << lastSeen();
89     setActivityLevel(NoActivity);
90   }
91 }
92
93 void Buffer::setActivityLevel(ActivityLevel level) {
94   _activityLevel = level;
95   if(bufferInfo().bufferId() > 0) {
96     Client::networkModel()->setBufferActivity(bufferInfo(), level);
97     //qDebug() << "setting level:" << bufferInfo() << lastSeen() << level;
98   }
99 }
100
101 void Buffer::updateActivityLevel(const Message &msg) {
102   if(isVisible())
103     return;
104
105   if(msg.flags() & Message::Self)       // don't update activity for our own messages
106     return;
107
108   if(lastSeenMsg().isValid() && lastSeenMsg() >= msg.msgId())
109     return;
110
111   ActivityLevel level = activityLevel() | OtherActivity;
112   if(msg.type() & (Message::Plain | Message::Notice | Message::Action))
113     level |= NewMessage;
114   
115   if(msg.flags() & Message::Highlight)
116     level |= Highlight;
117
118   if(level != activityLevel())
119     setActivityLevel(level);
120 }