nautilus-3.6.3/src/nautilus-progress-info-widget.c

No issues found

  1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
  2 /*
  3  * nautilus-progress-info-widget.h: file operation progress user interface.
  4  *
  5  * Copyright (C) 2007, 2011 Red Hat, Inc.
  6  *
  7  * This program is free software; you can redistribute it and/or
  8  * modify it under the terms of the GNU General Public License as
  9  * published by the Free Software Foundation; either version 2 of the
 10  * License, or (at your option) any later version.
 11  *
 12  * This program is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  * General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU General Public
 18  * License along with this program; if not, write to the
 19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 20  * Boston, MA 02111-1307, USA.
 21  *
 22  * Authors: Alexander Larsson <alexl@redhat.com>
 23  *          Cosimo Cecchi <cosimoc@redhat.com>
 24  *
 25  */
 26 
 27 #include <config.h>
 28 
 29 #include "nautilus-progress-info-widget.h"
 30 
 31 struct _NautilusProgressInfoWidgetPriv {
 32 	NautilusProgressInfo *info;
 33 
 34 	GtkWidget *status; /* GtkLabel */
 35 	GtkWidget *details; /* GtkLabel */
 36 	GtkWidget *progress_bar;
 37 };
 38 
 39 enum {
 40 	PROP_INFO = 1,
 41 	NUM_PROPERTIES
 42 };
 43 
 44 static GParamSpec *properties[NUM_PROPERTIES] = { NULL };
 45 
 46 G_DEFINE_TYPE (NautilusProgressInfoWidget, nautilus_progress_info_widget,
 47                GTK_TYPE_BOX);
 48 
 49 static void
 50 info_finished (NautilusProgressInfoWidget *self)
 51 {
 52 	gtk_widget_destroy (GTK_WIDGET (self));
 53 }
 54 
 55 static void
 56 update_data (NautilusProgressInfoWidget *self)
 57 {
 58 	char *status, *details;
 59 	char *markup;
 60 
 61 	status = nautilus_progress_info_get_status (self->priv->info);
 62 	gtk_label_set_text (GTK_LABEL (self->priv->status), status);
 63 	g_free (status);
 64 
 65 	details = nautilus_progress_info_get_details (self->priv->info);
 66 	markup = g_markup_printf_escaped ("<span size='small'>%s</span>", details);
 67 	gtk_label_set_markup (GTK_LABEL (self->priv->details), markup);
 68 	g_free (details);
 69 	g_free (markup);
 70 }
 71 
 72 static void
 73 update_progress (NautilusProgressInfoWidget *self)
 74 {
 75 	double progress;
 76 
 77 	progress = nautilus_progress_info_get_progress (self->priv->info);
 78 	if (progress < 0) {
 79 		gtk_progress_bar_pulse (GTK_PROGRESS_BAR (self->priv->progress_bar));
 80 	} else {
 81 		gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (self->priv->progress_bar), progress);
 82 	}
 83 }
 84 
 85 static void
 86 cancel_clicked (GtkWidget *button,
 87 		NautilusProgressInfoWidget *self)
 88 {
 89 	nautilus_progress_info_cancel (self->priv->info);
 90 	gtk_widget_set_sensitive (button, FALSE);
 91 }
 92 
 93 static void
 94 nautilus_progress_info_widget_constructed (GObject *obj)
 95 {
 96 	GtkWidget *label, *progress_bar, *hbox, *box, *button, *image;
 97 	NautilusProgressInfoWidget *self = NAUTILUS_PROGRESS_INFO_WIDGET (obj);
 98 
 99 	G_OBJECT_CLASS (nautilus_progress_info_widget_parent_class)->constructed (obj);
100 
101 	label = gtk_label_new ("status");
102 	gtk_widget_set_size_request (label, 500, -1);
103 	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
104 	gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
105 	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
106 	gtk_box_pack_start (GTK_BOX (self),
107 			    label,
108 			    TRUE, FALSE,
109 			    0);
110 	self->priv->status = label;
111 
112 	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
113 
114 	progress_bar = gtk_progress_bar_new ();
115 	self->priv->progress_bar = progress_bar;
116 	gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (progress_bar), 0.05);
117 	box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
118 	gtk_box_pack_start(GTK_BOX (box),
119 			   progress_bar,
120 			   TRUE, FALSE,
121 			   0);
122 	gtk_box_pack_start(GTK_BOX (hbox),
123 			   box,
124 			   TRUE, TRUE,
125 			   0);
126 
127 	image = gtk_image_new_from_stock (GTK_STOCK_CANCEL,
128 					  GTK_ICON_SIZE_BUTTON);
129 	button = gtk_button_new ();
130 	gtk_container_add (GTK_CONTAINER (button), image);
131 	gtk_box_pack_start (GTK_BOX (hbox),
132 			    button,
133 			    FALSE,FALSE,
134 			    0);
135 	g_signal_connect (button, "clicked",
136 			  G_CALLBACK (cancel_clicked), self);
137 
138 	gtk_box_pack_start (GTK_BOX (self),
139 			    hbox,
140 			    FALSE,FALSE,
141 			    0);
142 
143 	label = gtk_label_new ("details");
144 	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
145 	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
146 	gtk_box_pack_start (GTK_BOX (self),
147 			    label,
148 			    TRUE, FALSE,
149 			    0);
150 	self->priv->details = label;
151 	
152 	gtk_widget_show_all (GTK_WIDGET (self));
153 
154 	update_data (self);
155 	update_progress (self);
156 
157 	g_signal_connect_swapped (self->priv->info,
158 				  "changed",
159 				  G_CALLBACK (update_data), self);
160 	g_signal_connect_swapped (self->priv->info,
161 				  "progress-changed",
162 				  G_CALLBACK (update_progress), self);
163 	g_signal_connect_swapped (self->priv->info,
164 				  "finished",
165 				  G_CALLBACK (info_finished), self);
166 }
167 
168 static void
169 nautilus_progress_info_widget_dispose (GObject *obj)
170 {
171 	NautilusProgressInfoWidget *self = NAUTILUS_PROGRESS_INFO_WIDGET (obj);
172 
173 	g_clear_object (&self->priv->info);
174 
175 	G_OBJECT_CLASS (nautilus_progress_info_widget_parent_class)->dispose (obj);
176 }
177 
178 static void
179 nautilus_progress_info_widget_set_property (GObject *object,
180 					    guint property_id,
181 					    const GValue *value,
182 					    GParamSpec *pspec)
183 {
184 	NautilusProgressInfoWidget *self = NAUTILUS_PROGRESS_INFO_WIDGET (object);
185 
186 	switch (property_id) {
187 	case PROP_INFO:
188 		self->priv->info = g_value_dup_object (value);
189 		break;
190 	default:
191 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
192 		break;
193 	}		
194 }
195 
196 static void
197 nautilus_progress_info_widget_init (NautilusProgressInfoWidget *self)
198 {
199 	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NAUTILUS_TYPE_PROGRESS_INFO_WIDGET,
200 						  NautilusProgressInfoWidgetPriv);
201 
202 	
203 }
204 
205 static void
206 nautilus_progress_info_widget_class_init (NautilusProgressInfoWidgetClass *klass)
207 {
208 	GObjectClass *oclass;
209 
210 	oclass = G_OBJECT_CLASS (klass);
211 	oclass->set_property = nautilus_progress_info_widget_set_property;
212 	oclass->constructed = nautilus_progress_info_widget_constructed;
213 	oclass->dispose = nautilus_progress_info_widget_dispose;
214 
215 	properties[PROP_INFO] =
216 		g_param_spec_object ("info",
217 				     "NautilusProgressInfo",
218 				     "The NautilusProgressInfo associated with this widget",
219 				     NAUTILUS_TYPE_PROGRESS_INFO,
220 				     G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
221 				     G_PARAM_STATIC_STRINGS);
222 
223 	g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);
224 
225 	g_type_class_add_private (klass, sizeof (NautilusProgressInfoWidgetPriv));
226 }
227 
228 GtkWidget *
229 nautilus_progress_info_widget_new (NautilusProgressInfo *info)
230 {
231 	return g_object_new (NAUTILUS_TYPE_PROGRESS_INFO_WIDGET,
232 			     "info", info,
233 			     "orientation", GTK_ORIENTATION_VERTICAL,
234 			     "homogeneous", FALSE,
235 			     "spacing", 5,
236 			     NULL);
237 }