1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "networkmodel.h"
23 #include <QAbstractItemView>
25 #include "buffermodel.h"
27 #include "signalproxy.h"
29 #include "ircchannel.h"
31 #include "buffersettings.h"
33 #include "util.h" // get rid of this (needed for isChannelName)
35 /*****************************************
37 *****************************************/
38 NetworkItem::NetworkItem(const NetworkId &netid, AbstractTreeItem *parent)
39 : PropertyMapItem(QList<QString>() << "networkName" << "currentServer" << "nickCount", parent),
42 // DO NOT EMIT dataChanged() DIRECTLY IN NetworkItem
43 // use networkDataChanged() instead. Otherwise you will end up in a infinite loop
44 // as we "sync" the dataChanged() signals of NetworkItem and StatusBufferItem
45 setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
46 connect(this, SIGNAL(networkDataChanged(int)), this, SIGNAL(dataChanged(int)));
49 QVariant NetworkItem::data(int column, int role) const {
51 case NetworkModel::BufferIdRole:
52 case NetworkModel::BufferInfoRole:
53 case NetworkModel::BufferTypeRole:
54 case NetworkModel::BufferActivityRole:
56 return child(0)->data(column, role);
59 case NetworkModel::NetworkIdRole:
60 return qVariantFromValue(_networkId);
61 case NetworkModel::ItemTypeRole:
62 return NetworkModel::NetworkItemType;
63 case NetworkModel::ItemActiveRole:
66 return PropertyMapItem::data(column, role);
70 BufferItem *NetworkItem::findBufferItem(BufferId bufferId) {
71 BufferItem *bufferItem = 0;
73 for(int i = 0; i < childCount(); i++) {
74 bufferItem = qobject_cast<BufferItem *>(child(i));
77 if(bufferItem->bufferId() == bufferId)
84 BufferItem *NetworkItem::bufferItem(const BufferInfo &bufferInfo) {
85 BufferItem *bufferItem = findBufferItem(bufferInfo);
89 switch(bufferInfo.type()) {
90 case BufferInfo::StatusBuffer:
91 bufferItem = new StatusBufferItem(bufferInfo, this);
92 disconnect(this, SIGNAL(networkDataChanged(int)), this, SIGNAL(dataChanged(int)));
93 connect(this, SIGNAL(networkDataChanged(int)), bufferItem, SIGNAL(dataChanged(int)));
94 connect(bufferItem, SIGNAL(dataChanged(int)), this, SIGNAL(dataChanged(int)));
96 case BufferInfo::ChannelBuffer:
97 bufferItem = new ChannelBufferItem(bufferInfo, this);
99 case BufferInfo::QueryBuffer:
100 bufferItem = new QueryBufferItem(bufferInfo, this);
103 bufferItem = new BufferItem(bufferInfo, this);
106 newChild(bufferItem);
110 void NetworkItem::attachNetwork(Network *network) {
116 connect(network, SIGNAL(networkNameSet(QString)),
117 this, SLOT(setNetworkName(QString)));
118 connect(network, SIGNAL(currentServerSet(QString)),
119 this, SLOT(setCurrentServer(QString)));
120 connect(network, SIGNAL(ircChannelAdded(IrcChannel *)),
121 this, SLOT(attachIrcChannel(IrcChannel *)));
122 connect(network, SIGNAL(ircUserAdded(IrcUser *)),
123 this, SLOT(attachIrcUser(IrcUser *)));
124 connect(network, SIGNAL(connectedSet(bool)),
125 this, SIGNAL(networkDataChanged()));
126 connect(network, SIGNAL(destroyed()),
127 this, SIGNAL(networkDataChanged()));
129 emit networkDataChanged();
132 void NetworkItem::attachIrcChannel(IrcChannel *ircChannel) {
133 ChannelBufferItem *channelItem;
134 for(int i = 0; i < childCount(); i++) {
135 channelItem = qobject_cast<ChannelBufferItem *>(child(i));
139 if(channelItem->bufferName().toLower() == ircChannel->name().toLower()) {
140 channelItem->attachIrcChannel(ircChannel);
146 void NetworkItem::attachIrcUser(IrcUser *ircUser) {
147 QueryBufferItem *queryItem = 0;
148 for(int i = 0; i < childCount(); i++) {
149 queryItem = qobject_cast<QueryBufferItem *>(child(i));
153 if(queryItem->bufferName().toLower() == ircUser->nick().toLower()) {
154 queryItem->setIrcUser(ircUser);
160 void NetworkItem::setNetworkName(const QString &networkName) {
161 Q_UNUSED(networkName);
162 emit networkDataChanged(0);
165 void NetworkItem::setCurrentServer(const QString &serverName) {
166 Q_UNUSED(serverName);
167 emit networkDataChanged(1);
171 QString NetworkItem::toolTip(int column) const {
174 QStringList toolTip(QString("<b>%1</b>").arg(networkName()));
175 toolTip.append(tr("Server: %1").arg(currentServer()));
176 toolTip.append(tr("Users: %1").arg(nickCount()));
179 toolTip.append(tr("Lag: %1 msecs").arg(_network->latency()));
182 return QString("<p> %1 </p>").arg(toolTip.join("<br />"));
185 /*****************************************
187 *****************************************/
188 BufferItem::BufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent)
189 : PropertyMapItem(QStringList() << "bufferName" << "topic" << "nickCount", parent),
190 _bufferInfo(bufferInfo),
191 _activity(BufferInfo::NoActivity)
193 setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
196 void BufferItem::setActivityLevel(BufferInfo::ActivityLevel level) {
197 if(_activity != level) {
203 void BufferItem::clearActivityLevel() {
204 _activity = BufferInfo::NoActivity;
205 _lastSeenMarkerMsgId = _lastSeenMsgId;
209 void BufferItem::updateActivityLevel(const Message &msg) {
210 if(isCurrentBuffer()) {
214 if(msg.flags() & Message::Self) // don't update activity for our own messages
217 if(lastSeenMsgId() >= msg.msgId())
220 BufferInfo::ActivityLevel oldLevel = activityLevel();
222 _activity |= BufferInfo::OtherActivity;
223 if(msg.type() & (Message::Plain | Message::Notice | Message::Action))
224 _activity |= BufferInfo::NewMessage;
226 if(msg.flags() & Message::Highlight)
227 _activity |= BufferInfo::Highlight;
229 if(oldLevel != _activity)
233 QVariant BufferItem::data(int column, int role) const {
235 case NetworkModel::ItemTypeRole:
236 return NetworkModel::BufferItemType;
237 case NetworkModel::BufferIdRole:
238 return qVariantFromValue(bufferInfo().bufferId());
239 case NetworkModel::NetworkIdRole:
240 return qVariantFromValue(bufferInfo().networkId());
241 case NetworkModel::BufferInfoRole:
242 return qVariantFromValue(bufferInfo());
243 case NetworkModel::BufferTypeRole:
244 return int(bufferType());
245 case NetworkModel::ItemActiveRole:
247 case NetworkModel::BufferActivityRole:
248 return (int)activityLevel();
250 return PropertyMapItem::data(column, role);
254 bool BufferItem::setData(int column, const QVariant &value, int role) {
256 case NetworkModel::BufferActivityRole:
257 setActivityLevel((BufferInfo::ActivityLevel)value.toInt());
260 return PropertyMapItem::setData(column, value, role);
265 void BufferItem::setBufferName(const QString &name) {
266 _bufferInfo = BufferInfo(_bufferInfo.bufferId(), _bufferInfo.networkId(), _bufferInfo.type(), _bufferInfo.groupId(), name);
270 void BufferItem::setLastSeenMsgId(const MsgId &msgId) {
271 _lastSeenMsgId = msgId;
272 if(!isCurrentBuffer()) {
273 _lastSeenMarkerMsgId = msgId;
275 setActivityLevel(BufferInfo::NoActivity);
278 bool BufferItem::isCurrentBuffer() const {
279 return _bufferInfo.bufferId() == Client::bufferModel()->currentIndex().data(NetworkModel::BufferIdRole).value<BufferId>();
282 QString BufferItem::toolTip(int column) const {
284 return tr("<p> %1 - %2 </p>").arg(bufferInfo().bufferId().toInt()).arg(bufferName());
287 /*****************************************
289 *****************************************/
290 StatusBufferItem::StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent)
291 : BufferItem(bufferInfo, parent)
295 QString StatusBufferItem::toolTip(int column) const {
299 QString netName = Client::network(bufferInfo().networkId())->networkName();
300 toolTip.append(tr("<b>Status buffer of %1</b>").arg(netName));
302 return tr("<p> %1 </p>").arg(toolTip.join("<br />"));
305 /*****************************************
307 *****************************************/
308 QueryBufferItem::QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent)
309 : BufferItem(bufferInfo, parent),
312 setFlags(flags() | Qt::ItemIsDropEnabled | Qt::ItemIsEditable);
314 const Network *net = Client::network(bufferInfo.networkId());
318 IrcUser *ircUser = net->ircUser(bufferInfo.bufferName());
322 QVariant QueryBufferItem::data(int column, int role) const {
324 case NetworkModel::IrcUserRole:
325 return QVariant::fromValue<QObject *>(_ircUser);
326 case NetworkModel::UserAwayRole:
327 return (bool)_ircUser ? _ircUser->isAway() : false;
329 return BufferItem::data(column, role);
333 bool QueryBufferItem::setData(int column, const QVariant &value, int role) {
335 return BufferItem::setData(column, value, role);
340 QString newName = value.toString();
341 if(!newName.isEmpty()) {
342 Client::renameBuffer(bufferId(), newName);
350 return BufferItem::setData(column, value, role);
354 void QueryBufferItem::setBufferName(const QString &name) {
355 BufferItem::setBufferName(name);
356 NetworkId netId = data(0, NetworkModel::NetworkIdRole).value<NetworkId>();
357 const Network *net = Client::network(netId);
359 setIrcUser(net->ircUser(name));
362 QString QueryBufferItem::toolTip(int column) const {
363 // pretty much code duplication of IrcUserItem::toolTip() but inheritance won't solve this...
367 toolTip.append(tr("<b>Query with %1</b>").arg(bufferName()));
370 if(_ircUser->userModes() != "") toolTip[0].append(QString(" (%1)").arg(_ircUser->userModes()));
371 if(_ircUser->isAway()) {
372 toolTip[0].append(QString(" (away%1)").arg(!_ircUser->awayMessage().isEmpty() ? (QString(" ") + _ircUser->awayMessage()) : QString()));
374 if(!_ircUser->realName().isEmpty()) toolTip.append(_ircUser->realName());
375 if(!_ircUser->ircOperator().isEmpty()) toolTip.append(QString("%1 %2").arg(_ircUser->nick()).arg(_ircUser->ircOperator()));
376 if(!_ircUser->suserHost().isEmpty()) toolTip.append(_ircUser->suserHost());
377 if(!_ircUser->whoisServiceReply().isEmpty()) toolTip.append(_ircUser->whoisServiceReply());
379 toolTip.append(_ircUser->hostmask().remove(0, _ircUser->hostmask().indexOf("!")+1));
381 if(_ircUser->idleTime().isValid()) {
382 QDateTime now = QDateTime::currentDateTime();
383 QDateTime idle = _ircUser->idleTime();
384 int idleTime = idle.secsTo(now);
385 toolTip.append(tr("idling since %1").arg(secondsToString(idleTime)));
387 if(_ircUser->loginTime().isValid()) {
388 toolTip.append(tr("login time: %1").arg(_ircUser->loginTime().toString()));
391 if(!_ircUser->server().isEmpty()) toolTip.append(tr("server: %1").arg(_ircUser->server()));
394 return QString("<p> %1 </p>").arg(toolTip.join("<br />"));
397 void QueryBufferItem::setIrcUser(IrcUser *ircUser) {
398 if(_ircUser == ircUser)
402 disconnect(_ircUser, 0, this, 0);
406 connect(ircUser, SIGNAL(quited()), this, SLOT(removeIrcUser()));
407 connect(ircUser, SIGNAL(awaySet(bool)), this, SIGNAL(dataChanged()));
414 void QueryBufferItem::removeIrcUser() {
419 /*****************************************
421 *****************************************/
422 ChannelBufferItem::ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent)
423 : BufferItem(bufferInfo, parent),
426 const Network *net = Client::network(bufferInfo.networkId());
430 IrcChannel *ircChannel = net->ircChannel(bufferInfo.bufferName());
432 attachIrcChannel(ircChannel);
435 QVariant ChannelBufferItem::data(int column, int role) const {
437 case NetworkModel::IrcChannelRole:
438 return QVariant::fromValue<QObject *>(_ircChannel);
440 return BufferItem::data(column, role);
444 QString ChannelBufferItem::toolTip(int column) const {
448 toolTip.append(tr("<b>Channel %1</b>").arg(bufferName()));
450 //TODO: add channel modes
451 toolTip.append(tr("<b>Users:</b> %1").arg(nickCount()));
453 QString channelMode = _ircChannel->channelModeString(); // channelModeString is compiled on the fly -> thus cache the result
454 if(!channelMode.isEmpty())
455 toolTip.append(tr("<b>Mode:</b> %1").arg(channelMode));
459 bool showTopic = s.value("DisplayTopicInTooltip", QVariant(false)).toBool();
461 QString _topic = topic();
463 _topic = stripFormatCodes(_topic);
464 _topic.replace(QString("<"), QString("<"));
465 _topic.replace(QString(">"), QString(">"));
466 toolTip.append(QString("<font size='-2'> </font>"));
467 toolTip.append(tr("<b>Topic:</b> %1").arg(_topic));
471 toolTip.append(tr("Not active <br /> Double-click to join"));
474 return tr("<p> %1 </p>").arg(toolTip.join("<br />"));
477 void ChannelBufferItem::attachIrcChannel(IrcChannel *ircChannel) {
478 Q_ASSERT(!_ircChannel && ircChannel);
480 _ircChannel = ircChannel;
482 connect(ircChannel, SIGNAL(topicSet(QString)),
483 this, SLOT(setTopic(QString)));
484 connect(ircChannel, SIGNAL(ircUsersJoined(QList<IrcUser *>)),
485 this, SLOT(join(QList<IrcUser *>)));
486 connect(ircChannel, SIGNAL(ircUserParted(IrcUser *)),
487 this, SLOT(part(IrcUser *)));
488 connect(ircChannel, SIGNAL(parted()),
489 this, SLOT(ircChannelParted()));
490 connect(ircChannel, SIGNAL(ircUserModesSet(IrcUser *, QString)),
491 this, SLOT(userModeChanged(IrcUser *)));
492 connect(ircChannel, SIGNAL(ircUserModeAdded(IrcUser *, QString)),
493 this, SLOT(userModeChanged(IrcUser *)));
494 connect(ircChannel, SIGNAL(ircUserModeRemoved(IrcUser *, QString)),
495 this, SLOT(userModeChanged(IrcUser *)));
497 if(!ircChannel->ircUsers().isEmpty())
498 join(ircChannel->ircUsers());
503 void ChannelBufferItem::ircChannelParted() {
504 Q_CHECK_PTR(_ircChannel);
505 disconnect(_ircChannel, 0, this, 0);
511 void ChannelBufferItem::join(const QList<IrcUser *> &ircUsers) {
512 addUsersToCategory(ircUsers);
516 UserCategoryItem *ChannelBufferItem::findCategoryItem(int categoryId) {
517 UserCategoryItem *categoryItem = 0;
519 for(int i = 0; i < childCount(); i++) {
520 categoryItem = qobject_cast<UserCategoryItem *>(child(i));
523 if(categoryItem->categoryId() == categoryId)
529 void ChannelBufferItem::addUserToCategory(IrcUser *ircUser) {
530 addUsersToCategory(QList<IrcUser *>() << ircUser);
533 void ChannelBufferItem::addUsersToCategory(const QList<IrcUser *> &ircUsers) {
534 Q_ASSERT(_ircChannel);
536 QHash<UserCategoryItem *, QList<IrcUser *> > categories;
539 UserCategoryItem *categoryItem = 0;
541 foreach(IrcUser *ircUser, ircUsers) {
542 categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser));
543 categoryItem = findCategoryItem(categoryId);
545 categoryItem = new UserCategoryItem(categoryId, this);
546 categories[categoryItem] = QList<IrcUser *>();
547 newChild(categoryItem);
549 categories[categoryItem] << ircUser;
552 QHash<UserCategoryItem *, QList<IrcUser *> >::const_iterator catIter = categories.constBegin();
553 while(catIter != categories.constEnd()) {
554 catIter.key()->addUsers(catIter.value());
559 void ChannelBufferItem::part(IrcUser *ircUser) {
561 qWarning() << bufferName() << "ChannelBufferItem::part(): unknown User" << ircUser;
565 disconnect(ircUser, 0, this, 0);
566 removeUserFromCategory(ircUser);
570 void ChannelBufferItem::removeUserFromCategory(IrcUser *ircUser) {
572 // If we parted the channel there might still be some ircUsers connected.
573 // in that case we just ignore the call
574 Q_ASSERT(childCount() == 0);
578 UserCategoryItem *categoryItem = 0;
579 for(int i = 0; i < childCount(); i++) {
580 categoryItem = qobject_cast<UserCategoryItem *>(child(i));
581 if(categoryItem->removeUser(ircUser)) {
582 if(categoryItem->childCount() == 0)
589 void ChannelBufferItem::userModeChanged(IrcUser *ircUser) {
590 Q_ASSERT(_ircChannel);
592 int categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser));
593 UserCategoryItem *categoryItem = findCategoryItem(categoryId);
596 if(categoryItem->findIrcUser(ircUser)) {
597 return; // already in the right category;
600 categoryItem = new UserCategoryItem(categoryId, this);
601 newChild(categoryItem);
604 // find the item that needs reparenting
605 IrcUserItem *ircUserItem = 0;
606 for(int i = 0; i < childCount(); i++) {
607 UserCategoryItem *oldCategoryItem = qobject_cast<UserCategoryItem *>(child(i));
608 Q_ASSERT(oldCategoryItem);
609 IrcUserItem *userItem = oldCategoryItem->findIrcUser(ircUser);
611 ircUserItem = userItem;
617 qWarning() << "ChannelBufferItem::userModeChanged(IrcUser *): unable to determine old category of" << ircUser;
620 ircUserItem->reParent(categoryItem);
623 /*****************************************
624 * User Category Items (like @vh etc.)
625 *****************************************/
626 // we hardcode this even though we have PREFIX in network... but that wouldn't help with mapping modes to
627 // category strings anyway.
628 const QList<QChar> UserCategoryItem::categories = QList<QChar>() << 'q' << 'a' << 'o' << 'h' << 'v';
630 UserCategoryItem::UserCategoryItem(int category, AbstractTreeItem *parent)
631 : PropertyMapItem(QStringList() << "categoryName", parent),
634 setFlags(Qt::ItemIsEnabled);
635 setTreeItemFlags(AbstractTreeItem::DeleteOnLastChildRemoved);
636 setObjectName(parent->data(0, Qt::DisplayRole).toString() + "/" + QString::number(category));
639 // caching this makes no sense, since we display the user number dynamically
640 QString UserCategoryItem::categoryName() const {
641 int n = childCount();
643 case 0: return tr("%n Owner(s)", 0, n);
644 case 1: return tr("%n Admin(s)", 0, n);
645 case 2: return tr("%n Operator(s)", 0, n);
646 case 3: return tr("%n Half-Op(s)", 0, n);
647 case 4: return tr("%n Voiced", 0, n);
648 default: return tr("%n User(s)", 0, n);
652 IrcUserItem *UserCategoryItem::findIrcUser(IrcUser *ircUser) {
653 IrcUserItem *userItem = 0;
655 for(int i = 0; i < childCount(); i++) {
656 userItem = qobject_cast<IrcUserItem *>(child(i));
659 if(userItem->ircUser() == ircUser)
665 void UserCategoryItem::addUsers(const QList<IrcUser *> &ircUsers) {
666 QList<AbstractTreeItem *> userItems;
667 foreach(IrcUser *ircUser, ircUsers)
668 userItems << new IrcUserItem(ircUser, this);
669 newChilds(userItems);
673 bool UserCategoryItem::removeUser(IrcUser *ircUser) {
674 IrcUserItem *userItem = findIrcUser(ircUser);
675 bool success = (bool)userItem;
677 removeChild(userItem);
683 int UserCategoryItem::categoryFromModes(const QString &modes) {
684 for(int i = 0; i < categories.count(); i++) {
685 if(modes.contains(categories[i]))
688 return categories.count();
691 QVariant UserCategoryItem::data(int column, int role) const {
693 case TreeModel::SortRole:
695 case NetworkModel::ItemActiveRole:
697 case NetworkModel::ItemTypeRole:
698 return NetworkModel::UserCategoryItemType;
699 case NetworkModel::BufferIdRole:
700 return parent()->data(column, role);
701 case NetworkModel::NetworkIdRole:
702 return parent()->data(column, role);
703 case NetworkModel::BufferInfoRole:
704 return parent()->data(column, role);
706 return PropertyMapItem::data(column, role);
711 /*****************************************
713 *****************************************/
714 IrcUserItem::IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent)
715 : PropertyMapItem(QStringList() << "nickName", parent),
718 setObjectName(ircUser->nick());
719 connect(ircUser, SIGNAL(quited()), this, SLOT(ircUserQuited()));
720 connect(ircUser, SIGNAL(nickSet(QString)), this, SIGNAL(dataChanged()));
721 connect(ircUser, SIGNAL(awaySet(bool)), this, SIGNAL(dataChanged()));
724 QVariant IrcUserItem::data(int column, int role) const {
726 case NetworkModel::ItemActiveRole:
728 case NetworkModel::ItemTypeRole:
729 return NetworkModel::IrcUserItemType;
730 case NetworkModel::BufferIdRole:
731 return parent()->data(column, role);
732 case NetworkModel::NetworkIdRole:
733 return parent()->data(column, role);
734 case NetworkModel::BufferInfoRole:
735 return parent()->data(column, role);
736 case NetworkModel::IrcChannelRole:
737 return parent()->data(column, role);
738 case NetworkModel::IrcUserRole:
739 return QVariant::fromValue<QObject *>(_ircUser.data());
740 case NetworkModel::UserAwayRole:
741 return (bool)_ircUser ? _ircUser->isAway() : false;
743 return PropertyMapItem::data(column, role);
747 QString IrcUserItem::toolTip(int column) const {
749 QStringList toolTip(QString("<b>%1</b>").arg(nickName()));
750 if(_ircUser->userModes() != "") toolTip[0].append(QString(" (%1)").arg(_ircUser->userModes()));
751 if(_ircUser->isAway()) {
752 toolTip[0].append(" is away");
753 if(!_ircUser->awayMessage().isEmpty())
754 toolTip[0].append(QString(" (%1)").arg(_ircUser->awayMessage()));
756 if(!_ircUser->realName().isEmpty()) toolTip.append(_ircUser->realName());
757 if(!_ircUser->ircOperator().isEmpty()) toolTip.append(QString("%1 %2").arg(nickName()).arg(_ircUser->ircOperator()));
758 if(!_ircUser->suserHost().isEmpty()) toolTip.append(_ircUser->suserHost());
759 if(!_ircUser->whoisServiceReply().isEmpty()) toolTip.append(_ircUser->whoisServiceReply());
761 toolTip.append(_ircUser->hostmask().remove(0, _ircUser->hostmask().indexOf("!")+1));
763 if(_ircUser->idleTime().isValid()) {
764 QDateTime now = QDateTime::currentDateTime();
765 QDateTime idle = _ircUser->idleTime();
766 int idleTime = idle.secsTo(now);
767 toolTip.append(tr("idling since %1").arg(secondsToString(idleTime)));
769 if(_ircUser->loginTime().isValid()) {
770 toolTip.append(tr("login time: %1").arg(_ircUser->loginTime().toString()));
773 if(!_ircUser->server().isEmpty()) toolTip.append(tr("server: %1").arg(_ircUser->server()));
775 return QString("<p> %1 </p>").arg(toolTip.join("<br />"));
778 /*****************************************
780 *****************************************/
781 NetworkModel::NetworkModel(QObject *parent)
782 : TreeModel(NetworkModel::defaultHeader(), parent)
784 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
785 this, SLOT(checkForNewBuffers(const QModelIndex &, int, int)));
786 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
787 this, SLOT(checkForRemovedBuffers(const QModelIndex &, int, int)));
790 QList<QVariant >NetworkModel::defaultHeader() {
791 QList<QVariant> data;
792 data << tr("Buffer") << tr("Topic") << tr("Nick Count");
796 bool NetworkModel::isBufferIndex(const QModelIndex &index) const {
797 return index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType;
800 int NetworkModel::networkRow(NetworkId networkId) const {
801 NetworkItem *netItem = 0;
802 for(int i = 0; i < rootItem->childCount(); i++) {
803 netItem = qobject_cast<NetworkItem *>(rootItem->child(i));
806 if(netItem->networkId() == networkId)
812 QModelIndex NetworkModel::networkIndex(NetworkId networkId) {
813 int netRow = networkRow(networkId);
815 return QModelIndex();
817 return indexByItem(qobject_cast<NetworkItem *>(rootItem->child(netRow)));
820 NetworkItem *NetworkModel::findNetworkItem(NetworkId networkId) const {
821 int netRow = networkRow(networkId);
825 return qobject_cast<NetworkItem *>(rootItem->child(netRow));
828 NetworkItem *NetworkModel::networkItem(NetworkId networkId) {
829 NetworkItem *netItem = findNetworkItem(networkId);
832 netItem = new NetworkItem(networkId, rootItem);
833 rootItem->newChild(netItem);
838 void NetworkModel::networkRemoved(const NetworkId &networkId) {
839 int netRow = networkRow(networkId);
841 rootItem->removeChild(netRow);
845 QModelIndex NetworkModel::bufferIndex(BufferId bufferId) {
846 if(!_bufferItemCache.contains(bufferId))
847 return QModelIndex();
849 return indexByItem(_bufferItemCache[bufferId]);
852 BufferItem *NetworkModel::findBufferItem(BufferId bufferId) const {
853 if(_bufferItemCache.contains(bufferId))
854 return _bufferItemCache[bufferId];
859 BufferItem *NetworkModel::bufferItem(const BufferInfo &bufferInfo) {
860 if(_bufferItemCache.contains(bufferInfo.bufferId()))
861 return _bufferItemCache[bufferInfo.bufferId()];
863 NetworkItem *netItem = networkItem(bufferInfo.networkId());
864 return netItem->bufferItem(bufferInfo);
867 QStringList NetworkModel::mimeTypes() const {
868 // mimetypes we accept for drops
870 // comma separated list of colon separated pairs of networkid and bufferid
871 // example: 0:1,0:2,1:4
872 types << "application/Quassel/BufferItemList";
876 bool NetworkModel::mimeContainsBufferList(const QMimeData *mimeData) {
877 return mimeData->hasFormat("application/Quassel/BufferItemList");
880 QList< QPair<NetworkId, BufferId> > NetworkModel::mimeDataToBufferList(const QMimeData *mimeData) {
881 QList< QPair<NetworkId, BufferId> > bufferList;
883 if(!mimeContainsBufferList(mimeData))
886 QStringList rawBufferList = QString::fromAscii(mimeData->data("application/Quassel/BufferItemList")).split(",");
889 foreach(QString rawBuffer, rawBufferList) {
890 if(!rawBuffer.contains(":"))
892 networkId = rawBuffer.section(":", 0, 0).toInt();
893 bufferUid = rawBuffer.section(":", 1, 1).toInt();
894 bufferList.append(qMakePair(networkId, bufferUid));
900 QMimeData *NetworkModel::mimeData(const QModelIndexList &indexes) const {
901 QMimeData *mimeData = new QMimeData();
903 QStringList bufferlist;
904 QString netid, uid, bufferid;
905 foreach(QModelIndex index, indexes) {
906 netid = QString::number(index.data(NetworkIdRole).value<NetworkId>().toInt());
907 uid = QString::number(index.data(BufferIdRole).value<BufferId>().toInt());
908 bufferid = QString("%1:%2").arg(netid).arg(uid);
909 if(!bufferlist.contains(bufferid))
910 bufferlist << bufferid;
913 mimeData->setData("application/Quassel/BufferItemList", bufferlist.join(",").toAscii());
918 void NetworkModel::attachNetwork(Network *net) {
919 NetworkItem *netItem = networkItem(net->networkId());
920 netItem->attachNetwork(net);
923 void NetworkModel::bufferUpdated(BufferInfo bufferInfo) {
924 BufferItem *bufItem = bufferItem(bufferInfo);
925 QModelIndex itemindex = indexByItem(bufItem);
926 emit dataChanged(itemindex, itemindex);
929 void NetworkModel::removeBuffer(BufferId bufferId) {
930 BufferItem *buffItem = findBufferItem(bufferId);
934 buffItem->parent()->removeChild(buffItem);
937 MsgId NetworkModel::lastSeenMsgId(BufferId bufferId) const {
938 if(!_bufferItemCache.contains(bufferId))
941 return _bufferItemCache[bufferId]->lastSeenMsgId();
944 MsgId NetworkModel::lastSeenMarkerMsgId(BufferId bufferId) const {
945 if(!_bufferItemCache.contains(bufferId))
948 return _bufferItemCache[bufferId]->lastSeenMarkerMsgId();
951 void NetworkModel::setLastSeenMsgId(const BufferId &bufferId, const MsgId &msgId) {
952 BufferItem *bufferItem = findBufferItem(bufferId);
954 qDebug() << "NetworkModel::setLastSeenMsgId(): buffer is unknown:" << bufferId;
957 bufferItem->setLastSeenMsgId(msgId);
960 void NetworkModel::updateBufferActivity(const Message &msg) {
961 BufferItem *item = bufferItem(msg.bufferInfo());
962 item->updateActivityLevel(msg);
963 if(item->isCurrentBuffer())
964 emit setLastSeenMsg(item->bufferId(), msg.msgId());
967 void NetworkModel::setBufferActivity(const BufferId &bufferId, BufferInfo::ActivityLevel level) {
968 BufferItem *bufferItem = findBufferItem(bufferId);
970 qDebug() << "NetworkModel::setBufferActivity(): buffer is unknown:" << bufferId;
973 bufferItem->setActivityLevel(level);
976 void NetworkModel::clearBufferActivity(const BufferId &bufferId) {
977 BufferItem *bufferItem = findBufferItem(bufferId);
979 qDebug() << "NetworkModel::clearBufferActivity(): buffer is unknown:" << bufferId;
982 bufferItem->clearActivityLevel();
985 const Network *NetworkModel::networkByIndex(const QModelIndex &index) const {
986 QVariant netVariant = index.data(NetworkIdRole);
987 if(!netVariant.isValid())
990 NetworkId networkId = netVariant.value<NetworkId>();
991 return Client::network(networkId);
994 void NetworkModel::checkForRemovedBuffers(const QModelIndex &parent, int start, int end) {
995 if(parent.data(ItemTypeRole) != NetworkItemType)
998 for(int row = start; row <= end; row++) {
999 _bufferItemCache.remove(parent.child(row, 0).data(BufferIdRole).value<BufferId>());
1003 void NetworkModel::checkForNewBuffers(const QModelIndex &parent, int start, int end) {
1004 if(parent.data(ItemTypeRole) != NetworkItemType)
1007 for(int row = start; row <= end; row++) {
1008 QModelIndex child = parent.child(row, 0);
1009 _bufferItemCache[child.data(BufferIdRole).value<BufferId>()] = static_cast<BufferItem *>(child.internalPointer());
1013 QString NetworkModel::bufferName(BufferId bufferId) const {
1014 if(!_bufferItemCache.contains(bufferId))
1017 return _bufferItemCache[bufferId]->bufferName();
1020 BufferInfo::Type NetworkModel::bufferType(BufferId bufferId) const {
1021 if(!_bufferItemCache.contains(bufferId))
1022 return BufferInfo::InvalidBuffer;
1024 return _bufferItemCache[bufferId]->bufferType();
1027 BufferInfo NetworkModel::bufferInfo(BufferId bufferId) const {
1028 if(!_bufferItemCache.contains(bufferId))
1029 return BufferInfo();
1031 return _bufferItemCache[bufferId]->bufferInfo();
1034 NetworkId NetworkModel::networkId(BufferId bufferId) const {
1035 if(!_bufferItemCache.contains(bufferId))
1038 NetworkItem *netItem = qobject_cast<NetworkItem *>(_bufferItemCache[bufferId]->parent());
1040 return netItem->networkId();
1045 QString NetworkModel::networkName(BufferId bufferId) const {
1046 if(!_bufferItemCache.contains(bufferId))
1049 NetworkItem *netItem = qobject_cast<NetworkItem *>(_bufferItemCache[bufferId]->parent());
1051 return netItem->networkName();
1056 BufferId NetworkModel::bufferId(NetworkId networkId, const QString &bufferName, Qt::CaseSensitivity cs) const {
1057 const NetworkItem *netItem = findNetworkItem(networkId);
1061 for(int i = 0; i < netItem->childCount(); i++) {
1062 BufferItem *bufferItem = qobject_cast<BufferItem *>(netItem->child(i));
1063 if(bufferItem && !bufferItem->bufferName().compare(bufferName, cs))
1064 return bufferItem->bufferId();
1069 void NetworkModel::sortBufferIds(QList<BufferId> &bufferIds) const {
1070 QList<BufferItem *> bufferItems;
1071 foreach(BufferId bufferId, bufferIds) {
1072 if(_bufferItemCache.contains(bufferId))
1073 bufferItems << _bufferItemCache[bufferId];
1076 qSort(bufferItems.begin(), bufferItems.end(), bufferItemLessThan);
1079 foreach(BufferItem *bufferItem, bufferItems) {
1080 bufferIds << bufferItem->bufferId();
1084 QList<BufferId> NetworkModel::allBufferIdsSorted() const {
1085 QList<BufferId> bufferIds = allBufferIds();
1086 sortBufferIds(bufferIds);
1090 bool NetworkModel::bufferItemLessThan(const BufferItem *left, const BufferItem *right) {
1091 int leftType = left->bufferType();
1092 int rightType = right->bufferType();
1094 if(leftType != rightType)
1095 return leftType < rightType;
1097 return QString::compare(left->bufferName(), right->bufferName(), Qt::CaseInsensitive) < 0;