From 48fc9e9fb0abd42d47a95042837a026730f20f34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 24 Nov 2009 21:03:09 +0100
Subject: Integrated new IdleDetector.


diff --git a/SwifTools/Idle/ActualIdleDetector.cpp b/SwifTools/Idle/ActualIdleDetector.cpp
index 2e3f307..ef73a4c 100644
--- a/SwifTools/Idle/ActualIdleDetector.cpp
+++ b/SwifTools/Idle/ActualIdleDetector.cpp
@@ -1,6 +1,7 @@
 #include "SwifTools/Idle/ActualIdleDetector.h"
 
 #include <boost/bind.hpp>
+#include <iostream>
 
 #include "SwifTools/Idle/IdleQuerier.h"
 #include "Swiften/Network/Timer.h"
@@ -8,8 +9,8 @@
 
 namespace Swift {
 
-ActualIdleDetector::ActualIdleDetector(IdleQuerier* querier, TimerFactory* timerFactory, int refreshRateSeconds) : querier(querier), isIdle(false) {
-	timer = timerFactory->createTimer(refreshRateSeconds*1000);
+ActualIdleDetector::ActualIdleDetector(IdleQuerier* querier, TimerFactory* timerFactory, int refreshRateMilliseconds) : querier(querier) {
+	timer = timerFactory->createTimer(refreshRateMilliseconds);
 	timer->onTick.connect(boost::bind(&ActualIdleDetector::handleTimerTick, this));
 	timer->start();
 }
@@ -19,11 +20,7 @@ ActualIdleDetector::~ActualIdleDetector() {
 }
 
 void ActualIdleDetector::handleTimerTick() {
-	bool idle = (querier->getIdleTimeSeconds() >= getIdleTimeSeconds());
-	if (idle != isIdle) {
-		isIdle = idle;
-		onIdleChanged(isIdle);
-	}
+	setIdle(querier->getIdleTimeSeconds() >= getIdleTimeSeconds());
 }
 
 }
diff --git a/SwifTools/Idle/ActualIdleDetector.h b/SwifTools/Idle/ActualIdleDetector.h
index 48428bb..7bc36e2 100644
--- a/SwifTools/Idle/ActualIdleDetector.h
+++ b/SwifTools/Idle/ActualIdleDetector.h
@@ -11,7 +11,7 @@ namespace Swift {
 
 	class ActualIdleDetector : public IdleDetector, public boost::bsignals::trackable {
 		public:
-			ActualIdleDetector(IdleQuerier*, TimerFactory*, int refreshRateSeconds);
+			ActualIdleDetector(IdleQuerier*, TimerFactory*, int refreshRateMilliseconds);
 			~ActualIdleDetector();
 
 		private:
@@ -19,7 +19,6 @@ namespace Swift {
 
 		private:
 			IdleQuerier* querier;
-			bool isIdle;
 			boost::shared_ptr<Timer> timer;
 	};
 }
diff --git a/SwifTools/Idle/IdleDetector.h b/SwifTools/Idle/IdleDetector.h
index 1fb3d69..bd7175d 100644
--- a/SwifTools/Idle/IdleDetector.h
+++ b/SwifTools/Idle/IdleDetector.h
@@ -6,6 +6,7 @@
 namespace Swift {
 	class IdleDetector {
 		public:
+			IdleDetector() : idle(false) {}
 			virtual ~IdleDetector();
 
 			void setIdleTimeSeconds(int time) {
@@ -16,9 +17,22 @@ namespace Swift {
 				return idleTimeSeconds;
 			}
 
+			virtual bool isIdle() const {
+				return idle;
+			}
+
 			boost::signal<void (bool /* isIdle */)> onIdleChanged;
 
+		protected:
+			void setIdle(bool b) {
+				if (b != idle) {
+					idle = b;
+					onIdleChanged(b);
+				}
+			}
+
 		private:
 			int idleTimeSeconds;
+			bool idle;
 	};
 }
diff --git a/Swift/Controllers/IdleDetector.h b/Swift/Controllers/IdleDetector.h
deleted file mode 100644
index 72fe07c..0000000
--- a/Swift/Controllers/IdleDetector.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#pragma once
-
-#include <boost/signals.hpp>
-#include <boost/shared_ptr.hpp>
-
-namespace Swift {
-class IdleDetector {
-	public:
-		virtual ~IdleDetector() {};
-		virtual void forceNotIdle() = 0;
-		boost::signal<void ()> onInputIdle;
-		boost::signal<void ()> onInputNotIdle;
-};
-}
-
-
diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index f05d42d..f54e0a2 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -10,7 +10,6 @@
 #include "Swift/Controllers/ChatController.h"
 #include "Swift/Controllers/ChatWindowFactory.h"
 #include "Swift/Controllers/EventController.h"
-#include "Swift/Controllers/IdleDetector.h"
 #include "Swift/Controllers/LoginWindow.h"
 #include "Swift/Controllers/LoginWindowFactory.h"
 #include "Swift/Controllers/MainWindow.h"
@@ -60,10 +59,8 @@ static const String CLIENT_NODE = "http://swift.im";
 typedef std::pair<JID, ChatController*> JIDChatControllerPair;
 typedef std::pair<JID, MUCController*> JIDMUCControllerPair;
 
-MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory *treeWidgetFactory, SettingsProvider *settings, Application* application, SystemTray* systemTray, SoundPlayer* soundPlayer, IdleDetector* idleDetector)
-		: client_(NULL), chatWindowFactory_(chatWindowFactory), mainWindowFactory_(mainWindowFactory), loginWindowFactory_(loginWindowFactory), treeWidgetFactory_(treeWidgetFactory), settings_(settings),
-		xmppRosterController_(NULL), rosterController_(NULL), loginWindow_(NULL), clientVersionResponder_(NULL), nickResolver_(NULL), discoResponder_(NULL) {
-	idleDetector_ = idleDetector;
+MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory *treeWidgetFactory, SettingsProvider *settings, Application* application, SystemTray* systemTray, SoundPlayer* soundPlayer)
+		: timerFactory_(&boostIOServiceThread_.getIOService()), idleDetector_(&idleQuerier_, &timerFactory_, 100), client_(NULL), chatWindowFactory_(chatWindowFactory), mainWindowFactory_(mainWindowFactory), loginWindowFactory_(loginWindowFactory), treeWidgetFactory_(treeWidgetFactory), settings_(settings), xmppRosterController_(NULL), rosterController_(NULL), loginWindow_(NULL), clientVersionResponder_(NULL), nickResolver_(NULL), discoResponder_(NULL) {
 	application_ = application;
 	presenceOracle_ = NULL;
 	avatarManager_ = NULL;
@@ -82,6 +79,9 @@ MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowF
 	}
 	loginWindow_->onLoginRequest.connect(boost::bind(&MainController::handleLoginRequest, this, _1, _2, _3, _4));
 	loginWindow_->onCancelLoginRequest.connect(boost::bind(&MainController::handleCancelLoginRequest, this));
+
+	idleDetector_.setIdleTimeSeconds(600);
+	idleDetector_.onIdleChanged.connect(boost::bind(&MainController::handleInputIdleChanged, this, _1));
 }
 
 MainController::~MainController() {
@@ -165,9 +165,6 @@ void MainController::handleConnected() {
 	vCardRequest->onResponse.connect(boost::bind(&MainController::handleOwnVCardReceived, this, _1, _2));
 	vCardRequest->send();
 
-	idleDetector_->onInputIdle.connect(boost::bind(&MainController::handleInputIdle, this));
-	idleDetector_->onInputNotIdle.connect(boost::bind(&MainController::handleInputNotIdle, this));
-	
 	//Send presence last to catch all the incoming presences.
 	boost::shared_ptr<Presence> initialPresence;
 	if (queuedPresence_.get() != NULL) {
@@ -219,19 +216,23 @@ void MainController::sendPresence(boost::shared_ptr<Presence> presence) {
 	}
 }
 
-void MainController::handleInputIdle() {
-	preIdlePresence_ = lastSentPresence_;
-	boost::shared_ptr<Presence> presence(new Presence());
-	presence->setShow(StatusShow::Away);
-	presence->setStatus("Auto-away");
-	sendPresence(presence);
-}
-
-void MainController::handleInputNotIdle() {
-	if (client_) {
-		sendPresence(preIdlePresence_);
-	} else {
-		queuedPresence_ = preIdlePresence_;
+void MainController::handleInputIdleChanged(bool idle) {
+	if (!client_ || !client_->isAvailable()) {
+		return;
+	}
+	if (idle) {
+		preIdlePresence_ = lastSentPresence_;
+		boost::shared_ptr<Presence> presence(new Presence());
+		presence->setShow(StatusShow::Away);
+		presence->setStatus("Auto-away");
+		sendPresence(presence);
+	}
+	else {
+		if (client_) {
+			sendPresence(preIdlePresence_);
+		} else {
+			queuedPresence_ = preIdlePresence_;
+		}
 	}
 }
 
@@ -304,8 +305,6 @@ void MainController::handleCancelLoginRequest() {
 
 void MainController::signOut() {
 	logout();
-	idleDetector_->onInputIdle.connect(boost::bind(&MainController::handleInputIdle, this));
-	idleDetector_->onInputNotIdle.connect(boost::bind(&MainController::handleInputNotIdle, this));
 	loginWindow_->loggedOut();
 	foreach (JIDChatControllerPair controllerPair, chatControllers_) {
 		delete controllerPair.second;
diff --git a/Swift/Controllers/MainController.h b/Swift/Controllers/MainController.h
index df6901d..270b131 100644
--- a/Swift/Controllers/MainController.h
+++ b/Swift/Controllers/MainController.h
@@ -1,10 +1,13 @@
-#ifndef SWIFTEN_MainController_H
-#define SWIFTEN_MainController_H
+#pragma once
 
 #include <boost/signals.hpp>
 #include <boost/shared_ptr.hpp>
 #include <vector>
 
+#include "Swiften/Network/BoostIOServiceThread.h"
+#include "Swiften/Network/BoostTimerFactory.h"
+#include "SwifTools/Idle/PlatformIdleQuerier.h"
+#include "SwifTools/Idle/ActualIdleDetector.h"
 #include "Swiften/Base/String.h"
 #include "Swiften/Client/ClientError.h"
 #include "Swiften/JID/JID.h"
@@ -25,7 +28,6 @@ namespace Swift {
 	class ChatWindowFactory;
 	class ChatController;
 	class EventController;
-	class IdleDetector;
 	class MainWindowFactory;
 	class MainWindow;
 	class NickResolver;
@@ -47,7 +49,7 @@ namespace Swift {
 
 	class MainController : public MUCRegistry {
 		public:
-			MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory* treeWidgetFactory, SettingsProvider *settings, Application* application, SystemTray* systemTray, SoundPlayer* soundPlayer, IdleDetector* idleDetector);
+			MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory* treeWidgetFactory, SettingsProvider *settings, Application* application, SystemTray* systemTray, SoundPlayer* soundPlayer);
 			~MainController();
 
 
@@ -69,8 +71,7 @@ namespace Swift {
 			void handleOwnVCardReceived(boost::shared_ptr<VCard> vCard, const boost::optional<ErrorPayload>& error);
 			ChatController* getChatController(const JID &contact);
 			void sendPresence(boost::shared_ptr<Presence> presence);
-			void handleInputIdle();
-			void handleInputNotIdle();
+			void handleInputIdleChanged(bool);
 			void logout();
 			void signOut();
 
@@ -79,6 +80,11 @@ namespace Swift {
 			void performLoginFromCachedCredentials();
 			void reconnectAfterError();
 			void setManagersEnabled(bool enabled);
+
+			BoostIOServiceThread boostIOServiceThread_;
+			BoostTimerFactory timerFactory_;
+			PlatformIdleQuerier idleQuerier_;
+			ActualIdleDetector idleDetector_;
 			Client* client_;
 			ChatWindowFactory* chatWindowFactory_;
 			MainWindowFactory* mainWindowFactory_;
@@ -95,7 +101,6 @@ namespace Swift {
 			SoftwareVersionResponder* clientVersionResponder_;
 			NickResolver* nickResolver_;
 			DiscoInfoResponder* discoResponder_;
-			IdleDetector* idleDetector_;
 			boost::shared_ptr<CapsInfo> capsInfo_;
 			std::map<JID, MUCController*> mucControllers_;
 			std::map<JID, ChatController*> chatControllers_;
@@ -114,5 +119,3 @@ namespace Swift {
 			String certificateFile_;
 	};
 }
-#endif
-
diff --git a/Swift/QtUI/QtIdleDetector.cpp b/Swift/QtUI/QtIdleDetector.cpp
deleted file mode 100644
index 783cc14..0000000
--- a/Swift/QtUI/QtIdleDetector.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-#include "QtIdleDetector.h"
-
-#include <QCursor>
-
-namespace Swift {
-
-QtIdleDetector::QtIdleDetector() {
-	signalled_ = false;
-	lastMoveTime_ = QDateTime::currentDateTime();
-	lastPosition_ = QCursor::pos();
-	connect(&timer_, SIGNAL(timeout()), this, SLOT(handleTimerTick()));
-	timer_.start(1000);
-}
-
-QtIdleDetector::~QtIdleDetector() {
-}
-
-void QtIdleDetector::forceNotIdle() {
-
-}
-
-void QtIdleDetector::handleTimerTick() {
-	QPoint position = QCursor::pos();
-	QDateTime now = QDateTime::currentDateTime();
-	if (position != lastPosition_) {
-		if (signalled_) {
-			onInputNotIdle();
-		}
-		lastPosition_ = position;
-		lastMoveTime_ = now;
-		signalled_ = false;
-	} else if (!signalled_ && now > lastMoveTime_.addSecs(600) && position == lastPosition_) {
-		signalled_ = true;
-		onInputIdle();
-	}
-}
-
-}
diff --git a/Swift/QtUI/QtIdleDetector.h b/Swift/QtUI/QtIdleDetector.h
deleted file mode 100644
index 444fb15..0000000
--- a/Swift/QtUI/QtIdleDetector.h
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Swift/Controllers/IdleDetector.h"
-
-#include <QObject>
-#include <QPoint>
-#include <QTimer>
-#include <QDateTime>
-
-namespace Swift {
-class QtIdleDetector : public QObject, public IdleDetector {
-	Q_OBJECT
-	public:
-		QtIdleDetector();
-		virtual ~QtIdleDetector();
-		virtual void forceNotIdle();
-	private slots:
-		void handleTimerTick();
-	private:
-		QPoint lastPosition_;
-		QDateTime lastMoveTime_;
-		QTimer timer_;
-		bool signalled_;
-};
-}
diff --git a/Swift/QtUI/QtSwift.cpp b/Swift/QtUI/QtSwift.cpp
index 0def66f..2aaa27e 100644
--- a/Swift/QtUI/QtSwift.cpp
+++ b/Swift/QtUI/QtSwift.cpp
@@ -8,8 +8,6 @@
 #include "Roster/QtTreeWidgetFactory.h"
 #include "QtSystemTray.h"
 #include "QtSoundPlayer.h"
-#include "QtIdleDetector.h"
-
 #include <boost/bind.hpp>
 #include <QSplitter>
 
@@ -42,11 +40,10 @@ QtSwift::QtSwift(bool netbookMode) {
 	chatWindowFactory_ = new QtChatWindowFactory(treeWidgetFactory_, splitter_, settings_);
 	rosterWindowFactory_ = new QtMainWindowFactory(treeWidgetFactory_);
 	soundPlayer_ = new QtSoundPlayer();
-	idleDetector_ = new QtIdleDetector();
 	if (splitter_) {
 		splitter_->show();
 	}
-	mainController_ = new MainController(chatWindowFactory_, rosterWindowFactory_, loginWindowFactory_, treeWidgetFactory_, settings_, application_, systemTray_, soundPlayer_, idleDetector_);
+	mainController_ = new MainController(chatWindowFactory_, rosterWindowFactory_, loginWindowFactory_, treeWidgetFactory_, settings_, application_, systemTray_, soundPlayer_);
 }
 
 QtSwift::~QtSwift() {
@@ -60,7 +57,6 @@ QtSwift::~QtSwift() {
 	delete systemTray_;
 	delete splitter_;
 	delete soundPlayer_;
-	delete idleDetector_;
 }
 
 }
diff --git a/Swift/QtUI/QtSwift.h b/Swift/QtUI/QtSwift.h
index 60ff5a7..263dc05 100644
--- a/Swift/QtUI/QtSwift.h
+++ b/Swift/QtUI/QtSwift.h
@@ -7,7 +7,6 @@
 #include "QtMainWindowFactory.h"
 #include "QtChatWindowFactory.h"
 #include "QtSettingsProvider.h"
-#include "QtIdleDetector.h"
 
 class QSplitter;
 
@@ -37,7 +36,6 @@ namespace Swift {
 			QtSystemTray* systemTray_;
 			QSplitter* splitter_;
 			QtSoundPlayer* soundPlayer_;
-			QtIdleDetector* idleDetector_;
 			Application* application_;
 	};
 }
diff --git a/Swift/QtUI/SConscript b/Swift/QtUI/SConscript
index d30f3b9..800a009 100644
--- a/Swift/QtUI/SConscript
+++ b/Swift/QtUI/SConscript
@@ -52,7 +52,6 @@ sources = [
 		"QtAddContactDialog.cpp",
 		"QtChatWindow.cpp",
 		"QtChatWindowFactory.cpp",
-		"QtIdleDetector.cpp",
 		"QtJoinMUCDialog.cpp",
 		"QtLoginWindow.cpp",
 		"QtLoginWindowFactory.cpp",
-- 
cgit v0.10.2-6-g49f6