No issues found
1 /*
2 * Evolution calendar - Delegate selector dialog
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 * Authors:
19 * Damon Chaplin <damon@ximian.com>
20 *
21 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <gtk/gtk.h>
30 #include <libical/ical.h>
31 #include <libebook/libebook.h>
32 #include <libedataserverui/libedataserverui.h>
33
34 #include "e-util/e-util.h"
35 #include "e-util/e-util-private.h"
36 #include "e-delegate-dialog.h"
37
38 #define E_DELEGATE_DIALOG_GET_PRIVATE(obj) \
39 (G_TYPE_INSTANCE_GET_PRIVATE \
40 ((obj), E_TYPE_DELEGATE_DIALOG, EDelegateDialogPrivate))
41
42 struct _EDelegateDialogPrivate {
43 gchar *name;
44 gchar *address;
45
46 GtkBuilder *builder;
47
48 /* Widgets from the UI file */
49 GtkWidget *app;
50 GtkWidget *hbox;
51 GtkWidget *addressbook;
52
53 ENameSelector *name_selector;
54 GtkWidget *entry;
55 };
56
57 static const gchar *section_name = "Delegate To";
58
59 static gboolean get_widgets (EDelegateDialog *edd);
60 static void addressbook_clicked_cb (GtkWidget *widget, gpointer data);
61 static void addressbook_response_cb (GtkWidget *widget, gint response, gpointer data);
62
63 G_DEFINE_TYPE (EDelegateDialog, e_delegate_dialog, G_TYPE_OBJECT)
64
65 static void
66 e_delegate_dialog_finalize (GObject *object)
67 {
68 EDelegateDialogPrivate *priv;
69 GtkWidget *dialog;
70
71 priv = E_DELEGATE_DIALOG_GET_PRIVATE (object);
72
73 e_name_selector_cancel_loading (priv->name_selector);
74 g_object_unref (priv->name_selector);
75
76 /* Destroy the actual dialog. */
77 dialog = e_delegate_dialog_get_toplevel (E_DELEGATE_DIALOG (object));
78 gtk_widget_destroy (dialog);
79
80 g_free (priv->address);
81
82 /* Chain up to parent's finalize() method. */
83 G_OBJECT_CLASS (e_delegate_dialog_parent_class)->finalize (object);
84 }
85
86 static void
87 e_delegate_dialog_class_init (EDelegateDialogClass *class)
88 {
89 GObjectClass *object_class;
90
91 g_type_class_add_private (class, sizeof (EDelegateDialogPrivate));
92
93 object_class = G_OBJECT_CLASS (class);
94 object_class->finalize = e_delegate_dialog_finalize;
95 }
96
97 static void
98 e_delegate_dialog_init (EDelegateDialog *edd)
99 {
100 edd->priv = E_DELEGATE_DIALOG_GET_PRIVATE (edd);
101 }
102
103 EDelegateDialog *
104 e_delegate_dialog_construct (EDelegateDialog *edd,
105 ESourceRegistry *registry,
106 const gchar *name,
107 const gchar *address)
108 {
109 EDelegateDialogPrivate *priv;
110 EDestinationStore *destination_store;
111 EDestination *dest;
112 ENameSelectorModel *name_selector_model;
113 ENameSelectorDialog *name_selector_dialog;
114
115 g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);
116 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
117
118 priv = edd->priv;
119
120 /* Load the content widgets */
121
122 priv->builder = gtk_builder_new ();
123 e_load_ui_builder_definition (priv->builder, "e-delegate-dialog.ui");
124
125 if (!get_widgets (edd)) {
126 g_message ("e_delegate_dialog_construct(): Could not find all widgets in the XML file!");
127 goto error;
128 }
129
130 priv->name_selector = e_name_selector_new (registry);
131 e_name_selector_load_books (priv->name_selector);
132 name_selector_model = e_name_selector_peek_model (priv->name_selector);
133 e_name_selector_model_add_section (name_selector_model, section_name, section_name, NULL);
134
135 priv->entry = GTK_WIDGET (e_name_selector_peek_section_entry (priv->name_selector, section_name));
136 gtk_widget_show (priv->entry);
137 gtk_box_pack_start (GTK_BOX (priv->hbox), priv->entry, TRUE, TRUE, 6);
138
139 dest = e_destination_new ();
140
141 if (name != NULL && *name)
142 e_destination_set_name (dest, name);
143 if (address != NULL && *address)
144 e_destination_set_email (dest, address);
145
146 e_name_selector_model_peek_section (name_selector_model, section_name, NULL, &destination_store);
147 e_destination_store_append_destination (destination_store, dest);
148 g_object_unref (dest);
149
150 g_signal_connect (
151 priv->addressbook, "clicked",
152 G_CALLBACK (addressbook_clicked_cb), edd);
153
154 name_selector_dialog = e_name_selector_peek_dialog (priv->name_selector);
155 g_signal_connect (
156 name_selector_dialog, "response",
157 G_CALLBACK (addressbook_response_cb), edd);
158
159 return edd;
160
161 error:
162
163 g_object_unref (edd);
164 return NULL;
165 }
166
167 static gboolean
168 get_widgets (EDelegateDialog *edd)
169 {
170 EDelegateDialogPrivate *priv;
171
172 priv = edd->priv;
173
174 priv->app = e_builder_get_widget (priv->builder, "delegate-dialog");
175 priv->hbox = e_builder_get_widget (priv->builder, "delegate-hbox");
176 priv->addressbook = e_builder_get_widget (priv->builder, "addressbook");
177
178 return (priv->app
179 && priv->hbox
180 && priv->addressbook);
181 }
182
183 static void
184 addressbook_clicked_cb (GtkWidget *widget,
185 gpointer data)
186 {
187 EDelegateDialog *edd = data;
188
189 e_name_selector_show_dialog (
190 edd->priv->name_selector,
191 e_delegate_dialog_get_toplevel (edd));
192 }
193
194 static void
195 addressbook_response_cb (GtkWidget *widget,
196 gint response,
197 gpointer data)
198 {
199 EDelegateDialog *edd = data;
200 EDelegateDialogPrivate *priv;
201 ENameSelectorDialog *name_selector_dialog;
202
203 priv = edd->priv;
204
205 name_selector_dialog = e_name_selector_peek_dialog (priv->name_selector);
206 gtk_widget_hide (GTK_WIDGET (name_selector_dialog));
207 }
208
209 /**
210 * e_delegate_dialog_new:
211 *
212 * Creates a new event editor dialog.
213 *
214 * Return value: A newly-created event editor dialog, or NULL if the event
215 * editor could not be created.
216 **/
217 EDelegateDialog *
218 e_delegate_dialog_new (ESourceRegistry *registry,
219 const gchar *name,
220 const gchar *address)
221 {
222 EDelegateDialog *edd;
223
224 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
225
226 edd = g_object_new (E_TYPE_DELEGATE_DIALOG, NULL);
227
228 return e_delegate_dialog_construct (
229 E_DELEGATE_DIALOG (edd), registry, name, address);
230 }
231
232 gchar *
233 e_delegate_dialog_get_delegate (EDelegateDialog *edd)
234 {
235 EDelegateDialogPrivate *priv;
236 ENameSelectorModel *name_selector_model;
237 EDestinationStore *destination_store;
238 GList *destinations;
239 EDestination *destination;
240
241 g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);
242
243 priv = edd->priv;
244
245 name_selector_model = e_name_selector_peek_model (priv->name_selector);
246 e_name_selector_model_peek_section (name_selector_model, section_name, NULL, &destination_store);
247 destinations = e_destination_store_list_destinations (destination_store);
248 if (!destinations)
249 return NULL;
250
251 destination = destinations->data;
252
253 if (destination) {
254 g_free (priv->address);
255 priv->address = g_strdup (e_destination_get_email (destination));
256 }
257
258 g_list_free (destinations);
259 return g_strdup (priv->address);
260 }
261
262 gchar *
263 e_delegate_dialog_get_delegate_name (EDelegateDialog *edd)
264 {
265 EDelegateDialogPrivate *priv;
266 ENameSelectorModel *name_selector_model;
267 EDestinationStore *destination_store;
268 GList *destinations;
269 EDestination *destination;
270
271 g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);
272
273 priv = edd->priv;
274
275 name_selector_model = e_name_selector_peek_model (priv->name_selector);
276 e_name_selector_model_peek_section (name_selector_model, section_name, NULL, &destination_store);
277 destinations = e_destination_store_list_destinations (destination_store);
278 if (!destinations)
279 return NULL;
280
281 destination = destinations->data;
282
283 if (destination) {
284 g_free (priv->name);
285 priv->name = g_strdup (e_destination_get_name (destination));
286 }
287
288 g_list_free (destinations);
289 return g_strdup (priv->name);
290 }
291
292 GtkWidget *
293 e_delegate_dialog_get_toplevel (EDelegateDialog *edd)
294 {
295 g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);
296
297 return edd->priv->app;
298 }