From 2ef0428c1f9559454654546f3cce32155cf5409d Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 16 Aug 2009 13:35:34 +0100
Subject: Now show my avatar and JID in the 'me view'.

At the moment it's my JID rather than my Name, because the NickResolver doesn't cope with our own Name.
Also: the name resizing sometimes doesn't work. If it doesn't for you, make the roster very wide and then slowly resize it down.

diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index 0f654e4..7a55d99 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -113,7 +113,7 @@ void MainController::handleConnected() {
 	avatarManager_ = new AvatarManager(client_, client_, avatarStorage_, this);
 	
 	delete rosterController_;
-	rosterController_ = new RosterController(xmppRoster, avatarManager_, mainWindowFactory_, treeWidgetFactory_);
+	rosterController_ = new RosterController(jid_, xmppRoster, avatarManager_, mainWindowFactory_, treeWidgetFactory_, nickResolver_);
 	rosterController_->onStartChatRequest.connect(boost::bind(&MainController::handleChatRequest, this, _1));
 	rosterController_->onJoinMUCRequest.connect(boost::bind(&MainController::handleJoinMUCRequest, this, _1, _2));
 	rosterController_->onChangeStatusRequest.connect(boost::bind(&MainController::handleChangeStatusRequest, this, _1, _2));
diff --git a/Swift/Controllers/MainWindow.h b/Swift/Controllers/MainWindow.h
index 081fe6e..945439e 100644
--- a/Swift/Controllers/MainWindow.h
+++ b/Swift/Controllers/MainWindow.h
@@ -15,6 +15,9 @@ namespace Swift {
 		public:
 			virtual ~MainWindow() {};
 			virtual TreeWidget* getTreeWidget() = 0;
+			virtual void setMyName(const String& name) = 0;
+			virtual void setMyAvatarPath(const String& path) = 0;
+			virtual void setMyStatusText(const String& status) = 0;
 			
 			boost::signal<void (const JID&)> onStartChatRequest;
 			boost::signal<void (const JID&, const String&)> onJoinMUCRequest;
diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp
index 3662241..322d704 100644
--- a/Swift/Controllers/RosterController.cpp
+++ b/Swift/Controllers/RosterController.cpp
@@ -5,6 +5,7 @@
 #include "Swiften/Base/foreach.h"
 #include "Swift/Controllers/MainWindow.h"
 #include "Swift/Controllers/MainWindowFactory.h"
+#include "Swift/Controllers/NickResolver.h"
 #include "Swiften/Queries/Requests/GetRosterRequest.h"
 #include "Swiften/EventLoop/MainEventLoop.h"
 #include "Swiften/Roster/Roster.h"
@@ -15,13 +16,15 @@
 #include "Swiften/Roster/TreeWidgetFactory.h"
 #include "Swiften/Roster/XMPPRoster.h"
 
+
 namespace Swift {
 	
 /**
  * The controller does not gain ownership of these parameters.
  */
-RosterController::RosterController(boost::shared_ptr<XMPPRoster> xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, TreeWidgetFactory* treeWidgetFactory)
- : xmppRoster_(xmppRoster), mainWindowFactory_(mainWindowFactory), treeWidgetFactory_(treeWidgetFactory), mainWindow_(mainWindowFactory_->createMainWindow()), roster_(new Roster(mainWindow_->getTreeWidget(), treeWidgetFactory_)), offlineFilter_(new OfflineRosterFilter()) {
+RosterController::RosterController(const JID& jid, boost::shared_ptr<XMPPRoster> xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, TreeWidgetFactory* treeWidgetFactory, NickResolver* nickResolver)
+ : myJID_(jid), xmppRoster_(xmppRoster), mainWindowFactory_(mainWindowFactory), treeWidgetFactory_(treeWidgetFactory), mainWindow_(mainWindowFactory_->createMainWindow()), roster_(new Roster(mainWindow_->getTreeWidget(), treeWidgetFactory_)), offlineFilter_(new OfflineRosterFilter()) {
+	nickResolver_ = nickResolver;
 	roster_->addFilter(offlineFilter_);
 	avatarManager_ = avatarManager;
 	avatarManager_->onAvatarChanged.connect(boost::bind(&RosterController::handleAvatarChanged, this, _1, _2));
@@ -31,6 +34,9 @@ RosterController::RosterController(boost::shared_ptr<XMPPRoster> xmppRoster, Ava
 	mainWindow_->onShowOfflineToggled.connect(boost::bind(&RosterController::handleShowOfflineToggled, this, _1));
 	roster_->onUserAction.connect(boost::bind(&RosterController::handleUserAction, this, _1));
 	xmppRoster_->onJIDAdded.connect(boost::bind(&RosterController::handleOnJIDAdded, this, _1));
+	
+	mainWindow_->setMyAvatarPath(avatarManager_->getAvatarPath(myJID_).string());
+	mainWindow_->setMyName(nickResolver_->jidToNick(myJID_));
 }
 
 RosterController::~RosterController() {
@@ -78,6 +84,9 @@ void RosterController::handleIncomingPresence(boost::shared_ptr<Presence> presen
 void RosterController::handleAvatarChanged(const JID& jid, const String& hash) {
 	String path = avatarManager_->getAvatarPath(jid).string();
 	roster_->applyOnItems(SetAvatar(jid, path));
+	if (jid.equals(myJID_, JID::WithoutResource)) {
+		mainWindow_->setMyAvatarPath(path);
+	}
 }
 
 void RosterController::handleStartChatRequest(const JID& contact) {
diff --git a/Swift/Controllers/RosterController.h b/Swift/Controllers/RosterController.h
index f9f2258..2eddaf6 100644
--- a/Swift/Controllers/RosterController.h
+++ b/Swift/Controllers/RosterController.h
@@ -18,10 +18,11 @@ namespace Swift {
 	class MainWindowFactory;
 	class TreeWidgetFactory;
 	class OfflineRosterFilter;
+	class NickResolver;
 
 	class RosterController {
 		public:
-			RosterController(boost::shared_ptr<XMPPRoster> xmppRoster, AvatarManager* avatarManager, MainWindowFactory *mainWindowFactory, TreeWidgetFactory *treeWidgetFactory);
+			RosterController(const JID& jid, boost::shared_ptr<XMPPRoster> xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, TreeWidgetFactory* treeWidgetFactory, NickResolver* nickResolver);
 			~RosterController();
 			void showRosterWindow();
 			MainWindow* getWindow() {return mainWindow_;};
@@ -38,6 +39,7 @@ namespace Swift {
 			void handleUserAction(boost::shared_ptr<UserRosterAction> action);
 			void handleChangeStatusRequest(StatusShow::Type show, const String &statusText);
 			void handleShowOfflineToggled(bool state);
+			JID myJID_;
 			boost::shared_ptr<XMPPRoster> xmppRoster_;
 			MainWindowFactory* mainWindowFactory_;
 			TreeWidgetFactory* treeWidgetFactory_;
@@ -45,6 +47,7 @@ namespace Swift {
 			Roster* roster_;
 			OfflineRosterFilter* offlineFilter_;
 			AvatarManager* avatarManager_;
+			NickResolver* nickResolver_;
 	};
 }
 #endif
diff --git a/Swift/QtUI/QtMainWindow.cpp b/Swift/QtUI/QtMainWindow.cpp
index 9fa209b..cf4bfd2 100644
--- a/Swift/QtUI/QtMainWindow.cpp
+++ b/Swift/QtUI/QtMainWindow.cpp
@@ -68,5 +68,17 @@ void QtMainWindow::handleShowOfflineToggled(bool state) {
 	onShowOfflineToggled(state);
 }
 
+void QtMainWindow::setMyName(const String& name) {
+	meView_->setName(P2QSTRING(name));
+}
+
+void QtMainWindow::setMyAvatarPath(const String& path) {
+	meView_->setAvatar(P2QSTRING(path));
+}
+
+void QtMainWindow::setMyStatusText(const String& status) {
+	meView_->setStatusText(P2QSTRING(status));
+}
+
 }
 
diff --git a/Swift/QtUI/QtMainWindow.h b/Swift/QtUI/QtMainWindow.h
index f56262d..44133af 100644
--- a/Swift/QtUI/QtMainWindow.h
+++ b/Swift/QtUI/QtMainWindow.h
@@ -24,6 +24,9 @@ namespace Swift {
 			QtMainWindow(QtTreeWidgetFactory *treeWidgetFactory);
 			TreeWidget* getTreeWidget();
 			std::vector<QMenu*> getMenus() {return menus_;}
+			void setMyName(const String& name);
+			void setMyAvatarPath(const String& path);
+			void setMyStatusText(const String& status);
 		private slots:
 			void handleStatusChanged(StatusShow::Type showType, const QString &statusMessage);
 			void handleShowOfflineToggled(bool);
diff --git a/Swift/QtUI/QtRosterHeader.cpp b/Swift/QtUI/QtRosterHeader.cpp
index c479643..86e64ca 100644
--- a/Swift/QtUI/QtRosterHeader.cpp
+++ b/Swift/QtUI/QtRosterHeader.cpp
@@ -4,6 +4,7 @@
 #include <QVBoxLayout>
 #include <QIcon>
 #include <QSizePolicy>
+#include <qdebug.h>
 
 #include "QtStatusWidget.h"
 
@@ -14,9 +15,6 @@ QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) {
 	vLayout->setContentsMargins(0,0,0,0);
 	setLayout(vLayout);
 
-	//QHBoxLayout* topLayout = new QHBoxLayout();
-	//vLayout->addLayout(topLayout);
-	
 	toolBar_ = new QToolBar(this);
 	vLayout->addWidget(toolBar_);
 
@@ -27,8 +25,8 @@ QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) {
 
 	nameLabel_ = new QLabel(this);
 	setName("Me");
-	//topLayout->addWidget(nameLabel_);
 	toolBar_->addWidget(nameLabel_);
+	//nameLabel_->setMaximumWidth(width() - 5 - statusWidget_->width());
 		
 	QHBoxLayout* expandedLayout = new QHBoxLayout();
 	expandedLayout->setContentsMargins(5,5,5,5);
@@ -43,7 +41,7 @@ QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) {
 	statusEdit_->resize(statusEdit_->width(), 64);
 	statusEdit_->setAcceptRichText(false);
 	statusEdit_->setReadOnly(false);
-	setStatusText("Could be here, could be away.");
+	setStatusText("");
 
 	vLayout->addLayout(expandedLayout);
 
@@ -51,6 +49,7 @@ QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) {
 }
 
 void QtRosterHeader::handleChangeStatusRequest(StatusShow::Type type) {
+	Q_UNUSED(type);
 	emitStatus();
 }
 
@@ -63,9 +62,30 @@ void QtRosterHeader::setStatusText(const QString& statusMessage) {
 }
 
 void QtRosterHeader::setName(const QString& name) {
-	QString escapedName = name;
+	name_ = name;
+	resizeNameLabel();
+}
+
+void QtRosterHeader::resizeNameLabel() {	
+	QString escapedName = name_;
 	escapedName.replace("<","&lt;");
 	nameLabel_->setText("<b>" + escapedName + "</b>");
+	int reductionCount = 0;
+	while (nameLabel_->sizeHint().width() + statusWidget_->width() + 30 > width()) {
+		qDebug() << nameLabel_->sizeHint().width() << " " << statusWidget_->width() << " " << width();
+		reductionCount++;
+		QString reducedName = name_;
+		reducedName.remove(name_.length() - reductionCount, reductionCount);
+		reducedName.replace("<","&lt;");
+		nameLabel_->setText("<b>" + reducedName +  + "...</b>");
+		qDebug() << "Shrunk " << escapedName << " down to " << reducedName;
+	}
+	nameLabel_->setToolTip(name_);
+}
+
+void QtRosterHeader::resizeEvent(QResizeEvent* event) {
+	QWidget::resizeEvent(event);
+	resizeNameLabel();
 }
 
 void QtRosterHeader::setAvatar(const QString& path) {
diff --git a/Swift/QtUI/QtRosterHeader.h b/Swift/QtUI/QtRosterHeader.h
index aeccf9a..ae510ee 100644
--- a/Swift/QtUI/QtRosterHeader.h
+++ b/Swift/QtUI/QtRosterHeader.h
@@ -7,6 +7,7 @@
 #include <QSize>
 #include <QToolBar>
 
+#include "Swiften/Base/String.h"
 #include "Swiften/Elements/StatusShow.h"
 
 namespace Swift {
@@ -25,7 +26,11 @@ namespace Swift {
 		void onChangeStatusRequest(StatusShow::Type showType, const QString &statusMessage);
 	private slots:
 		void handleChangeStatusRequest(StatusShow::Type type);
+	protected:
+		virtual void resizeEvent(QResizeEvent* event);
 	private:
+		void resizeNameLabel();
+		QString name_;
 		QLabel* avatarLabel_;
 		QLabel* nameLabel_;
 		QTextEdit* statusEdit_;
-- 
cgit v0.10.2-6-g49f6