Fixing BR #147. Needs core update.
[quassel.git] / src / client / networkmodel.cpp
index 0582d44..c797179 100644 (file)
 #include "signalproxy.h"
 #include "network.h"
 #include "ircchannel.h"
-#include "ircuser.h"
 
 #include "buffersettings.h"
 
 #include "util.h" // get rid of this (needed for isChannelName)
 
-// #define PHONDEV
-
 /*****************************************
 *  Fancy Buffer Items
 *****************************************/
@@ -42,7 +39,8 @@ BufferItem::BufferItem(BufferInfo bufferInfo, AbstractTreeItem *parent)
   : PropertyMapItem(QStringList() << "bufferName" << "topic" << "nickCount", parent),
     _bufferInfo(bufferInfo),
     _bufferName(bufferInfo.bufferName()),
-    _activity(Buffer::NoActivity)
+    _activity(Buffer::NoActivity),
+    _ircChannel(0)
 {
   Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
   if(bufferType() == BufferInfo::QueryBuffer)
@@ -55,21 +53,13 @@ BufferItem::BufferItem(BufferInfo bufferInfo, AbstractTreeItem *parent)
   setFlags(flags);
 }
 
-const BufferInfo &BufferItem::bufferInfo() const {
-  return _bufferInfo;
-}
-
 quint64 BufferItem::id() const {
   return qHash(bufferInfo().bufferId());
 }
 
-bool BufferItem::isStatusBuffer() const {
-  return bufferType() == BufferInfo::StatusBuffer;
-}
-
-BufferInfo::Type BufferItem::bufferType() const {
-  return bufferInfo().type();
-}
+// bool BufferItem::isStatusBuffer() const {
+//   return bufferType() == BufferInfo::StatusBuffer;
+// }
 
 bool BufferItem::isActive() const {
   if(bufferType() == BufferInfo::ChannelBuffer)
@@ -78,10 +68,11 @@ bool BufferItem::isActive() const {
     return qobject_cast<NetworkItem *>(parent())->isActive();
 }
 
-bool BufferItem::setActivityLevel(Buffer::ActivityLevel level) {
-  _activity = level;
-  emit dataChanged();
-  return true;
+void BufferItem::setActivityLevel(Buffer::ActivityLevel level) {
+  if(_activity != level) {
+    _activity = level;
+    emit dataChanged();
+  }
 }
 
 void BufferItem::updateActivityLevel(Buffer::ActivityLevel level) {
@@ -115,7 +106,8 @@ QVariant BufferItem::data(int column, int role) const {
 bool BufferItem::setData(int column, const QVariant &value, int role) {
   switch(role) {
   case NetworkModel::BufferActivityRole:
-    return setActivityLevel((Buffer::ActivityLevel)value.toInt());
+    setActivityLevel((Buffer::ActivityLevel)value.toInt());
+    return true;
   default:
     return PropertyMapItem::setData(column, value, role);
   }
@@ -153,6 +145,9 @@ void BufferItem::attachIrcChannel(IrcChannel *ircChannel) {
 }
 
 void BufferItem::ircChannelDestroyed() {
+  Q_CHECK_PTR(_ircChannel);
+  disconnect(_ircChannel, 0, this, 0);
+  _ircChannel = 0;
   emit dataChanged();
   removeAllChilds();
 }
@@ -168,7 +163,7 @@ void BufferItem::setBufferName(const QString &name) {
   _bufferName = name;
   // as long as we need those bufferInfos, we have to update that one aswell.
   // pretty ugly though :/
-  _bufferInfo = BufferInfo(_bufferInfo.bufferId(), _bufferInfo.networkId(), _bufferInfo.type(), _bufferInfo.groupId(), _bufferInfo.bufferName());
+  _bufferInfo = BufferInfo(_bufferInfo.bufferId(), _bufferInfo.networkId(), _bufferInfo.type(), _bufferInfo.groupId(), name);
   emit dataChanged(0);
 }
 
@@ -259,23 +254,23 @@ void BufferItem::removeUserFromCategory(IrcUser *ircUser) {
   UserCategoryItem *categoryItem = 0;
   for(int i = 0; i < childCount(); i++) {
     categoryItem = qobject_cast<UserCategoryItem *>(child(i));
-    if(success = categoryItem->removeUser(ircUser)) {
+    if((success = categoryItem->removeUser(ircUser))) {
       if(categoryItem->childCount() == 0)
        removeChild(i);
       break;
     }
   }
 
-  if(!success) {
-    qDebug() << "didn't find User:" << ircUser << qHash(ircUser);
-    qDebug() << "==== Childlist for Item:" << this << id() << bufferName() << "====";
-    for(int i = 0; i < childCount(); i++) {
-      categoryItem = qobject_cast<UserCategoryItem *>(child(i));
-      categoryItem->dumpChildList();
-    }
-    qDebug() << "==== End Of Childlist for Item:" << this << id() << bufferName() << "====";
-  }
-  Q_ASSERT(success);
+//   if(!success) {
+//     qDebug() << "didn't find User:" << ircUser << qHash(ircUser);
+//     qDebug() << "==== Childlist for Item:" << this << id() << bufferName() << "====";
+//     for(int i = 0; i < childCount(); i++) {
+//       categoryItem = qobject_cast<UserCategoryItem *>(child(i));
+//       categoryItem->dumpChildList();
+//     }
+//     qDebug() << "==== End Of Childlist for Item:" << this << id() << bufferName() << "====";
+//   }
+//   Q_ASSERT(success);
 }
 
 void BufferItem::userModeChanged(IrcUser *ircUser) {
@@ -297,14 +292,14 @@ QString BufferItem::toolTip(int column) const {
   switch(bufferType()) {
     case BufferInfo::StatusBuffer: {
       QString netName = Client::network(bufferInfo().networkId())->networkName();
-      toolTip.append(QString("<b>Status buffer from %1</b>").arg(netName));
+      toolTip.append(tr("<b>Status buffer of %1</b>").arg(netName));
       break;
     }
     case BufferInfo::ChannelBuffer:
-      toolTip.append(QString("<h4>Channel %1</h4>").arg(bufferName()));
+      toolTip.append(tr("<b>Channel %1</b>").arg(bufferName()));
       if(isActive()) {
         //TODO: add channel modes 
-        toolTip.append(QString("<b>Users:</b> %1").arg(nickCount()));
+        toolTip.append(tr("<b>Users:</b> %1").arg(nickCount()));
 
         BufferSettings s;
         bool showTopic = s.value("DisplayTopicInTooltip", QVariant(false)).toBool();
@@ -314,23 +309,28 @@ QString BufferItem::toolTip(int column) const {
             _topic.replace(QString("<"), QString("&lt;"));
             _topic.replace(QString(">"), QString("&gt;"));
             toolTip.append(QString("<font size='-2'>&nbsp;</font>"));
-            toolTip.append(QString("<b>Topic:</b> %1").arg(_topic));
+            toolTip.append(tr("<b>Topic:</b> %1").arg(_topic));
           }
         }
+       if(_ircChannel) {
+         QString channelMode = _ircChannel->channelModeString(); // channelModeString is compiled on the fly -> thus cache the result
+         if(!channelMode.isEmpty())
+           toolTip.append(tr("<b>Mode:</b> %1").arg(channelMode));
+       }
       } else {
-        toolTip.append(QString("Not active <br /> Double-click to join"));
+        toolTip.append(tr("Not active <br /> Double-click to join"));
       }
       break;
     case BufferInfo::QueryBuffer:
-      toolTip.append(QString("<b>Query with %1</b>").arg(bufferName()));
-      if(topic() != "") toolTip.append(QString("Away Message: %1").arg(topic()));
+      toolTip.append(tr("<b>Query with %1</b>").arg(bufferName()));
+      if(topic() != "") toolTip.append(tr("Away Message: %1").arg(topic()));
       break;
     default: //this should not happen
-      toolTip.append(QString("%1 - %2").arg(bufferInfo().bufferId().toInt()).arg(bufferName()));
+      toolTip.append(tr("%1 - %2").arg(bufferInfo().bufferId().toInt()).arg(bufferName()));
       break;
   }
 
-  return QString("<p> %1 </p>").arg(toolTip.join("<br />"));
+  return tr("<p> %1 </p>").arg(toolTip.join("<br />"));
 }
 
 /*
@@ -423,9 +423,9 @@ void NetworkItem::attachNetwork(Network *network) {
          this, SLOT(attachIrcChannel(QString)));
   connect(network, SIGNAL(connectedSet(bool)),
          this, SIGNAL(dataChanged()));
+  connect(network, SIGNAL(destroyed()),
+         this, SIGNAL(dataChanged()));
   
-  // FIXME: connect this and that...
-
   emit dataChanged();
 }
 
@@ -473,27 +473,26 @@ QString NetworkItem::toolTip(int column) const {
 *****************************************/
 // we hardcode this even though we have PREFIX in network... but that wouldn't help with mapping modes to
 // category strings anyway.
-// TODO make this translateable depending on the number of users in a category
-//      -> we can't set the real string here, because tr() needs to get the actual number as second param
-//      -> tr("%n User(s)", n) needs to be used somewhere where we do know the user number n
-const QList<UserCategoryItem::Category> UserCategoryItem::categories = QList<UserCategoryItem::Category>()
-  << UserCategoryItem::Category('q', tr("Owners"))
-  << UserCategoryItem::Category('a', tr("Admins"))
-  << UserCategoryItem::Category('o', tr("Operators"))
-  << UserCategoryItem::Category('h', tr("Half-Ops"))
-  << UserCategoryItem::Category('v', tr("Voiced"));
+const QList<QChar> UserCategoryItem::categories = QList<QChar>() << 'q' << 'a' << 'o' << 'h' << 'v';
 
 UserCategoryItem::UserCategoryItem(int category, AbstractTreeItem *parent)
-  : PropertyMapItem(QStringList() << "categoryId", parent),
+  : PropertyMapItem(QStringList() << "categoryName", parent),
     _category(category)
 {
+
 }
 
-QString UserCategoryItem::categoryId() {
-  if(_category < categories.count())
-    return categories[_category].displayString;
-  else
-    return tr("Users");
+// caching this makes no sense, since we display the user number dynamically
+QString UserCategoryItem::categoryName() const {
+  int n = childCount();
+  switch(_category) {
+    case 0: return tr("%n Owner(s)", 0, n);
+    case 1: return tr("%n Admin(s)", 0, n);
+    case 2: return tr("%n Operator(s)", 0, n);
+    case 3: return tr("%n Half-Op(s)", 0, n);
+    case 4: return tr("%n Voiced", 0, n);
+    default: return tr("%n User(s)", 0, n);
+  }
 }
 
 quint64 UserCategoryItem::id() const {
@@ -513,7 +512,7 @@ bool UserCategoryItem::removeUser(IrcUser *ircUser) {
 
 int UserCategoryItem::categoryFromModes(const QString &modes) {
   for(int i = 0; i < categories.count(); i++) {
-    if(modes.contains(categories[i].mode))
+    if(modes.contains(categories[i]))
       return i;
   }
   return categories.count();
@@ -521,6 +520,8 @@ int UserCategoryItem::categoryFromModes(const QString &modes) {
 
 QVariant UserCategoryItem::data(int column, int role) const {
   switch(role) {
+  case TreeModel::SortRole:
+    return _category;
   case NetworkModel::ItemActiveRole:
     return true;
   case NetworkModel::ItemTypeRole:
@@ -536,7 +537,7 @@ QVariant UserCategoryItem::data(int column, int role) const {
   }
 }
 
-     
+
 /*****************************************
 *  Irc User Items
 *****************************************/
@@ -568,14 +569,6 @@ bool IrcUserItem::isActive() const {
     return false;
 }
 
-IrcUser *IrcUserItem::ircUser() {
-  return _ircUser;
-}
-
-quint64 IrcUserItem::id() const {
-  return _id;
-}
-
 QVariant IrcUserItem::data(int column, int role) const {
   switch(role) {
   case NetworkModel::ItemActiveRole:
@@ -596,33 +589,24 @@ QVariant IrcUserItem::data(int column, int role) const {
 QString IrcUserItem::toolTip(int column) const {
   Q_UNUSED(column);
   QStringList toolTip(QString("<b>%1</b>").arg(nickName()));
+  if(_ircUser->userModes() != "") toolTip[0].append(QString(" (%1)").arg(_ircUser->userModes()));
   if(_ircUser->isAway()) toolTip[0].append(" is away");
   if(!_ircUser->awayMessage().isEmpty()) toolTip[0].append(QString(" (%1)").arg(_ircUser->awayMessage()));
   if(!_ircUser->realName().isEmpty()) toolTip.append(_ircUser->realName());
-  if(!_ircUser->ircOperator().isEmpty()) toolTip.append(_ircUser->ircOperator());
-  toolTip.append(_ircUser->hostmask());
+  if(!_ircUser->ircOperator().isEmpty()) toolTip.append(QString("%1 %2").arg(nickName()).arg(_ircUser->ircOperator()));
+  if(!_ircUser->suserHost().isEmpty()) toolTip.append(_ircUser->suserHost());
+  if(!_ircUser->whoisServiceReply().isEmpty()) toolTip.append(_ircUser->whoisServiceReply());
+
+  toolTip.append(_ircUser->hostmask().remove(0, _ircUser->hostmask().indexOf("!")+1));
 
   if(_ircUser->idleTime().isValid()) {
     QDateTime now = QDateTime::currentDateTime();
     QDateTime idle = _ircUser->idleTime();
     int idleTime = idle.secsTo(now);
-
-    QList< QPair<int, QString> > timeUnit;
-    timeUnit.append(qMakePair(365*60*60, tr("year")));
-    timeUnit.append(qMakePair(24*60*60, tr("day")));
-    timeUnit.append(qMakePair(60*60, tr("h")));
-    timeUnit.append(qMakePair(60, tr("min")));
-    timeUnit.append(qMakePair(1, tr("sec")));
-
-    QString idleString(' ');
-    for(int i=0; i < timeUnit.size(); i++) {
-      int n = idleTime / timeUnit[i].first;
-      if(n > 0) {
-        idleString += QString("%1 %2 ").arg(QString::number(n), timeUnit[i].second);
-      }
-      idleTime = idleTime % timeUnit[i].first;
-    }
-    toolTip.append(tr("idling since %1").arg(idleString));
+    toolTip.append(tr("idling since %1").arg(secondsToString(idleTime)));
+  }
+  if(_ircUser->loginTime().isValid()) {
+    toolTip.append(tr("login time: %1").arg(_ircUser->loginTime().toString()));
   }
 
   if(!_ircUser->server().isEmpty()) toolTip.append(tr("server: %1").arg(_ircUser->server()));