summaryrefslogtreecommitdiff
path: root/plugins/thunar-tpa/thunar-tpa.c
blob: 184237c510a36bfd9f9b39100ca5bf134b1ed20a (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*-
 * Copyright (c) 2006 Benedikt Meurer <benny@xfce.org>
 * Copyright (c) 2010 Nick Schermer <nick@xfce.org>
 * Copyright (c) 2010 Jannis Pohlmann <jannis@xfce.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * 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, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>

#include <gtk/gtk.h>

#include <libxfce4util/libxfce4util.h>

#include <libxfce4ui/libxfce4ui.h>

#include <libxfce4panel/libxfce4panel.h>
#include <libxfce4panel/xfce-panel-macros.h>

#include <thunar-tpa/thunar-tpa-bindings.h>

typedef struct _ThunarTpaClass ThunarTpaClass;
typedef struct _ThunarTpa      ThunarTpa;



#define THUNAR_TYPE_TPA            (thunar_tpa_get_type ())
#define THUNAR_TPA(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_TYPE_TPA, ThunarTpa))
#define THUNAR_TPA_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_TYPE_TPA, ThunarTpaClass))
#define THUNAR_IS_TPA(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_TYPE_TPA))
#define THUNAR_IS_TPA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_TYPE_TPA))
#define THUNAR_TPA_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_TYPE_TPA, ThunarTpaClass))



GType           thunar_tpa_get_type            (void);
void            thunar_tpa_register_type       (XfcePanelTypeModule *type_module);
static void     thunar_tpa_finalize            (GObject             *object);
static void     thunar_tpa_construct           (XfcePanelPlugin     *panel_plugin);
static gboolean thunar_tpa_size_changed        (XfcePanelPlugin     *panel_plugin,
                                                gint                 size);
static void     thunar_tpa_error               (ThunarTpa           *plugin,
                                                GError              *error);
static void     thunar_tpa_state               (ThunarTpa           *plugin,
                                                gboolean             full);
static void     thunar_tpa_display_trash_reply (DBusGProxy          *proxy,
                                                GError              *error,
                                                gpointer             user_data);
static void     thunar_tpa_empty_trash_reply   (DBusGProxy          *proxy,
                                                GError              *error,
                                                gpointer             user_data);
static void     thunar_tpa_move_to_trash_reply (DBusGProxy          *proxy,
                                                GError              *error,
                                                gpointer             user_data);
static void     thunar_tpa_query_trash_reply   (DBusGProxy          *proxy,
                                                gboolean             full,
                                                GError              *error,
                                                gpointer             user_data);
static void     thunar_tpa_drag_data_received  (GtkWidget           *button,
                                                GdkDragContext      *context,
                                                gint                 x,
                                                gint                 y,
                                                GtkSelectionData    *selection_data,
                                                guint                info,
                                                guint                time,
                                                ThunarTpa           *plugin);
static gboolean thunar_tpa_enter_notify_event  (GtkWidget           *button,
                                                GdkEventCrossing    *event,
                                                ThunarTpa           *plugin);
static gboolean thunar_tpa_leave_notify_event  (GtkWidget           *button,
                                                GdkEventCrossing    *event,
                                                ThunarTpa           *plugin);
static void     thunar_tpa_trash_changed       (DBusGProxy          *proxy,
                                                gboolean             full,
                                                ThunarTpa           *plugin);
static void     thunar_tpa_display_trash       (ThunarTpa           *plugin);
static void     thunar_tpa_empty_trash         (ThunarTpa           *plugin);
static gboolean thunar_tpa_move_to_trash       (ThunarTpa           *plugin,
                                                const gchar        **uri_list);
static void     thunar_tpa_query_trash         (ThunarTpa           *plugin);



struct _ThunarTpaClass
{
  XfcePanelPluginClass __parent__;
};

struct _ThunarTpa
{
  XfcePanelPlugin __parent__;

  /* widgets */
  GtkWidget      *button;
  GtkWidget      *image;
  GtkWidget      *mi;

  DBusGProxy     *proxy;
  DBusGProxyCall *display_trash_call;
  DBusGProxyCall *empty_trash_call;
  DBusGProxyCall *move_to_trash_call;
  DBusGProxyCall *query_trash_call;
};

/* Target types for dropping to the trash can */
enum
{
  TARGET_TEXT_URI_LIST,
};

static const GtkTargetEntry drop_targets[] =
{
  { "text/uri-list", 0, TARGET_TEXT_URI_LIST, },
};



/* define the plugin */
XFCE_PANEL_DEFINE_PLUGIN (ThunarTpa, thunar_tpa)



static void
thunar_tpa_class_init (ThunarTpaClass *klass)
{
  XfcePanelPluginClass *plugin_class;
  GObjectClass         *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = thunar_tpa_finalize;

  plugin_class = XFCE_PANEL_PLUGIN_CLASS (klass);
  plugin_class->construct = thunar_tpa_construct;
  plugin_class->size_changed = thunar_tpa_size_changed;
}



static void
thunar_tpa_init (ThunarTpa *plugin)
{
  DBusGConnection *connection;
  GError          *err = NULL;

  /* setup the button for the trash plugin */
  plugin->button = xfce_create_panel_button ();
  xfce_panel_plugin_add_action_widget (XFCE_PANEL_PLUGIN (plugin), plugin->button);
  gtk_drag_dest_set (plugin->button, GTK_DEST_DEFAULT_ALL, drop_targets, G_N_ELEMENTS (drop_targets), GDK_ACTION_MOVE);
  g_signal_connect_swapped (G_OBJECT (plugin->button), "clicked", G_CALLBACK (thunar_tpa_display_trash), plugin);
  g_signal_connect (G_OBJECT (plugin->button), "drag-data-received", G_CALLBACK (thunar_tpa_drag_data_received), plugin);
  g_signal_connect (G_OBJECT (plugin->button), "enter-notify-event", G_CALLBACK (thunar_tpa_enter_notify_event), plugin);
  g_signal_connect (G_OBJECT (plugin->button), "leave-notify-event", G_CALLBACK (thunar_tpa_leave_notify_event), plugin);
  gtk_container_add (GTK_CONTAINER (plugin), plugin->button);
  gtk_widget_show (plugin->button);

  /* setup the image for the trash plugin */
  plugin->image = gtk_image_new_from_icon_name ("user-trash", GTK_ICON_SIZE_BUTTON);
  gtk_container_add (GTK_CONTAINER (plugin->button), plugin->image);
  gtk_widget_show (plugin->image);

  /* prepare the menu item */
  plugin->mi = gtk_menu_item_new_with_mnemonic (_("_Empty Trash"));
  g_signal_connect_swapped (G_OBJECT (plugin->mi), "activate", G_CALLBACK (thunar_tpa_empty_trash), plugin);
  gtk_widget_show (plugin->mi);

  /* try to connect to the D-BUS session daemon */
  connection = dbus_g_bus_get (DBUS_BUS_SESSION, &err);
  if (G_UNLIKELY (connection == NULL))
    {
      /* we failed to connect, display an error plugin/tooltip */
      thunar_tpa_error (plugin, err);
      g_error_free (err);
    }
  else
    {
      /* grab a proxy for the /org/xfce/FileManager object on org.xfce.FileManager */
      plugin->proxy = dbus_g_proxy_new_for_name (connection, "org.xfce.FileManager", "/org/xfce/FileManager", "org.xfce.Trash");

      /* connect to the "TrashChanged" signal */
      dbus_g_proxy_add_signal (plugin->proxy, "TrashChanged", G_TYPE_BOOLEAN, G_TYPE_INVALID);
      dbus_g_proxy_connect_signal (plugin->proxy, "TrashChanged", G_CALLBACK (thunar_tpa_trash_changed), plugin, NULL);
    }
}



static void
thunar_tpa_finalize (GObject *object)
{
  ThunarTpa *plugin = THUNAR_TPA (object);

  /* release the proxy object */
  if (G_LIKELY (plugin->proxy != NULL))
    {
      /* cancel any pending calls */
      if (G_UNLIKELY (plugin->display_trash_call != NULL))
        dbus_g_proxy_cancel_call (plugin->proxy, plugin->display_trash_call);
      if (G_UNLIKELY (plugin->empty_trash_call != NULL))
        dbus_g_proxy_cancel_call (plugin->proxy, plugin->empty_trash_call);
      if (G_UNLIKELY (plugin->move_to_trash_call != NULL))
        dbus_g_proxy_cancel_call (plugin->proxy, plugin->move_to_trash_call);
      if (G_UNLIKELY (plugin->query_trash_call != NULL))
        dbus_g_proxy_cancel_call (plugin->proxy, plugin->query_trash_call);

      /* disconnect the signal and release the proxy */
      dbus_g_proxy_disconnect_signal (plugin->proxy, "TrashChanged", G_CALLBACK (thunar_tpa_trash_changed), plugin);
      g_object_unref (G_OBJECT (plugin->proxy));
    }

  (*G_OBJECT_CLASS (thunar_tpa_parent_class)->finalize) (object);
}



static void
thunar_tpa_construct (XfcePanelPlugin *panel_plugin)
{
  ThunarTpa *plugin = THUNAR_TPA (panel_plugin);

  /* make the plugin fit a single row */
  xfce_panel_plugin_set_small (panel_plugin, TRUE);

  /* add the "Empty Trash" menu item */
  xfce_panel_plugin_menu_insert_item (panel_plugin, GTK_MENU_ITEM (plugin->mi));

  /* update the state of the trash plugin */
  thunar_tpa_query_trash (plugin);
}



static gboolean
thunar_tpa_size_changed (XfcePanelPlugin *panel_plugin,
                         gint             size)
{
  ThunarTpa *plugin = THUNAR_TPA (panel_plugin);
  gint       image_size;

  /* make the plugin fit a single row */
  size /= xfce_panel_plugin_get_nrows (panel_plugin);
  gtk_widget_set_size_request (GTK_WIDGET (panel_plugin), size, size);

#if LIBXFCE4PANEL_CHECK_VERSION (4,13,0)
  image_size = xfce_panel_plugin_get_icon_size (panel_plugin);
#else
  image_size = size - 2; // fall-back for older panel versions
#endif
  gtk_image_set_pixel_size (GTK_IMAGE (plugin->image), image_size);

  return TRUE;
}



static void
thunar_tpa_error (ThunarTpa *plugin,
                  GError    *error)
{
  gchar *tooltip;

  /* reset to empty first */
  thunar_tpa_state (plugin, FALSE);

  /* strip off additional whitespace */
  g_strstrip (error->message);

  /* tell the user that we failed to connect to the trash */
  tooltip = g_strdup_printf ("%s: %s.", _("Failed to connect to the Trash"), error->message);
  gtk_widget_set_tooltip_text (plugin->button, tooltip);
  g_free (tooltip);

  /* setup an error plugin */
  gtk_image_set_from_icon_name (GTK_IMAGE (plugin->image), "stock_dialog-error", GTK_ICON_SIZE_BUTTON);
}



static void
thunar_tpa_state (ThunarTpa *plugin,
                  gboolean   full)
{
  /* tell the user whether the trash is full or empty */
  gtk_widget_set_tooltip_text (plugin->button, full ? _("Trash contains files") : _("Trash is empty"));

  /* setup the appropriate plugin */
  gtk_image_set_from_icon_name (GTK_IMAGE (plugin->image), full ? "user-trash-full" : "user-trash", GTK_ICON_SIZE_BUTTON);

  /* sensitivity of the menu item */
  gtk_widget_set_sensitive (plugin->mi, full);
}



static void
thunar_tpa_display_trash_reply (DBusGProxy *proxy,
                                GError     *error,
                                gpointer    user_data)
{
  ThunarTpa *plugin = THUNAR_TPA (user_data);

  /* reset the call */
  plugin->display_trash_call = NULL;

  /* check if we failed */
  if (G_UNLIKELY (error != NULL))
    {
      /* display an error message to the user */
      g_strstrip (error->message);
      xfce_dialog_show_error (NULL, error, "%s.", _("Failed to connect to the Trash"));
      g_error_free (error);
    }
}



static void
thunar_tpa_empty_trash_reply (DBusGProxy *proxy,
                              GError     *error,
                              gpointer    user_data)
{
  ThunarTpa *plugin = THUNAR_TPA (user_data);

  /* reset the call */
  plugin->empty_trash_call = NULL;

  /* check if we failed */
  if (G_UNLIKELY (error != NULL))
    {
      /* display an error message to the user */
      g_strstrip (error->message);
      xfce_dialog_show_error (NULL, error, "%s.", _("Failed to connect to the Trash"));
      g_error_free (error);
    }
  else
    {
      /* query the new state of the trash */
      thunar_tpa_query_trash (plugin);
    }
}



static void
thunar_tpa_move_to_trash_reply (DBusGProxy *proxy,
                                GError     *error,
                                gpointer    user_data)
{
  ThunarTpa *plugin = THUNAR_TPA (user_data);

  /* reset the call */
  plugin->move_to_trash_call = NULL;

  /* check if we failed */
  if (G_UNLIKELY (error != NULL))
    {
      /* display an error message to the user */
      g_strstrip (error->message);
      xfce_dialog_show_error (NULL, error, "%s.", _("Failed to connect to the Trash"));
      g_error_free (error);
    }
  else
    {
      /* query the new state of the trash */
      thunar_tpa_query_trash (plugin);
    }
}



static void
thunar_tpa_query_trash_reply (DBusGProxy *proxy,
                              gboolean    full,
                              GError     *error,
                              gpointer    user_data)
{
  ThunarTpa *plugin = THUNAR_TPA (user_data);

  /* reset the call */
  plugin->query_trash_call = NULL;

  /* check if we failed */
  if (G_UNLIKELY (error != NULL))
    {
      /* setup an error tooltip/plugin */
      thunar_tpa_error (plugin, error);
      g_error_free (error);
    }
  else
    {
      /* update the tooltip/plugin accordingly */
      thunar_tpa_state (plugin, full);
    }
}



static void
thunar_tpa_drag_data_received (GtkWidget        *button,
                               GdkDragContext   *context,
                               gint              x,
                               gint              y,
                               GtkSelectionData *selection_data,
                               guint             info,
                               guint             timestamp,
                               ThunarTpa        *plugin)
{
  gboolean succeed = FALSE;
  gchar  **uri_list;

  g_return_if_fail (THUNAR_IS_TPA (plugin));
  g_return_if_fail (plugin->button == button);

  /* determine the type of drop we received */
  if (G_LIKELY (info == TARGET_TEXT_URI_LIST))
    {
      /* check if the data is valid for text/uri-list */
      uri_list = gtk_selection_data_get_uris (selection_data);
      if (G_LIKELY (uri_list != NULL))
        {
          succeed = thunar_tpa_move_to_trash (plugin, (const gchar **) uri_list);
          g_strfreev (uri_list);
        }
    }

  /* finish the drag */
  gtk_drag_finish (context, succeed, TRUE, timestamp);
}



static gboolean
thunar_tpa_enter_notify_event (GtkWidget        *button,
                               GdkEventCrossing *event,
                               ThunarTpa        *plugin)
{
  g_return_val_if_fail (THUNAR_IS_TPA (plugin), FALSE);
  g_return_val_if_fail (plugin->button == button, FALSE);

  /* query the new state of the trash */
  thunar_tpa_query_trash (plugin);

  return FALSE;
}



static gboolean
thunar_tpa_leave_notify_event (GtkWidget        *button,
                               GdkEventCrossing *event,
                               ThunarTpa        *plugin)
{
  g_return_val_if_fail (THUNAR_IS_TPA (plugin), FALSE);
  g_return_val_if_fail (plugin->button == button, FALSE);

  /* query the new state of the trash */
  thunar_tpa_query_trash (plugin);

  return FALSE;
}



static void
thunar_tpa_trash_changed (DBusGProxy *proxy,
                          gboolean    full,
                          ThunarTpa  *plugin)
{
  g_return_if_fail (THUNAR_IS_TPA (plugin));
  g_return_if_fail (plugin->proxy == proxy);

  /* change the status plugin/tooltip appropriately */
  thunar_tpa_state (plugin, full);
}



static void
thunar_tpa_display_trash (ThunarTpa *plugin)
{
  gchar *display_name;
  gchar *startup_id;

  g_return_if_fail (THUNAR_IS_TPA (plugin));

  /* check if we are connected to the bus */
  if (G_LIKELY (plugin->proxy != NULL))
    {
      /* cancel any pending call */
      if (G_UNLIKELY (plugin->display_trash_call != NULL))
        dbus_g_proxy_cancel_call (plugin->proxy, plugin->display_trash_call);

      /* schedule a new call */
      display_name = gdk_screen_make_display_name (gtk_widget_get_screen (GTK_WIDGET (plugin)));
      startup_id = g_strdup_printf ("_TIME%d", gtk_get_current_event_time ());
      plugin->display_trash_call = org_xfce_Trash_display_trash_async (plugin->proxy, display_name, startup_id, thunar_tpa_display_trash_reply, plugin);
      g_free (startup_id);
      g_free (display_name);
    }
}



static void
thunar_tpa_empty_trash (ThunarTpa *plugin)
{
  gchar *display_name;
  gchar *startup_id;

  g_return_if_fail (THUNAR_IS_TPA (plugin));

  /* check if we are connected to the bus */
  if (G_LIKELY (plugin->proxy != NULL))
    {
      /* cancel any pending call */
      if (G_UNLIKELY (plugin->empty_trash_call != NULL))
        dbus_g_proxy_cancel_call (plugin->proxy, plugin->empty_trash_call);

      /* schedule a new call */
      display_name = gdk_screen_make_display_name (gtk_widget_get_screen (GTK_WIDGET (plugin)));
      startup_id = g_strdup_printf ("_TIME%d", gtk_get_current_event_time ());
      plugin->empty_trash_call = org_xfce_Trash_empty_trash_async (plugin->proxy, display_name, startup_id, thunar_tpa_empty_trash_reply, plugin);
      g_free (startup_id);
      g_free (display_name);
    }
}



static gboolean
thunar_tpa_move_to_trash (ThunarTpa    *plugin,
                          const gchar **uri_list)
{
  gchar *display_name;
  gchar *startup_id;

  g_return_val_if_fail (THUNAR_IS_TPA (plugin), FALSE);
  g_return_val_if_fail (uri_list != NULL, FALSE);

  /* check if we are connected to the bus */
  if (G_UNLIKELY (plugin->proxy == NULL))
    return FALSE;

  /* cancel any pending call */
  if (G_UNLIKELY (plugin->move_to_trash_call != NULL))
    dbus_g_proxy_cancel_call (plugin->proxy, plugin->move_to_trash_call);

  /* schedule a new call */
  display_name = gdk_screen_make_display_name (gtk_widget_get_screen (GTK_WIDGET (plugin)));
  startup_id = g_strdup_printf ("_TIME%d", gtk_get_current_event_time ());
  plugin->move_to_trash_call = org_xfce_Trash_move_to_trash_async (plugin->proxy, uri_list, display_name, startup_id, thunar_tpa_move_to_trash_reply, plugin);
  g_free (startup_id);
  g_free (display_name);

  return TRUE;
}



static void
thunar_tpa_query_trash (ThunarTpa *plugin)
{
  g_return_if_fail (THUNAR_IS_TPA (plugin));

  /* check if we are connected to the bus */
  if (G_LIKELY (plugin->proxy != NULL))
    {
      /* cancel any pending call */
      if (G_UNLIKELY (plugin->query_trash_call != NULL))
        dbus_g_proxy_cancel_call (plugin->proxy, plugin->query_trash_call);

      /* schedule a new call */
      plugin->query_trash_call = org_xfce_Trash_query_trash_async (plugin->proxy, thunar_tpa_query_trash_reply, plugin);
    }
}