evolution-3.6.4/widgets/misc/e-attachment-dialog.c

No issues found

  1 /*
  2  * e-attachment-dialog.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 19  *
 20  */
 21 
 22 #ifdef HAVE_CONFIG_H
 23 #include <config.h>
 24 #endif
 25 
 26 #include "e-attachment-dialog.h"
 27 
 28 #include <glib/gi18n.h>
 29 
 30 #define E_ATTACHMENT_DIALOG_GET_PRIVATE(obj) \
 31 	(G_TYPE_INSTANCE_GET_PRIVATE \
 32 	((obj), E_TYPE_ATTACHMENT_DIALOG, EAttachmentDialogPrivate))
 33 
 34 struct _EAttachmentDialogPrivate {
 35 	EAttachment *attachment;
 36 	GtkWidget *display_name_entry;
 37 	GtkWidget *description_entry;
 38 	GtkWidget *content_type_label;
 39 	GtkWidget *disposition_checkbox;
 40 };
 41 
 42 enum {
 43 	PROP_0,
 44 	PROP_ATTACHMENT
 45 };
 46 
 47 G_DEFINE_TYPE (
 48 	EAttachmentDialog,
 49 	e_attachment_dialog,
 50 	GTK_TYPE_DIALOG)
 51 
 52 static void
 53 attachment_dialog_update (EAttachmentDialog *dialog)
 54 {
 55 	EAttachment *attachment;
 56 	GFileInfo *file_info;
 57 	GtkWidget *widget;
 58 	const gchar *content_type;
 59 	const gchar *display_name;
 60 	const gchar *description;
 61 	const gchar *disposition;
 62 	gchar *type_description = NULL;
 63 	gboolean sensitive;
 64 	gboolean active;
 65 
 66 	attachment = e_attachment_dialog_get_attachment (dialog);
 67 
 68 	if (attachment != NULL) {
 69 		file_info = e_attachment_get_file_info (attachment);
 70 		description = e_attachment_get_description (attachment);
 71 		disposition = e_attachment_get_disposition (attachment);
 72 	} else {
 73 		file_info = NULL;
 74 		description = NULL;
 75 		disposition = NULL;
 76 	}
 77 
 78 	if (file_info != NULL) {
 79 		content_type = g_file_info_get_content_type (file_info);
 80 		display_name = g_file_info_get_display_name (file_info);
 81 	} else {
 82 		content_type = NULL;
 83 		display_name = NULL;
 84 	}
 85 
 86 	if (content_type != NULL) {
 87 		gchar *comment;
 88 		gchar *mime_type;
 89 
 90 		comment = g_content_type_get_description (content_type);
 91 		mime_type = g_content_type_get_mime_type (content_type);
 92 
 93 		type_description =
 94 			g_strdup_printf ("%s (%s)", comment, mime_type);
 95 
 96 		g_free (comment);
 97 		g_free (mime_type);
 98 	}
 99 
100 	sensitive = G_IS_FILE_INFO (file_info);
101 
102 	gtk_dialog_set_response_sensitive (
103 		GTK_DIALOG (dialog), GTK_RESPONSE_OK, sensitive);
104 
105 	widget = dialog->priv->display_name_entry;
106 	gtk_widget_set_sensitive (widget, sensitive);
107 	if (display_name != NULL)
108 		gtk_entry_set_text (GTK_ENTRY (widget), display_name);
109 
110 	widget = dialog->priv->description_entry;
111 	gtk_widget_set_sensitive (widget, sensitive);
112 	if (description != NULL)
113 		gtk_entry_set_text (GTK_ENTRY (widget), description);
114 
115 	widget = dialog->priv->content_type_label;
116 	gtk_label_set_text (GTK_LABEL (widget), type_description);
117 
118 	active = (g_strcmp0 (disposition, "inline") == 0);
119 	widget = dialog->priv->disposition_checkbox;
120 	gtk_widget_set_sensitive (widget, sensitive);
121 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), active);
122 
123 	g_free (type_description);
124 }
125 
126 static void
127 attachment_dialog_set_property (GObject *object,
128                                 guint property_id,
129                                 const GValue *value,
130                                 GParamSpec *pspec)
131 {
132 	switch (property_id) {
133 		case PROP_ATTACHMENT:
134 			e_attachment_dialog_set_attachment (
135 				E_ATTACHMENT_DIALOG (object),
136 				g_value_get_object (value));
137 			return;
138 	}
139 
140 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
141 }
142 
143 static void
144 attachment_dialog_get_property (GObject *object,
145                                 guint property_id,
146                                 GValue *value,
147                                 GParamSpec *pspec)
148 {
149 	switch (property_id) {
150 		case PROP_ATTACHMENT:
151 			g_value_set_object (
152 				value, e_attachment_dialog_get_attachment (
153 				E_ATTACHMENT_DIALOG (object)));
154 			return;
155 	}
156 
157 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158 }
159 
160 static void
161 attachment_dialog_dispose (GObject *object)
162 {
163 	EAttachmentDialogPrivate *priv;
164 
165 	priv = E_ATTACHMENT_DIALOG_GET_PRIVATE (object);
166 
167 	if (priv->attachment != NULL) {
168 		g_object_unref (priv->attachment);
169 		priv->attachment = NULL;
170 	}
171 
172 	if (priv->display_name_entry != NULL) {
173 		g_object_unref (priv->display_name_entry);
174 		priv->display_name_entry = NULL;
175 	}
176 
177 	if (priv->description_entry != NULL) {
178 		g_object_unref (priv->description_entry);
179 		priv->description_entry = NULL;
180 	}
181 
182 	if (priv->content_type_label != NULL) {
183 		g_object_unref (priv->content_type_label);
184 		priv->content_type_label = NULL;
185 	}
186 
187 	if (priv->disposition_checkbox != NULL) {
188 		g_object_unref (priv->disposition_checkbox);
189 		priv->disposition_checkbox = NULL;
190 	}
191 
192 	/* Chain up to parent's dispose() method. */
193 	G_OBJECT_CLASS (e_attachment_dialog_parent_class)->dispose (object);
194 }
195 
196 static void
197 attachment_dialog_map (GtkWidget *widget)
198 {
199 	GtkWidget *action_area;
200 	GtkWidget *content_area;
201 
202 	/* Chain up to parent's map() method. */
203 	GTK_WIDGET_CLASS (e_attachment_dialog_parent_class)->map (widget);
204 
205 	/* XXX Override GtkDialog's broken style property defaults. */
206 	action_area = gtk_dialog_get_action_area (GTK_DIALOG (widget));
207 	content_area = gtk_dialog_get_content_area (GTK_DIALOG (widget));
208 
209 	gtk_box_set_spacing (GTK_BOX (content_area), 12);
210 	gtk_container_set_border_width (GTK_CONTAINER (action_area), 0);
211 	gtk_container_set_border_width (GTK_CONTAINER (content_area), 12);
212 }
213 
214 static void
215 attachment_dialog_response (GtkDialog *dialog,
216                             gint response_id)
217 {
218 	EAttachmentDialogPrivate *priv;
219 	EAttachment *attachment;
220 	GtkToggleButton *button;
221 	GFileInfo *file_info;
222 	CamelMimePart *mime_part;
223 	const gchar *attribute;
224 	const gchar *text;
225 	gboolean active;
226 
227 	if (response_id != GTK_RESPONSE_OK)
228 		return;
229 
230 	priv = E_ATTACHMENT_DIALOG_GET_PRIVATE (dialog);
231 	g_return_if_fail (E_IS_ATTACHMENT (priv->attachment));
232 	attachment = priv->attachment;
233 
234 	file_info = e_attachment_get_file_info (attachment);
235 	g_return_if_fail (G_IS_FILE_INFO (file_info));
236 
237 	mime_part = e_attachment_get_mime_part (attachment);
238 
239 	attribute = G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME;
240 	text = gtk_entry_get_text (GTK_ENTRY (priv->display_name_entry));
241 	g_file_info_set_attribute_string (file_info, attribute, text);
242 
243 	if (mime_part != NULL)
244 		camel_mime_part_set_filename (mime_part, text);
245 
246 	attribute = G_FILE_ATTRIBUTE_STANDARD_DESCRIPTION;
247 	text = gtk_entry_get_text (GTK_ENTRY (priv->description_entry));
248 	g_file_info_set_attribute_string (file_info, attribute, text);
249 
250 	if (mime_part != NULL)
251 		camel_mime_part_set_description (mime_part, text);
252 
253 	button = GTK_TOGGLE_BUTTON (priv->disposition_checkbox);
254 	active = gtk_toggle_button_get_active (button);
255 	text = active ? "inline" : "attachment";
256 	e_attachment_set_disposition (attachment, text);
257 
258 	if (mime_part != NULL)
259 		camel_mime_part_set_disposition (mime_part, text);
260 
261 	g_object_notify (G_OBJECT (attachment), "file-info");
262 }
263 
264 static void
265 e_attachment_dialog_class_init (EAttachmentDialogClass *class)
266 {
267 	GObjectClass *object_class;
268 	GtkWidgetClass *widget_class;
269 	GtkDialogClass *dialog_class;
270 
271 	g_type_class_add_private (class, sizeof (EAttachmentDialogPrivate));
272 
273 	object_class = G_OBJECT_CLASS (class);
274 	object_class->set_property = attachment_dialog_set_property;
275 	object_class->get_property = attachment_dialog_get_property;
276 	object_class->dispose = attachment_dialog_dispose;
277 
278 	widget_class = GTK_WIDGET_CLASS (class);
279 	widget_class->map = attachment_dialog_map;
280 
281 	dialog_class = GTK_DIALOG_CLASS (class);
282 	dialog_class->response = attachment_dialog_response;
283 
284 	g_object_class_install_property (
285 		object_class,
286 		PROP_ATTACHMENT,
287 		g_param_spec_object (
288 			"attachment",
289 			"Attachment",
290 			NULL,
291 			E_TYPE_ATTACHMENT,
292 			G_PARAM_READWRITE |
293 			G_PARAM_CONSTRUCT));
294 }
295 
296 static void
297 e_attachment_dialog_init (EAttachmentDialog *dialog)
298 {
299 	GtkWidget *container;
300 	GtkWidget *widget;
301 
302 	dialog->priv = E_ATTACHMENT_DIALOG_GET_PRIVATE (dialog);
303 
304 	gtk_dialog_add_button (
305 		GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
306 	gtk_dialog_add_button (
307 		GTK_DIALOG (dialog), GTK_STOCK_OK, GTK_RESPONSE_OK);
308 	gtk_window_set_icon_name (
309 		GTK_WINDOW (dialog), "mail-attachment");
310 	gtk_window_set_title (
311 		GTK_WINDOW (dialog), _("Attachment Properties"));
312 
313 	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
314 
315 	container = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
316 
317 	widget = gtk_table_new (4, 2, FALSE);
318 	gtk_table_set_col_spacings (GTK_TABLE (widget), 6);
319 	gtk_table_set_row_spacings (GTK_TABLE (widget), 6);
320 	gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
321 	gtk_widget_show (widget);
322 
323 	container = widget;
324 
325 	widget = gtk_entry_new ();
326 	gtk_entry_set_activates_default (GTK_ENTRY (widget), TRUE);
327 	gtk_table_attach (
328 		GTK_TABLE (container), widget,
329 		1, 2, 0, 1, GTK_FILL | GTK_EXPAND, 0, 0, 0);
330 	dialog->priv->display_name_entry = g_object_ref (widget);
331 	gtk_widget_show (widget);
332 
333 	widget = gtk_label_new_with_mnemonic (_("F_ilename:"));
334 	gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
335 	gtk_label_set_mnemonic_widget (
336 		GTK_LABEL (widget), dialog->priv->display_name_entry);
337 	gtk_table_attach (
338 		GTK_TABLE (container), widget,
339 		0, 1, 0, 1, GTK_FILL, 0, 0, 0);
340 	gtk_widget_show (widget);
341 
342 	widget = gtk_entry_new ();
343 	gtk_entry_set_activates_default (GTK_ENTRY (widget), TRUE);
344 	gtk_table_attach (
345 		GTK_TABLE (container), widget,
346 		1, 2, 1, 2, GTK_FILL | GTK_EXPAND, 0, 0, 0);
347 	dialog->priv->description_entry = g_object_ref (widget);
348 	gtk_widget_show (widget);
349 
350 	widget = gtk_label_new_with_mnemonic (_("_Description:"));
351 	gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
352 	gtk_label_set_mnemonic_widget (
353 		GTK_LABEL (widget), dialog->priv->description_entry);
354 	gtk_table_attach (
355 		GTK_TABLE (container), widget,
356 		0, 1, 1, 2, GTK_FILL, 0, 0, 0);
357 	gtk_widget_show (widget);
358 
359 	widget = gtk_label_new (NULL);
360 	gtk_label_set_selectable (GTK_LABEL (widget), TRUE);
361 	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
362 	gtk_table_attach (
363 		GTK_TABLE (container), widget,
364 		1, 2, 2, 3, GTK_FILL | GTK_EXPAND, 0, 0, 0);
365 	dialog->priv->content_type_label = g_object_ref (widget);
366 	gtk_widget_show (widget);
367 
368 	widget = gtk_label_new (_("MIME Type:"));
369 	gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
370 	gtk_table_attach (
371 		GTK_TABLE (container), widget,
372 		0, 1, 2, 3, GTK_FILL, 0, 0, 0);
373 	gtk_widget_show (widget);
374 
375 	widget = gtk_check_button_new_with_mnemonic (
376 		_("_Suggest automatic display of attachment"));
377 	gtk_table_attach (
378 		GTK_TABLE (container), widget,
379 		0, 2, 3, 4, GTK_FILL | GTK_EXPAND, 0, 0, 0);
380 	dialog->priv->disposition_checkbox = g_object_ref (widget);
381 	gtk_widget_show (widget);
382 }
383 
384 GtkWidget *
385 e_attachment_dialog_new (GtkWindow *parent,
386                          EAttachment *attachment)
387 {
388 	if (parent != NULL)
389 		g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
390 	if (attachment != NULL)
391 		g_return_val_if_fail (E_IS_ATTACHMENT (attachment), NULL);
392 
393 	return g_object_new (
394 		E_TYPE_ATTACHMENT_DIALOG,
395 		"transient-for", parent, "attachment", attachment, NULL);
396 }
397 
398 EAttachment *
399 e_attachment_dialog_get_attachment (EAttachmentDialog *dialog)
400 {
401 	g_return_val_if_fail (E_IS_ATTACHMENT_DIALOG (dialog), NULL);
402 
403 	return dialog->priv->attachment;
404 }
405 
406 void
407 e_attachment_dialog_set_attachment (EAttachmentDialog *dialog,
408                                     EAttachment *attachment)
409 {
410 	g_return_if_fail (E_IS_ATTACHMENT_DIALOG (dialog));
411 
412 	if (attachment != NULL) {
413 		g_return_if_fail (E_IS_ATTACHMENT (attachment));
414 		g_object_ref (attachment);
415 	}
416 
417 	if (dialog->priv->attachment != NULL)
418 		g_object_unref (dialog->priv->attachment);
419 
420 	dialog->priv->attachment = attachment;
421 
422 	attachment_dialog_update (dialog);
423 
424 	g_object_notify (G_OBJECT (dialog), "attachment");
425 }