Browse Source

Added Titlebar and /me support, fixed a bug with race conditions & query tabs

Niklas Boelter 12 years ago
parent
commit
93efbcb2d7
5 changed files with 103 additions and 8 deletions
  1. 93 2
      Controller.cpp
  2. 4 1
      Controller.h
  3. 4 4
      MainWindow.cpp
  4. 1 0
      MainWindow.h
  5. 1 1
      main.cpp

+ 93 - 2
Controller.cpp

@@ -37,6 +37,7 @@ Controller::Controller(MainWindow *mw)
 	connect(this, SIGNAL(got003(int, QStringList, Prefix)), this, SLOT(handle003(int, QStringList, Prefix)));
 	connect(this, SIGNAL(got004(int, QStringList, Prefix)), this, SLOT(handle004(int, QStringList, Prefix)));
 	connect(this, SIGNAL(got005(int, QStringList, Prefix)), this, SLOT(handle005(int, QStringList, Prefix)));
+	connect(this, SIGNAL(got332(int, QStringList, Prefix)), this, SLOT(handle332(int, QStringList, Prefix)));
 	connect(this, SIGNAL(got353(int, QStringList, Prefix)), this, SLOT(handle353(int, QStringList, Prefix)));
 	connect(this, SIGNAL(got366(int, QStringList, Prefix)), this, SLOT(handle366(int, QStringList, Prefix)));
 	
@@ -82,8 +83,13 @@ void Controller::openConnections()
 		prefix.insert('+', 'v');
 		prefix.insert('@', 'o');
 		support_prefix.insert(i, prefix);
+		
 		channels.append(QSet<QString>());
+		
 		registered.insert(i, false);
+		
+		titlebars.insert(QString("%1").arg(i), "Connecting ...");
+		
 		Connection *connection = new Connection(i);
 		connect(connection, SIGNAL( dataReady(int) ), this, SLOT( dataReady(int) ));
 		connect(connection, SIGNAL( connected(int) ), this, SLOT( connected(int) ));
@@ -134,6 +140,9 @@ void Controller::socketmessage(int cid, QString message)
 }
 void Controller::connected(int cid)
 {
+	titlebars.insert(QString("%1").arg(cid), 
+					 QString("Connected to %1:%2").arg(connectionsettings.at(cid).host).arg(connectionsettings.at(cid).port));
+	updateTitleBar();
 	socketmessage(cid, QString("Connected to %1").arg(connectionsettings.at(cid).name));
 	// TODO
 	mynicks.insert(cid, settings.value("nickname").toString());
@@ -142,6 +151,9 @@ void Controller::connected(int cid)
 }
 void Controller::disconnected(int cid)
 {
+	titlebars.insert(QString("%1").arg(cid), 
+					 QString("Disconnected from %1:%2").arg(connectionsettings.at(cid).host).arg(connectionsettings.at(cid).port));
+	updateTitleBar();
 	socketmessage(cid, QString("Disconnected from %1").arg(connectionsettings.at(cid).name));
 }
 void Controller::parse(int cid, QString line)
@@ -253,7 +265,17 @@ bool Controller::isServer(QString prefix)
 }
 void Controller::readInput(QString line)
 {
+	QString tmp;
 	if(line.size() == 0) return;
+	if(line.toLower().startsWith("/me ") && tab.name != "")
+	{
+		line = line.mid(3);
+		tmp = QString("PRIVMSG %1 :%2ACTION%3%4").arg(tab.name).arg(char(1)).arg(line).arg(char(1));
+		sendLine(tab.cid, tmp);
+		replaceHTML(line);
+		addLine(getTab(tab.identifier), QString("<strong style=\"color:blue\">*%1</strong><span style=\"white-space:pre\">%2</span>").arg(mynicks.value(tab.cid)).arg(line));
+		return;
+	}
 	if(line.startsWith("/"))
 	{
 			line = line.mid(1);
@@ -286,14 +308,27 @@ void Controller::addTab(QString key, quint16 maximumBlockCount)
 }
 void Controller::renameTab(int cid, QString oldName, QString newName)
 {
+	Q_ASSERT(!channels[cid].contains(oldName)); // This should never be called for channel tabs!
+	Q_ASSERT(!channels[cid].contains(newName));
 	QString oldKey = QString("%1/%2").arg(cid).arg(oldName);
 	QString newKey = QString("%1/%2").arg(cid).arg(newName);
+	QString titlebar;
 	QTextCursor * cursor = getTab(oldKey);
 	if(cursor == 0)
 	{
 		return;
 	}
 	tabs.remove(oldKey);
+	if(tabs.contains(newKey)) // Tab Name collision! We rename the other one as it is probably just a disconnected user's query
+	{
+		renameTab(cid, newName, QString("%1~").arg(newName));
+	}
+	if(titlebars.contains(oldKey)) // Also rename the Titlebar!
+	{
+		titlebar = titlebars.value(oldKey);
+		titlebars.remove(oldKey);
+		titlebars.insert(newKey, titlebar);
+	}
 	tabs.insert(newKey, cursor);
 	mw->renameTab(oldKey, newKey, newName);
 }
@@ -345,6 +380,7 @@ void Controller::tabChanged(QString identifier)
 		tab.name = identifier.mid(pos+1);
 	}
 	updateChannelList();
+	updateTitleBar();
 }
 void Controller::updateChannelList()
 {
@@ -359,6 +395,19 @@ void Controller::updateChannelList()
 		return;
 	}
 }
+void Controller::updateTitleBar()
+{
+	if(titlebars.contains(tab.identifier))
+	{
+		mw->setTitleBarContent(titlebars.value(tab.identifier));
+	}
+	else
+	{
+		qWarning() << "Got tab without titlebar";
+		mw->setTitleBarContent("");
+	}
+
+}
 void Controller::handleJoin(int cid, QStringList params, Prefix prefix)
 {
 	if(params.count() < 1)
@@ -450,6 +499,11 @@ void Controller::handlePrivmsg(int cid, QStringList params, Prefix prefix)
 {
 	QTextCursor *cursor;
 	QString line = params.at(1);
+	if(line.contains(mynicks.value(cid)))
+	{
+		// TODO
+		QSound::play("/Applications/Adium.app/Contents/Resources/Sounds/TokyoTrainStation.AdiumSoundset/New_Message.m4a");
+	}
 	replaceHTML(line);
 	line = line.replace(QRegExp("((http|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?)"),"<a href=\"\\1\">\\1</a>");
 	line = QString("<strong>&lt;%1&gt;</strong>&nbsp;<span style=\"white-space:pre\">%2</span>").arg(prefix.nickname).arg(line);
@@ -459,14 +513,26 @@ void Controller::handlePrivmsg(int cid, QStringList params, Prefix prefix)
 	}
 	else
 	{
-		cursor = getTab(cid, prefix.nickname);
+		QString identifier = QString("%1/%2").arg(cid).arg(prefix.nickname);
+		cursor = getTab(identifier);
 		if(cursor == 0)
 		{
-			QString identifier = QString("%1/%2").arg(cid).arg(prefix.nickname);
+			// TODO
+			QSound::play("/Applications/Adium.app/Contents/Resources/Sounds/TokyoTrainStation.AdiumSoundset/New_Message.m4a");
 			addTab(identifier, settings.value("maxPrivateHistory").toInt());
 			mw->addTab(prefix.nickname, identifier, QString("%1").arg(cid), true);
 			cursor = getTab(cid, prefix.nickname);
 		}
+		if(!prefix.isServer)
+		{
+			QString prefstr = QString("%1!%2@%3").arg(prefix.nickname).arg(prefix.username).arg(prefix.hostname);
+			if(titlebars.value(identifier) != prefstr)
+			{
+				qDebug() << "Updating prefix in query title bar";
+				titlebars.insert(identifier, prefstr);
+				updateTitleBar();
+			}
+		}
 	}
 	if(cursor != 0) 
 	{
@@ -533,12 +599,23 @@ void Controller::handleNick(int cid, QStringList params, Prefix prefix)
 			addLine(cursor, QString("%1 is now known as %2.").arg(prefix.nickname).arg(params.at(0)));
 		}
 		userlists->changeNick(cid, channel, prefix.nickname, params.at(0));
+		if(tab.identifier == identifier)
+		{
+			updateChannelList();
+		}
 	}
 	cursor = getTab(cid, prefix.nickname);
 	if(cursor != 0)
 	{
 		addLine(cursor, QString("%1 is now known as %2.").arg(prefix.nickname).arg(params.at(0)));
 		renameTab(cid, prefix.nickname, params.at(0));
+		QString prefstr = QString("%1!%2@%3").arg(prefix.nickname).arg(prefix.username).arg(prefix.hostname);
+		qDebug() << "Updating prefix in query title bar";
+		titlebars.insert(identifier, prefstr);
+		if(tab.cid == cid && tab.name == prefix.nickname)
+		{
+			updateTitleBar();
+		}
 	}
 }
 void Controller::handleNotice(int cid, QStringList params, Prefix prefix)
@@ -597,6 +674,20 @@ void Controller::handle004(int cid, QStringList params, Prefix prefix)
 void Controller::handle005(int cid, QStringList params, Prefix prefix)
 {
 }
+void Controller::handle332(int cid, QStringList params, Prefix prefix)
+{
+	if(params.count() < 3)
+	{
+		qWarning() << "Not enough parameters for RPL_TOPIC";
+		return;
+	}
+	QString identifier = QString("%1/%2").arg(cid).arg(params.at(1));
+	titlebars.insert(identifier, params.at(2));
+	if(tab.identifier == identifier)
+	{
+		updateTitleBar();
+	}
+}
 void Controller::handle353(int cid, QStringList params, Prefix prefix)
 {
 	QString identifier;

+ 4 - 1
Controller.h

@@ -70,6 +70,7 @@ signals:
 	void got003(int cid, QStringList params, Prefix prefix);
 	void got004(int cid, QStringList params, Prefix prefix);
 	void got005(int cid, QStringList params, Prefix prefix);
+	void got332(int cid, QStringList params, Prefix prefix);
 	void got353(int cid, QStringList params, Prefix prefix);
 	void got366(int cid, QStringList params, Prefix prefix);
 	
@@ -83,6 +84,7 @@ private:
 	// Content of Tabs, Channel User Lists
 	QHash<QString, QTextCursor *> tabs;
 	UserLists *userlists;
+	QHash<QString, QString> titlebars;
 	// List of Connections, each having a channel name hash which points to a hash of nickname -> NamesEntry
 	// My own nicks
 	QHash<int, QString> mynicks;
@@ -101,7 +103,7 @@ private:
 	QTextCursor* getTab(int cid, QString name); // getTab() returns 0 when there is no matching tab(!)
 	QTextCursor* getTab(QString identifier); // getTab() returns 0 when there is no matching tab(!)
 	void updateChannelList();
-	
+	void updateTitleBar();
 	// Content Stuff
 	void addLine(QTextCursor *cursor, QString line);
 	void replaceHTML(QString & line);
@@ -133,6 +135,7 @@ private slots:
 	void handle003(int cid, QStringList params, Prefix prefix);
 	void handle004(int cid, QStringList params, Prefix prefix);
 	void handle005(int cid, QStringList params, Prefix prefix);
+	void handle332(int cid, QStringList params, Prefix prefix);
 	void handle353(int cid, QStringList params, Prefix prefix);
 	void handle366(int cid, QStringList params, Prefix prefix);
 };

+ 4 - 4
MainWindow.cpp

@@ -128,10 +128,6 @@ void MainWindow::renameTab(QString oldIdentifier, QString newIdentifier, QString
 {
 	QTreeWidgetItem *item = tabs.value(oldIdentifier);
 	tabs.remove(oldIdentifier);
-	if(tabs.contains(newIdentifier))
-	{
-		renameTab(newIdentifier, QString("%1~").arg(newIdentifier), QString("%1~").arg(newLabel));
-	}
 	tabs.insert(newIdentifier, item);
 	item->setText(0, newLabel);
 	item->setData(0, Qt::UserRole, newIdentifier);
@@ -160,6 +156,10 @@ void MainWindow::setChannelList(QStringList list)
 	channelmodel->setStringList(list);
 	channelobjects->setVisible(true);
 }
+void MainWindow::setTitleBarContent(QString content)
+{
+	titlebar->setText(content);
+}
 void MainWindow::hideChannelList()
 {
 	channelobjects->setVisible(false);

+ 1 - 0
MainWindow.h

@@ -42,6 +42,7 @@ public:
 	bool contentAtBottom();
 	void contentScrollDown();
 	void setChannelList(QStringList list);
+	void setTitleBarContent(QString content);
 	void hideChannelList();
 private:
 	QList<QString> history;

+ 1 - 1
main.cpp

@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
 	freenode.name = "Freenode";
 	freenode.isSSL = true;
 	freenode.ignoreSSLErrors = true;
-	//connections.append(freenode);
+	connections.append(freenode);
 	settings.beginWriteArray("connections", connections.size());
 	for(int i = 0; i < connections.size(); i++)
 	{