~josephjamesmills/ubuntutv/mockup-ppa-branch

« back to all changes in this revision

Viewing changes to Unity-2d/ubuntu-tv/libunity-2d-private/src/launcherdeviceslist.cpp

  • Committer: Joseph Mills
  • Date: 2012-11-19 21:25:03 UTC
  • Revision ID: josephjamesmills@gmail.com-20121119212503-lwix013dozhb0ar4
adding Unity 2d stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Olivier Tilloy <olivier.tilloy@canonical.com>
 
6
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include "launcherdeviceslist.h"
 
21
 
 
22
LauncherDevicesList::LauncherDevicesList(QObject* parent) :
 
23
    QAbstractListModel(parent)
 
24
{
 
25
    m_volume_monitor = g_volume_monitor_get();
 
26
 
 
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);
 
32
    }
 
33
    g_list_free(volumes);
 
34
 
 
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);
 
40
    }
 
41
    g_list_free(mounts);
 
42
 
 
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);
 
47
}
 
48
 
 
49
LauncherDevicesList::~LauncherDevicesList()
 
50
{
 
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);
 
54
 
 
55
    QList<LauncherDevice*>::iterator iter;
 
56
    for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
 
57
        delete *iter;
 
58
    }
 
59
}
 
60
 
 
61
int
 
62
LauncherDevicesList::rowCount(const QModelIndex &parent) const
 
63
{
 
64
    Q_UNUSED(parent)
 
65
 
 
66
    return m_devices.size();
 
67
}
 
68
 
 
69
QVariant
 
70
LauncherDevicesList::data(const QModelIndex &index, int role) const
 
71
{
 
72
    Q_UNUSED(role);
 
73
 
 
74
    if (!index.isValid()) {
 
75
        return QVariant();
 
76
    }
 
77
 
 
78
    return QVariant::fromValue(m_devices.at(index.row()));
 
79
}
 
80
 
 
81
void
 
82
LauncherDevicesList::onVolumeAddedProxy(GVolumeMonitor* volume_monitor, GVolume* volume, gpointer data)
 
83
{
 
84
    LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
 
85
    return _this->onVolumeAdded(volume_monitor, volume);
 
86
}
 
87
 
 
88
void
 
89
LauncherDevicesList::onVolumeAdded(GVolumeMonitor* volume_monitor, GVolume* volume)
 
90
{
 
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);
 
96
        endInsertRows();
 
97
        g_signal_connect(volume, "removed",
 
98
                         G_CALLBACK(LauncherDevicesList::onVolumeRemovedProxy), this);
 
99
    }
 
100
}
 
101
 
 
102
void
 
103
LauncherDevicesList::onVolumeRemovedProxy(GVolume* volume, gpointer data)
 
104
{
 
105
    LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
 
106
    return _this->onVolumeRemoved(volume);
 
107
}
 
108
 
 
109
void
 
110
LauncherDevicesList::onVolumeRemoved(GVolume* volume)
 
111
{
 
112
    QList<LauncherDevice*>::iterator iter;
 
113
    int i = 0;
 
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);
 
118
            endRemoveRows();
 
119
            delete device;
 
120
            break;
 
121
        }
 
122
        ++i;
 
123
    }
 
124
}
 
125
 
 
126
void
 
127
LauncherDevicesList::onMountAddedProxy(GVolumeMonitor* volume_monitor, GMount* mount, gpointer data)
 
128
{
 
129
    LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
 
130
    return _this->onMountAdded(volume_monitor, mount);
 
131
}
 
132
 
 
133
void
 
134
LauncherDevicesList::onMountAdded(GVolumeMonitor* volume_monitor, GMount* mount)
 
135
{
 
136
    if (!g_mount_can_unmount(mount)) {
 
137
        return;
 
138
    }
 
139
 
 
140
    GVolume* volume = g_mount_get_volume(mount);
 
141
    if (volume == NULL) {
 
142
        return;
 
143
    }
 
144
 
 
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);
 
150
            return;
 
151
        }
 
152
    }
 
153
 
 
154
    LauncherDevice* device = new LauncherDevice;
 
155
    device->setVolume(volume);
 
156
    beginInsertRows(QModelIndex(), m_devices.size(), m_devices.size());
 
157
    m_devices.append(device);
 
158
    endInsertRows();
 
159
    g_signal_connect(mount, "unmounted",
 
160
                     G_CALLBACK(LauncherDevicesList::onMountUnmountedProxy), this);
 
161
 
 
162
    g_object_unref(volume);
 
163
}
 
164
 
 
165
void
 
166
LauncherDevicesList::onMountUnmountedProxy(GMount* mount, gpointer data)
 
167
{
 
168
    LauncherDevicesList* _this = static_cast<LauncherDevicesList*>(data);
 
169
    return _this->onMountUnmounted(mount);
 
170
}
 
171
 
 
172
void
 
173
LauncherDevicesList::onMountUnmounted(GMount* mount)
 
174
{
 
175
    QList<LauncherDevice*>::iterator iter;
 
176
    int i = 0;
 
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);
 
183
            if (m == NULL) {
 
184
                /* The volume can't eject and it is not mounted: this is our
 
185
                   device. */
 
186
                beginRemoveRows(QModelIndex(), i, i);
 
187
                LauncherDevice* device = m_devices.takeAt(i);
 
188
                endRemoveRows();
 
189
                delete device;
 
190
                break;
 
191
            } else {
 
192
                g_object_unref(m);
 
193
            }
 
194
        }
 
195
        ++i;
 
196
    }
 
197
}
 
198
 
 
199
#include "launcherdeviceslist.moc"