Some more tweaking for QuasselTopia.
[quassel.git] / src / client / buffer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel 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) any later version.                                   *
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 "client.h"
25 #include "ircchannel.h"
26 #include "nickmodel.h"
27 #include "util.h"
28
29
30 Buffer::Buffer(BufferInfo bufferid, QObject *parent)
31   : QObject(parent),
32     _bufferInfo(bufferid),
33     _active(false),
34     _ircChannel(0), _nickModel(0)
35 {
36   if(bufferid.buffer().isEmpty())
37     _type = StatusType;
38   else if(isChannelName(bufferid.buffer()))
39     _type = ChannelType;
40   else
41     _type = QueryType;
42
43   _nickModel = new NickModel(0, this);
44 /*
45   QSettings s;
46   s.beginGroup(QString("GUI/BufferStates/%1/%2").arg(netname).arg(bufname));
47   state->splitterState = s.value("Splitter").toByteArray();
48   s.endGroup();
49   */
50   emit bufferUpdated(this);
51 }
52
53 Buffer::~Buffer() {
54   //delete widget;
55   /*
56   QSettings s;
57   s.beginGroup(QString("GUI/BufferStates/%1/%2").arg(networkName).arg(bufferName));
58   s.setValue("Splitter", state->splitterState);
59   s.endGroup();
60 */
61   //delete state;
62 }
63
64 Buffer::Type Buffer::bufferType() const {
65   return _type;
66 }
67
68 bool Buffer::isActive() const {
69   // FIXME determine status by checking for a networkInfo objekt
70   return true;
71 }
72
73 BufferInfo Buffer::bufferInfo() const {
74    return _bufferInfo;
75 }
76
77 void Buffer::updateBufferInfo(BufferInfo bufferid) {
78   _bufferInfo = bufferid;
79 }
80
81 uint Buffer::uid() const {
82   return bufferInfo().uid();
83 }
84
85 uint Buffer::networkId() const {
86   return bufferInfo().networkId();
87 }
88
89 QString Buffer::networkName() const {
90   return bufferInfo().network();
91 }
92
93 QString Buffer::name() const {
94   if(bufferType() == StatusType)
95     return tr("Status Buffer");
96   else
97     return bufferInfo().buffer();
98 }
99
100 QList<AbstractUiMsg *> Buffer::contents() const {
101   return layoutedMsgs;
102 }
103
104 QVariantMap Buffer::nickList() const {
105   // FIXME should return a Map or List of IrcUsers in the future
106   return QVariantMap();
107 }
108
109 QString Buffer::topic() const {
110   if(ircChannel()) return ircChannel()->topic();
111   return QString();
112 }
113
114 QString Buffer::ownNick() const {
115   // FIXME if(ircChannel()) return ircChannel()->ownNick();
116   return QString();
117 }
118
119 bool Buffer::isStatusBuffer() const {
120    return bufferType() == StatusType;
121 }
122
123 void Buffer::setActive(bool a) {
124 //   if(a != active) {
125 //     active = a;
126 //     emit bufferUpdated(this);
127 //  }
128 }
129
130 void Buffer::appendMsg(const Message &msg) {
131   AbstractUiMsg *m = Client::layoutMsg(msg);
132   layoutedMsgs.append(m);
133   emit msgAppended(m);
134 }
135
136 void Buffer::prependMsg(const Message &msg) {
137   layoutQueue.append(msg);
138 }
139
140 bool Buffer::layoutMsg() {
141   if(layoutQueue.count()) {
142     AbstractUiMsg *m = Client::layoutMsg(layoutQueue.takeFirst());
143     layoutedMsgs.prepend(m);
144     emit msgPrepended(m);
145   }
146   return layoutQueue.count();
147 }
148
149 void Buffer::processUserInput(QString msg) {
150   // TODO User Input processing (plugins) -> well, this goes through MainWin into Core for processing... so...
151   emit userInput(_bufferInfo, msg);
152 }
153
154 NickModel *Buffer::nickModel() const {
155   return _nickModel;
156 }
157
158 IrcChannel *Buffer::ircChannel() const {
159   return _ircChannel;
160 }
161
162 void Buffer::setIrcChannel(IrcChannel *ircchan) {
163   if(_ircChannel) {
164     disconnect(_ircChannel, 0, this, 0);
165   }
166   _ircChannel = ircchan;
167   if(_ircChannel) {
168     emit topicSet(_ircChannel->topic());
169     connect(_ircChannel, SIGNAL(topicSet(QString)), this, SIGNAL(topicSet(QString)));
170     connect(_ircChannel, SIGNAL(destroyed()), this, SLOT(setIrcChannel()));
171   }
172   _nickModel->setIrcChannel(ircChannel());
173 }
174
175 // no longer needed
176 // back reference:
177
178 // void Buffer::addNick(QString nick, QVariantMap props) {
179 //   if(nick == ownNick()) setActive(true);
180 //   nicks[nick] = props;
181 //   emit nickListChanged(nicks);
182 // }
183
184 // void Buffer::updateNick(QString nick, QVariantMap props) {
185 //   nicks[nick] = props;
186 //   emit nickListChanged(nicks);
187 // }
188
189 // void Buffer::renameNick(QString oldnick, QString newnick) {
190 //   QVariant v = nicks.take(oldnick);
191 //   nicks[newnick] = v;
192 //   emit nickListChanged(nicks);
193 // }
194
195 // void Buffer::removeNick(QString nick) {
196 //   if(nick == ownNick()) setActive(false);
197 //   nicks.remove(nick);
198 //   emit nickListChanged(nicks);
199 // }
200
201 // void Buffer::setOwnNick(QString nick) {
202 //   _ownNick = nick;
203 //   emit ownNickSet(nick);
204 // }