From 68263f7cb360ae52915017df202775665c4eeeb6 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Tue, 28 Apr 2015 14:28:02 +0200
Subject: Allow editing bookmarks of MUC chats in the "Recent Chats"

Test-Information:

Tested with online and offline account. Tested with not bookmarked
and already bookmarked chats. Works as expected.

Change-Id: Ib8851a70a7a82a198ee5b7a207816f03ad9df61e

diff --git a/Swift/QtUI/ChatList/ChatListModel.cpp b/Swift/QtUI/ChatList/ChatListModel.cpp
index b7ce239..2447aa1 100644
--- a/Swift/QtUI/ChatList/ChatListModel.cpp
+++ b/Swift/QtUI/ChatList/ChatListModel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -131,6 +131,18 @@ QMimeData* ChatListModel::mimeData(const QModelIndexList& indexes) const {
 	return data;
 }
 
+const ChatListMUCItem* ChatListModel::getChatListMUCItem(const JID& roomJID) const {
+	const ChatListMUCItem* mucItem = NULL;
+	for (int i = 0; i < mucBookmarks_->rowCount(); i++) {
+		ChatListMUCItem* item = dynamic_cast<ChatListMUCItem*>(mucBookmarks_->item(i));
+		if (item->getBookmark().getRoom() == roomJID) {
+			mucItem = item;
+			break;
+		}
+	}
+	return mucItem;
+}
+
 int ChatListModel::columnCount(const QModelIndex& /*parent*/) const {
 	return 1;
 }
diff --git a/Swift/QtUI/ChatList/ChatListModel.h b/Swift/QtUI/ChatList/ChatListModel.h
index fc0ae32..ea85efb 100644
--- a/Swift/QtUI/ChatList/ChatListModel.h
+++ b/Swift/QtUI/ChatList/ChatListModel.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -10,11 +10,14 @@
 #include <QPersistentModelIndex>
 
 #include <Swiften/MUC/MUCBookmark.h>
+
 #include <Swift/Controllers/UIInterfaces/ChatListWindow.h>
 
 #include <Swift/QtUI/ChatList/ChatListGroupItem.h>
 
 namespace Swift {
+	class ChatListMUCItem;
+
 	class ChatListModel : public QAbstractItemModel {
 		Q_OBJECT
 		public:
@@ -33,6 +36,9 @@ namespace Swift {
 			void clearBookmarks();
 			void setRecents(const std::list<ChatListWindow::Chat>& recents);
 			QMimeData* mimeData(const QModelIndexList& indexes) const;
+
+			const ChatListMUCItem* getChatListMUCItem(const JID& roomJID) const;
+
 		private:
 			ChatListGroupItem* mucBookmarks_;
 			ChatListGroupItem* recents_;
diff --git a/Swift/QtUI/ChatList/QtChatListWindow.cpp b/Swift/QtUI/ChatList/QtChatListWindow.cpp
index 71d4f1b..7b40f9c 100644
--- a/Swift/QtUI/ChatList/QtChatListWindow.cpp
+++ b/Swift/QtUI/ChatList/QtChatListWindow.cpp
@@ -1,10 +1,10 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
 
-#include "Swift/QtUI/ChatList/QtChatListWindow.h"
+#include <Swift/QtUI/ChatList/QtChatListWindow.h>
 
 #include <boost/bind.hpp>
 
@@ -20,6 +20,7 @@
 #include <Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h>
 #include <Swift/Controllers/UIEvents/RequestChatUIEvent.h>
 #include <Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h>
+
 #include <Swift/QtUI/ChatList/ChatListMUCItem.h>
 #include <Swift/QtUI/ChatList/ChatListRecentItem.h>
 #include <Swift/QtUI/ChatList/ChatListWhiteboardItem.h>
@@ -58,7 +59,6 @@ QtChatListWindow::~QtChatListWindow() {
 	delete model_;
 	delete delegate_;
 	delete mucMenu_;
-	delete mucRecentsMenu_;
 	delete emptyMenu_;
 }
 
@@ -89,9 +89,6 @@ void QtChatListWindow::setupContextMenus() {
 	onlineOnlyActions_ << mucMenu_->addAction(tr("Add New Bookmark"), this, SLOT(handleAddBookmark()));
 	onlineOnlyActions_ << mucMenu_->addAction(tr("Edit Bookmark"), this, SLOT(handleEditBookmark()));
 	onlineOnlyActions_ << mucMenu_->addAction(tr("Remove Bookmark"), this, SLOT(handleRemoveBookmark()));
-	mucRecentsMenu_ = new QMenu();
-	onlineOnlyActions_ << mucRecentsMenu_->addAction(tr("Add to Bookmarks"), this, SLOT(handleAddBookmarkFromRecents()));
-	mucRecentsMenu_->addAction(tr("Clear recents"), this, SLOT(handleClearRecentsRequested()));
 	emptyMenu_ = new QMenu();
 	onlineOnlyActions_ << emptyMenu_->addAction(tr("Add New Bookmark"), this, SLOT(handleAddBookmark()));
 }
@@ -148,13 +145,13 @@ void QtChatListWindow::setOnline(bool isOnline) {
 }
 
 void QtChatListWindow::handleRemoveBookmark() {
-	ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(contextMenuItem_);
+	const ChatListMUCItem* mucItem = dynamic_cast<const ChatListMUCItem*>(contextMenuItem_);
 	if (!mucItem) return;
 	eventStream_->send(boost::shared_ptr<UIEvent>(new RemoveMUCBookmarkUIEvent(mucItem->getBookmark())));
 }
 
 void QtChatListWindow::handleAddBookmarkFromRecents() {
-	ChatListRecentItem* item = dynamic_cast<ChatListRecentItem*>(contextMenuItem_);
+	const ChatListRecentItem* item = dynamic_cast<const ChatListRecentItem*>(contextMenuItem_);
 	if (item) {
 		const ChatListWindow::Chat& chat = item->getChat();
 		MUCBookmark bookmark(chat.jid, chat.jid.toBare().toString());
@@ -170,7 +167,7 @@ void QtChatListWindow::handleAddBookmark() {
 
 
 void QtChatListWindow::handleEditBookmark() {
-	ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(contextMenuItem_);
+	const ChatListMUCItem* mucItem = dynamic_cast<const ChatListMUCItem*>(contextMenuItem_);
 	if (!mucItem) return;
 	QtEditBookmarkWindow* window = new QtEditBookmarkWindow(eventStream_, mucItem->getBookmark());
 	window->show();
@@ -209,7 +206,19 @@ void QtChatListWindow::contextMenuEvent(QContextMenuEvent* event) {
 	if (recentItem) {
 		const ChatListWindow::Chat& chat = recentItem->getChat();
 		if (chat.isMUC) {
-			mucRecentsMenu_->exec(QCursor::pos());
+			QMenu mucRecentsMenu;
+			QAction* bookmarkAction = NULL;
+			const ChatListMUCItem* mucItem = model_->getChatListMUCItem(chat.jid);
+			if (mucItem) {
+				contextMenuItem_ = mucItem;
+				bookmarkAction = mucRecentsMenu.addAction(tr("Edit Bookmark"), this, SLOT(handleEditBookmark()));
+			}
+			else {
+				bookmarkAction = mucRecentsMenu.addAction(tr("Add to Bookmarks"), this, SLOT(handleAddBookmarkFromRecents()));
+			}
+			bookmarkAction->setEnabled(isOnline_);
+			mucRecentsMenu.addAction(tr("Clear recents"), this, SLOT(handleClearRecentsRequested()));
+			mucRecentsMenu.exec(QCursor::pos());
 			return;
 		}
 	}
diff --git a/Swift/QtUI/ChatList/QtChatListWindow.h b/Swift/QtUI/ChatList/QtChatListWindow.h
index 9738413..627dcd4 100644
--- a/Swift/QtUI/ChatList/QtChatListWindow.h
+++ b/Swift/QtUI/ChatList/QtChatListWindow.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -8,10 +8,11 @@
 
 #include <QTreeView>
 
-#include "Swift/Controllers/UIInterfaces/ChatListWindow.h"
-#include "Swift/Controllers/UIEvents/UIEventStream.h"
-#include "Swift/QtUI/ChatList/ChatListModel.h"
-#include "Swift/QtUI/ChatList/ChatListDelegate.h"
+#include <Swift/Controllers/UIEvents/UIEventStream.h>
+#include <Swift/Controllers/UIInterfaces/ChatListWindow.h>
+
+#include <Swift/QtUI/ChatList/ChatListDelegate.h>
+#include <Swift/QtUI/ChatList/ChatListModel.h>
 
 namespace Swift {
 	class SettingsProvider;
@@ -54,8 +55,7 @@ namespace Swift {
 			ChatListDelegate* delegate_;
 			QMenu* mucMenu_;
 			QMenu* emptyMenu_;
-			QMenu* mucRecentsMenu_;
-			ChatListItem* contextMenuItem_;
+			const ChatListItem* contextMenuItem_;
 			SettingsProvider* settings_;
 			QList<QAction*> onlineOnlyActions_;
 			bool isOnline_;
-- 
cgit v0.10.2-6-g49f6