From 348a4f13c5f00a27ad5c7e4f392948de7cc454e3 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Mon, 5 Apr 2010 12:01:48 +0100
Subject: Persist Sound Toggleness.

Resolves: #192

diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index ccce0bb..8aeca97 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -71,8 +71,8 @@ MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowF
 	eventController_->onEventQueueLengthChange.connect(boost::bind(&MainController::handleEventQueueLengthChange, this, _1));
 
 	systemTrayController_ = new SystemTrayController(eventController_, systemTray);
-	soundEventController_ = new SoundEventController(eventController_, soundPlayer, settings->getBoolSetting("playSounds", true), uiEventStream_);
 	loginWindow_ = loginWindowFactory_->createLoginWindow(uiEventStream_);
+	soundEventController_ = new SoundEventController(eventController_, soundPlayer, settings, uiEventStream_);
 	foreach (String profile, settings->getAvailableProfiles()) {
 		ProfileSettingsProvider* profileSettings = new ProfileSettingsProvider(profile, settings);
 		loginWindow_->addAvailableAccount(profileSettings->getStringSetting("jid"), profileSettings->getStringSetting("pass"), profileSettings->getStringSetting("certificate"));
diff --git a/Swift/Controllers/SoundEventController.cpp b/Swift/Controllers/SoundEventController.cpp
index 29f7fbf..b1fa584 100644
--- a/Swift/Controllers/SoundEventController.cpp
+++ b/Swift/Controllers/SoundEventController.cpp
@@ -9,13 +9,17 @@
 
 namespace Swift {
 
-SoundEventController::SoundEventController(EventController* eventController, SoundPlayer* soundPlayer, bool playSounds, UIEventStream* uiEvents) {
+SoundEventController::SoundEventController(EventController* eventController, SoundPlayer* soundPlayer, SettingsProvider* settings, UIEventStream* uiEvents) {
+	uiEvents_ = uiEvents;
+	settings_ = settings;
 	eventController_ = eventController;
-	eventController_->onEventQueueEventAdded.connect(boost::bind(&SoundEventController::handleEventQueueEventAdded, this, _1));
 	soundPlayer_ = soundPlayer;
-	playSounds_ = playSounds;
-	uiEvents_ = uiEvents;
 	uiEvents_->onUIEvent.connect(boost::bind(&SoundEventController::handleUIEvent, this, _1));
+	eventController_->onEventQueueEventAdded.connect(boost::bind(&SoundEventController::handleEventQueueEventAdded, this, _1));
+
+	bool playSounds = settings->getBoolSetting("playSounds", true);
+	playSounds_ = !playSounds;
+	setPlaySounds(playSounds);	
 }
 
 void SoundEventController::handleEventQueueEventAdded(boost::shared_ptr<StanzaEvent>) {
@@ -23,7 +27,12 @@ void SoundEventController::handleEventQueueEventAdded(boost::shared_ptr<StanzaEv
 }
 
 void SoundEventController::setPlaySounds(bool playSounds) {
+	bool transmit = playSounds != playSounds_;
 	playSounds_ = playSounds;
+	settings_->storeBool("playSounds", playSounds);
+	if (transmit) {
+		uiEvents_->send(boost::shared_ptr<ToggleSoundsUIEvent>(new ToggleSoundsUIEvent(playSounds_)));
+	}
 }
 
 void SoundEventController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
diff --git a/Swift/Controllers/SoundEventController.h b/Swift/Controllers/SoundEventController.h
index 34499d4..e9a853f 100644
--- a/Swift/Controllers/SoundEventController.h
+++ b/Swift/Controllers/SoundEventController.h
@@ -3,7 +3,7 @@
 #include <boost/shared_ptr.hpp>
 
 #include "Swiften/Events/StanzaEvent.h"
-
+#include "Swiften/Settings/SettingsProvider.h"
 #include "Swift/Controllers/UIEvents/UIEvent.h"
 
 namespace Swift {
@@ -12,8 +12,9 @@ namespace Swift {
 	class UIEventStream;
 	class SoundEventController {
 		public:
-			SoundEventController(EventController* eventController, SoundPlayer* soundPlayer, bool playSounds, UIEventStream* uiEvents);
+			SoundEventController(EventController* eventController, SoundPlayer* soundPlayer, SettingsProvider* settings, UIEventStream* uiEvents);
 			void setPlaySounds(bool playSounds);
+			bool getSoundEnabled() {return playSounds_;};
 		private:
 			void handleUIEvent(boost::shared_ptr<UIEvent> event);
 			void handleEventQueueEventAdded(boost::shared_ptr<StanzaEvent> event);
@@ -21,5 +22,6 @@ namespace Swift {
 			SoundPlayer* soundPlayer_;
 			bool playSounds_;
 			UIEventStream* uiEvents_;
+			SettingsProvider* settings_;
 	};
 }
diff --git a/Swift/QtUI/QtLoginWindow.cpp b/Swift/QtUI/QtLoginWindow.cpp
index 4f6b423..ff2a50b 100644
--- a/Swift/QtUI/QtLoginWindow.cpp
+++ b/Swift/QtUI/QtLoginWindow.cpp
@@ -1,5 +1,7 @@
 #include "QtLoginWindow.h"
 
+#include <boost/bind.hpp>
+
 #include "Swift/Controllers/UIEvents/UIEventStream.h"
 #include "Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h"
 #include "Swift/Controllers/UIEvents/ToggleSoundsUIEvent.h"
@@ -117,21 +119,29 @@ QtLoginWindow::QtLoginWindow(UIEventStream* uiEventStream) : QMainWindow() {
 	connect(xmlConsoleAction, SIGNAL(activated()), SLOT(handleShowXMLConsole()));
 	toolsMenu_->addAction(xmlConsoleAction);
 
-	QAction* toggleSoundsAction = new QAction(tr("Toggle Sounds"), this);
-	toggleSoundsAction->setCheckable(true);
-	toggleSoundsAction->setChecked(true);
-	connect(toggleSoundsAction, SIGNAL(toggled(bool)), SLOT(handleToggleSounds(bool)));
-	swiftMenu_->addAction(toggleSoundsAction);
+	toggleSoundsAction_ = new QAction(tr("Toggle Sounds"), this);
+	toggleSoundsAction_->setCheckable(true);
+	toggleSoundsAction_->setChecked(true);
+	connect(toggleSoundsAction_, SIGNAL(toggled(bool)), SLOT(handleToggleSounds(bool)));
+	swiftMenu_->addAction(toggleSoundsAction_);
 
 	
 	QAction* quitAction = new QAction("Quit", this);
 	connect(quitAction, SIGNAL(activated()), SLOT(handleQuit()));
 	swiftMenu_->addAction(quitAction);
-	
+
 	setInitialMenus();
+	uiEventStream_->onUIEvent.connect(boost::bind(&QtLoginWindow::handleUIEvent, this, _1));
 	this->show();
 }
 
+void QtLoginWindow::handleUIEvent(boost::shared_ptr<UIEvent> event) {
+	boost::shared_ptr<ToggleSoundsUIEvent> soundEvent = boost::dynamic_pointer_cast<ToggleSoundsUIEvent>(event);
+	if (soundEvent) {
+		toggleSoundsAction_->setChecked(soundEvent->getEnabled());
+	}
+}
+
 /**
  * Move and resize the window, but respect minimum sizes.
  * (Like QWidget::setGeometry, only that will truncate the window
diff --git a/Swift/QtUI/QtLoginWindow.h b/Swift/QtUI/QtLoginWindow.h
index 40ea4ec..69327e9 100644
--- a/Swift/QtUI/QtLoginWindow.h
+++ b/Swift/QtUI/QtLoginWindow.h
@@ -10,6 +10,7 @@
 #include <QMenuBar>
 
 #include "Swift/Controllers/UIInterfaces/LoginWindow.h"
+#include "Swift/Controllers/UIEvents/UIEventStream.h"
 #include "Swift/Controllers/MainWindow.h"
 #include "QtAboutWidget.h"
 
@@ -18,7 +19,6 @@ class QToolButton;
 class QComboBox;
 
 namespace Swift {
-	class UIEventStream;
 	class QtLoginWindow : public QMainWindow, public LoginWindow {
 		Q_OBJECT
 		public:
@@ -45,6 +45,7 @@ namespace Swift {
 			void handleUsernameTextChanged();
 			void resizeEvent(QResizeEvent* event);
 			void moveEvent(QMoveEvent* event);
+			void handleUIEvent(boost::shared_ptr<UIEvent> event);
 
 		private:
 			void setInitialMenus();
@@ -62,6 +63,7 @@ namespace Swift {
 			QMenuBar* menuBar_;
 			QMenu* swiftMenu_;
 			QMenu* toolsMenu_;
+			QAction* toggleSoundsAction_;
 			UIEventStream* uiEventStream_;
 			QPointer<QtAboutWidget> aboutDialog_;
 	};
-- 
cgit v0.10.2-6-g49f6