Back to index

unity  6.0.0
TrashLauncherIcon.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2010 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Jason Smith <jason.smith@canonical.com>
00018  *              Andrea Azzarone <azzaronea@gmail.com>
00019  */
00020 
00021 #include "TrashLauncherIcon.h"
00022 
00023 #include <glib/gi18n-lib.h>
00024 #include <Nux/WindowCompositor.h>
00025 #include <NuxCore/Logger.h>
00026 
00027 #include "Launcher.h"
00028 #include "QuicklistManager.h"
00029 #include "QuicklistMenuItemLabel.h"
00030 
00031 namespace unity
00032 {
00033 namespace launcher
00034 {
00035 namespace
00036 {
00037   nux::logging::Logger logger("unity.launcher.TrashLauncherIcon");
00038 }
00039 
00040 TrashLauncherIcon::TrashLauncherIcon()
00041   : SimpleLauncherIcon()
00042   , on_trash_changed_handler_id_(0)
00043   , proxy_("org.gnome.Nautilus", "/org/gnome/Nautilus", "org.gnome.Nautilus.FileOperations")
00044 {
00045   tooltip_text = _("Trash");
00046   icon_name = "user-trash";
00047   SetQuirk(QUIRK_VISIBLE, true);
00048   SetQuirk(QUIRK_RUNNING, false);
00049   SetIconType(TYPE_TRASH);
00050   SetShortcut('t');
00051 
00052   glib::Object<GFile> location(g_file_new_for_uri("trash:///"));
00053 
00054   glib::Error err;
00055   trash_monitor_ = g_file_monitor_directory(location,
00056                                             G_FILE_MONITOR_NONE,
00057                                             NULL,
00058                                             &err);
00059 
00060   if (err)
00061   {
00062     LOG_ERROR(logger) << "Could not create file monitor for trash uri: " << err;
00063   }
00064   else
00065   {
00066     on_trash_changed_handler_id_ = g_signal_connect(trash_monitor_,
00067                                                   "changed",
00068                                                   G_CALLBACK(&TrashLauncherIcon::OnTrashChanged),
00069                                                   this);
00070   }
00071 
00072   UpdateTrashIcon();
00073 }
00074 
00075 TrashLauncherIcon::~TrashLauncherIcon()
00076 {
00077   if (on_trash_changed_handler_id_ != 0)
00078     g_signal_handler_disconnect((gpointer) trash_monitor_,
00079                                 on_trash_changed_handler_id_);
00080 }
00081 
00082 std::list<DbusmenuMenuitem*> TrashLauncherIcon::GetMenus()
00083 {
00084   std::list<DbusmenuMenuitem*> result;
00085   DbusmenuMenuitem* menu_item;
00086 
00087   /* Empty Trash */
00088   menu_item = dbusmenu_menuitem_new();
00089   g_object_ref(menu_item);
00090 
00091   dbusmenu_menuitem_property_set(menu_item, DBUSMENU_MENUITEM_PROP_LABEL, _("Empty Trash..."));
00092   dbusmenu_menuitem_property_set_bool(menu_item, DBUSMENU_MENUITEM_PROP_ENABLED, !empty_);
00093   dbusmenu_menuitem_property_set_bool(menu_item, DBUSMENU_MENUITEM_PROP_VISIBLE, true);
00094 
00095   g_signal_connect(menu_item,
00096                    DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED,
00097                    (GCallback)&TrashLauncherIcon::OnEmptyTrash, this);
00098   result.push_back(menu_item);
00099 
00100   return result;
00101 }
00102 
00103 void TrashLauncherIcon::ActivateLauncherIcon(ActionArg arg)
00104 {
00105   SimpleLauncherIcon::ActivateLauncherIcon(arg);
00106 
00107   glib::Error error;
00108   g_spawn_command_line_async("xdg-open trash://", &error);
00109 }
00110 
00111 void TrashLauncherIcon::OnEmptyTrash(DbusmenuMenuitem* item, int time, TrashLauncherIcon* self)
00112 {
00113   self->proxy_.Call("EmptyTrash");
00114 }
00115 
00116 void TrashLauncherIcon::UpdateTrashIcon()
00117 {
00118   glib::Object<GFile> location(g_file_new_for_uri("trash:///"));
00119 
00120   g_file_query_info_async(location,
00121                           G_FILE_ATTRIBUTE_STANDARD_ICON,
00122                           G_FILE_QUERY_INFO_NONE,
00123                           0,
00124                           NULL,
00125                           &TrashLauncherIcon::UpdateTrashIconCb,
00126                           this);
00127 
00128 }
00129 
00130 void TrashLauncherIcon::UpdateTrashIconCb(GObject* source,
00131                                           GAsyncResult* res,
00132                                           gpointer data)
00133 {
00134   TrashLauncherIcon* self = (TrashLauncherIcon*) data;
00135 
00136   // FIXME: should use the generic LoadIcon function (not taking from the unity theme)
00137   glib::Object<GFileInfo> info(g_file_query_info_finish(G_FILE(source), res, NULL));
00138 
00139   if (info)
00140   {
00141     glib::Object<GIcon> icon(g_file_info_get_icon(info));
00142     glib::String icon_string(g_icon_to_string(icon));
00143 
00144     self->icon_name = icon_string.Str();
00145 
00146     self->empty_ = (self->icon_name == "user-trash");
00147   }
00148 }
00149 
00150 void TrashLauncherIcon::OnTrashChanged(GFileMonitor* monitor,
00151                                        GFile* file,
00152                                        GFile* other_file,
00153                                        GFileMonitorEvent event_type,
00154                                        gpointer data)
00155 {
00156   TrashLauncherIcon* self = (TrashLauncherIcon*) data;
00157   self->UpdateTrashIcon();
00158 }
00159 
00160 
00161 nux::DndAction TrashLauncherIcon::OnQueryAcceptDrop(DndData const& dnd_data)
00162 {
00163   return nux::DNDACTION_MOVE;
00164 }
00165 
00166 bool TrashLauncherIcon::OnShouldHighlightOnDrag(DndData const& dnd_data)
00167 {
00168   return true;
00169 }
00170 
00171 
00172 void TrashLauncherIcon::OnAcceptDrop(DndData const& dnd_data)
00173 {
00174   for (auto it : dnd_data.Uris())
00175   {
00176     glib::Object<GFile> file(g_file_new_for_uri(it.c_str()));
00177     g_file_trash(file, NULL, NULL);
00178   }
00179 
00180   SetQuirk(LauncherIcon::QUIRK_PULSE_ONCE, true);
00181 }
00182 
00183 std::string TrashLauncherIcon::GetName() const
00184 {
00185   return "TrashLauncherIcon";
00186 }
00187 
00188 } // namespace launcher
00189 } // namespace unity