Back to index

unity  6.0.0
DeviceLauncherSection.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00017  */
00018 
00019 #include "DeviceLauncherSection.h"
00020 
00021 namespace unity
00022 {
00023 namespace launcher
00024 {
00025 
00026 DeviceLauncherSection::DeviceLauncherSection()
00027   : monitor_(g_volume_monitor_get())
00028 {
00029   typedef glib::Signal<void, GVolumeMonitor*, GVolume*> VolumeSignal;
00030   sig_manager_.Add(new VolumeSignal(monitor_, "volume-added", sigc::mem_fun(this, &DeviceLauncherSection::OnVolumeAdded)));
00031   sig_manager_.Add(new VolumeSignal(monitor_, "volume-removed", sigc::mem_fun(this, &DeviceLauncherSection::OnVolumeRemoved)));
00032 
00033   device_populate_idle_.Run([&] () {
00034     PopulateEntries();
00035     return false;
00036   });
00037 }
00038 
00039 void DeviceLauncherSection::PopulateEntries()
00040 {
00041   GList* volumes = g_volume_monitor_get_volumes(monitor_);
00042 
00043   for (GList* v = volumes; v; v = v->next)
00044   {
00045     if (!G_IS_VOLUME(v->data))
00046       continue;
00047 
00048     // This will unref the volume, since the list entries needs that.
00049     // We'll keep a reference in the icon.
00050     glib::Object<GVolume> volume(G_VOLUME(v->data));
00051     DeviceLauncherIcon* icon = new DeviceLauncherIcon(volume);
00052 
00053     map_[volume] = icon;
00054     IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
00055   }
00056 
00057   g_list_free(volumes);
00058 }
00059 
00060 /* Uses a std::map to track all the volume icons shown and not shown.
00061  * Keep in mind: when "volume-removed" is recevied we should erase
00062  * the pair (GVolume - DeviceLauncherIcon) from the std::map to avoid leaks
00063  */
00064 void DeviceLauncherSection::OnVolumeAdded(GVolumeMonitor* monitor, GVolume* volume)
00065 {
00066   // This just wraps the volume in a glib::Object, global ref_count is only
00067   // temporary changed.
00068   glib::Object<GVolume> gvolume(volume, glib::AddRef());
00069   DeviceLauncherIcon* icon = new DeviceLauncherIcon(gvolume);
00070 
00071   map_[gvolume] = icon;
00072   IconAdded.emit(AbstractLauncherIcon::Ptr(icon));
00073 }
00074 
00075 void DeviceLauncherSection::OnVolumeRemoved(GVolumeMonitor* monitor, GVolume* volume)
00076 {
00077   auto volume_it = map_.find(volume);
00078 
00079   // It should not happen! Let me do the check anyway.
00080   if (volume_it != map_.end())
00081   {
00082     volume_it->second->OnRemoved();
00083     map_.erase(volume_it);
00084   }
00085 }
00086 
00087 } // namespace launcher
00088 } // namespace unity