From f1675c651a08c294447389176262ab890cc6e7b5 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 4 Apr 2010 12:55:11 +0100
Subject: Add 'remove' context menu item for rosters (not implemented).


diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp
index 888d324..f05bc9f 100644
--- a/Swift/Controllers/RosterController.cpp
+++ b/Swift/Controllers/RosterController.cpp
@@ -19,6 +19,7 @@
 #include "Swiften/Roster/TreeWidgetFactory.h"
 #include "Swiften/Roster/XMPPRoster.h"
 #include "Swift/Controllers/UIEvents/AddContactUIEvent.h"
+#include "Swift/Controllers/UIEvents/RemoveItemRosterAction.h"
 
 
 namespace Swift {
@@ -96,6 +97,15 @@ void RosterController::handleUserAction(boost::shared_ptr<UserRosterAction> acti
 		ContactRosterItem *contactItem = dynamic_cast<ContactRosterItem*>(chatAction->getRosterItem());
 		assert(contactItem);
 		onStartChatRequest(contactItem->getJID().toBare());
+		return;
+	}
+
+	boost::shared_ptr<RemoveItemRosterAction> removeAction = boost::dynamic_pointer_cast<RemoveItemRosterAction>(action);
+	if (removeAction.get() != NULL) {
+		ContactRosterItem *contactItem = dynamic_cast<ContactRosterItem*>(chatAction->getRosterItem());
+		assert(contactItem);
+		//FIXME: remove it
+		return;
 	}
 }
 
@@ -142,6 +152,7 @@ void RosterController::handleOnJIDUpdated(const JID& jid, const String& oldName,
 void RosterController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
 	boost::shared_ptr<AddContactUIEvent> addContactEvent = boost::dynamic_pointer_cast<AddContactUIEvent>(event);
 	if (addContactEvent) {
+		
 		presenceOracle_->requestSubscription(addContactEvent->getJID());
 	}
 }
diff --git a/Swift/Controllers/UIEvents/RemoveItemRosterAction.h b/Swift/Controllers/UIEvents/RemoveItemRosterAction.h
new file mode 100644
index 0000000..6741bc6
--- /dev/null
+++ b/Swift/Controllers/UIEvents/RemoveItemRosterAction.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "Swiften/Roster/UserRosterAction.h"
+
+namespace Swift {
+class RosterItem;
+class TreeWidgetItem;
+
+class RemoveItemRosterAction : public UserRosterAction {
+	public:
+		virtual ~RemoveItemRosterAction() {};
+
+};
+
+}
diff --git a/Swift/QtUI/ChatList/ChatListMUCItem.cpp b/Swift/QtUI/ChatList/ChatListMUCItem.cpp
index 428cf4b..3a32495 100644
--- a/Swift/QtUI/ChatList/ChatListMUCItem.cpp
+++ b/Swift/QtUI/ChatList/ChatListMUCItem.cpp
@@ -12,7 +12,6 @@ boost::shared_ptr<MUCBookmark> ChatListMUCItem::getBookmark() {
 }
 
 QVariant ChatListMUCItem::data(int role) { 
-	printf("Getting role %d\n", role);
 	switch (role) {
 		case Qt::DisplayRole: return P2QSTRING(bookmark_->getName());
 			/*case Qt::TextColorRole: return textColor_;
diff --git a/Swift/QtUI/ChatList/ChatListModel.cpp b/Swift/QtUI/ChatList/ChatListModel.cpp
index 1a1bb80..827650c 100644
--- a/Swift/QtUI/ChatList/ChatListModel.cpp
+++ b/Swift/QtUI/ChatList/ChatListModel.cpp
@@ -59,15 +59,12 @@ QModelIndex ChatListModel::parent(const QModelIndex& index) const {
 
 int ChatListModel::rowCount(const QModelIndex& parentIndex) const {
 	ChatListGroupItem* parent = NULL;
-	printf("Counting\n");
 	if (parentIndex.isValid()) {
-		printf("Valid index\n");
 		parent = dynamic_cast<ChatListGroupItem*>(static_cast<ChatListItem*>(parentIndex.internalPointer()));
 	} else {
 		parent = root_;
 	}
 	int count = (parent ? parent->rowCount() : 0);
-	printf("Count returned as %d, muc count is %d\n", count, mucBookmarks_->rowCount());
 	return count;
 }
 
diff --git a/Swift/QtUI/ContextMenus/QtContextmenu.h b/Swift/QtUI/ContextMenus/QtContextmenu.h
new file mode 100644
index 0000000..cc0227d
--- /dev/null
+++ b/Swift/QtUI/ContextMenus/QtContextmenu.h
@@ -0,0 +1,9 @@
+#pragma once
+
+namespace Swift {
+	class QtTreeWidgetItem;
+	class QtContextMenu {
+		public:
+			virtual void show(QtTreeWidgetItem* item) = 0;
+	};
+}
diff --git a/Swift/QtUI/ContextMenus/QtRosterContextMenu.cpp b/Swift/QtUI/ContextMenus/QtRosterContextMenu.cpp
new file mode 100644
index 0000000..feb1226
--- /dev/null
+++ b/Swift/QtUI/ContextMenus/QtRosterContextMenu.cpp
@@ -0,0 +1,33 @@
+#include "Swift/QtUI/ContextMenus/QtRosterContextMenu.h"
+
+#include <QMenu>
+#include <QDebug>
+
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/String.h"
+#include "Swift/Controllers/UIEvents/RemoveItemRosterAction.h"
+#include "Swift/QtUI/Roster/QtTreeWidgetItem.h"
+#include "Swift/QtUI/QtSwiftUtil.h"
+
+namespace Swift {
+
+QtRosterContextMenu::QtRosterContextMenu(UIEventStream* eventStream) {
+	eventStream_ = eventStream;
+}
+
+void QtRosterContextMenu::show(QtTreeWidgetItem* item) {
+	if (!item->isContact()) {
+		return;
+	}
+	item_ = item;
+	QMenu* contextMenu = new QMenu();
+	contextMenu->addAction("Remove", this, SLOT(handleRemove()));
+	contextMenu->exec(QCursor::pos());
+}
+
+void QtRosterContextMenu::handleRemove() {
+	item_->performUserAction(boost::shared_ptr<UserRosterAction>(new RemoveItemRosterAction()));
+}
+
+}
diff --git a/Swift/QtUI/ContextMenus/QtRosterContextMenu.h b/Swift/QtUI/ContextMenus/QtRosterContextMenu.h
new file mode 100644
index 0000000..74dcb98
--- /dev/null
+++ b/Swift/QtUI/ContextMenus/QtRosterContextMenu.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <QObject>
+
+#include "Swift/QtUI/ContextMenus/QtContextMenu.h"
+#include "Swift/Controllers/UIEvents/UIEventStream.h"
+
+namespace Swift {
+	class QtRosterContextMenu : public QObject, public QtContextMenu {
+		Q_OBJECT
+		public:
+			QtRosterContextMenu(UIEventStream* eventStream);
+			void show(QtTreeWidgetItem* item);
+
+		private slots:
+			void handleRemove();
+
+		private:
+			QtTreeWidgetItem* item_;
+			UIEventStream* eventStream_;
+	};
+}
diff --git a/Swift/QtUI/QtMainWindow.cpp b/Swift/QtUI/QtMainWindow.cpp
index b8d9a5c..c0276c4 100644
--- a/Swift/QtUI/QtMainWindow.cpp
+++ b/Swift/QtUI/QtMainWindow.cpp
@@ -44,6 +44,8 @@ QtMainWindow::QtMainWindow(UIEventStream* uiEventStream, QtTreeWidgetFactory *tr
 	contactTabLayout->setContentsMargins(0, 0, 0, 0);
 	
 	treeWidget_ = dynamic_cast<QtTreeWidget*>(treeWidgetFactory->createTreeWidget());
+	contextMenu_ = new QtRosterContextMenu(uiEventStream_);
+	treeWidget_->setContextMenu(contextMenu_);
 	treeWidget_->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
 	contactTabLayout->addWidget(treeWidget_);
 
@@ -80,6 +82,10 @@ QtMainWindow::QtMainWindow(UIEventStream* uiEventStream, QtTreeWidgetFactory *tr
 	chatMenu->addAction(signOutAction);
 }
 
+QtMainWindow::~QtMainWindow() {
+	delete contextMenu_;
+}
+
 QtEventWindow* QtMainWindow::getEventWindow() {
 	return eventWindow_;
 }
diff --git a/Swift/QtUI/QtMainWindow.h b/Swift/QtUI/QtMainWindow.h
index ac3e444..414d11e 100644
--- a/Swift/QtUI/QtMainWindow.h
+++ b/Swift/QtUI/QtMainWindow.h
@@ -7,6 +7,7 @@
 #include "Swift/QtUI/QtRosterHeader.h"
 #include "Swift/QtUI/EventViewer/QtEventWindow.h"
 #include "Swift/QtUI/ChatList/QtChatListWindow.h"
+#include "Swift/QtUI/ContextMenus/QtRosterContextMenu.h"
 
 #include <vector>
 
@@ -29,6 +30,7 @@ namespace Swift {
 		Q_OBJECT
 		public:
 			QtMainWindow(UIEventStream* eventStream, QtTreeWidgetFactory *treeWidgetFactory);
+			~QtMainWindow();
 			TreeWidget* getTreeWidget();
 			std::vector<QMenu*> getMenus() {return menus_;}
 			void setMyName(const String& name);
@@ -60,6 +62,7 @@ namespace Swift {
 			QtEventWindow* eventWindow_;
 			QtChatListWindow* chatListWindow_;
 			UIEventStream* uiEventStream_;
+			QtRosterContextMenu* contextMenu_;
 	};
 }
 
diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index 7caa150..be11a3e 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -4,6 +4,7 @@
 #include "Swiften/Roster/OpenChatRosterAction.h"
 
 #include <qdebug.h>
+#include <QMenu>
 
 namespace Swift {
 
@@ -15,6 +16,7 @@ QtTreeWidget::QtTreeWidget(QWidget* parent) : QTreeView(parent) {
 	delegate_ = new RosterDelegate();
 	setItemDelegate(delegate_);
 	setHeaderHidden(true);
+	contextMenu_ = NULL;
 #ifdef SWIFT_PLATFORM_MACOSX
 	setAlternatingRowColors(true);
 #endif
@@ -33,6 +35,10 @@ QtTreeWidget::~QtTreeWidget() {
 	delete delegate_;
 }
 
+void QtTreeWidget::setContextMenu(QtContextMenu* contextMenu) {
+	contextMenu_ = contextMenu;
+}
+
 QtTreeWidgetItem* QtTreeWidget::getRoot() {
 	return treeRoot_;
 }
@@ -44,6 +50,16 @@ void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
 	}
 }
 
+void QtTreeWidget::contextMenuEvent(QContextMenuEvent* event) {
+	if (!contextMenu_) {
+		return;
+	}
+	QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(selectedIndexes()[0].internalPointer());
+	if (qtItem) {
+		contextMenu_->show(qtItem);
+	}
+}
+
 void QtTreeWidget::handleExpanded(const QModelIndex& index) {
 	QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(index.internalPointer());
 	if (qtItem) {
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index 13b6d8e..15eb22f 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -10,6 +10,7 @@
 #include "Swift/QtUI/Roster/QtTreeWidget.h"
 #include "Swift/QtUI/Roster/RosterModel.h"
 #include "Swift/QtUI/Roster/RosterDelegate.h"
+#include "Swift/QtUI/ContextMenus/QtContextMenu.h"
 
 namespace Swift {
 
@@ -20,18 +21,22 @@ class QtTreeWidget : public QTreeView, public TreeWidget {
 		~QtTreeWidget();
 		void show();
 		QtTreeWidgetItem* getRoot();
+		void setContextMenu(QtContextMenu* contextMenu);
 	private slots:
 		void handleItemActivated(const QModelIndex&);
 		void handleModelItemExpanded(const QModelIndex&, bool expanded);
 		void handleExpanded(const QModelIndex&);
 		void handleCollapsed(const QModelIndex&);
 		void handleDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
+	protected:
+		void contextMenuEvent(QContextMenuEvent* event);
+
 	private:
 		void drawBranches(QPainter*, const QRect&, const QModelIndex&) const;
 		RosterModel* model_;
 		RosterDelegate* delegate_;
 		QtTreeWidgetItem* treeRoot_;
-		
+		QtContextMenu* contextMenu_;
 };
 
 }
diff --git a/Swift/QtUI/SConscript b/Swift/QtUI/SConscript
index 9883040..9980b45 100644
--- a/Swift/QtUI/SConscript
+++ b/Swift/QtUI/SConscript
@@ -89,6 +89,7 @@ sources = [
     "ChatList/ChatListModel.cpp",
     "ChatList/ChatListDelegate.cpp",
     "ChatList/ChatListMUCItem.cpp",
+    "ContextMenus/QtRosterContextMenu.cpp",
     "QtSubscriptionRequestWindow.cpp",
     "QtRosterHeader.cpp",
     "qrc_DefaultTheme.cc",
-- 
cgit v0.10.2-6-g49f6