Added qmafw-gst-subtitles-renderer-0.0.55 for Meego Harmattan 1.2
[mafwsubrenderer] / qmafw-gst-subtitles-renderer / src / MafwMmcMonitor.cpp
diff --git a/qmafw-gst-subtitles-renderer/src/MafwMmcMonitor.cpp b/qmafw-gst-subtitles-renderer/src/MafwMmcMonitor.cpp
new file mode 100644 (file)
index 0000000..94fd5b5
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * This file is part of QMAFW
+ *
+ * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights
+ * reserved.
+ *
+ * Contact: Visa Smolander <visa.smolander@nokia.com>
+ *
+ * This software, including documentation, is protected by copyright controlled
+ * by Nokia Corporation. All rights are reserved. Copying, including
+ * reproducing, storing, adapting or translating, any or all of this material
+ * requires the prior written consent of Nokia Corporation. This material also
+ * contains confidential information which may not be disclosed to others
+ * without the prior written consent of Nokia.
+ *
+ */
+
+#include "MafwMmcMonitor.h"
+#include <QDebug>
+#include <glib.h>
+
+#include <usb_moded-dbus.h>
+#include <QDBusConnection>
+
+const QString MafwMmcMonitor::MMC_URI_PREFIX="file:///home/user/MyDocs";
+
+MafwMmcMonitor::MafwMmcMonitor(QObject* parent) : QObject(parent), m_mounted(false)
+{
+    m_gVolMonitor=g_volume_monitor_get();
+
+    g_signal_connect(m_gVolMonitor, "mount-removed",
+                     G_CALLBACK(unmountEvent), this);
+    g_signal_connect(m_gVolMonitor, "mount-added",
+                     G_CALLBACK(mountEvent), this);
+
+    GList* mounts=g_volume_monitor_get_mounts(m_gVolMonitor);
+    if( mounts )
+    {
+        for( guint i=0; i<g_list_length(mounts); i++ )
+        {
+            GMount* m=(GMount*)g_list_nth_data(mounts, i);
+            if(m && isMyDocs(m))
+            {
+                m_mounted = true;
+            }
+            g_object_unref(m);
+        }
+        g_list_free(mounts);
+    }
+
+    QDBusConnection conn = QDBusConnection::systemBus();
+    if( !conn.connect(USB_MODE_SERVICE,
+                      USB_MODE_OBJECT,
+                      USB_MODE_INTERFACE,
+                      USB_MODE_SIGNAL_NAME,
+                      this,
+                      SLOT(preUnmountEvent(QString))) )
+    {
+        qCritical() << "MafwGstRenderer could not connect to USB pre unmount signal!";
+    }
+}
+
+MafwMmcMonitor::~MafwMmcMonitor()
+{
+    g_object_unref(m_gVolMonitor);
+
+    QDBusConnection conn = QDBusConnection::systemBus();
+    conn.disconnect(USB_MODE_SERVICE,
+                    USB_MODE_OBJECT,
+                    USB_MODE_INTERFACE,
+                    USB_MODE_SIGNAL_NAME,
+                    this,
+                    SLOT(preUnmountEvent(QString)));
+}
+
+bool MafwMmcMonitor::isMounted()
+{
+    return m_mounted;
+}
+
+void MafwMmcMonitor::preUnmountEvent(const QString &state)
+{
+    //this slot will receive other information from usb_moded besides pre-unmount
+    if( state == USB_PRE_UNMOUNT )
+    {
+        qDebug() << "preUnmountEvent";
+        // This assumes that preunmount always affects MyDocs
+        Q_EMIT preUnmount();
+    }
+}
+
+void MafwMmcMonitor::unmountEvent(GVolumeMonitor * mon,
+                                       GMount  * mount,
+                                       gpointer userData)
+{
+    Q_UNUSED(mon);
+
+    qDebug() << "unmountEvent";
+
+    if( isMyDocs(mount) )
+    {
+        MafwMmcMonitor* self=static_cast<MafwMmcMonitor*>(userData);
+        self->m_mounted = false;
+    }
+}
+
+void MafwMmcMonitor::mountEvent(GVolumeMonitor * mon,
+                                       GMount  * mount,
+                                       gpointer userData)
+{
+    Q_UNUSED(mon);
+
+    qDebug() << "mountEvent";
+
+    if( isMyDocs(mount) )
+    {
+        MafwMmcMonitor* self=static_cast<MafwMmcMonitor*>(userData);
+        self->m_mounted = true;
+    }
+}
+
+bool MafwMmcMonitor::isMyDocs(GMount* mount)
+{
+    bool isIt=false;
+    GFile *root=0;
+    root = g_mount_get_root(mount);
+    if( root )
+    {
+        char* uri=g_file_get_uri(root);
+        if( uri && MMC_URI_PREFIX.compare(uri)==0 )
+        {
+            isIt=true;
+        }
+        qDebug() << "MafwMmcMonitor::isMyDocs" << uri << isIt;
+        g_free(uri);
+        g_object_unref(root);
+    }
+    return isIt;
+}