evolution-3.6.4/mail/em-folder-selector.c

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		Jeffrey Stedfast <fejj@ximian.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include <string.h>
 29 #include <glib/gi18n.h>
 30 #include <e-util/e-util.h>
 31 
 32 #include <libemail-engine/e-mail-session.h>
 33 
 34 #include "em-folder-tree.h"
 35 #include "em-folder-selector.h"
 36 #include "em-folder-utils.h"
 37 #include "em-utils.h"
 38 
 39 #define d(x)
 40 
 41 #define EM_FOLDER_SELECTOR_GET_PRIVATE(obj) \
 42 	(G_TYPE_INSTANCE_GET_PRIVATE \
 43 	((obj), EM_TYPE_FOLDER_SELECTOR, EMFolderSelectorPrivate))
 44 
 45 struct _EMFolderSelectorPrivate {
 46 	EMFolderTree *folder_tree;  /* not referenced */
 47 	EMFolderTreeModel *model;
 48 };
 49 
 50 enum {
 51 	PROP_0,
 52 	PROP_MODEL
 53 };
 54 
 55 /* XXX EMFolderSelector is an EAlertSink, but it just uses the default
 56  *     message dialog implementation.  We should do something nicer. */
 57 
 58 G_DEFINE_TYPE_WITH_CODE (
 59 	EMFolderSelector,
 60 	em_folder_selector,
 61 	GTK_TYPE_DIALOG,
 62 	G_IMPLEMENT_INTERFACE (E_TYPE_ALERT_SINK, NULL))
 63 
 64 static void
 65 folder_selector_set_model (EMFolderSelector *emfs,
 66                            EMFolderTreeModel *model)
 67 {
 68 	g_return_if_fail (EM_IS_FOLDER_TREE_MODEL (model));
 69 	g_return_if_fail (emfs->priv->model == NULL);
 70 
 71 	emfs->priv->model = g_object_ref (model);
 72 }
 73 
 74 static void
 75 folder_selector_set_property (GObject *object,
 76                               guint property_id,
 77                               const GValue *value,
 78                               GParamSpec *pspec)
 79 {
 80 	switch (property_id) {
 81 		case PROP_MODEL:
 82 			folder_selector_set_model (
 83 				EM_FOLDER_SELECTOR (object),
 84 				g_value_get_object (value));
 85 			return;
 86 	}
 87 
 88 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 89 }
 90 
 91 static void
 92 folder_selector_get_property (GObject *object,
 93                               guint property_id,
 94                               GValue *value,
 95                               GParamSpec *pspec)
 96 {
 97 	switch (property_id) {
 98 		case PROP_MODEL:
 99 			g_value_set_object (
100 				value,
101 				em_folder_selector_get_model (
102 				EM_FOLDER_SELECTOR (object)));
103 			return;
104 	}
105 
106 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107 }
108 
109 static void
110 folder_selector_dispose (GObject *object)
111 {
112 	EMFolderSelector *emfs = EM_FOLDER_SELECTOR (object);
113 
114 	if (emfs->created_id != 0) {
115 		g_signal_handler_disconnect (
116 			emfs->priv->model, emfs->created_id);
117 		emfs->created_id = 0;
118 	}
119 
120 	if (emfs->priv->model != NULL) {
121 		g_object_unref (emfs->priv->model);
122 		emfs->priv->model = NULL;
123 	}
124 
125 	/* Chain up to parent's dispose() method. */
126 	G_OBJECT_CLASS (em_folder_selector_parent_class)->dispose (object);
127 }
128 
129 static void
130 folder_selector_finalize (GObject *object)
131 {
132 	EMFolderSelector *emfs = EM_FOLDER_SELECTOR (object);
133 
134 	g_free (emfs->selected_uri);
135 	g_free (emfs->created_uri);
136 
137 	/* Chain up to parent's finalize() method. */
138 	G_OBJECT_CLASS (em_folder_selector_parent_class)->finalize (object);
139 }
140 
141 static void
142 em_folder_selector_class_init (EMFolderSelectorClass *class)
143 {
144 	GObjectClass *object_class;
145 
146 	g_type_class_add_private (class, sizeof (EMFolderSelectorPrivate));
147 
148 	object_class = G_OBJECT_CLASS (class);
149 	object_class->set_property = folder_selector_set_property;
150 	object_class->get_property = folder_selector_get_property;
151 	object_class->dispose = folder_selector_dispose;
152 	object_class->finalize = folder_selector_finalize;
153 
154 	g_object_class_install_property (
155 		object_class,
156 		PROP_MODEL,
157 		g_param_spec_object (
158 			"model",
159 			NULL,
160 			NULL,
161 			EM_TYPE_FOLDER_TREE_MODEL,
162 			G_PARAM_READWRITE |
163 			G_PARAM_CONSTRUCT_ONLY |
164 			G_PARAM_STATIC_STRINGS));
165 }
166 
167 static void
168 em_folder_selector_init (EMFolderSelector *emfs)
169 {
170 	emfs->priv = EM_FOLDER_SELECTOR_GET_PRIVATE (emfs);
171 }
172 
173 static void
174 emfs_response (GtkWidget *dialog,
175                gint response,
176                EMFolderSelector *emfs)
177 {
178 	EMFolderTree *folder_tree;
179 	EMailSession *session;
180 
181 	if (response != EM_FOLDER_SELECTOR_RESPONSE_NEW)
182 		return;
183 
184 	folder_tree = em_folder_selector_get_folder_tree (emfs);
185 
186 	g_object_set_data (
187 		G_OBJECT (folder_tree), "select", GUINT_TO_POINTER (1));
188 
189 	session = em_folder_tree_get_session (folder_tree);
190 
191 	em_folder_utils_create_folder (
192 		GTK_WINDOW (dialog), session, folder_tree,
193 		em_folder_selector_get_selected_uri (emfs));
194 
195 	g_signal_stop_emission_by_name (emfs, "response");
196 }
197 
198 static void
199 emfs_create_name_changed (GtkEntry *entry,
200                           EMFolderSelector *emfs)
201 {
202 	EMFolderTree *folder_tree;
203 	gchar *path;
204 	const gchar *text = NULL;
205 	gboolean active;
206 
207 	if (gtk_entry_get_text_length (emfs->name_entry) > 0)
208 		text = gtk_entry_get_text (emfs->name_entry);
209 
210 	folder_tree = em_folder_selector_get_folder_tree (emfs);
211 
212 	path = em_folder_tree_get_selected_uri (folder_tree);
213 	active = text && path && !strchr (text, '/');
214 	g_free (path);
215 
216 	gtk_dialog_set_response_sensitive (
217 		GTK_DIALOG (emfs), GTK_RESPONSE_OK, active);
218 }
219 
220 static void
221 folder_selected_cb (EMFolderTree *emft,
222                     CamelStore *store,
223                     const gchar *folder_name,
224                     CamelFolderInfoFlags flags,
225                     EMFolderSelector *emfs)
226 {
227 	if (emfs->name_entry)
228 		emfs_create_name_changed (emfs->name_entry, emfs);
229 	else
230 		gtk_dialog_set_response_sensitive (
231 			GTK_DIALOG (emfs), GTK_RESPONSE_OK, TRUE);
232 }
233 
234 static void
235 folder_activated_cb (EMFolderTree *emft,
236                      CamelStore *store,
237                      const gchar *folder_name,
238                      EMFolderSelector *emfs)
239 {
240 	gtk_dialog_response ((GtkDialog *) emfs, GTK_RESPONSE_OK);
241 }
242 
243 static void
244 folder_selector_construct (EMFolderSelector *emfs,
245                            guint32 flags,
246                            const gchar *title,
247                            const gchar *text,
248                            const gchar *oklabel)
249 {
250 	EMailSession *session;
251 	EMFolderTreeModel *model;
252 	GtkWidget *content_area;
253 	GtkWidget *container;
254 	GtkWidget *widget;
255 
256 	model = em_folder_selector_get_model (emfs);
257 	session = em_folder_tree_model_get_session (model);
258 
259 	gtk_window_set_default_size (GTK_WINDOW (emfs), 350, 300);
260 	gtk_window_set_title (GTK_WINDOW (emfs), title);
261 	gtk_container_set_border_width (GTK_CONTAINER (emfs), 6);
262 
263 	content_area = gtk_dialog_get_content_area (GTK_DIALOG (emfs));
264 	gtk_box_set_spacing (GTK_BOX (content_area), 6);
265 	gtk_container_set_border_width (GTK_CONTAINER (content_area), 6);
266 
267 	container = content_area;
268 
269 	emfs->flags = flags;
270 	if (flags & EM_FOLDER_SELECTOR_CAN_CREATE) {
271 		gtk_dialog_add_button (
272 			GTK_DIALOG (emfs), GTK_STOCK_NEW,
273 			EM_FOLDER_SELECTOR_RESPONSE_NEW);
274 		g_signal_connect (
275 			emfs, "response",
276 			G_CALLBACK (emfs_response), emfs);
277 	}
278 
279 	gtk_dialog_add_buttons (
280 		GTK_DIALOG (emfs),
281 		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
282 		oklabel ? oklabel : GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
283 
284 	gtk_dialog_set_response_sensitive (
285 		GTK_DIALOG (emfs), GTK_RESPONSE_OK, FALSE);
286 	gtk_dialog_set_default_response (
287 		GTK_DIALOG (emfs), GTK_RESPONSE_OK);
288 
289 	widget = gtk_scrolled_window_new (NULL, NULL);
290 	gtk_scrolled_window_set_policy (
291 		GTK_SCROLLED_WINDOW (widget),
292 		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
293 	gtk_scrolled_window_set_shadow_type (
294 		GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
295 	gtk_box_pack_end (GTK_BOX (container), widget, TRUE, TRUE, 6);
296 	gtk_widget_show (widget);
297 
298 	container = widget;
299 
300 	widget = em_folder_tree_new_with_model (
301 		session, E_ALERT_SINK (emfs), model);
302 	emu_restore_folder_tree_state (EM_FOLDER_TREE (widget));
303 	gtk_container_add (GTK_CONTAINER (container), widget);
304 	emfs->priv->folder_tree = EM_FOLDER_TREE (widget);
305 	gtk_widget_show (widget);
306 
307 	g_signal_connect (
308 		widget, "folder-selected",
309 		G_CALLBACK (folder_selected_cb), emfs);
310 	g_signal_connect (
311 		widget, "folder-activated",
312 		G_CALLBACK (folder_activated_cb), emfs);
313 
314 	container = content_area;
315 
316 	if (text != NULL) {
317 		widget = gtk_label_new (text);
318 		gtk_label_set_justify (GTK_LABEL (widget), GTK_JUSTIFY_LEFT);
319 		gtk_widget_show (widget);
320 
321 		gtk_box_pack_end (GTK_BOX (container), widget, FALSE, TRUE, 6);
322 	}
323 
324 	gtk_widget_grab_focus (GTK_WIDGET (emfs->priv->folder_tree));
325 }
326 
327 GtkWidget *
328 em_folder_selector_new (GtkWindow *parent,
329                         EMFolderTreeModel *model,
330                         guint32 flags,
331                         const gchar *title,
332                         const gchar *text,
333                         const gchar *oklabel)
334 {
335 	EMFolderSelector *emfs;
336 
337 	g_return_val_if_fail (EM_IS_FOLDER_TREE_MODEL (model), NULL);
338 
339 	emfs = g_object_new (
340 		EM_TYPE_FOLDER_SELECTOR,
341 		"transient-for", parent,
342 		"model", model, NULL);
343 	folder_selector_construct (emfs, flags, title, text, oklabel);
344 
345 	return (GtkWidget *) emfs;
346 }
347 
348 static void
349 emfs_create_name_activate (GtkEntry *entry,
350                            EMFolderSelector *emfs)
351 {
352 	if (gtk_entry_get_text_length (emfs->name_entry) > 0) {
353 		EMFolderTree *folder_tree;
354 		gchar *path;
355 		const gchar *text;
356 
357 		text = gtk_entry_get_text (emfs->name_entry);
358 
359 		folder_tree = em_folder_selector_get_folder_tree (emfs);
360 		path = em_folder_tree_get_selected_uri (folder_tree);
361 
362 		if (text && path && !strchr (text, '/'))
363 			g_signal_emit_by_name (emfs, "response", GTK_RESPONSE_OK);
364 		g_free (path);
365 	}
366 }
367 
368 GtkWidget *
369 em_folder_selector_create_new (GtkWindow *parent,
370                                EMFolderTreeModel *model,
371                                guint32 flags,
372                                const gchar *title,
373                                const gchar *text)
374 {
375 	EMFolderSelector *emfs;
376 	EMFolderTree *folder_tree;
377 	GtkWidget *hbox, *w;
378 	GtkWidget *container;
379 
380 	g_return_val_if_fail (EM_IS_FOLDER_TREE_MODEL (model), NULL);
381 
382 	/* remove the CREATE flag if it is there since that's the
383 	 * whole purpose of this dialog */
384 	flags &= ~EM_FOLDER_SELECTOR_CAN_CREATE;
385 
386 	emfs = g_object_new (
387 		EM_TYPE_FOLDER_SELECTOR,
388 		"transient-for", parent,
389 		"model", model, NULL);
390 	folder_selector_construct (emfs, flags, title, text, _("C_reate"));
391 
392 	folder_tree = em_folder_selector_get_folder_tree (emfs);
393 	em_folder_tree_set_excluded (folder_tree, EMFT_EXCLUDE_NOINFERIORS);
394 
395 	hbox = gtk_hbox_new (FALSE, 0);
396 	w = gtk_label_new_with_mnemonic (_("Folder _name:"));
397 	gtk_box_pack_start ((GtkBox *) hbox, w, FALSE, FALSE, 6);
398 	emfs->name_entry = (GtkEntry *) gtk_entry_new ();
399 	gtk_label_set_mnemonic_widget (
400 		GTK_LABEL (w), (GtkWidget *) emfs->name_entry);
401 	g_signal_connect (
402 		emfs->name_entry, "changed",
403 		G_CALLBACK (emfs_create_name_changed), emfs);
404 	g_signal_connect (
405 		emfs->name_entry, "activate",
406 		G_CALLBACK (emfs_create_name_activate), emfs);
407 	gtk_box_pack_start (
408 		(GtkBox *) hbox, (GtkWidget *) emfs->name_entry,
409 		TRUE, FALSE, 6);
410 	gtk_widget_show_all (hbox);
411 
412 	container = gtk_dialog_get_content_area (GTK_DIALOG (emfs));
413 	gtk_box_pack_start (GTK_BOX (container), hbox, FALSE, TRUE, 0);
414 
415 	gtk_widget_grab_focus ((GtkWidget *) emfs->name_entry);
416 
417 	return (GtkWidget *) emfs;
418 }
419 
420 EMFolderTreeModel *
421 em_folder_selector_get_model (EMFolderSelector *emfs)
422 {
423 	g_return_val_if_fail (EM_IS_FOLDER_SELECTOR (emfs), NULL);
424 
425 	return emfs->priv->model;
426 }
427 
428 EMFolderTree *
429 em_folder_selector_get_folder_tree (EMFolderSelector *emfs)
430 {
431 	g_return_val_if_fail (EM_IS_FOLDER_SELECTOR (emfs), NULL);
432 
433 	return emfs->priv->folder_tree;
434 }
435 
436 const gchar *
437 em_folder_selector_get_selected_uri (EMFolderSelector *emfs)
438 {
439 	EMFolderTree *folder_tree;
440 	gchar *uri;
441 
442 	g_return_val_if_fail (EM_IS_FOLDER_SELECTOR (emfs), NULL);
443 
444 	folder_tree = em_folder_selector_get_folder_tree (emfs);
445 	uri = em_folder_tree_get_selected_uri (folder_tree);
446 
447 	if (uri == NULL)
448 		return NULL;
449 
450 	if (emfs->name_entry) {
451 		const gchar *name;
452 		gchar *temp_uri;
453 
454 		name = gtk_entry_get_text (emfs->name_entry);
455 		temp_uri = g_strconcat (uri, "/", name, NULL);
456 
457 		g_free (uri);
458 		uri = temp_uri;
459 	}
460 
461 	g_free (emfs->selected_uri);
462 	emfs->selected_uri = uri;  /* takes ownership */
463 
464 	return uri;
465 }