~3v1n0/unity/scale-window-cast-protection

702.3.1 by Michael Terry
add modelines and fix up some inconsistent tabbings
1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
624.1.1 by Jason Smith
Implement trash can
2
/*
4036.11.12 by Marco Trevisan (Treviño)
TrashLauncherIcon: extend WindowedLauncherIcon and add support to manage windows
3
 * Copyright (C) 2010-2015 Canonical Ltd
624.1.1 by Jason Smith
Implement trash can
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 3 as
7
 * published by the Free Software Foundation.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * Authored by: Jason Smith <jason.smith@canonical.com>
1396.1.1 by Andrea Azzarone
Uses the nautilus3 dbus interface to fix bug #808162.
18
 *              Andrea Azzarone <azzaronea@gmail.com>
3212.2.2 by Marco Trevisan (Treviño)
TestTrashLauncherIcon: add activation test
19
 *              Marco Trevisan <marco.trevisan@canonical.com>
624.1.1 by Jason Smith
Implement trash can
20
 */
21
22
#include "TrashLauncherIcon.h"
1346.5.1 by Andrea Azzarone
Uses DNDCollectionWindow to read drag information before mouse enters launcher x window.
23
2861.2.2 by Jussi Pakkanen
Obtain GETTEXT_PACKAGE from config.h on demand rather than specify it with -D to every compiled file.
24
#include "config.h"
1396.1.1 by Andrea Azzarone
Uses the nautilus3 dbus interface to fix bug #808162.
25
#include <glib/gi18n-lib.h>
2207.1.1 by Thomi Richards
The TrashLauncherIcon would cause glib to assert if the user is running a filesystem (such as gvfs) that does not support the Trash:/// uri. This commit makes it log an error message instead. Also fixed whitespace issues.
26
#include <NuxCore/Logger.h>
4036.11.25 by Marco Trevisan (Treviño)
TrashLauncherIcon: Also get windows for trash path
27
#include <UnityCore/DesktopUtilities.h>
3626.1.1 by Chris Townsend
Removed the ZeitgeistUtils.h as this is no longer needed since Zeitgeist in main is now fixed.
28
#include <zeitgeist.h>
1346.5.1 by Andrea Azzarone
Uses DNDCollectionWindow to read drag information before mouse enters launcher x window.
29
798.3.1 by Stefano Candori
Added quicklist for trashbin
30
#include "QuicklistMenuItemLabel.h"
3477.5.39 by Marco Trevisan (Treviño)
TrashLauncherIcon: use AppManager's UnityApplication for logging removal events
31
#include "unity-shared/DesktopApplicationManager.h"
32
#include "unity-shared/GnomeFileManager.h"
624.1.1 by Jason Smith
Implement trash can
33
1635.1.31 by Tim Penhey
A few tweaks to get it to compile.
34
namespace unity
35
{
36
namespace launcher
37
{
2207.1.1 by Thomi Richards
The TrashLauncherIcon would cause glib to assert if the user is running a filesystem (such as gvfs) that does not support the Trash:/// uri. This commit makes it log an error message instead. Also fixed whitespace issues.
38
namespace
39
{
3477.5.24 by Marco Trevisan (Treviño)
Merging with trunk
40
  DECLARE_LOGGER(logger, "unity.launcher.icon.trash");
2655.7.6 by Pawel Stolowski
Moved ZEITGEIST_UNITY_ACTOR to unnamed namspace; formatting fixes.
41
  const std::string ZEITGEIST_UNITY_ACTOR = "application://compiz.desktop";
3212.3.1 by Marco Trevisan (Treviño)
TrashLauncherIcon: activate the org.gtk.Application when calling the "EmptyTrash" method
42
  const std::string TRASH_URI = "trash:";
4036.11.25 by Marco Trevisan (Treviño)
TrashLauncherIcon: Also get windows for trash path
43
  const std::string TRASH_PATH = "file://" + DesktopUtilities::GetUserTrashDirectory();
2207.1.1 by Thomi Richards
The TrashLauncherIcon would cause glib to assert if the user is running a filesystem (such as gvfs) that does not support the Trash:/// uri. This commit makes it log an error message instead. Also fixed whitespace issues.
44
}
675 by Alex Launi
Bring back set trash icon depending on whether there are files in the trash or not
45
4036.11.14 by Marco Trevisan (Treviño)
StorageLauncherIcon: add new base-class for storage icons and use it for Trash
46
TrashLauncherIcon::TrashLauncherIcon(FileManager::Ptr const& fm)
4036.11.47 by Marco Trevisan (Treviño)
FileManagerLauncherIcon: fix diamond-problem by using virtual inheritance of WindowedLauncherIcon
47
  : WindowedLauncherIcon(IconType::TRASH)
48
  , StorageLauncherIcon(GetIconType(), fm ? fm : GnomeFileManager::Get())
3427.1.17 by Marco Trevisan (Treviño)
TrashLauncherIcon: initialize empty_ boolean value
49
  , empty_(true)
624.1.1 by Jason Smith
Implement trash can
50
{
1233.2.27 by Jason Smith
migrate tooltip to using a nux::Property
51
  tooltip_text = _("Trash");
1738.11.2 by Andrea Azzarone
Makes icon_name a SimpleLauncherIcon property.
52
  icon_name = "user-trash";
2529.14.2 by Marco Trevisan (Treviño)
LauncherIcon: set the default icon position to FLOATING; HUD, BFB to BEGIN; Trash to END
53
  position = Position::END;
2529.6.5 by Marco Trevisan (Treviño)
AbstractLauncherIcon: use enum class for Quirk and IconType
54
  SetQuirk(Quirk::VISIBLE, true);
3566.4.10 by Marco Trevisan (Treviño)
LauncherIcon: use nux::Animation for each Quirk property
55
  SkipQuirkAnimation(Quirk::VISIBLE);
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
56
  SetShortcut('t');
1882.2.14 by Thomi Richards
Refactor of launcher icon classes.
57
4146.1.2 by Andrea Azzarone
Use _source_manager from LauncherIcon.
58
  _source_manager.AddIdle([this]{
4146.1.1 by Andrea Azzarone
Create GFileMonitor in an idle to avoid blocking calls.
59
    glib::Object<GFile> location(g_file_new_for_uri(TRASH_URI.c_str()));
60
61
    glib::Error err;
62
    trash_monitor_ = g_file_monitor_directory(location, G_FILE_MONITOR_NONE, cancellable_, &err);
63
    g_file_monitor_set_rate_limit(trash_monitor_, 1000);
64
65
    if (err)
66
    {
67
      LOG_ERROR(logger) << "Could not create file monitor for trash uri: " << err;
68
    }
69
    else
70
    {
71
      glib_signals_.Add<void, GFileMonitor*, GFile*, GFile*, GFileMonitorEvent>(trash_monitor_, "changed",
72
      [this] (GFileMonitor*, GFile*, GFile*, GFileMonitorEvent) {
73
        UpdateTrashIcon();
74
      });
75
    }
76
77
    return false;
78
  });
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
79
80
  UpdateTrashIcon();
4036.11.29 by Marco Trevisan (Treviño)
TrashLauncherIcon: call UpdateStorageWindows (instead of EnsureWindowState) on init
81
  UpdateStorageWindows();
4036.11.12 by Marco Trevisan (Treviño)
TrashLauncherIcon: extend WindowedLauncherIcon and add support to manage windows
82
}
83
4036.11.33 by Marco Trevisan (Treviño)
StorageLauncherIcon: reduce the calls to FM's WindowsForLocation caching results
84
WindowList TrashLauncherIcon::GetStorageWindows() const
4036.11.12 by Marco Trevisan (Treviño)
TrashLauncherIcon: extend WindowedLauncherIcon and add support to manage windows
85
{
4036.11.25 by Marco Trevisan (Treviño)
TrashLauncherIcon: Also get windows for trash path
86
  auto windows = file_manager_->WindowsForLocation(TRASH_URI);
87
  auto const& path_wins = file_manager_->WindowsForLocation(TRASH_PATH);
88
  windows.insert(end(windows), begin(path_wins), end(path_wins));
89
  return windows;
624.1.1 by Jason Smith
Implement trash can
90
}
91
4036.11.12 by Marco Trevisan (Treviño)
TrashLauncherIcon: extend WindowedLauncherIcon and add support to manage windows
92
void TrashLauncherIcon::OpenInstanceLauncherIcon(Time timestamp)
93
{
94
  file_manager_->OpenTrash(timestamp);
3270.3.3 by Marco Trevisan (Treviño)
TrashLauncherIcon: disconnect from FileManager signal on destruction
95
}
96
2547.2.14 by Marco Trevisan (Treviño)
sed s/MenuItemsList/MenuItemsVector/g
97
AbstractLauncherIcon::MenuItemsVector TrashLauncherIcon::GetMenus()
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
98
{
2547.2.14 by Marco Trevisan (Treviño)
sed s/MenuItemsList/MenuItemsVector/g
99
  MenuItemsVector result;
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
100
798.3.1 by Stefano Candori
Added quicklist for trashbin
101
  /* Empty Trash */
2547.2.8 by Marco Trevisan (Treviño)
LauncherIcon: Use a vector of glib::Object<DbusmenuMenuitem> to get the Menus
102
  glib::Object<DbusmenuMenuitem> menu_item(dbusmenu_menuitem_new());
3389.1.1 by Sebastien Bacher
use correct ellipses
103
  dbusmenu_menuitem_property_set(menu_item, DBUSMENU_MENUITEM_PROP_LABEL, _("Empty Trash…"));
1396.1.1 by Andrea Azzarone
Uses the nautilus3 dbus interface to fix bug #808162.
104
  dbusmenu_menuitem_property_set_bool(menu_item, DBUSMENU_MENUITEM_PROP_ENABLED, !empty_);
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
105
  dbusmenu_menuitem_property_set_bool(menu_item, DBUSMENU_MENUITEM_PROP_VISIBLE, true);
4036.11.54 by Marco Trevisan (Treviño)
TrashLauncherIcon: use WindowedLauncherIcon shared glib::SignalManager
106
  glib_signals_.Add<void, DbusmenuMenuitem*, unsigned>(menu_item, DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED,
3212.3.1 by Marco Trevisan (Treviño)
TrashLauncherIcon: activate the org.gtk.Application when calling the "EmptyTrash" method
107
  [this] (DbusmenuMenuitem*, unsigned timestamp) {
3212.3.8 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManager implementation for EmptyTrash
108
    file_manager_->EmptyTrash(timestamp);
2547.2.10 by Marco Trevisan (Treviño)
TrashLauncherIcon: use glib::Signal for entry-activated action
109
  });
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
110
1029.1.2 by Didier Roche
use regular and not tweaked quicklist system for trashlaunchericon (LP: #741793)
111
  result.push_back(menu_item);
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
112
4036.11.60 by Marco Trevisan (Treviño)
TrashLauncherIcon: add Quit and Windows quicklists
113
  if (IsRunning())
114
  {
115
    auto const& windows_items = GetWindowsMenuItems();
116
    if (!windows_items.empty())
117
    {
118
      menu_item = dbusmenu_menuitem_new();
119
      dbusmenu_menuitem_property_set(menu_item, DBUSMENU_MENUITEM_PROP_TYPE, DBUSMENU_CLIENT_TYPES_SEPARATOR);
120
      result.push_back(menu_item);
121
122
      result.insert(end(result), begin(windows_items), end(windows_items));
123
    }
124
125
    menu_item = dbusmenu_menuitem_new();
126
    dbusmenu_menuitem_property_set(menu_item, DBUSMENU_MENUITEM_PROP_TYPE, DBUSMENU_CLIENT_TYPES_SEPARATOR);
127
    result.push_back(menu_item);
128
129
    menu_item = dbusmenu_menuitem_new();
130
    dbusmenu_menuitem_property_set(menu_item, DBUSMENU_MENUITEM_PROP_LABEL, _("Quit"));
131
    dbusmenu_menuitem_property_set_bool(menu_item, DBUSMENU_MENUITEM_PROP_ENABLED, true);
132
    dbusmenu_menuitem_property_set_bool(menu_item, DBUSMENU_MENUITEM_PROP_VISIBLE, true);
133
    result.push_back(menu_item);
134
135
    glib_signals_.Add<void, DbusmenuMenuitem*, unsigned>(menu_item, DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, [this] (DbusmenuMenuitem*, int) {
136
      Quit();
137
    });
138
  }
139
1029.1.2 by Didier Roche
use regular and not tweaked quicklist system for trashlaunchericon (LP: #741793)
140
  return result;
798.3.1 by Stefano Candori
Added quicklist for trashbin
141
}
142
1396.1.1 by Andrea Azzarone
Uses the nautilus3 dbus interface to fix bug #808162.
143
void TrashLauncherIcon::UpdateTrashIcon()
144
{
3212.2.1 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManagerOpener to open it with correct event timestamp
145
  glib::Object<GFile> location(g_file_new_for_uri(TRASH_URI.c_str()));
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
146
147
  g_file_query_info_async(location,
148
                          G_FILE_ATTRIBUTE_STANDARD_ICON,
149
                          G_FILE_QUERY_INFO_NONE,
150
                          0,
2541.1.1 by Andrea Azzarone
Cherrypick rev 2539 from lp:~3v1n0/unity/launcher-icon-construction-type. Thank you Marco.
151
                          cancellable_,
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
152
                          &TrashLauncherIcon::UpdateTrashIconCb,
153
                          this);
675 by Alex Launi
Bring back set trash icon depending on whether there are files in the trash or not
154
}
155
3212.2.1 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManagerOpener to open it with correct event timestamp
156
void TrashLauncherIcon::UpdateTrashIconCb(GObject* source, GAsyncResult* res, gpointer data)
675 by Alex Launi
Bring back set trash icon depending on whether there are files in the trash or not
157
{
3212.2.1 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManagerOpener to open it with correct event timestamp
158
  auto self = static_cast<TrashLauncherIcon*>(data);
675 by Alex Launi
Bring back set trash icon depending on whether there are files in the trash or not
159
1143.2.1 by Didier Roche
make the trash look good: only one instance, handled in a more async way (LP: #761643)
160
  // FIXME: should use the generic LoadIcon function (not taking from the unity theme)
3212.2.1 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManagerOpener to open it with correct event timestamp
161
  glib::Object<GFileInfo> info(g_file_query_info_finish(G_FILE(source), res, nullptr));
1396.1.1 by Andrea Azzarone
Uses the nautilus3 dbus interface to fix bug #808162.
162
163
  if (info)
1882.2.14 by Thomi Richards
Refactor of launcher icon classes.
164
  {
2818.1.1 by Michal Hruby
Fix refcounting bug in TrashLauncherIcon
165
    glib::Object<GIcon> icon(g_file_info_get_icon(info), glib::AddRef());
1738.11.2 by Andrea Azzarone
Makes icon_name a SimpleLauncherIcon property.
166
    glib::String icon_string(g_icon_to_string(icon));
1882.2.14 by Thomi Richards
Refactor of launcher icon classes.
167
1738.11.2 by Andrea Azzarone
Makes icon_name a SimpleLauncherIcon property.
168
    self->icon_name = icon_string.Str();
1738.11.3 by Andrea Azzarone
Fixes.
169
    self->empty_ = (self->icon_name == "user-trash");
675 by Alex Launi
Bring back set trash icon depending on whether there are files in the trash or not
170
  }
171
}
172
1396.1.2 by Andrea Azzarone
Merge trunk.
173
2332.2.1 by Andrea Azzarone
Fixes bug #880798.
174
nux::DndAction TrashLauncherIcon::OnQueryAcceptDrop(DndData const& dnd_data)
831.3.2 by Jason Smith
implement dragging to trash
175
{
2865.2.13 by Tim Penhey
Use a more sane X11 macro.
176
#ifdef USE_X11
831.3.4 by Jason Smith
allow icons to specify the action they wish to perform
177
  return nux::DNDACTION_MOVE;
2849.2.1 by Tim Penhey
#define out the drag and drop code for no X.
178
#else
179
  return nux::DNDACTION_NONE;
180
#endif
831.3.2 by Jason Smith
implement dragging to trash
181
}
1307 by Neil Jagdish Patel
Update formatting to match style (as close as possible)
182
2332.2.1 by Andrea Azzarone
Fixes bug #880798.
183
bool TrashLauncherIcon::OnShouldHighlightOnDrag(DndData const& dnd_data)
184
{
185
  return true;
186
}
187
188
void TrashLauncherIcon::OnAcceptDrop(DndData const& dnd_data)
831.3.2 by Jason Smith
implement dragging to trash
189
{
3212.2.1 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManagerOpener to open it with correct event timestamp
190
  for (auto const& uri : dnd_data.Uris())
831.3.2 by Jason Smith
implement dragging to trash
191
  {
3477.5.42 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManager to trash files on DnD
192
    if (file_manager_->TrashFile(uri))
2655.7.1 by Pawel Stolowski
Register file delete events in zeitgeist when file is dropped onto Trash launcher icon.
193
    {
3477.5.42 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManager to trash files on DnD
194
      /* Log ZG event when moving file to trash; this is requred by File Scope.
195
       * See https://bugs.launchpad.net/unity/+bug/870150 */
3477.5.39 by Marco Trevisan (Treviño)
TrashLauncherIcon: use AppManager's UnityApplication for logging removal events
196
      auto const& unity_app = ApplicationManager::Default().GetUnityApplication();
197
      auto subject = std::make_shared<desktop::ApplicationSubject>();
198
      subject->uri = uri;
199
      subject->origin = glib::String(g_path_get_dirname(uri.c_str())).Str();
3477.5.42 by Marco Trevisan (Treviño)
TrashLauncherIcon: use FileManager to trash files on DnD
200
      glib::Object<GFile> file(g_file_new_for_uri(uri.c_str()));
2655.7.6 by Pawel Stolowski
Moved ZEITGEIST_UNITY_ACTOR to unnamed namspace; formatting fixes.
201
      glib::String parse_name(g_file_get_parse_name(file));
3477.5.39 by Marco Trevisan (Treviño)
TrashLauncherIcon: use AppManager's UnityApplication for logging removal events
202
      subject->text = glib::String(g_path_get_basename(parse_name)).Str();
203
      unity_app->LogEvent(ApplicationEventType::DELETE, subject);
2655.7.1 by Pawel Stolowski
Register file delete events in zeitgeist when file is dropped onto Trash launcher icon.
204
    }
831.3.2 by Jason Smith
implement dragging to trash
205
  }
1882.2.14 by Thomi Richards
Refactor of launcher icon classes.
206
2529.6.5 by Marco Trevisan (Treviño)
AbstractLauncherIcon: use enum class for Quirk and IconType
207
  SetQuirk(LauncherIcon::Quirk::PULSE_ONCE, true);
3785.5.9 by Marco Trevisan (Treviño)
{Trash,Volume}LauncherIcon: also shimmer on drop-accept
208
  FullyAnimateQuirkDelayed(100, LauncherIcon::Quirk::SHIMMER);
831.3.2 by Jason Smith
implement dragging to trash
209
}
1396.1.1 by Andrea Azzarone
Uses the nautilus3 dbus interface to fix bug #808162.
210
1882.2.14 by Thomi Richards
Refactor of launcher icon classes.
211
std::string TrashLauncherIcon::GetName() const
212
{
213
  return "TrashLauncherIcon";
214
}
215
1635.1.31 by Tim Penhey
A few tweaks to get it to compile.
216
} // namespace launcher
1396.1.1 by Andrea Azzarone
Uses the nautilus3 dbus interface to fix bug #808162.
217
} // namespace unity