~canonical-dx-team/unity/unity.fix-ql-losing-focus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2010 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Jason Smith <jason.smith@canonical.com>
 */

#include "TrashLauncherIcon.h"
#include "Launcher.h"
#include "Nux/WindowCompositor.h"

#include "QuicklistManager.h"
#include "QuicklistMenuItemLabel.h"

#include <gio/gio.h>
#include <glib/gi18n-lib.h>

TrashLauncherIcon::TrashLauncherIcon (Launcher* IconManager)
:   SimpleLauncherIcon(IconManager)
{
  SetTooltipText (_("Trash"));
  SetIconName ("user-trash");
  SetQuirk (QUIRK_VISIBLE, true);
  SetQuirk (QUIRK_RUNNING, false);
  SetIconType (TYPE_TRASH);
  SetShortcut ('t');

  m_TrashMonitor = g_file_monitor_directory (g_file_new_for_uri("trash:///"),
					     G_FILE_MONITOR_NONE,
					     NULL,
					     NULL);

  g_signal_connect(m_TrashMonitor,
                   "changed",
                   G_CALLBACK (&TrashLauncherIcon::OnTrashChanged),
                   this);

  UpdateTrashIcon ();
}

TrashLauncherIcon::~TrashLauncherIcon()
{
  g_object_unref (m_TrashMonitor);
}

nux::Color 
TrashLauncherIcon::BackgroundColor ()
{
  return nux::Color (0xFF333333);
}

nux::Color 
TrashLauncherIcon::GlowColor ()
{
  return nux::Color (0xFF333333);
}

void
TrashLauncherIcon::EnsureMenuItemsReady ()
{
  DbusmenuMenuitem *menu_item;

  /* Empty Trash */
  if (_menu_items.find ("Empty") == _menu_items.end ())
  {
    menu_item = dbusmenu_menuitem_new ();
    g_object_ref (menu_item);

    dbusmenu_menuitem_property_set (menu_item, DBUSMENU_MENUITEM_PROP_LABEL, _("Empty Trash"));
    dbusmenu_menuitem_property_set_bool (menu_item, DBUSMENU_MENUITEM_PROP_ENABLED, true);
    dbusmenu_menuitem_property_set_bool (menu_item, DBUSMENU_MENUITEM_PROP_VISIBLE, true);

    g_signal_connect (menu_item,
                      DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, 
                      (GCallback)&TrashLauncherIcon::OnEmptyTrash, this);

    _menu_items["Empty"] = menu_item;
  }
}

void
TrashLauncherIcon::OnMouseClick (int button)
{
  SimpleLauncherIcon::OnMouseClick (button);

  if (button == 1)
    ActivateLauncherIcon ();

  else if (button == 3 && _empty == FALSE)
  {
    EnsureMenuItemsReady ();

    _quicklist->RemoveAllMenuItem ();
    QuicklistMenuItemLabel* item = new QuicklistMenuItemLabel (_menu_items["Empty"], NUX_TRACKER_LOCATION);
    _quicklist->AddMenuItem (item);
    
    int tip_x = _launcher->GetBaseWidth () + 1; //icon_x + icon_w;
    nux::Point3 center = GetCenter ();
    int tip_y = center.y+ _launcher->GetParent ()->GetGeometry ().y;
    QuicklistManager::Default ()->ShowQuicklist (_quicklist, tip_x, tip_y);
    nux::GetWindowCompositor ().SetAlwaysOnFrontWindow (_quicklist);
  }
}

void
TrashLauncherIcon::ActivateLauncherIcon ()
{
  GError *error = NULL;

  g_spawn_command_line_async ("xdg-open trash://", &error);

  if (error)
    g_error_free (error);
}

void 
TrashLauncherIcon::OnEmptyTrash(DbusmenuMenuitem *item, int time, TrashLauncherIcon *self)
{
  GtkWidget *dialog;
  dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
                                   GTK_MESSAGE_WARNING,
                                   GTK_BUTTONS_CANCEL,
                                   NULL);

  g_object_set (GTK_DIALOG (dialog),
		"text", _("Empty all items from Trash?"),
		"secondary-text", _("All items in the Trash will be permanently deleted."),
		NULL);
  gtk_dialog_add_button (GTK_DIALOG (dialog), _("Empty Trash"), GTK_RESPONSE_OK );

  QuicklistManager::Default ()->HideQuicklist (self->_quicklist);

  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) 
    g_thread_create ((GThreadFunc)&TrashLauncherIcon::EmptyTrashAction, NULL, FALSE, NULL);

  gtk_widget_destroy (dialog);

}

void
TrashLauncherIcon::EmptyTrashAction()
{
  // This function runs in a different thread
  // created in TrashLauncherIcon::OnEmptyTrash
  GFile *location;
  location = g_file_new_for_uri ("trash:///");
  
  RecursiveDelete (location);

  g_object_unref (location);

}
void
TrashLauncherIcon::RecursiveDelete(GFile *location)
{

  GFileInfo *info;
  GFile *child;
  GFileEnumerator *enumerator;

  enumerator = g_file_enumerate_children (location,
                                          G_FILE_ATTRIBUTE_STANDARD_NAME "," 
                                          G_FILE_ATTRIBUTE_STANDARD_TYPE,
					                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
					                                NULL,
					                                NULL);
  if (enumerator)
  {
    while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
    {
      child = g_file_get_child (location, g_file_info_get_name (info));
      if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) RecursiveDelete (child);

      g_file_delete (child, NULL, NULL);
      g_object_unref (child);
      g_object_unref (info);
       
    }

    g_file_enumerator_close (enumerator, NULL, NULL);
    g_object_unref (enumerator);
  }

}

void
TrashLauncherIcon::UpdateTrashIcon ()
{
  GFile *location;
  location = g_file_new_for_uri ("trash:///");

  g_file_query_info_async (location,
                           G_FILE_ATTRIBUTE_STANDARD_ICON, 
                           G_FILE_QUERY_INFO_NONE, 
                           0, 
                           NULL, 
                           &TrashLauncherIcon::UpdateTrashIconCb, 
                           this);

  g_object_unref (location);
}

void
TrashLauncherIcon::UpdateTrashIconCb (GObject      *source,
                                      GAsyncResult *res,
                                      gpointer      data)
{
  TrashLauncherIcon *self = (TrashLauncherIcon*) data;
  GFileInfo *info;
  GIcon *icon;
  gchar *icon_name;

  info = g_file_query_info_finish (G_FILE (source), res, NULL);

  if (info != NULL) {
    icon = g_file_info_get_icon (info);
    icon_name = g_icon_to_string (icon);
    self->SetIconName (icon_name);

    if (g_strcmp0 (icon_name, "user-trash") == 0) self->_empty = TRUE;
    else self->_empty = FALSE;

    g_object_unref(info);
  }
}

void
TrashLauncherIcon::OnTrashChanged (GFileMonitor        *monitor,
                                   GFile               *file,
                                   GFile               *other_file,
                                   GFileMonitorEvent    event_type,
                                   gpointer             data)
{
    TrashLauncherIcon *self = (TrashLauncherIcon*) data;
    self->UpdateTrashIcon ();
}

nux::DndAction 
TrashLauncherIcon::OnQueryAcceptDrop (std::list<char *> uris)
{
  return nux::DNDACTION_MOVE;
}
  
void 
TrashLauncherIcon::OnAcceptDrop (std::list<char *> uris)
{
  std::list<char *>::iterator it;
  
  for (it = uris.begin (); it != uris.end (); it++)
  {
    GFile *file = g_file_new_for_uri (*it);
    g_file_trash (file, NULL, NULL);
    g_object_unref (file);
  }
}