summary refs log tree commit diff
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-04-15 19:04:02 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-04-15 19:04:02 +0300
commite1d48367f8ccb83fc1fcdaee899e5bf4f8ac2669 (patch)
treeae84a8e5f6d1208d5cbf7a05c6e8ab2eaf322c48
parentMove the main window to the center (diff)
downloadnheko-e1d48367f8ccb83fc1fcdaee899e5bf4f8ac2669.tar.xz
Show the unread message count on the window title
-rw-r--r--include/ChatPage.h2
-rw-r--r--include/RoomInfoListItem.h6
-rw-r--r--include/RoomList.h3
-rw-r--r--src/ChatPage.cc15
-rw-r--r--src/MainWindow.cc1
-rw-r--r--src/RoomList.cc15
6 files changed, 41 insertions, 1 deletions
diff --git a/include/ChatPage.h b/include/ChatPage.h
index fd0b63dc..182dca3a 100644
--- a/include/ChatPage.h
+++ b/include/ChatPage.h
@@ -48,8 +48,10 @@ public:
 
 signals:
 	void close();
+	void changeWindowTitle(const QString &msg);
 
 private slots:
+	void showUnreadMessageNotification(int count);
 	void updateTopBarAvatar(const QString &roomid, const QPixmap &img);
 	void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name);
 	void setOwnAvatar(const QPixmap &img);
diff --git a/include/RoomInfoListItem.h b/include/RoomInfoListItem.h
index 24e84bd3..5c403dc3 100644
--- a/include/RoomInfoListItem.h
+++ b/include/RoomInfoListItem.h
@@ -42,6 +42,7 @@ public:
 	inline bool isPressed();
 	inline RoomInfo info();
 	inline void setAvatar(const QImage &avatar_image);
+	inline int unreadMessageCount();
 
 signals:
 	void clicked(const RoomInfo &info_);
@@ -82,6 +83,11 @@ private:
 	int unread_msg_count_;
 };
 
+inline int RoomInfoListItem::unreadMessageCount()
+{
+	return unread_msg_count_;
+}
+
 inline bool RoomInfoListItem::isPressed()
 {
 	return is_pressed_;
diff --git a/include/RoomList.h b/include/RoomList.h
index d6066166..e22f0954 100644
--- a/include/RoomList.h
+++ b/include/RoomList.h
@@ -48,6 +48,7 @@ public:
 
 signals:
 	void roomChanged(const RoomInfo &info);
+	void totalUnreadMessageCountUpdated(int count);
 
 public slots:
 	void updateRoomAvatar(const QString &roomid, const QPixmap &img);
@@ -55,6 +56,8 @@ public slots:
 	void updateUnreadMessageCount(const QString &roomid, int count);
 
 private:
+	void calculateUnreadMessageCount();
+
 	Ui::RoomList *ui;
 
 	QMap<QString, RoomInfoListItem *> rooms_;
diff --git a/src/ChatPage.cc b/src/ChatPage.cc
index 33cacb3e..3bae177b 100644
--- a/src/ChatPage.cc
+++ b/src/ChatPage.cc
@@ -64,11 +64,17 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
 		view_manager_,
 		SLOT(setHistoryView(const RoomInfo &)));
 
+	// TODO: Better pass the whole RoomInfo struct instead of the roomid.
 	connect(view_manager_,
 		SIGNAL(unreadMessages(const QString &, int)),
 		room_list_,
 		SLOT(updateUnreadMessageCount(const QString &, int)));
 
+	connect(room_list_,
+		SIGNAL(totalUnreadMessageCountUpdated(int)),
+		this,
+		SLOT(showUnreadMessageNotification(int)));
+
 	connect(text_input_,
 		SIGNAL(sendTextMessage(const QString &)),
 		view_manager_,
@@ -206,6 +212,15 @@ void ChatPage::changeTopRoomInfo(const RoomInfo &info)
 	current_room_ = info;
 }
 
+void ChatPage::showUnreadMessageNotification(int count)
+{
+	// TODO: Make the default title a const.
+	if (count == 0)
+		emit changeWindowTitle("nheko");
+	else
+		emit changeWindowTitle(QString("nheko (%1)").arg(count));
+}
+
 ChatPage::~ChatPage()
 {
 	sync_timer_->stop();
diff --git a/src/MainWindow.cc b/src/MainWindow.cc
index d0d01bcb..3d591755 100644
--- a/src/MainWindow.cc
+++ b/src/MainWindow.cc
@@ -51,6 +51,7 @@ MainWindow::MainWindow(QWidget *parent)
 	connect(register_page_, SIGNAL(backButtonClicked()), this, SLOT(showWelcomePage()));
 
 	connect(chat_page_, SIGNAL(close()), this, SLOT(showWelcomePage()));
+	connect(chat_page_, SIGNAL(changeWindowTitle(QString)), this, SLOT(setWindowTitle(QString)));
 
 	connect(client_.data(),
 		SIGNAL(loginSuccess(QString, QString, QString)),
diff --git a/src/RoomList.cc b/src/RoomList.cc
index b1186d39..99553842 100644
--- a/src/RoomList.cc
+++ b/src/RoomList.cc
@@ -89,6 +89,18 @@ void RoomList::updateUnreadMessageCount(const QString &roomid, int count)
 	}
 
 	rooms_[roomid]->updateUnreadMessageCount(count);
+
+	calculateUnreadMessageCount();
+}
+
+void RoomList::calculateUnreadMessageCount()
+{
+	int total_unread_msgs = 0;
+
+	for (const auto &room : rooms_)
+		total_unread_msgs += room->unreadMessageCount();
+
+	emit totalUnreadMessageCountUpdated(total_unread_msgs);
 }
 
 void RoomList::setInitialRooms(const Rooms &rooms)
@@ -132,11 +144,12 @@ void RoomList::highlightSelectedRoom(const RoomInfo &info)
 		return;
 	}
 
-
 	// TODO: Send a read receipt for the last event.
 	auto room = rooms_[info.id()];
 	room->clearUnreadMessageCount();
 
+	calculateUnreadMessageCount();
+
 	for (auto it = rooms_.constBegin(); it != rooms_.constEnd(); it++) {
 		if (it.key() != info.id())
 			it.value()->setPressedState(false);