nautilus-3.6.3/src/nautilus-trash-bar.c

No issues found

  1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
  2 /*
  3  * Copyright (C) 2006 Paolo Borelli <pborelli@katamail.com>
  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 as published by
  7  * the Free Software Foundation; either version 2 of the License, or
  8  * (at your option) any later version.
  9  *
 10  * This program is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU General Public License
 16  * along with this program; if not, write to the Free Software
 17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 18  *
 19  * Authors: Paolo Borelli <pborelli@katamail.com>
 20  *
 21  */
 22 
 23 #include "config.h"
 24 
 25 #include <glib/gi18n.h>
 26 #include <gtk/gtk.h>
 27 
 28 #include "nautilus-trash-bar.h"
 29 
 30 #include "nautilus-view.h"
 31 #include <libnautilus-private/nautilus-file-operations.h>
 32 #include <libnautilus-private/nautilus-file-utilities.h>
 33 #include <libnautilus-private/nautilus-file.h>
 34 #include <libnautilus-private/nautilus-trash-monitor.h>
 35 
 36 #define NAUTILUS_TRASH_BAR_GET_PRIVATE(o)\
 37 	(G_TYPE_INSTANCE_GET_PRIVATE ((o), NAUTILUS_TYPE_TRASH_BAR, NautilusTrashBarPrivate))
 38 
 39 enum {
 40 	PROP_VIEW = 1,
 41 	NUM_PROPERTIES
 42 };
 43 
 44 enum {
 45 	TRASH_BAR_RESPONSE_EMPTY = 1,
 46 	TRASH_BAR_RESPONSE_RESTORE
 47 };
 48 
 49 struct NautilusTrashBarPrivate
 50 {
 51 	NautilusView *view;
 52 	gulong selection_handler_id;
 53 };
 54 
 55 G_DEFINE_TYPE (NautilusTrashBar, nautilus_trash_bar, GTK_TYPE_INFO_BAR);
 56 
 57 static void
 58 selection_changed_cb (NautilusView *view,
 59 		      NautilusTrashBar *bar)
 60 {
 61 	int count;
 62 
 63 	count = nautilus_view_get_selection_count (view);
 64 
 65 	gtk_info_bar_set_response_sensitive (GTK_INFO_BAR (bar),
 66 					     TRASH_BAR_RESPONSE_RESTORE,
 67 					     (count > 0));
 68 }
 69 
 70 static void
 71 connect_view_and_update_button (NautilusTrashBar *bar)
 72 {
 73 	bar->priv->selection_handler_id =
 74 		g_signal_connect (bar->priv->view, "selection-changed",
 75 				  G_CALLBACK (selection_changed_cb), bar);
 76 
 77 	selection_changed_cb (bar->priv->view, bar);
 78 }
 79 
 80 static void
 81 nautilus_trash_bar_set_property (GObject      *object,
 82 				 guint         prop_id,
 83 				 const GValue *value,
 84 				 GParamSpec   *pspec)
 85 {
 86 	NautilusTrashBar *bar;
 87 
 88 	bar = NAUTILUS_TRASH_BAR (object);
 89 
 90 	switch (prop_id) {
 91 	case PROP_VIEW:
 92 		bar->priv->view = g_value_get_object (value);
 93 		connect_view_and_update_button (bar);
 94 		break;
 95 	default:
 96 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 97 		break;
 98 	}
 99 }
100 
101 static void
102 nautilus_trash_bar_dispose (GObject *obj)
103 {
104 	NautilusTrashBar *bar;
105 
106 	bar = NAUTILUS_TRASH_BAR (obj);
107 
108 	if (bar->priv->selection_handler_id) {
109 		g_signal_handler_disconnect (bar->priv->view, bar->priv->selection_handler_id);
110 	}
111 
112 	G_OBJECT_CLASS (nautilus_trash_bar_parent_class)->dispose (obj);
113 }
114 
115 static void
116 nautilus_trash_bar_trash_state_changed (NautilusTrashMonitor *trash_monitor,
117 					gboolean              state,
118 					gpointer              data)
119 {
120 	NautilusTrashBar *bar;
121 
122 	bar = NAUTILUS_TRASH_BAR (data);
123 
124 	gtk_info_bar_set_response_sensitive (GTK_INFO_BAR (bar),
125 					     TRASH_BAR_RESPONSE_EMPTY,
126 					     !nautilus_trash_monitor_is_empty ());
127 }
128 
129 static void
130 nautilus_trash_bar_class_init (NautilusTrashBarClass *klass)
131 {
132 	GObjectClass *object_class;
133 
134 	object_class = G_OBJECT_CLASS (klass);
135 
136 	object_class->set_property = nautilus_trash_bar_set_property;
137 	object_class->dispose = nautilus_trash_bar_dispose;
138 
139 	g_object_class_install_property (object_class,
140 					 PROP_VIEW,
141 					 g_param_spec_object ("view",
142 							      "view",
143 							      "the NautilusView",
144 							      NAUTILUS_TYPE_VIEW,
145 							      G_PARAM_WRITABLE |
146 							      G_PARAM_CONSTRUCT_ONLY |
147 							      G_PARAM_STATIC_STRINGS));
148 
149 	g_type_class_add_private (klass, sizeof (NautilusTrashBarPrivate));
150 }
151 
152 static void
153 trash_bar_response_cb (GtkInfoBar *infobar,
154 		       gint response_id,
155 		       gpointer user_data)
156 {
157 	NautilusTrashBar *bar;
158 	GtkWidget *window;
159 	GList *files;
160 
161 	bar = NAUTILUS_TRASH_BAR (infobar);
162 	window = gtk_widget_get_toplevel (GTK_WIDGET (bar));
163 
164 	switch (response_id) {
165 	case TRASH_BAR_RESPONSE_EMPTY:
166 		nautilus_file_operations_empty_trash (window);
167 		break;
168 	case TRASH_BAR_RESPONSE_RESTORE:
169 		files = nautilus_view_get_selection (bar->priv->view);
170 		nautilus_restore_files_from_trash (files, GTK_WINDOW (window));
171 		nautilus_file_list_free (files);
172 		break;
173 	default:
174 		break;
175 	}
176 }
177 
178 static void
179 nautilus_trash_bar_init (NautilusTrashBar *bar)
180 {
181 	GtkWidget *content_area, *action_area, *w;
182 	GtkWidget *label;
183 	PangoAttrList *attrs;
184 
185 	bar->priv = NAUTILUS_TRASH_BAR_GET_PRIVATE (bar);
186 	content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (bar));
187 	action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (bar));
188 
189 	gtk_orientable_set_orientation (GTK_ORIENTABLE (action_area),
190 					GTK_ORIENTATION_HORIZONTAL);
191 
192 	attrs = pango_attr_list_new ();
193 	pango_attr_list_insert (attrs, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
194 	label = gtk_label_new (_("Trash"));
195 	gtk_label_set_attributes (GTK_LABEL (label), attrs);
196 	pango_attr_list_unref (attrs);
197 
198 	gtk_widget_show (label);
199 	gtk_container_add (GTK_CONTAINER (content_area), label);
200 
201 	w = gtk_info_bar_add_button (GTK_INFO_BAR (bar),
202 				     _("Restore"),
203 				     TRASH_BAR_RESPONSE_RESTORE);
204 	gtk_widget_set_tooltip_text (w,
205 				     _("Restore selected items to their original position"));
206 
207 	w = gtk_info_bar_add_button (GTK_INFO_BAR (bar),
208 	/* Translators: "Empty" is an action (for the trash) , not a state */
209 				     _("Empty"),
210 				     TRASH_BAR_RESPONSE_EMPTY);
211 	gtk_widget_set_tooltip_text (w,
212 				     _("Delete all items in the Trash"));
213 
214 	g_signal_connect_object (nautilus_trash_monitor_get (),
215 				 "trash_state_changed",
216 				 G_CALLBACK (nautilus_trash_bar_trash_state_changed),
217 				 bar,
218 				 0);
219 	nautilus_trash_bar_trash_state_changed (nautilus_trash_monitor_get (),
220 						FALSE, bar);
221 
222 	g_signal_connect (bar, "response",
223 			  G_CALLBACK (trash_bar_response_cb), bar);
224 }
225 
226 GtkWidget *
227 nautilus_trash_bar_new (NautilusView *view)
228 {
229 	return g_object_new (NAUTILUS_TYPE_TRASH_BAR,
230 			     "view", view,
231 			     "message-type", GTK_MESSAGE_QUESTION,
232 			     NULL);
233 }