From 3f06fdbf826d8d1c1e243cff5fcbf27870f4f0f8 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 4 Mar 2011 16:02:25 +0000
Subject: Create an edit contact menu item.

Resolves: #769
Release-notes: It is now possible to edit contacts from the standard menus (without needing to right-click on the item).

diff --git a/Swift/QtUI/QtMainWindow.cpp b/Swift/QtUI/QtMainWindow.cpp
index 99cbcf5..ca357e3 100644
--- a/Swift/QtUI/QtMainWindow.cpp
+++ b/Swift/QtUI/QtMainWindow.cpp
@@ -92,6 +92,10 @@ QtMainWindow::QtMainWindow(QtSettingsProvider* settings, UIEventStream* uiEventS
 	addUserAction_ = new QAction(tr("&Add Contact"), this);
 	connect(addUserAction_, SIGNAL(triggered(bool)), this, SLOT(handleAddUserActionTriggered(bool)));
 	actionsMenu->addAction(addUserAction_);
+	editUserAction_ = new QAction(tr("&Edit Selected Contact"), this);
+	connect(editUserAction_, SIGNAL(triggered(bool)), treeWidget_, SLOT(handleEditUserActionTriggered(bool)));
+	actionsMenu->addAction(editUserAction_);
+	editUserAction_->setEnabled(false);
 	chatUserAction_ = new QAction(tr("Start &Chat"), this);
 	connect(chatUserAction_, SIGNAL(triggered(bool)), this, SLOT(handleChatUserActionTriggered(bool)));
 	actionsMenu->addAction(chatUserAction_);
@@ -100,6 +104,8 @@ QtMainWindow::QtMainWindow(QtSettingsProvider* settings, UIEventStream* uiEventS
 	connect(signOutAction, SIGNAL(triggered()), SLOT(handleSignOutAction()));
 	actionsMenu->addAction(signOutAction);
 
+	connect(treeWidget_, SIGNAL(onSomethingSelectedChanged(bool)), editUserAction_, SLOT(setEnabled(bool)));
+
 	lastOfflineState_ = false;
 	uiEventStream_->onUIEvent.connect(boost::bind(&QtMainWindow::handleUIEvent, this, _1));
 }
diff --git a/Swift/QtUI/QtMainWindow.h b/Swift/QtUI/QtMainWindow.h
index 0dc9d36..3462bb0 100644
--- a/Swift/QtUI/QtMainWindow.h
+++ b/Swift/QtUI/QtMainWindow.h
@@ -63,6 +63,7 @@ namespace Swift {
 			QtTreeWidget* treeWidget_;
 			QtRosterHeader* meView_;
 			QAction* addUserAction_;
+			QAction* editUserAction_;
 			QAction* chatUserAction_;
 			QAction* showOfflineAction_;
 			QtTabWidget* tabs_;
diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index a76dc70..3198ca5 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -66,8 +66,23 @@ void QtTreeWidget::handleClicked(const QModelIndex& index) {
 	if (item) {
 		setExpanded(index, !isExpanded(index));
 	}
+	currentChanged(index, QModelIndex());
 }
 
+void QtTreeWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous) {
+	bool valid = false;
+	if (!editable_ || selectedIndexes().empty() || !selectedIndexes()[0].isValid()) {
+		/* I didn't quite understand why using current didn't seem to work here.*/
+	}
+	else if (current.isValid()) {
+		RosterItem* item = static_cast<RosterItem*>(current.internalPointer());
+		if (dynamic_cast<ContactRosterItem*>(item)) {
+			valid = true;
+		}
+	}
+	onSomethingSelectedChanged(valid);
+	QTreeView::currentChanged(current, previous);
+}
 
 void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
 	RosterItem* item = static_cast<RosterItem*>(index.internalPointer());
@@ -77,6 +92,23 @@ void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
 	}
 }
 
+void QtTreeWidget::handleEditUserActionTriggered(bool /*checked*/) {
+	if (!editable_) {
+		return;
+	}
+	if (selectedIndexes().empty()) {
+		return;
+	}
+	QModelIndex index = selectedIndexes()[0];
+	if (!index.isValid()) {
+		return;
+	}
+	RosterItem* item = static_cast<RosterItem*>(index.internalPointer());
+	if (ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item)) {
+		eventStream_->send(boost::make_shared<RequestContactEditorUIEvent>(contact->getJID()));
+	}
+}
+
 void QtTreeWidget::contextMenuEvent(QContextMenuEvent* event) {
 	if (!editable_) {
 		return;
@@ -101,18 +133,22 @@ void QtTreeWidget::contextMenuEvent(QContextMenuEvent* event) {
 		}
 	}
 	else if (GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item)) {
-		QAction* renameGroup = contextMenu.addAction(tr("Rename"));
+		QAction* renameGroupAction = contextMenu.addAction(tr("Rename"));
 		QAction* result = contextMenu.exec(event->globalPos());
-		if (result == renameGroup) {
-			bool ok;
-			QString newName = QInputDialog::getText(NULL, tr("Rename group"), tr("New name for %1").arg(P2QSTRING(group->getDisplayName())), QLineEdit::Normal, P2QSTRING(group->getDisplayName()), &ok);
-			if (ok) {
-				eventStream_->send(boost::make_shared<RenameGroupUIEvent>(group->getDisplayName(), Q2PSTRING(newName)));
-			}
+		if (result == renameGroupAction) {
+			renameGroup(group);
 		}
 	}
 }
 
+void QtTreeWidget::renameGroup(GroupRosterItem* group) {
+	bool ok;
+	QString newName = QInputDialog::getText(NULL, tr("Rename group"), tr("New name for %1").arg(P2QSTRING(group->getDisplayName())), QLineEdit::Normal, P2QSTRING(group->getDisplayName()), &ok);
+	if (ok) {
+		eventStream_->send(boost::make_shared<RenameGroupUIEvent>(group->getDisplayName(), Q2PSTRING(newName)));
+	}
+}
+
 void QtTreeWidget::handleExpanded(const QModelIndex& index) {
 	GroupRosterItem* item = dynamic_cast<GroupRosterItem*>(static_cast<RosterItem*>(index.internalPointer()));
 	if (item) {
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index ce2351b..1ab8c8e 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -25,6 +25,12 @@ class QtTreeWidget : public QTreeView{
 		Roster* getRoster() {return roster_;}
 		void setEditable(bool b) { editable_ = b; }
 
+	signals:
+		void onSomethingSelectedChanged(bool);
+
+	public slots:
+		void handleEditUserActionTriggered(bool checked);
+
 	private slots:
 		void handleItemActivated(const QModelIndex&);
 		void handleModelItemExpanded(const QModelIndex&, bool expanded);
@@ -33,8 +39,11 @@ class QtTreeWidget : public QTreeView{
 		void handleClicked(const QModelIndex&);
 	protected:
 		void contextMenuEvent(QContextMenuEvent* event);
+	protected slots:
+		virtual void currentChanged(const QModelIndex& current, const QModelIndex& previous);
 
 	private:
+		void renameGroup(GroupRosterItem* group);
 		void drawBranches(QPainter*, const QRect&, const QModelIndex&) const;
 		RosterModel* model_;
 		Roster* roster_;
-- 
cgit v0.10.2-6-g49f6