Added DUI control panel applet
[mafwsubrenderer] / qmafw-gst-subtitles-renderer / src / MafwMmcMonitor.cpp
1 /*
2  * This file is part of QMAFW
3  *
4  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights
5  * reserved.
6  *
7  * Contact: Visa Smolander <visa.smolander@nokia.com>
8  *
9  * This software, including documentation, is protected by copyright controlled
10  * by Nokia Corporation. All rights are reserved. Copying, including
11  * reproducing, storing, adapting or translating, any or all of this material
12  * requires the prior written consent of Nokia Corporation. This material also
13  * contains confidential information which may not be disclosed to others
14  * without the prior written consent of Nokia.
15  *
16  */
17
18 #include "MafwMmcMonitor.h"
19 #include <QDebug>
20 #include <glib.h>
21
22 #include <usb_moded-dbus.h>
23 #include <QDBusConnection>
24
25 const QString MafwMmcMonitor::MMC_URI_PREFIX="file:///home/user/MyDocs";
26
27 MafwMmcMonitor::MafwMmcMonitor(QObject* parent) : QObject(parent), m_mounted(false)
28 {
29     m_gVolMonitor=g_volume_monitor_get();
30
31     g_signal_connect(m_gVolMonitor, "mount-removed",
32                      G_CALLBACK(unmountEvent), this);
33     g_signal_connect(m_gVolMonitor, "mount-added",
34                      G_CALLBACK(mountEvent), this);
35
36     GList* mounts=g_volume_monitor_get_mounts(m_gVolMonitor);
37     if( mounts )
38     {
39         for( guint i=0; i<g_list_length(mounts); i++ )
40         {
41             GMount* m=(GMount*)g_list_nth_data(mounts, i);
42             if(m && isMyDocs(m))
43             {
44                 m_mounted = true;
45             }
46             g_object_unref(m);
47         }
48         g_list_free(mounts);
49     }
50
51     QDBusConnection conn = QDBusConnection::systemBus();
52     if( !conn.connect(USB_MODE_SERVICE,
53                       USB_MODE_OBJECT,
54                       USB_MODE_INTERFACE,
55                       USB_MODE_SIGNAL_NAME,
56                       this,
57                       SLOT(preUnmountEvent(QString))) )
58     {
59         qCritical() << "MafwGstRenderer could not connect to USB pre unmount signal!";
60     }
61 }
62
63 MafwMmcMonitor::~MafwMmcMonitor()
64 {
65     g_object_unref(m_gVolMonitor);
66
67     QDBusConnection conn = QDBusConnection::systemBus();
68     conn.disconnect(USB_MODE_SERVICE,
69                     USB_MODE_OBJECT,
70                     USB_MODE_INTERFACE,
71                     USB_MODE_SIGNAL_NAME,
72                     this,
73                     SLOT(preUnmountEvent(QString)));
74 }
75
76 bool MafwMmcMonitor::isMounted()
77 {
78     return m_mounted;
79 }
80
81 void MafwMmcMonitor::preUnmountEvent(const QString &state)
82 {
83     //this slot will receive other information from usb_moded besides pre-unmount
84     if( state == USB_PRE_UNMOUNT )
85     {
86         qDebug() << "preUnmountEvent";
87         // This assumes that preunmount always affects MyDocs
88         Q_EMIT preUnmount();
89     }
90 }
91
92 void MafwMmcMonitor::unmountEvent(GVolumeMonitor * mon,
93                                        GMount  * mount,
94                                        gpointer userData)
95 {
96     Q_UNUSED(mon);
97
98     qDebug() << "unmountEvent";
99
100     if( isMyDocs(mount) )
101     {
102         MafwMmcMonitor* self=static_cast<MafwMmcMonitor*>(userData);
103         self->m_mounted = false;
104     }
105 }
106
107 void MafwMmcMonitor::mountEvent(GVolumeMonitor * mon,
108                                        GMount  * mount,
109                                        gpointer userData)
110 {
111     Q_UNUSED(mon);
112
113     qDebug() << "mountEvent";
114
115     if( isMyDocs(mount) )
116     {
117         MafwMmcMonitor* self=static_cast<MafwMmcMonitor*>(userData);
118         self->m_mounted = true;
119     }
120 }
121
122 bool MafwMmcMonitor::isMyDocs(GMount* mount)
123 {
124     bool isIt=false;
125     GFile *root=0;
126     root = g_mount_get_root(mount);
127     if( root )
128     {
129         char* uri=g_file_get_uri(root);
130         if( uri && MMC_URI_PREFIX.compare(uri)==0 )
131         {
132             isIt=true;
133         }
134         qDebug() << "MafwMmcMonitor::isMyDocs" << uri << isIt;
135         g_free(uri);
136         g_object_unref(root);
137     }
138     return isIt;
139 }