>From b16777e603daa259d67332a2f0f401927527d296 Mon Sep 17 00:00:00 2001
From: Salvatore Iovene <salvatore.iovene@linux.intel.com>
Date: Wed, 15 Jun 2011 04:05:21 -0700
Subject: [PATCH 7/7] syncevo-dbus-server: use a "factory" to create the appropriate NotificationManager.

---
 src/Makefile-gen.am                |    1 +
 src/NotificationManager.h          |    4 ++-
 src/NotificationManagerBase.h      |   43 +++++++++++++++++++++++++++
 src/NotificationManagerFactory.cpp |   57 ++++++++++++++++++++++++++++++++++++
 src/NotificationManagerFactory.h   |   43 +++++++++++++++++++++++++++
 src/syncevo-dbus-server.cpp        |   32 ++++++++++----------
 6 files changed, 163 insertions(+), 17 deletions(-)
 create mode 100644 src/NotificationManagerBase.h
 create mode 100644 src/NotificationManagerFactory.cpp
 create mode 100644 src/NotificationManagerFactory.h

diff --git a/src/Makefile-gen.am b/src/Makefile-gen.am
index 7f1881e..c2af170 100644
--- a/src/Makefile-gen.am
+++ b/src/Makefile-gen.am
@@ -186,6 +186,7 @@ syncevo_dbus_server_SOURCES = \
 	NotificationBackendNoop.cpp \
 	NotificationBackendLibnotify.cpp \
 	NotificationBackendMLite.cpp \
+	NotificationManagerFactory.cpp \
 	syncevo-dbus-server.cpp \
 	$(CORE_SOURCES)
 if ENABLE_UNIT_TESTS
diff --git a/src/NotificationManager.h b/src/NotificationManager.h
index 659f11a..5a2cbba 100644
--- a/src/NotificationManager.h
+++ b/src/NotificationManager.h
@@ -20,6 +20,8 @@
 #ifndef __NOTIFICATION_MANAGER_H
 #define __NOTIFICATION_MANAGER_H
 
+#include "NotificationManagerBase.h"
+
 #include "syncevo/declarations.h"
 
 #include <string>
@@ -27,7 +29,7 @@
 SE_BEGIN_CXX
 
 template <class T>
-class NotificationManager {
+class NotificationManager : public NotificationManagerBase {
     public:
         NotificationManager();
         virtual ~NotificationManager();
diff --git a/src/NotificationManagerBase.h b/src/NotificationManagerBase.h
new file mode 100644
index 0000000..562e1f6
--- /dev/null
+++ b/src/NotificationManagerBase.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2011 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) version 3.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301  USA
+ */
+
+#ifndef __NOTIFICATION_MANAGER_BASE_H
+#define __NOTIFICATION_MANAGER_BASE_H
+
+#include "syncevo/declarations.h"
+
+#include <string>
+
+SE_BEGIN_CXX
+
+class NotificationManagerBase {
+    public:
+        /** Initializes the backend. */
+        virtual bool init() { return true; }
+
+        /** Publishes the notification through the backend. */
+        virtual void publish(const std::string& summary,
+                             const std::string& body,
+                             const std::string& viewParam = std::string()) {};
+};
+
+SE_END_CXX
+
+#endif
+
diff --git a/src/NotificationManagerFactory.cpp b/src/NotificationManagerFactory.cpp
new file mode 100644
index 0000000..59abe74
--- /dev/null
+++ b/src/NotificationManagerFactory.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2011 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) version 3.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301  USA
+ */
+
+#include "NotificationManagerFactory.h"
+
+#include "NotificationBackendNoop.h"
+#include "NotificationBackendMLite.h"
+#include "NotificationBackendLibnotify.h"
+#include "NotificationManager.h"
+
+#include <fstream>
+
+SE_BEGIN_CXX
+
+#define SYNC_UI_PATH "/usr/bin/sync-ui"
+
+NotificationManagerBase* NotificationManagerFactory::createManager()
+{
+    NotificationManagerBase *mgr;
+
+    /* Detect what kind of manager we need: if /usr/bin/sync-ui
+     * exists, we shall use the MLite backend; otherwise, if
+     * libnotify is enabled, we shall use the libnotify backend;
+     * if everything fails, then we'll use the no-op backend.
+     */
+    std::ifstream path(SYNC_UI_PATH);
+    if(path) {
+        mgr = new NotificationManager<NotificationBackendMLite>();
+    } else {
+#if HAS_NOTIFY
+        mgr = new NotificationManager<NotificationBackendLibnotify>();
+#else
+        mgr = new NotificationManager<NotificationBackendNoop>();
+#endif
+    }
+
+    return mgr;
+}
+
+SE_END_CXX
+
diff --git a/src/NotificationManagerFactory.h b/src/NotificationManagerFactory.h
new file mode 100644
index 0000000..b541baa
--- /dev/null
+++ b/src/NotificationManagerFactory.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2011 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) version 3.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301  USA
+ */
+
+#ifndef __NOTIFICATION_MANAGER_FACTORY_H
+#define __NOTIFICATION_MANAGER_FACTORY_H
+
+#include "syncevo/declarations.h"
+#include "NotificationManager.h"
+
+SE_BEGIN_CXX
+
+class NotificationBackendBase;
+
+class NotificationManagerFactory {
+    public:
+        /** Creates the appropriate NotificationManager for the current
+         * platform.
+         * Note: NotificationManagerFactory does not take ownership of
+         * the returned pointer: the user must delete it when done.
+         */
+        static NotificationManagerBase* createManager();
+};
+
+SE_END_CXX
+
+#endif
+
diff --git a/src/syncevo-dbus-server.cpp b/src/syncevo-dbus-server.cpp
index e34ad20..8b1e0e3 100644
--- a/src/syncevo-dbus-server.cpp
+++ b/src/syncevo-dbus-server.cpp
@@ -92,10 +92,7 @@ extern "C" {
 #include <kwallet.h>
 #endif
 
-#include "NotificationBackendNoop.h"
-#include "NotificationBackendMLite.h"
-#include "NotificationBackendLibnotify.h"
-#include "NotificationManager.h"
+#include "NotificationManagerFactory.h"
 
 using namespace GDBusCXX;
 using namespace SyncEvo;
@@ -813,13 +810,7 @@ class AutoSyncManager : public SessionListener
     bool m_syncSuccessStart;
 
     /** used to send notifications */
-#if(defined(HAS_MLITE))
-    NotificationManager<NotificationBackendMLite> m_notify;
-#elif(defined(HAS_LIBNOTIFY))
-    NotificationManager<NotificationBackendLibnotify> m_notify; 
-#else
-    NotificationManager<NotificationBackendNoop> m_notify;
-#endif
+    NotificationManagerBase* m_notificationManager;
 
     /** 
      * It reads all peers which are enabled to do auto sync and store them in
@@ -852,11 +843,17 @@ class AutoSyncManager : public SessionListener
 
  public:
     AutoSyncManager(DBusServer &server)
-        : m_server(server), m_syncSuccessStart(false) 
+        : m_server(server), m_syncSuccessStart(false), m_notificationManager(0)
     { 
         init();
     }
 
+    virtual ~AutoSyncManager()
+    {
+        if(m_notificationManager != 0)
+            delete m_notificationManager;
+    }
+
     /**
      * prevent dbus server automatic termination when it has
      * any auto sync task enabled in the configs.
@@ -6431,7 +6428,10 @@ void AutoSyncManager::init()
         initConfig(server.first);
     }
 
-    m_notify.init();
+    if(m_notificationManager == 0) {
+        m_notificationManager = NotificationManagerFactory::createManager();
+        m_notificationManager->init();
+    }
 }
 
 void AutoSyncManager::initConfig(const string &configName)
@@ -6639,7 +6639,7 @@ void AutoSyncManager::syncSuccessStart()
         string summary = StringPrintf(_("%s is syncing"), m_activeTask->m_peer.c_str());
         string body = StringPrintf(_("We have just started to sync your computer with the %s sync service."), m_activeTask->m_peer.c_str());
         //TODO: set config information for 'sync-ui'
-        m_notify.publish(summary, body);
+        m_notificationManager->publish(summary, body);
     }
 }
 
@@ -6654,13 +6654,13 @@ void AutoSyncManager::syncDone(SyncMLStatus status)
             summary = StringPrintf(_("%s sync complete"), m_activeTask->m_peer.c_str());
             body = StringPrintf(_("We have just finished syncing your computer with the %s sync service."), m_activeTask->m_peer.c_str());
             //TODO: set config information for 'sync-ui'
-            m_notify.publish(summary, body);
+            m_notificationManager->publish(summary, body);
         } else if(m_syncSuccessStart || (!m_syncSuccessStart && status == STATUS_FATAL)) {
             //if sync is successfully started and has errors, or not started successful with a fatal problem
             summary = StringPrintf(_("Sync problem."));
             body = StringPrintf(_("Sorry, there's a problem with your sync that you need to attend to."));
             //TODO: set config information for 'sync-ui'
-            m_notify.publish(summary, body);
+            m_notificationManager->publish(summary, body);
         }
     }
     m_session.reset();
-- 
1.7.2.2

