From 87775c50a1c9a223f554c37e549ea8a2b03c9a89 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Thu, 15 Oct 2009 21:26:00 +0100
Subject: Save the geometry of the chat and roster windows.


diff --git a/Swift/QtUI/QtChatTabs.cpp b/Swift/QtUI/QtChatTabs.cpp
index 00d07c8..2dbeed7 100644
--- a/Swift/QtUI/QtChatTabs.cpp
+++ b/Swift/QtUI/QtChatTabs.cpp
@@ -1,6 +1,9 @@
 #include "QtChatTabs.h"
 
+#include <algorithm>
+
 #include <QCloseEvent>
+#include <QDesktopWidget>
 #include <QtGlobal>
 #include <QTabWidget>
 #include <QLayout>
@@ -20,7 +23,7 @@ QtChatTabs::QtChatTabs() : QWidget() {
 	layout->setContentsMargins(0, 3, 0, 0);
 	layout->addWidget(tabs_);
 	setLayout(layout);
-	resize(400, 300);
+	//resize(400, 300);
 }
 
 void QtChatTabs::closeEvent(QCloseEvent* event) {
@@ -99,4 +102,12 @@ void QtChatTabs::handleTabTitleUpdated() {
 	}
 }
 
+void QtChatTabs::resizeEvent(QResizeEvent* event) {
+	emit geometryChanged();
+}
+
+void QtChatTabs::moveEvent(QMoveEvent* event) {
+	emit geometryChanged();	
+}
+
 }
diff --git a/Swift/QtUI/QtChatTabs.h b/Swift/QtUI/QtChatTabs.h
index 87b8b10..feb8ebc 100644
--- a/Swift/QtUI/QtChatTabs.h
+++ b/Swift/QtUI/QtChatTabs.h
@@ -2,6 +2,8 @@
 
 #include "QtTabbable.h"
 #include <QWidget>
+#include <QRect>
+
 class QTabWidget;
 
 namespace Swift {
@@ -11,9 +13,13 @@ namespace Swift {
 			QtChatTabs();
 			void addTab(QtTabbable* tab);
 			void minimise();
+		signals:
+			void geometryChanged();
 
 		protected slots:
 			void closeEvent(QCloseEvent* event);
+			void resizeEvent(QResizeEvent* event);
+			void moveEvent(QMoveEvent* event);
 
 		private slots:
 			void handleTabClosing();
diff --git a/Swift/QtUI/QtChatWindowFactory.cpp b/Swift/QtUI/QtChatWindowFactory.cpp
index b3efeac..c0f1e09 100644
--- a/Swift/QtUI/QtChatWindowFactory.cpp
+++ b/Swift/QtUI/QtChatWindowFactory.cpp
@@ -1,5 +1,7 @@
 #include "QtChatWindowFactory.h"
 
+#include <QDesktopWidget>
+
 #include "QtChatTabs.h"
 #include "QtChatWindow.h"
 #include "QtSwiftUtil.h"
@@ -7,10 +9,17 @@
 
 
 namespace Swift {
-QtChatWindowFactory::QtChatWindowFactory(QtTreeWidgetFactory *treeWidgetFactory, QSplitter* splitter) : treeWidgetFactory_(treeWidgetFactory) {
+QtChatWindowFactory::QtChatWindowFactory(QtTreeWidgetFactory *treeWidgetFactory, QSplitter* splitter, QtSettingsProvider* settings) : treeWidgetFactory_(treeWidgetFactory) {
+	settings_ = settings;
 	tabs_ = new QtChatTabs();
 	if (splitter) {
 		splitter->addWidget(tabs_);
+	} else {
+		QVariant chatTabsGeometryVariant = settings_->getQSettings()->value("chatTabsGeometry");
+		if (chatTabsGeometryVariant.isValid()) {
+			tabs_->restoreGeometry(chatTabsGeometryVariant.toByteArray());
+		}
+		connect(tabs_, SIGNAL(geometryChanged()), this, SLOT(handleWindowGeometryChanged()));
 	}
 }
 
@@ -25,4 +34,8 @@ ChatWindow* QtChatWindowFactory::createChatWindow(const JID &contact) {
 	return chatWindow;
 }
 
+void QtChatWindowFactory::handleWindowGeometryChanged() {
+	settings_->getQSettings()->setValue("chatTabsGeometry", tabs_->saveGeometry());
+}
+
 }
diff --git a/Swift/QtUI/QtChatWindowFactory.h b/Swift/QtUI/QtChatWindowFactory.h
index 1205164..4a5a7fb 100644
--- a/Swift/QtUI/QtChatWindowFactory.h
+++ b/Swift/QtUI/QtChatWindowFactory.h
@@ -3,17 +3,23 @@
 
 #include "Swift/Controllers/ChatWindowFactory.h"
 #include "Swiften/JID/JID.h"
+#include "QtSettingsProvider.h"
 
+#include <QObject>
 #include <QSplitter>
 namespace Swift {
 	class QtTreeWidgetFactory;
 	class QtChatTabs;
-	class QtChatWindowFactory : public ChatWindowFactory {
+	class QtChatWindowFactory : public QObject, public ChatWindowFactory {
+		Q_OBJECT
 		public:
-			QtChatWindowFactory(QtTreeWidgetFactory *treeWidgetFactory, QSplitter* splitter);
+			QtChatWindowFactory(QtTreeWidgetFactory *treeWidgetFactory, QSplitter* splitter, QtSettingsProvider* settings);
 			ChatWindow* createChatWindow(const JID &contact);
+		private slots:
+			void handleWindowGeometryChanged();
 		private:
 			QtTreeWidgetFactory* treeWidgetFactory_;
+			QtSettingsProvider* settings_;
 			QtChatTabs* tabs_;
 	};
 }
diff --git a/Swift/QtUI/QtLoginWindow.cpp b/Swift/QtUI/QtLoginWindow.cpp
index 23eece6..e84e316 100644
--- a/Swift/QtUI/QtLoginWindow.cpp
+++ b/Swift/QtUI/QtLoginWindow.cpp
@@ -4,9 +4,12 @@
 #include "QtSwiftUtil.h"
 #include "QtMainWindow.h"
 
+#include <algorithm>
+
 #include <QApplication>
 #include <QBoxLayout>
 #include <QComboBox>
+#include <QDesktopWidget>
 #include <QFileDialog>
 #include <QStatusBar>
 #include <QToolButton>
@@ -101,6 +104,29 @@ QtLoginWindow::QtLoginWindow() : QMainWindow() {
 	this->show();
 }
 
+/**
+ * Move and resize the window, but respect minimum sizes.
+ * (Like QWidget::setGeometry, only that will truncate the window
+ *  the setGeometry docs say that it shouldn't do this, but I've just seen it
+ *  maybe we can remove this method if that's a Qt bug (or I'm misusing it)).
+ */
+void QtLoginWindow::setGentleGeometry(const QRect& rect) {
+	resize(rect.size());
+	move(rect.topLeft());
+}
+
+QRect QtLoginWindow::defaultPosition() {
+	QDesktopWidget desktop;
+	int windowWidth = 200;
+	int windowHeight = 500;
+	QRect screen = desktop.screenGeometry(-1); //appear on default screen
+	windowWidth = std::min(windowWidth, screen.width());
+	windowHeight = std::min(windowHeight, screen.height());
+	int left = (screen.width() - windowWidth) / 2;
+	int height = (screen.height() - windowHeight) / 2;
+	return QRect(left, height, windowWidth, windowHeight);
+}
+
 void QtLoginWindow::addAvailableAccount(const String& defaultJID, const String& defaultPassword, const String& defaultCertificate) {
 	QString username = P2QSTRING(defaultJID);
 	int index = -1;
@@ -216,4 +242,13 @@ void QtLoginWindow::bringToFront() {
 	}
 }
 
+void QtLoginWindow::resizeEvent(QResizeEvent* event) {
+	emit geometryChanged();
+}
+
+void QtLoginWindow::moveEvent(QMoveEvent* event) {
+	emit geometryChanged();	
+}
+
+
 }
diff --git a/Swift/QtUI/QtLoginWindow.h b/Swift/QtUI/QtLoginWindow.h
index ea40c71..5abb10a 100644
--- a/Swift/QtUI/QtLoginWindow.h
+++ b/Swift/QtUI/QtLoginWindow.h
@@ -27,6 +27,11 @@ namespace Swift {
 			virtual void loggedOut();
 			virtual void setMessage(const String& message);
 			virtual void addAvailableAccount(const String& defaultJID, const String& defaultPassword, const String& defaultCertificate);
+			static QRect defaultPosition();
+			void setGentleGeometry(const QRect&);
+		signals:
+			void geometryChanged();
+
 		private slots:
 			void loginClicked();
 			void handleCertficateChecked(bool);
@@ -34,6 +39,8 @@ namespace Swift {
 			void handleAbout();
 			void bringToFront();
 			void handleUsernameTextChanged();
+			void resizeEvent(QResizeEvent* event);
+			void moveEvent(QMoveEvent* event);
 
 		private:
 			void setInitialMenus();
diff --git a/Swift/QtUI/QtLoginWindowFactory.cpp b/Swift/QtUI/QtLoginWindowFactory.cpp
index e351f43..c395c57 100644
--- a/Swift/QtUI/QtLoginWindowFactory.cpp
+++ b/Swift/QtUI/QtLoginWindowFactory.cpp
@@ -1,19 +1,33 @@
 #include "QtLoginWindowFactory.h"
 #include "QtLoginWindow.h"
 
+#include <QDesktopWidget>
+
 namespace Swift {
 
-QtLoginWindowFactory::QtLoginWindowFactory(QSplitter* splitter, QtSystemTray* systemTray) : QObject() {
+QtLoginWindowFactory::QtLoginWindowFactory(QSplitter* splitter, QtSystemTray* systemTray, QtSettingsProvider* settings) : QObject() {
+	settings_ = settings;
 	splitter_ = splitter;
 	systemTray_ = systemTray;
 }
 
 LoginWindow* QtLoginWindowFactory::createLoginWindow() {
-	QtLoginWindow* window = new QtLoginWindow();
+	window_ = new QtLoginWindow();
 	if (splitter_) {
-		splitter_->insertWidget(0, window);
+		splitter_->insertWidget(0, window_);
+		return window_;
+	}
+	connect(systemTray_, SIGNAL(clicked()), window_, SLOT(bringToFront()));
+	QVariant loginWindowGeometryVariant = settings_->getQSettings()->value("loginWindowGeometry");
+	if (loginWindowGeometryVariant.isValid()) {
+		window_->restoreGeometry(loginWindowGeometryVariant.toByteArray());
 	}
-	connect(systemTray_, SIGNAL(clicked()), window, SLOT(bringToFront()));
-	return window;
+	connect(window_, SIGNAL(geometryChanged()), this, SLOT(handleWindowGeometryChanged()));
+	return window_;
 }
+
+void QtLoginWindowFactory::handleWindowGeometryChanged() {
+	settings_->getQSettings()->setValue("loginWindowGeometry", window_->saveGeometry());
+}
+
 }
diff --git a/Swift/QtUI/QtLoginWindowFactory.h b/Swift/QtUI/QtLoginWindowFactory.h
index 0ae8284..dd5bc57 100644
--- a/Swift/QtUI/QtLoginWindowFactory.h
+++ b/Swift/QtUI/QtLoginWindowFactory.h
@@ -6,15 +6,22 @@
 
 #include <QSplitter>
 
+#include "QtSettingsProvider.h"
+
 namespace Swift {
+	class QtLoginWindow;
 	class QtLoginWindowFactory : public QObject, public LoginWindowFactory {
 		Q_OBJECT
 		public:
-			QtLoginWindowFactory(QSplitter* splitter, QtSystemTray* systemTray);
+			QtLoginWindowFactory(QSplitter* splitter, QtSystemTray* systemTray, QtSettingsProvider* settings);
 			LoginWindow* createLoginWindow();
+		private slots:
+			void handleWindowGeometryChanged();
 		private:
 			QSplitter* splitter_;
 			QtSystemTray* systemTray_;
+			QtSettingsProvider* settings_;
+			QtLoginWindow* window_;
 	};
 }
 
diff --git a/Swift/QtUI/QtSettingsProvider.cpp b/Swift/QtUI/QtSettingsProvider.cpp
index 7012288..767e41b 100644
--- a/Swift/QtUI/QtSettingsProvider.cpp
+++ b/Swift/QtUI/QtSettingsProvider.cpp
@@ -45,5 +45,9 @@ void QtSettingsProvider::createProfile(const String& profile) {
 	settings_.setValue("profileList", stringList);
 }
 
+QSettings* QtSettingsProvider::getQSettings() {
+	return &settings_;
+}
+
 }
 
diff --git a/Swift/QtUI/QtSettingsProvider.h b/Swift/QtUI/QtSettingsProvider.h
index a8d5bcc..359e537 100644
--- a/Swift/QtUI/QtSettingsProvider.h
+++ b/Swift/QtUI/QtSettingsProvider.h
@@ -17,6 +17,7 @@ class QtSettingsProvider : public SettingsProvider {
 		virtual void storeBool(const String &settingPath, bool settingValue);
 		virtual std::vector<String> getAvailableProfiles();
 		virtual void createProfile(const String& profile);
+		QSettings* getQSettings();
 	private:
 		QSettings settings_;
 };
diff --git a/Swift/QtUI/QtSwift.cpp b/Swift/QtUI/QtSwift.cpp
index a949b7d..656e8ce 100644
--- a/Swift/QtUI/QtSwift.cpp
+++ b/Swift/QtUI/QtSwift.cpp
@@ -2,6 +2,8 @@
 
 #include "QtLoginWindowFactory.h"
 #include "QtChatWindowFactory.h"
+#include "QtLoginWindow.h"
+#include "QtChatTabs.h"
 #include "QtMainWindowFactory.h"
 #include "Roster/QtTreeWidgetFactory.h"
 #include "QtSystemTray.h"
@@ -27,18 +29,18 @@ QtSwift::QtSwift(bool netbookMode) {
 	} else {
 		splitter_ = NULL;
 	}
-	treeWidgetFactory_ = new QtTreeWidgetFactory(); 
-	systemTray_ = new QtSystemTray();
-	loginWindowFactory_ = new QtLoginWindowFactory(splitter_, systemTray_);
-	chatWindowFactory_ = new QtChatWindowFactory(treeWidgetFactory_, splitter_);
-	rosterWindowFactory_ = new QtMainWindowFactory(treeWidgetFactory_);
-	soundPlayer_ = new QtSoundPlayer();
 	QCoreApplication::setApplicationName("Swift");
 	QCoreApplication::setOrganizationName("Swift");
 	QCoreApplication::setOrganizationDomain("swift.im");
 	QCoreApplication::setApplicationVersion(buildVersion);
 	settings_ = new QtSettingsProvider();
 	application_ = new PlatformApplication("Swift");
+	treeWidgetFactory_ = new QtTreeWidgetFactory(); 
+	systemTray_ = new QtSystemTray();
+	loginWindowFactory_ = new QtLoginWindowFactory(splitter_, systemTray_, settings_);
+	chatWindowFactory_ = new QtChatWindowFactory(treeWidgetFactory_, splitter_, settings_);
+	rosterWindowFactory_ = new QtMainWindowFactory(treeWidgetFactory_);
+	soundPlayer_ = new QtSoundPlayer();
 	if (splitter_) {
 		splitter_->show();
 	}
-- 
cgit v0.10.2-6-g49f6