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
=== modified file 'plugins/unityshell/src/DeviceLauncherIcon.cpp'
--- plugins/unityshell/src/DeviceLauncherIcon.cpp 2012-04-03 13:44:01 +0000
+++ plugins/unityshell/src/DeviceLauncherIcon.cpp 2012-04-24 19:07:27 +0000
@@ -43,7 +43,7 @@
4343
44}44}
4545
46DeviceLauncherIcon::DeviceLauncherIcon(GVolume* volume)46DeviceLauncherIcon::DeviceLauncherIcon(glib::Object<GVolume> const& volume)
47 : SimpleLauncherIcon()47 : SimpleLauncherIcon()
48 , volume_(volume)48 , volume_(volume)
49 , device_file_(g_volume_get_identifier(volume_, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE))49 , device_file_(g_volume_get_identifier(volume_, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE))
5050
=== modified file 'plugins/unityshell/src/DeviceLauncherIcon.h'
--- plugins/unityshell/src/DeviceLauncherIcon.h 2012-04-03 13:44:01 +0000
+++ plugins/unityshell/src/DeviceLauncherIcon.h 2012-04-24 19:07:27 +0000
@@ -36,7 +36,7 @@
36{36{
3737
38public:38public:
39 DeviceLauncherIcon(GVolume* volume);39 DeviceLauncherIcon(glib::Object<GVolume> const& volume);
4040
41 void UpdateVisibility(int visibility = -1);41 void UpdateVisibility(int visibility = -1);
42 void OnRemoved();42 void OnRemoved();
@@ -67,7 +67,7 @@
67 void ShowNotification(std::string const&, unsigned, GdkPixbuf*, std::string const&);67 void ShowNotification(std::string const&, unsigned, GdkPixbuf*, std::string const&);
6868
69private:69private:
70 GVolume* volume_;70 glib::Object<GVolume> volume_;
71 glib::String device_file_;71 glib::String device_file_;
72 std::string name_;72 std::string name_;
73 glib::Object<GduDevice> gdu_device_;73 glib::Object<GduDevice> gdu_device_;
7474
=== modified file 'plugins/unityshell/src/DeviceLauncherSection.cpp'
--- plugins/unityshell/src/DeviceLauncherSection.cpp 2012-03-14 06:24:18 +0000
+++ plugins/unityshell/src/DeviceLauncherSection.cpp 2012-04-24 19:07:27 +0000
@@ -25,128 +25,101 @@
2525
26DeviceLauncherSection::DeviceLauncherSection()26DeviceLauncherSection::DeviceLauncherSection()
27 : monitor_(g_volume_monitor_get())27 : monitor_(g_volume_monitor_get())
28{ 28{
29 on_volume_added_handler_id_ = g_signal_connect(monitor_,29 typedef glib::Signal<void, GVolumeMonitor*, GVolume*> VolumeSignal;
30 "volume-added",30 sig_manager_.Add(new VolumeSignal(monitor_, "volume-added", sigc::mem_fun(this, &DeviceLauncherSection::OnVolumeAdded)));
31 G_CALLBACK(&DeviceLauncherSection::OnVolumeAdded),31 sig_manager_.Add(new VolumeSignal(monitor_, "volume-removed", sigc::mem_fun(this, &DeviceLauncherSection::OnVolumeRemoved)));
32 this);32
3333 typedef glib::Signal<void, GVolumeMonitor*, GMount*> MountSignal;
34 on_volume_removed_handler_id_ = g_signal_connect(monitor_,34 sig_manager_.Add(new MountSignal(monitor_, "mount-added", sigc::mem_fun(this, &DeviceLauncherSection::OnMountAdded)));
35 "volume-removed",35 sig_manager_.Add(new MountSignal(monitor_, "mount-pre-unmount", sigc::mem_fun(this, &DeviceLauncherSection::OnMountPreUnmount)));
36 G_CALLBACK(&DeviceLauncherSection::OnVolumeRemoved),36
37 this);37 on_device_populate_entry_id_ = g_idle_add([] (gpointer data) {
38 38 auto self = static_cast<DeviceLauncherSection*>(data);
39 on_mount_added_handler_id_ = g_signal_connect(monitor_,39 self->PopulateEntries();
40 "mount-added",40 self->on_device_populate_entry_id_ = 0;
41 G_CALLBACK(&DeviceLauncherSection::OnMountAdded),41 return FALSE;
42 this);42 }, this);
43
44 on_mount_pre_unmount_handler_id_ = g_signal_connect(monitor_,
45 "mount-pre-unmount",
46 G_CALLBACK(&DeviceLauncherSection::OnMountPreUnmount),
47 this);
48
49 on_device_populate_entry_id_ = g_idle_add((GSourceFunc)&DeviceLauncherSection::PopulateEntries, this);
50}43}
5144
52DeviceLauncherSection::~DeviceLauncherSection()45DeviceLauncherSection::~DeviceLauncherSection()
53{46{
54 if (on_volume_added_handler_id_)
55 g_signal_handler_disconnect((gpointer) monitor_,
56 on_volume_added_handler_id_);
57
58 if (on_volume_removed_handler_id_)
59 g_signal_handler_disconnect((gpointer) monitor_,
60 on_volume_removed_handler_id_);
61
62 if (on_mount_added_handler_id_)
63 g_signal_handler_disconnect((gpointer) monitor_,
64 on_mount_added_handler_id_);
65
66 if (on_mount_pre_unmount_handler_id_)
67 g_signal_handler_disconnect((gpointer) monitor_,
68 on_mount_pre_unmount_handler_id_);
69
70 if (on_device_populate_entry_id_)47 if (on_device_populate_entry_id_)
71 g_source_remove(on_device_populate_entry_id_);48 g_source_remove(on_device_populate_entry_id_);
72}49}
7350
74bool DeviceLauncherSection::PopulateEntries(DeviceLauncherSection* self)51void DeviceLauncherSection::PopulateEntries()
75{52{
76 GList* volumes = g_volume_monitor_get_volumes(self->monitor_);53 GList* volumes = g_volume_monitor_get_volumes(monitor_);
7754
78 for (GList* v = volumes; v; v = v->next)55 for (GList* v = volumes; v; v = v->next)
79 {56 {
80 glib::Object<GVolume> volume((GVolume* )v->data);57 if (!G_IS_VOLUME(v->data))
58 continue;
59
60 // This will unref the volume, since the list entries needs that.
61 // We'll keep a reference in the icon.
62 glib::Object<GVolume> volume(G_VOLUME(v->data));
81 DeviceLauncherIcon* icon = new DeviceLauncherIcon(volume);63 DeviceLauncherIcon* icon = new DeviceLauncherIcon(volume);
8264
83 self->map_[volume] = icon;65 map_[volume] = icon;
84 self->IconAdded.emit(AbstractLauncherIcon::Ptr(icon));66 IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
85 }67 }
8668
87 g_list_free(volumes);69 g_list_free(volumes);
88
89 self->on_device_populate_entry_id_ = 0;
90
91 return false;
92}70}
9371
94/* Uses a std::map to track all the volume icons shown and not shown.72/* Uses a std::map to track all the volume icons shown and not shown.
95 * Keep in mind: when "volume-removed" is recevied we should erase73 * Keep in mind: when "volume-removed" is recevied we should erase
96 * the pair (GVolume - DeviceLauncherIcon) from the std::map to avoid leaks74 * the pair (GVolume - DeviceLauncherIcon) from the std::map to avoid leaks
97 */75 */
98void DeviceLauncherSection::OnVolumeAdded(GVolumeMonitor* monitor,76void DeviceLauncherSection::OnVolumeAdded(GVolumeMonitor* monitor, GVolume* volume)
99 GVolume* volume,
100 DeviceLauncherSection* self)
101{77{
102 DeviceLauncherIcon* icon = new DeviceLauncherIcon(volume);78 // This just wraps the volume in a glib::Object, global ref_count is only
103 79 // temporary changed.
104 self->map_[volume] = icon;80 glib::Object<GVolume> gvolume(volume, glib::AddRef());
105 self->IconAdded.emit(AbstractLauncherIcon::Ptr(icon));81 DeviceLauncherIcon* icon = new DeviceLauncherIcon(gvolume);
82
83 map_[gvolume] = icon;
84 IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
106}85}
10786
108void DeviceLauncherSection::OnVolumeRemoved(GVolumeMonitor* monitor,87void DeviceLauncherSection::OnVolumeRemoved(GVolumeMonitor* monitor, GVolume* volume)
109 GVolume* volume,
110 DeviceLauncherSection* self)
111{88{
89 auto volume_it = map_.find(volume);
90
112 // It should not happen! Let me do the check anyway.91 // It should not happen! Let me do the check anyway.
113 if (self->map_.find(volume) != self->map_.end())92 if (volume_it != map_.end())
114 { 93 {
115 self->map_[volume]->OnRemoved();94 volume_it->second->OnRemoved();
116 self->map_.erase(volume);95 map_.erase(volume_it);
117 }96 }
118}97}
11998
120/* Keep in mind: we could have a GMount without a related GVolume99/* Keep in mind: we could have a GMount without a related GVolume
121 * so check everything to avoid unwanted behaviors.100 * so check everything to avoid unwanted behaviors.
122 */101 */
123void DeviceLauncherSection::OnMountAdded(GVolumeMonitor* monitor,102void DeviceLauncherSection::OnMountAdded(GVolumeMonitor* monitor, GMount* mount)
124 GMount* mount,
125 DeviceLauncherSection* self)
126{103{
127 std::map<GVolume* , DeviceLauncherIcon* >::iterator it;
128 glib::Object<GVolume> volume(g_mount_get_volume(mount));104 glib::Object<GVolume> volume(g_mount_get_volume(mount));
129105
130 it = self->map_.find(volume);106 auto volume_it = map_.find(volume);
131107
132 if (it != self->map_.end())108 if (volume_it != map_.end())
133 it->second->UpdateVisibility(1);109 volume_it->second->UpdateVisibility(1);
134}110}
135111
136/* We don't use "mount-removed" signal since it is received after "volume-removed"112/* We don't use "mount-removed" signal since it is received after "volume-removed"
137 * signal. You should read also the comment above.113 * signal. You should read also the comment above.
138 */114 */
139void DeviceLauncherSection::OnMountPreUnmount(GVolumeMonitor* monitor,115void DeviceLauncherSection::OnMountPreUnmount(GVolumeMonitor* monitor, GMount* mount)
140 GMount* mount,
141 DeviceLauncherSection* self)
142{116{
143 std::map<GVolume* , DeviceLauncherIcon* >::iterator it;
144 glib::Object<GVolume> volume(g_mount_get_volume(mount));117 glib::Object<GVolume> volume(g_mount_get_volume(mount));
145118
146 it = self->map_.find(volume);119 auto volume_it = map_.find(volume);
147120
148 if (it != self->map_.end())121 if (volume_it != map_.end())
149 it->second->UpdateVisibility(0);122 volume_it->second->UpdateVisibility(0);
150}123}
151124
152} // namespace launcher125} // namespace launcher
153126
=== modified file 'plugins/unityshell/src/DeviceLauncherSection.h'
--- plugins/unityshell/src/DeviceLauncherSection.h 2012-03-14 06:24:18 +0000
+++ plugins/unityshell/src/DeviceLauncherSection.h 2012-04-24 19:07:27 +0000
@@ -25,6 +25,7 @@
25#include <sigc++/sigc++.h>25#include <sigc++/sigc++.h>
26#include <sigc++/signal.h>26#include <sigc++/signal.h>
27#include <UnityCore/GLibWrapper.h>27#include <UnityCore/GLibWrapper.h>
28#include <UnityCore/GLibSignal.h>
2829
29#include "DeviceLauncherIcon.h"30#include "DeviceLauncherIcon.h"
3031
@@ -45,31 +46,17 @@
45 sigc::signal<void, AbstractLauncherIcon::Ptr> IconAdded;46 sigc::signal<void, AbstractLauncherIcon::Ptr> IconAdded;
4647
47private:48private:
48 static bool PopulateEntries(DeviceLauncherSection* self);49 void PopulateEntries();
49 50
50 static void OnVolumeAdded(GVolumeMonitor* monitor,51 void OnVolumeAdded(GVolumeMonitor* monitor, GVolume* volume);
51 GVolume* volume,52 void OnVolumeRemoved(GVolumeMonitor* monitor, GVolume* volume);
52 DeviceLauncherSection* self);53 void OnMountAdded(GVolumeMonitor* monitor, GMount* mount);
5354 void OnMountPreUnmount(GVolumeMonitor* monitor, GMount* mount);
54 static void OnVolumeRemoved(GVolumeMonitor* monitor,
55 GVolume* volume,
56 DeviceLauncherSection* self);
57
58 static void OnMountAdded(GVolumeMonitor* monitor,
59 GMount* mount,
60 DeviceLauncherSection* self);
61
62 static void OnMountPreUnmount(GVolumeMonitor* monitor,
63 GMount* mount,
64 DeviceLauncherSection* self);
6555
66private:56private:
57 glib::SignalManager sig_manager_;
67 glib::Object<GVolumeMonitor> monitor_;58 glib::Object<GVolumeMonitor> monitor_;
68 std::map<GVolume*, DeviceLauncherIcon*> map_;59 std::map<GVolume*, DeviceLauncherIcon*> map_;
69 gulong on_volume_added_handler_id_;
70 gulong on_volume_removed_handler_id_;
71 gulong on_mount_added_handler_id_;
72 gulong on_mount_pre_unmount_handler_id_;
73 gulong on_device_populate_entry_id_;60 gulong on_device_populate_entry_id_;
74};61};
7562