gtktrashmonitor.c 6.36 KB
Newer Older
1 2 3 4 5 6
/* GTK - The GIMP Toolkit
 * gtktrashmonitor.h: Monitor the trash:/// folder to see if there is trash or not
 * Copyright (C) 2011 Suse
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
7
 * published by the Free Software Foundation; either version 2.1 of the
8 9 10 11 12
 * 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
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
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
 *
 * Authors: Federico Mena Quintero <federico@gnome.org>
 */

#include "config.h"

#include "gtkintl.h"
#include "gtkmarshalers.h"
#include "gtktrashmonitor.h"

struct _GtkTrashMonitor
{
  GObject parent;

  GFileMonitor *file_monitor;
  gulong file_monitor_changed_id;
  
  guint has_trash : 1;
};

struct _GtkTrashMonitorClass
{
  GObjectClass parent_class;

  void (* trash_state_changed) (GtkTrashMonitor *monitor);
};

enum {
  TRASH_STATE_CHANGED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (GtkTrashMonitor, _gtk_trash_monitor, G_TYPE_OBJECT)

static GtkTrashMonitor *the_trash_monitor;

#define ICON_NAME_TRASH_EMPTY "user-trash-symbolic"
#define ICON_NAME_TRASH_FULL  "user-trash-full-symbolic"

static void
gtk_trash_monitor_dispose (GObject *object)
{
  GtkTrashMonitor *monitor;

  monitor = GTK_TRASH_MONITOR (object);

  if (monitor->file_monitor)
    {
      g_signal_handler_disconnect (monitor->file_monitor, monitor->file_monitor_changed_id);
      monitor->file_monitor_changed_id = 0;

      g_clear_object (&monitor->file_monitor);
    }

  G_OBJECT_CLASS (_gtk_trash_monitor_parent_class)->dispose (object);
}

static void
_gtk_trash_monitor_class_init (GtkTrashMonitorClass *class)
{
  GObjectClass *gobject_class;

  gobject_class = (GObjectClass *) class;

  gobject_class->dispose = gtk_trash_monitor_dispose;

  signals[TRASH_STATE_CHANGED] =
    g_signal_new (I_("trash-state-changed"),
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkTrashMonitorClass, trash_state_changed),
		  NULL, NULL,
91
		  NULL,
92 93 94 95 96 97 98 99
		  G_TYPE_NONE, 0);
}

/* Updates the internal has_trash flag and emits the "trash-state-changed" signal */
static void
update_has_trash_and_notify (GtkTrashMonitor *monitor,
			     gboolean has_trash)
{
100 101 102
  if (monitor->has_trash == !!has_trash)
    return;

103 104 105 106 107
  monitor->has_trash = !!has_trash;

  g_signal_emit (monitor, signals[TRASH_STATE_CHANGED], 0); 
}

108 109 110 111 112
/* Use G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT since we only want to know whether the
 * trash is empty or not, not access its children. This is available for the
 * trash backend since it uses a cache. In this way we prevent flooding the
 * trash backend with enumeration requests when trashing > 1000 files
 */
113
static void
114 115 116
trash_query_info_cb (GObject *source,
                     GAsyncResult *result,
                     gpointer user_data)
117 118
{
  GtkTrashMonitor *monitor = GTK_TRASH_MONITOR (user_data);
119 120 121
  GFileInfo *info;
  guint32 item_count;
  gboolean has_trash = FALSE;
122

123
  info = g_file_query_info_finish (G_FILE (source), result, NULL);
124

125
  if (info != NULL)
126
    {
127 128 129 130 131
      item_count = g_file_info_get_attribute_uint32 (info,
                                                     G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT);
      has_trash = item_count > 0;

      g_object_unref (info);
132 133
    }

134
  update_has_trash_and_notify (monitor, has_trash);
135

136
  g_object_unref (monitor); /* was reffed in recompute_trash_state() */
137 138 139 140 141 142 143 144 145
}

/* Asynchronously recomputes whether there is trash or not */
static void
recompute_trash_state (GtkTrashMonitor *monitor)
{
  GFile *file;

  file = g_file_new_for_uri ("trash:///");
146 147 148 149 150
  g_file_query_info_async (file,
                           G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT,
                           G_FILE_QUERY_INFO_NONE,
                           G_PRIORITY_DEFAULT, NULL,
                           trash_query_info_cb, g_object_ref (monitor));
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

  g_object_unref (file);
}

/* Callback used when the "trash:///" file monitor changes; we just recompute the trash state
 * whenever something happens.
 */
static void
file_monitor_changed_cb (GFileMonitor	   *file_monitor,
			 GFile		   *child,
			 GFile		   *other_file,
			 GFileMonitorEvent  event_type,
			 GtkTrashMonitor   *monitor)
{
  recompute_trash_state (monitor);
}

static void
_gtk_trash_monitor_init (GtkTrashMonitor *monitor)
{
  GFile *file;

  file = g_file_new_for_uri ("trash:///");

  monitor->file_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);

  g_object_unref (file);

  if (monitor->file_monitor)
    monitor->file_monitor_changed_id = g_signal_connect (monitor->file_monitor, "changed",
							 G_CALLBACK (file_monitor_changed_cb), monitor);

  recompute_trash_state (monitor);
}

/**
 * _gtk_trash_monitor_get:
 *
189
 * Returns: (transfer full): a new reference to the singleton
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
 * #GtkTrashMonitor object.  Be sure to call g_object_unref() on it when you are
 * done with the trash monitor.
 */
GtkTrashMonitor *
_gtk_trash_monitor_get (void)
{
  if (the_trash_monitor != NULL)
    {
      g_object_ref (the_trash_monitor);
    }
  else
    {
      the_trash_monitor = g_object_new (GTK_TYPE_TRASH_MONITOR, NULL);
      g_object_add_weak_pointer (G_OBJECT (the_trash_monitor), (gpointer *) &the_trash_monitor);
    }

  return the_trash_monitor;
}

/**
 * _gtk_trash_monitor_get_icon:
 * @monitor: a #GtkTrashMonitor
 *
213
 * Returns: (transfer full): the #GIcon that should be used to represent
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
 * the state of the trash folder on screen, based on whether there is trash or
 * not.
 */
GIcon *
_gtk_trash_monitor_get_icon (GtkTrashMonitor *monitor)
{
  const char *icon_name;

  g_return_val_if_fail (GTK_IS_TRASH_MONITOR (monitor), NULL);

  if (monitor->has_trash)
    icon_name = ICON_NAME_TRASH_FULL;
  else
    icon_name = ICON_NAME_TRASH_EMPTY;

  return g_themed_icon_new (icon_name);
}

/**
 * _gtk_trash_monitor_get_has_trash:
 * @monitor: a #GtkTrashMonitor
 *
236
 * Returns: #TRUE if there is trash in the trash:/// folder, or #FALSE otherwise.
237 238 239 240 241 242 243 244
 */
gboolean
_gtk_trash_monitor_get_has_trash (GtkTrashMonitor *monitor)
{
  g_return_val_if_fail (GTK_IS_TRASH_MONITOR (monitor), FALSE);

  return monitor->has_trash;
}