2
* Copyright (C) 2010 Canonical, Ltd.
5
* Olivier Tilloy <olivier.tilloy@canonical.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 3.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "launcherdeviceslist.h"
22
LauncherDevicesList::LauncherDevicesList(QObject* parent) :
23
QAbstractListModel(parent)
25
m_volume_monitor = g_volume_monitor_get();
27
GList* volumes = g_volume_monitor_get_volumes(m_volume_monitor);
28
for(GList* li = volumes; li != NULL; li = g_list_next(li)) {
29
GVolume* volume = (GVolume*) li->data;
30
onVolumeAdded(m_volume_monitor, volume);
31
g_object_unref(volume);
35
GList* mounts = g_volume_monitor_get_mounts(m_volume_monitor);
36
for(GList* li = mounts; li != NULL; li = g_list_next(li)) {
37
GMount* mount = (GMount*) li->data;
38
onMountAdded(m_volume_monitor, mount);
39
g_object_unref(mount);
43
m_handler_id_volume = g_signal_connect(m_volume_monitor, "volume-added",
44
G_CALLBACK(LauncherDevicesList::onVolumeAddedProxy), this);
45
m_handler_id_mount = g_signal_connect(m_volume_monitor, "mount-added",
46
G_CALLBACK(LauncherDevicesList::onMountAddedProxy), this);
49
LauncherDevicesList::~LauncherDevicesList()
51
g_signal_handler_disconnect(m_volume_monitor, m_handler_id_volume);
52
g_signal_handler_disconnect(m_volume_monitor, m_handler_id_mount);
53
g_object_unref(m_volume_monitor);
55
QList<LauncherDevice*>::iterator iter;
56
for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
62
LauncherDevicesList::rowCount(const QModelIndex &parent) const
66
return m_devices.size();
70
LauncherDevicesList::data(const QModelIndex &index, int role) const
74
if (!index.isValid()) {
78
return QVariant::fromValue(m_devices.at(index.row()));
82
LauncherDevicesList::onVolumeAddedProxy(GVolumeMonitor* volume_monitor, GVolume* volume, gpointer data)
84
LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
85
return _this->onVolumeAdded(volume_monitor, volume);
89
LauncherDevicesList::onVolumeAdded(GVolumeMonitor* volume_monitor, GVolume* volume)
91
if (g_volume_can_eject(volume)) {
92
LauncherDevice* device = new LauncherDevice;
93
device->setVolume(volume);
94
beginInsertRows(QModelIndex(), m_devices.size(), m_devices.size());
95
m_devices.append(device);
97
g_signal_connect(volume, "removed",
98
G_CALLBACK(LauncherDevicesList::onVolumeRemovedProxy), this);
103
LauncherDevicesList::onVolumeRemovedProxy(GVolume* volume, gpointer data)
105
LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
106
return _this->onVolumeRemoved(volume);
110
LauncherDevicesList::onVolumeRemoved(GVolume* volume)
112
QList<LauncherDevice*>::iterator iter;
114
for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
115
if ((*iter)->getVolume() == volume) {
116
beginRemoveRows(QModelIndex(), i, i);
117
LauncherDevice* device = m_devices.takeAt(i);
127
LauncherDevicesList::onMountAddedProxy(GVolumeMonitor* volume_monitor, GMount* mount, gpointer data)
129
LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
130
return _this->onMountAdded(volume_monitor, mount);
134
LauncherDevicesList::onMountAdded(GVolumeMonitor* volume_monitor, GMount* mount)
136
if (!g_mount_can_unmount(mount)) {
140
GVolume* volume = g_mount_get_volume(mount);
141
if (volume == NULL) {
145
QList<LauncherDevice*>::const_iterator iter;
146
for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
147
if ((*iter)->getVolume() == volume) {
148
/* The device is already displayed. */
149
g_object_unref(volume);
154
LauncherDevice* device = new LauncherDevice;
155
device->setVolume(volume);
156
beginInsertRows(QModelIndex(), m_devices.size(), m_devices.size());
157
m_devices.append(device);
159
g_signal_connect(mount, "unmounted",
160
G_CALLBACK(LauncherDevicesList::onMountUnmountedProxy), this);
162
g_object_unref(volume);
166
LauncherDevicesList::onMountUnmountedProxy(GMount* mount, gpointer data)
168
LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
169
return _this->onMountUnmounted(mount);
173
LauncherDevicesList::onMountUnmounted(GMount* mount)
175
QList<LauncherDevice*>::iterator iter;
177
for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
178
/* At this point the mount is unmounted, so we can't rely on comparing
179
it to the current device's mount. */
180
GVolume* volume = (*iter)->getVolume();
181
if (!g_volume_can_eject(volume)) {
182
GMount* m = g_volume_get_mount(volume);
184
/* The volume can't eject and it is not mounted: this is our
186
beginRemoveRows(QModelIndex(), i, i);
187
LauncherDevice* device = m_devices.takeAt(i);
199
#include "launcherdeviceslist.moc"