From 6cdd48b30526761f5c6365496e30580ed3bd8a82 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 5 Oct 2010 17:14:08 +0200
Subject: Added FreeDesktopNotifier.


diff --git a/SwifTools/Notifier/Notifier.cpp b/SwifTools/Notifier/Notifier.cpp
index ec6a12f..44fe2a8 100644
--- a/SwifTools/Notifier/Notifier.cpp
+++ b/SwifTools/Notifier/Notifier.cpp
@@ -8,6 +8,9 @@
 
 namespace Swift {
 
+const int Notifier::DEFAULT_STATUS_NOTIFICATION_TIMEOUT_SECONDS;
+const int Notifier::DEFAULT_MESSAGE_NOTIFICATION_TIMEOUT_SECONDS;
+
 Notifier::Notifier() : enabled(true) {
 }
 
diff --git a/SwifTools/Notifier/Notifier.h b/SwifTools/Notifier/Notifier.h
index dab6e90..ad9b324 100644
--- a/SwifTools/Notifier/Notifier.h
+++ b/SwifTools/Notifier/Notifier.h
@@ -46,6 +46,9 @@ namespace Swift {
 			static std::vector<Type> getAllTypes();
 			static std::vector<Type> getDefaultTypes();
 
+			static const int DEFAULT_STATUS_NOTIFICATION_TIMEOUT_SECONDS = 3;
+			static const int DEFAULT_MESSAGE_NOTIFICATION_TIMEOUT_SECONDS = 5;
+
 		private:
 			bool enabled;
 	};
diff --git a/SwifTools/Notifier/SnarlNotifier.cpp b/SwifTools/Notifier/SnarlNotifier.cpp
index 9162ff7..1822b49 100644
--- a/SwifTools/Notifier/SnarlNotifier.cpp
+++ b/SwifTools/Notifier/SnarlNotifier.cpp
@@ -34,7 +34,7 @@ SnarlNotifier::~SnarlNotifier() {
 }
 
 void SnarlNotifier::doShowMessage(Type type, const String& subject, const String& description, const boost::filesystem::path& picture, boost::function<void()> callback) {
-	int timeout = (type == Type::IncomingMessage || type == Type::SystemMessage) ? MESSAGE_NOTIFICATION_TIMEOUT : STATUS_NOTIFICATION_TIMEOUT;
+	int timeout = (type == Type::IncomingMessage || type == Type::SystemMessage) ? DEFAULT_MESSAGE_NOTIFICATION_TIMEOUT_SECONDS : DEFAULT_STATUS_NOTIFICATION_TIMEOUT_SECONDS;
 	int notificationID = snarl.ShowMessageEx(typeToString(type).getUTF8Data(), subject.getUTF8Data(), description.getUTF8Data(), timeout, picture.string().c_str(), window->getID(), SWIFT_SNARLNOTIFIER_MESSAGE_ID);
 	if (notificationID > 0) {
 		notifications.insert(std::make_pair(notificationID, callback));
diff --git a/SwifTools/Notifier/SnarlNotifier.h b/SwifTools/Notifier/SnarlNotifier.h
index 0fad5c7..83d4715 100644
--- a/SwifTools/Notifier/SnarlNotifier.h
+++ b/SwifTools/Notifier/SnarlNotifier.h
@@ -25,8 +25,6 @@ namespace Swift {
 			void handleMessageReceived(MSG* message);
 
 		private:
-			static const int STATUS_NOTIFICATION_TIMEOUT = 3;
-			static const int MESSAGE_NOTIFICATION_TIMEOUT = 5;
 			Snarl::SnarlInterface snarl;
 			Win32NotifierWindow* window;
 			typedef std::map<int, boost::function<void()> > NotificationsMap;
diff --git a/Swift/QtUI/FreeDesktopNotifier.cpp b/Swift/QtUI/FreeDesktopNotifier.cpp
new file mode 100644
index 0000000..2560882
--- /dev/null
+++ b/Swift/QtUI/FreeDesktopNotifier.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+//#pragma GCC diagnostic ignored "-Wredundant-decls"
+
+#include "Swift/QtUI/FreeDesktopNotifier.h"
+
+#include <QMap>
+#include <QString>
+#include <QStringList>
+#include <QtDBus/QtDBus>
+#include <algorithm>
+
+#include "QtSwiftUtil.h"
+
+namespace Swift {
+
+FreeDesktopNotifier::FreeDesktopNotifier(const String& name) : applicationName(name) {
+}
+
+void FreeDesktopNotifier::doShowMessage(Type type, const String& subject, const String& description, const boost::filesystem::path& picture, boost::function<void()>) {
+	QDBusConnection bus = QDBusConnection::sessionBus();
+	if (!bus.isConnected()) {
+		return;
+	}
+	std::vector<Notifier::Type> defaultTypes = getDefaultTypes();
+	if (std::find(defaultTypes.begin(), defaultTypes.end(), type) == defaultTypes.end()) {
+		return;
+	}
+
+	int timeout = (type == IncomingMessage || type == SystemMessage) ? DEFAULT_MESSAGE_NOTIFICATION_TIMEOUT_SECONDS : DEFAULT_STATUS_NOTIFICATION_TIMEOUT_SECONDS;
+
+	QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "", "Notify");
+
+	QStringList actions;
+	QMap<QString, QVariant> hints;
+	msg << P2QSTRING(applicationName);
+	msg << quint32(0); // ID of previous notification to replace
+	msg << picture.string().c_str(); // Icon to display
+	msg << P2QSTRING(subject); // Summary / Header of the message to display
+	msg << P2QSTRING(description); // Body of the message to display
+	msg << actions; // Actions from which the user may choose
+	msg << hints; // Hints to the server displaying the message
+	msg << qint32(timeout*1000); // Timeout in milliseconds
+
+	bus.call(msg);
+}
+
+}
diff --git a/Swift/QtUI/FreeDesktopNotifier.h b/Swift/QtUI/FreeDesktopNotifier.h
new file mode 100644
index 0000000..087e9e3
--- /dev/null
+++ b/Swift/QtUI/FreeDesktopNotifier.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "SwifTools/Notifier/Notifier.h"
+
+namespace Swift {
+	class FreeDesktopNotifier : public Notifier {
+		public:
+			FreeDesktopNotifier(const String& name);
+
+			virtual void doShowMessage(Type type, const String& subject, const String& description, const boost::filesystem::path& picture, boost::function<void()> callback);
+		
+		private:
+			String applicationName;
+	};
+}
diff --git a/Swift/QtUI/QtSwift.cpp b/Swift/QtUI/QtSwift.cpp
index 13543af..26d1664 100644
--- a/Swift/QtUI/QtSwift.cpp
+++ b/Swift/QtUI/QtSwift.cpp
@@ -41,6 +41,8 @@
 #elif defined(HAVE_SNARL)
 #include "QtWin32NotifierWindow.h"
 #include "SwifTools/Notifier/SnarlNotifier.h"
+#elif defined(SWIFTEN_PLATFORM_LINUX)
+#include "FreeDesktopNotifier.h"
 #else
 #include "SwifTools/Notifier/NullNotifier.h"
 #endif
@@ -103,6 +105,8 @@ QtSwift::QtSwift(po::variables_map options) : autoUpdater_(NULL) {
 #elif defined(HAVE_SNARL)
 	notifierWindow_ = new QtWin32NotifierWindow();
 	notifier_ = new SnarlNotifier(SWIFT_APPLICATION_NAME, notifierWindow_, applicationPathProvider_->getResourcePath("/images/logo-icon-32.png"));
+#elif defined(SWIFTEN_PLATFORM_LINUX)
+	notifier_ = new FreeDesktopNotifier(SWIFT_APPLICATION_NAME);
 #else
 	notifier_ = new NullNotifier();
 #endif
diff --git a/Swift/QtUI/SConscript b/Swift/QtUI/SConscript
index a3631e5..3d4fc9f 100644
--- a/Swift/QtUI/SConscript
+++ b/Swift/QtUI/SConscript
@@ -125,6 +125,9 @@ if env["PLATFORM"] == "win32" :
   myenv.RES("../resources/Windows/Swift.rc")
   sources += ["../resources/Windows/Swift.res"]
 
+if env["PLATFORM"] == "posix" :
+	sources += ["FreeDesktopNotifier.cpp"]
+
 if env["PLATFORM"] == "darwin" or env["PLATFORM"] == "win32" :
   swiftProgram = myenv.Program("Swift", sources)
 else :
-- 
cgit v0.10.2-6-g49f6