BR#138: prettifyed the whois output of the core
[quassel.git] / src / client / networkmodel.cpp
index b10afd0..9d463f6 100644 (file)
 #include "ircchannel.h"
 #include "ircuser.h"
 
+#include "buffersettings.h"
+
 #include "util.h" // get rid of this (needed for isChannelName)
 
+
 /*****************************************
 *  Fancy Buffer Items
 *****************************************/
 BufferItem::BufferItem(BufferInfo bufferInfo, AbstractTreeItem *parent)
   : PropertyMapItem(QStringList() << "bufferName" << "topic" << "nickCount", parent),
     _bufferInfo(bufferInfo),
+    _bufferName(bufferInfo.bufferName()),
     _activity(Buffer::NoActivity)
 {
   Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
@@ -50,21 +54,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)
@@ -126,8 +122,8 @@ void BufferItem::attachIrcChannel(IrcChannel *ircChannel) {
 
   connect(ircChannel, SIGNAL(topicSet(QString)),
          this, SLOT(setTopic(QString)));
-  connect(ircChannel, SIGNAL(ircUserJoined(IrcUser *)),
-         this, SLOT(join(IrcUser *)));
+  connect(ircChannel, SIGNAL(ircUsersJoined(QList<IrcUser *>)),
+         this, SLOT(join(QList<IrcUser *>)));
   connect(ircChannel, SIGNAL(ircUserParted(IrcUser *)),
          this, SLOT(part(IrcUser *)));
   connect(ircChannel, SIGNAL(destroyed()),
@@ -141,9 +137,7 @@ void BufferItem::attachIrcChannel(IrcChannel *ircChannel) {
 
   if(!ircChannel->ircUsers().isEmpty()) {
     qWarning() << "Channel" << ircChannel->name() << "has already users which is quite surprising :)";
-    foreach(IrcUser *ircUser, ircChannel->ircUsers()) {
-      join(ircUser);
-    }
+    join(ircChannel->ircUsers());
   }
   
   emit dataChanged();
@@ -158,7 +152,15 @@ QString BufferItem::bufferName() const {
   if(bufferType() == BufferInfo::StatusBuffer)
     return tr("Status Buffer");
   else
-    return bufferInfo().bufferName();
+    return _bufferName;
+}
+
+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(), name);
+  emit dataChanged(0);
 }
 
 QString BufferItem::topic() const {
@@ -187,30 +189,41 @@ void BufferItem::setTopic(const QString &topic) {
   emit dataChanged(1);
 }
 
-void BufferItem::join(IrcUser *ircUser) {
-  if(!ircUser)
-    return;
+void BufferItem::join(const QList<IrcUser *> &ircUsers) {
+  addUsersToCategory(ircUsers);
 
-  addUserToCategory(ircUser);
-  connect(ircUser, SIGNAL(destroyed()),
-         this, SLOT(ircUserDestroyed()));
+  foreach(IrcUser *ircUser, ircUsers) {
+    if(!ircUser)
+      continue;
+    connect(ircUser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
+  }
+  
   emit dataChanged(2);
 }
 
 void BufferItem::addUserToCategory(IrcUser *ircUser) {
+  addUsersToCategory(QList<IrcUser *>() << ircUser);
+}
+
+void BufferItem::addUsersToCategory(const QList<IrcUser *> &ircUsers) {
   Q_ASSERT(_ircChannel);
 
-  UserCategoryItem *categoryItem;
-  int categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser));
-  if(!(categoryItem = qobject_cast<UserCategoryItem *>(childById(qHash(categoryId))))) {
-    categoryItem = new UserCategoryItem(categoryId, this);
-    newChild(categoryItem);
+  QHash<UserCategoryItem *, QList<IrcUser *> > categories;
+  foreach(IrcUser *ircUser, ircUsers) {
+    UserCategoryItem *categoryItem;
+    int categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser));
+    if(!(categoryItem = qobject_cast<UserCategoryItem *>(childById(qHash(categoryId))))) {
+      categoryItem = new UserCategoryItem(categoryId, this);
+      categories[categoryItem] = QList<IrcUser *>();
+      newChild(categoryItem);
+    }
+    categories[categoryItem] << ircUser;
   }
-  categoryItem->addUser(ircUser);
 
-  int totalusers = 0;
-  for(int i = 0; i < childCount(); i++) {
-    totalusers += child(i)->childCount();
+  QHash<UserCategoryItem *, QList<IrcUser *> >::const_iterator catIter = categories.constBegin();
+  while(catIter != categories.constEnd()) {
+    catIter.key()->addUsers(catIter.value());
+    catIter++;
   }
 }
 
@@ -254,11 +267,6 @@ void BufferItem::removeUserFromCategory(IrcUser *ircUser) {
     qDebug() << "==== End Of Childlist for Item:" << this << id() << bufferName() << "====";
   }
   Q_ASSERT(success);
-
-  int totalusers = 0;
-  for(int i = 0; i < childCount(); i++) {
-    totalusers += child(i)->childCount();
-  }
 }
 
 void BufferItem::userModeChanged(IrcUser *ircUser) {
@@ -273,6 +281,49 @@ void BufferItem::userModeChanged(IrcUser *ircUser) {
   addUserToCategory(ircUser);
 }
 
+QString BufferItem::toolTip(int column) const {
+  Q_UNUSED(column);
+  QStringList toolTip;
+
+  switch(bufferType()) {
+    case BufferInfo::StatusBuffer: {
+      QString netName = Client::network(bufferInfo().networkId())->networkName();
+      toolTip.append(tr("<b>Status buffer from %1</b>").arg(netName));
+      break;
+    }
+    case BufferInfo::ChannelBuffer:
+      toolTip.append(tr("<b>Channel %1</b>").arg(bufferName()));
+      if(isActive()) {
+        //TODO: add channel modes 
+        toolTip.append(tr("<b>Users:</b> %1").arg(nickCount()));
+
+        BufferSettings s;
+        bool showTopic = s.value("DisplayTopicInTooltip", QVariant(false)).toBool();
+        if(showTopic) {
+          QString _topic = topic();
+          if(_topic != "") {
+            _topic.replace(QString("<"), QString("&lt;"));
+            _topic.replace(QString(">"), QString("&gt;"));
+            toolTip.append(QString("<font size='-2'>&nbsp;</font>"));
+            toolTip.append(tr("<b>Topic:</b> %1").arg(_topic));
+          }
+        }
+      } else {
+        toolTip.append(tr("Not active <br /> Double-click to join"));
+      }
+      break;
+    case BufferInfo::QueryBuffer:
+      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(tr("%1 - %2").arg(bufferInfo().bufferId().toInt()).arg(bufferName()));
+      break;
+  }
+
+  return tr("<p> %1 </p>").arg(toolTip.join("<br />"));
+}
+
 /*
 void BufferItem::setLastMsgInsert(QDateTime msgDate) {
   if(msgDate.isValid() && msgDate > _lastMsgInsert)
@@ -363,8 +414,6 @@ void NetworkItem::attachNetwork(Network *network) {
          this, SLOT(attachIrcChannel(QString)));
   connect(network, SIGNAL(connectedSet(bool)),
          this, SIGNAL(dataChanged()));
-  
-  // FIXME: connect this and that...
 
   emit dataChanged();
 }
@@ -396,6 +445,18 @@ void NetworkItem::setCurrentServer(const QString &serverName) {
   emit dataChanged(1);
 }
 
+
+QString NetworkItem::toolTip(int column) const {
+  Q_UNUSED(column);
+
+  QStringList toolTip(QString("<b>%1</b>").arg(networkName()));
+  toolTip.append(QString("Server: %1").arg(currentServer()));
+  toolTip.append(QString("Users: %1").arg(nickCount()));
+
+  return QString("<p> %1 </p>").arg(toolTip.join("<br />"));
+}
+
+
 /*****************************************
 *  User Category Items (like @vh etc.)
 *****************************************/
@@ -428,8 +489,11 @@ quint64 UserCategoryItem::id() const {
   return qHash(_category);
 }
 
-void UserCategoryItem::addUser(IrcUser *ircUser) {
-  newChild(new IrcUserItem(ircUser, this));
+void UserCategoryItem::addUsers(const QList<IrcUser *> &ircUsers) {
+  QList<AbstractTreeItem *> userItems;
+  foreach(IrcUser *ircUser, ircUsers)
+    userItems << new IrcUserItem(ircUser, this);
+  newChilds(userItems);
 }
 
 bool UserCategoryItem::removeUser(IrcUser *ircUser) {
@@ -475,6 +539,8 @@ IrcUserItem::IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent)
   
   connect(ircUser, SIGNAL(nickSet(QString)),
          this, SLOT(setNick(QString)));
+  connect(ircUser, SIGNAL(awaySet(bool)),
+          this, SLOT(setAway(bool)));
 }
 
 QString IrcUserItem::nickName() const {
@@ -519,33 +585,21 @@ 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());
+  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()));
@@ -558,6 +612,11 @@ void IrcUserItem::setNick(QString newNick) {
   emit dataChanged(0);
 }
 
+void IrcUserItem::setAway(bool away) {
+  Q_UNUSED(away);
+  emit dataChanged(0);
+}
+
 /*****************************************
  * NetworkModel
  *****************************************/