Merge lp:~3v1n0/unity/device-launcher-section-c++ into lp:unity

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Andrea Azzarone
Approved revision: no longer in the source branch.
Merged at revision: 2334
Proposed branch: lp:~3v1n0/unity/device-launcher-section-c++
Merge into: lp:unity
Diff against target: 265 lines (+59/-99)
4 files modified
plugins/unityshell/src/DeviceLauncherIcon.cpp (+1/-1)
plugins/unityshell/src/DeviceLauncherIcon.h (+2/-2)
plugins/unityshell/src/DeviceLauncherSection.cpp (+48/-75)
plugins/unityshell/src/DeviceLauncherSection.h (+8/-21)
To merge this branch: bzr merge lp:~3v1n0/unity/device-launcher-section-c++
Reviewer Review Type Date Requested Status
Andrea Azzarone (community) Approve
Review via email: mp+103349@code.launchpad.net

Commit message

DeviceLauncherSection: updated to use glib::SignalManager and glib::Object

This allow to be more safe; it fixes also some crashes caused by the fact that DeviceLauncherIcon didn't own the GVolume* object. Now it uses glib::Object completely, so we can automagically ref/unref the volume when the icon is created/removed.

To post a comment you must log in.
Revision history for this message
Andrea Azzarone (azzar1) wrote :

LGTM.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/unityshell/src/DeviceLauncherIcon.cpp'
2--- plugins/unityshell/src/DeviceLauncherIcon.cpp 2012-04-03 13:44:01 +0000
3+++ plugins/unityshell/src/DeviceLauncherIcon.cpp 2012-04-24 19:07:27 +0000
4@@ -43,7 +43,7 @@
5
6 }
7
8-DeviceLauncherIcon::DeviceLauncherIcon(GVolume* volume)
9+DeviceLauncherIcon::DeviceLauncherIcon(glib::Object<GVolume> const& volume)
10 : SimpleLauncherIcon()
11 , volume_(volume)
12 , device_file_(g_volume_get_identifier(volume_, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE))
13
14=== modified file 'plugins/unityshell/src/DeviceLauncherIcon.h'
15--- plugins/unityshell/src/DeviceLauncherIcon.h 2012-04-03 13:44:01 +0000
16+++ plugins/unityshell/src/DeviceLauncherIcon.h 2012-04-24 19:07:27 +0000
17@@ -36,7 +36,7 @@
18 {
19
20 public:
21- DeviceLauncherIcon(GVolume* volume);
22+ DeviceLauncherIcon(glib::Object<GVolume> const& volume);
23
24 void UpdateVisibility(int visibility = -1);
25 void OnRemoved();
26@@ -67,7 +67,7 @@
27 void ShowNotification(std::string const&, unsigned, GdkPixbuf*, std::string const&);
28
29 private:
30- GVolume* volume_;
31+ glib::Object<GVolume> volume_;
32 glib::String device_file_;
33 std::string name_;
34 glib::Object<GduDevice> gdu_device_;
35
36=== modified file 'plugins/unityshell/src/DeviceLauncherSection.cpp'
37--- plugins/unityshell/src/DeviceLauncherSection.cpp 2012-03-14 06:24:18 +0000
38+++ plugins/unityshell/src/DeviceLauncherSection.cpp 2012-04-24 19:07:27 +0000
39@@ -25,128 +25,101 @@
40
41 DeviceLauncherSection::DeviceLauncherSection()
42 : monitor_(g_volume_monitor_get())
43-{
44- on_volume_added_handler_id_ = g_signal_connect(monitor_,
45- "volume-added",
46- G_CALLBACK(&DeviceLauncherSection::OnVolumeAdded),
47- this);
48-
49- on_volume_removed_handler_id_ = g_signal_connect(monitor_,
50- "volume-removed",
51- G_CALLBACK(&DeviceLauncherSection::OnVolumeRemoved),
52- this);
53-
54- on_mount_added_handler_id_ = g_signal_connect(monitor_,
55- "mount-added",
56- G_CALLBACK(&DeviceLauncherSection::OnMountAdded),
57- this);
58-
59- on_mount_pre_unmount_handler_id_ = g_signal_connect(monitor_,
60- "mount-pre-unmount",
61- G_CALLBACK(&DeviceLauncherSection::OnMountPreUnmount),
62- this);
63-
64- on_device_populate_entry_id_ = g_idle_add((GSourceFunc)&DeviceLauncherSection::PopulateEntries, this);
65+{
66+ typedef glib::Signal<void, GVolumeMonitor*, GVolume*> VolumeSignal;
67+ sig_manager_.Add(new VolumeSignal(monitor_, "volume-added", sigc::mem_fun(this, &DeviceLauncherSection::OnVolumeAdded)));
68+ sig_manager_.Add(new VolumeSignal(monitor_, "volume-removed", sigc::mem_fun(this, &DeviceLauncherSection::OnVolumeRemoved)));
69+
70+ typedef glib::Signal<void, GVolumeMonitor*, GMount*> MountSignal;
71+ sig_manager_.Add(new MountSignal(monitor_, "mount-added", sigc::mem_fun(this, &DeviceLauncherSection::OnMountAdded)));
72+ sig_manager_.Add(new MountSignal(monitor_, "mount-pre-unmount", sigc::mem_fun(this, &DeviceLauncherSection::OnMountPreUnmount)));
73+
74+ on_device_populate_entry_id_ = g_idle_add([] (gpointer data) {
75+ auto self = static_cast<DeviceLauncherSection*>(data);
76+ self->PopulateEntries();
77+ self->on_device_populate_entry_id_ = 0;
78+ return FALSE;
79+ }, this);
80 }
81
82 DeviceLauncherSection::~DeviceLauncherSection()
83 {
84- if (on_volume_added_handler_id_)
85- g_signal_handler_disconnect((gpointer) monitor_,
86- on_volume_added_handler_id_);
87-
88- if (on_volume_removed_handler_id_)
89- g_signal_handler_disconnect((gpointer) monitor_,
90- on_volume_removed_handler_id_);
91-
92- if (on_mount_added_handler_id_)
93- g_signal_handler_disconnect((gpointer) monitor_,
94- on_mount_added_handler_id_);
95-
96- if (on_mount_pre_unmount_handler_id_)
97- g_signal_handler_disconnect((gpointer) monitor_,
98- on_mount_pre_unmount_handler_id_);
99-
100 if (on_device_populate_entry_id_)
101 g_source_remove(on_device_populate_entry_id_);
102 }
103
104-bool DeviceLauncherSection::PopulateEntries(DeviceLauncherSection* self)
105+void DeviceLauncherSection::PopulateEntries()
106 {
107- GList* volumes = g_volume_monitor_get_volumes(self->monitor_);
108+ GList* volumes = g_volume_monitor_get_volumes(monitor_);
109
110 for (GList* v = volumes; v; v = v->next)
111 {
112- glib::Object<GVolume> volume((GVolume* )v->data);
113+ if (!G_IS_VOLUME(v->data))
114+ continue;
115+
116+ // This will unref the volume, since the list entries needs that.
117+ // We'll keep a reference in the icon.
118+ glib::Object<GVolume> volume(G_VOLUME(v->data));
119 DeviceLauncherIcon* icon = new DeviceLauncherIcon(volume);
120
121- self->map_[volume] = icon;
122- self->IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
123+ map_[volume] = icon;
124+ IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
125 }
126
127 g_list_free(volumes);
128-
129- self->on_device_populate_entry_id_ = 0;
130-
131- return false;
132 }
133
134 /* Uses a std::map to track all the volume icons shown and not shown.
135 * Keep in mind: when "volume-removed" is recevied we should erase
136 * the pair (GVolume - DeviceLauncherIcon) from the std::map to avoid leaks
137 */
138-void DeviceLauncherSection::OnVolumeAdded(GVolumeMonitor* monitor,
139- GVolume* volume,
140- DeviceLauncherSection* self)
141+void DeviceLauncherSection::OnVolumeAdded(GVolumeMonitor* monitor, GVolume* volume)
142 {
143- DeviceLauncherIcon* icon = new DeviceLauncherIcon(volume);
144-
145- self->map_[volume] = icon;
146- self->IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
147+ // This just wraps the volume in a glib::Object, global ref_count is only
148+ // temporary changed.
149+ glib::Object<GVolume> gvolume(volume, glib::AddRef());
150+ DeviceLauncherIcon* icon = new DeviceLauncherIcon(gvolume);
151+
152+ map_[gvolume] = icon;
153+ IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
154 }
155
156-void DeviceLauncherSection::OnVolumeRemoved(GVolumeMonitor* monitor,
157- GVolume* volume,
158- DeviceLauncherSection* self)
159+void DeviceLauncherSection::OnVolumeRemoved(GVolumeMonitor* monitor, GVolume* volume)
160 {
161+ auto volume_it = map_.find(volume);
162+
163 // It should not happen! Let me do the check anyway.
164- if (self->map_.find(volume) != self->map_.end())
165- {
166- self->map_[volume]->OnRemoved();
167- self->map_.erase(volume);
168+ if (volume_it != map_.end())
169+ {
170+ volume_it->second->OnRemoved();
171+ map_.erase(volume_it);
172 }
173 }
174
175 /* Keep in mind: we could have a GMount without a related GVolume
176 * so check everything to avoid unwanted behaviors.
177 */
178-void DeviceLauncherSection::OnMountAdded(GVolumeMonitor* monitor,
179- GMount* mount,
180- DeviceLauncherSection* self)
181+void DeviceLauncherSection::OnMountAdded(GVolumeMonitor* monitor, GMount* mount)
182 {
183- std::map<GVolume* , DeviceLauncherIcon* >::iterator it;
184 glib::Object<GVolume> volume(g_mount_get_volume(mount));
185
186- it = self->map_.find(volume);
187+ auto volume_it = map_.find(volume);
188
189- if (it != self->map_.end())
190- it->second->UpdateVisibility(1);
191+ if (volume_it != map_.end())
192+ volume_it->second->UpdateVisibility(1);
193 }
194
195 /* We don't use "mount-removed" signal since it is received after "volume-removed"
196 * signal. You should read also the comment above.
197 */
198-void DeviceLauncherSection::OnMountPreUnmount(GVolumeMonitor* monitor,
199- GMount* mount,
200- DeviceLauncherSection* self)
201+void DeviceLauncherSection::OnMountPreUnmount(GVolumeMonitor* monitor, GMount* mount)
202 {
203- std::map<GVolume* , DeviceLauncherIcon* >::iterator it;
204 glib::Object<GVolume> volume(g_mount_get_volume(mount));
205
206- it = self->map_.find(volume);
207+ auto volume_it = map_.find(volume);
208
209- if (it != self->map_.end())
210- it->second->UpdateVisibility(0);
211+ if (volume_it != map_.end())
212+ volume_it->second->UpdateVisibility(0);
213 }
214
215 } // namespace launcher
216
217=== modified file 'plugins/unityshell/src/DeviceLauncherSection.h'
218--- plugins/unityshell/src/DeviceLauncherSection.h 2012-03-14 06:24:18 +0000
219+++ plugins/unityshell/src/DeviceLauncherSection.h 2012-04-24 19:07:27 +0000
220@@ -25,6 +25,7 @@
221 #include <sigc++/sigc++.h>
222 #include <sigc++/signal.h>
223 #include <UnityCore/GLibWrapper.h>
224+#include <UnityCore/GLibSignal.h>
225
226 #include "DeviceLauncherIcon.h"
227
228@@ -45,31 +46,17 @@
229 sigc::signal<void, AbstractLauncherIcon::Ptr> IconAdded;
230
231 private:
232- static bool PopulateEntries(DeviceLauncherSection* self);
233-
234- static void OnVolumeAdded(GVolumeMonitor* monitor,
235- GVolume* volume,
236- DeviceLauncherSection* self);
237-
238- static void OnVolumeRemoved(GVolumeMonitor* monitor,
239- GVolume* volume,
240- DeviceLauncherSection* self);
241-
242- static void OnMountAdded(GVolumeMonitor* monitor,
243- GMount* mount,
244- DeviceLauncherSection* self);
245-
246- static void OnMountPreUnmount(GVolumeMonitor* monitor,
247- GMount* mount,
248- DeviceLauncherSection* self);
249+ void PopulateEntries();
250+
251+ void OnVolumeAdded(GVolumeMonitor* monitor, GVolume* volume);
252+ void OnVolumeRemoved(GVolumeMonitor* monitor, GVolume* volume);
253+ void OnMountAdded(GVolumeMonitor* monitor, GMount* mount);
254+ void OnMountPreUnmount(GVolumeMonitor* monitor, GMount* mount);
255
256 private:
257+ glib::SignalManager sig_manager_;
258 glib::Object<GVolumeMonitor> monitor_;
259 std::map<GVolume*, DeviceLauncherIcon*> map_;
260- gulong on_volume_added_handler_id_;
261- gulong on_volume_removed_handler_id_;
262- gulong on_mount_added_handler_id_;
263- gulong on_mount_pre_unmount_handler_id_;
264 gulong on_device_populate_entry_id_;
265 };
266