aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/dialogs/e-delegate-dialog.c
blob: ca253ca4309ab615f01410b5af847e4c0d5a6b4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Evolution calendar - Delegate selector dialog
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Damon Chaplin <damon@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <libical/ical.h>
#include <libebook/e-destination.h>
#include <libedataserverui/e-name-selector.h>
#include "e-util/e-util.h"
#include "e-util/e-util-private.h"
#include "e-delegate-dialog.h"

#define E_DELEGATE_DIALOG_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_DELEGATE_DIALOG, EDelegateDialogPrivate))

struct _EDelegateDialogPrivate {
    gchar *name;
    gchar *address;

    GtkBuilder *builder;

    /* Widgets from the UI file */
    GtkWidget *app;
    GtkWidget *hbox;
    GtkWidget *addressbook;

    ENameSelector *name_selector;
    GtkWidget *entry;
};

static const gchar *section_name = "Delegate To";

static gboolean get_widgets         (EDelegateDialog *edd);
static void addressbook_clicked_cb              (GtkWidget *widget, gpointer data);
static void addressbook_response_cb             (GtkWidget *widget, gint response, gpointer data);

G_DEFINE_TYPE (EDelegateDialog, e_delegate_dialog, G_TYPE_OBJECT)

static void
e_delegate_dialog_finalize (GObject *object)
{
    EDelegateDialogPrivate *priv;
    GtkWidget *dialog;

    priv = E_DELEGATE_DIALOG_GET_PRIVATE (object);

    e_name_selector_cancel_loading (priv->name_selector);
    g_object_unref (priv->name_selector);

    /* Destroy the actual dialog. */
    dialog = e_delegate_dialog_get_toplevel (E_DELEGATE_DIALOG (object));
    gtk_widget_destroy (dialog);

    g_free (priv->address);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_delegate_dialog_parent_class)->finalize (object);
}

static void
e_delegate_dialog_class_init (EDelegateDialogClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EDelegateDialogPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = e_delegate_dialog_finalize;
}

static void
e_delegate_dialog_init (EDelegateDialog *edd)
{
    edd->priv = E_DELEGATE_DIALOG_GET_PRIVATE (edd);
}

EDelegateDialog *
e_delegate_dialog_construct (EDelegateDialog *edd,
                             ESourceRegistry *registry,
                             const gchar *name,
                             const gchar *address)
{
    EDelegateDialogPrivate *priv;
    EDestinationStore *destination_store;
    EDestination *dest;
    ENameSelectorModel *name_selector_model;
    ENameSelectorDialog *name_selector_dialog;

    g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);
    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);

    priv = edd->priv;

    /* Load the content widgets */

    priv->builder = gtk_builder_new ();
    e_load_ui_builder_definition (priv->builder, "e-delegate-dialog.ui");

    if (!get_widgets (edd)) {
        g_message ("e_delegate_dialog_construct(): Could not find all widgets in the XML file!");
        goto error;
    }

    priv->name_selector = e_name_selector_new (registry);
    e_name_selector_load_books (priv->name_selector);
    name_selector_model = e_name_selector_peek_model (priv->name_selector);
    e_name_selector_model_add_section (name_selector_model, section_name, section_name, NULL);

    priv->entry = GTK_WIDGET (e_name_selector_peek_section_entry (priv->name_selector, section_name));
    gtk_widget_show (priv->entry);
    gtk_box_pack_start (GTK_BOX (priv->hbox), priv->entry, TRUE, TRUE, 6);

    dest = e_destination_new ();

    if (name != NULL && *name)
        e_destination_set_name (dest, name);
    if (address != NULL && *address)
        e_destination_set_email (dest, address);

    e_name_selector_model_peek_section (name_selector_model, section_name, NULL, &destination_store);
    e_destination_store_append_destination (destination_store, dest);
    g_object_unref (dest);

    g_signal_connect (
        priv->addressbook, "clicked",
        G_CALLBACK (addressbook_clicked_cb), edd);

    name_selector_dialog = e_name_selector_peek_dialog (priv->name_selector);
    g_signal_connect (
        name_selector_dialog, "response",
        G_CALLBACK (addressbook_response_cb), edd);

    return edd;

 error:

    g_object_unref (edd);
    return NULL;
}

static gboolean
get_widgets (EDelegateDialog *edd)
{
    EDelegateDialogPrivate *priv;

    priv = edd->priv;

    priv->app = e_builder_get_widget (priv->builder, "delegate-dialog");
    priv->hbox = e_builder_get_widget (priv->builder, "delegate-hbox");
    priv->addressbook = e_builder_get_widget (priv->builder, "addressbook");

    return (priv->app
        && priv->hbox
        && priv->addressbook);
}

static void
addressbook_clicked_cb (GtkWidget *widget,
                        gpointer data)
{
    EDelegateDialog *edd = data;

    e_name_selector_show_dialog (edd->priv->name_selector,
                     e_delegate_dialog_get_toplevel (edd));
}

static void
addressbook_response_cb (GtkWidget *widget,
                         gint response,
                         gpointer data)
{
    EDelegateDialog *edd = data;
    EDelegateDialogPrivate *priv;
    ENameSelectorDialog *name_selector_dialog;

    priv = edd->priv;

    name_selector_dialog = e_name_selector_peek_dialog (priv->name_selector);
    gtk_widget_hide (GTK_WIDGET (name_selector_dialog));
}

/**
 * e_delegate_dialog_new:
 *
 * Creates a new event editor dialog.
 *
 * Return value: A newly-created event editor dialog, or NULL if the event
 * editor could not be created.
 **/
EDelegateDialog *
e_delegate_dialog_new (ESourceRegistry *registry,
                       const gchar *name,
                       const gchar *address)
{
    EDelegateDialog *edd;

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);

    edd = g_object_new (E_TYPE_DELEGATE_DIALOG, NULL);

    return e_delegate_dialog_construct (
        E_DELEGATE_DIALOG (edd), registry, name, address);
}

gchar *
e_delegate_dialog_get_delegate (EDelegateDialog *edd)
{
    EDelegateDialogPrivate *priv;
    ENameSelectorModel *name_selector_model;
    EDestinationStore *destination_store;
    GList *destinations;
    EDestination *destination;

    g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);

    priv = edd->priv;

    name_selector_model = e_name_selector_peek_model (priv->name_selector);
    e_name_selector_model_peek_section (name_selector_model, section_name, NULL, &destination_store);
    destinations = e_destination_store_list_destinations (destination_store);
    if (!destinations)
        return NULL;

    destination = destinations->data;

    if (destination) {
        g_free (priv->address);
        priv->address = g_strdup (e_destination_get_email (destination));
    }

    g_list_free (destinations);
    return g_strdup (priv->address);
}

gchar *
e_delegate_dialog_get_delegate_name (EDelegateDialog *edd)
{
    EDelegateDialogPrivate *priv;
    ENameSelectorModel *name_selector_model;
    EDestinationStore *destination_store;
    GList *destinations;
    EDestination *destination;

    g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);

    priv = edd->priv;

    name_selector_model = e_name_selector_peek_model (priv->name_selector);
    e_name_selector_model_peek_section (name_selector_model, section_name, NULL, &destination_store);
    destinations = e_destination_store_list_destinations (destination_store);
    if (!destinations)
        return NULL;

    destination = destinations->data;

    if (destination) {
        g_free (priv->name);
        priv->name = g_strdup (e_destination_get_name (destination));
    }

    g_list_free (destinations);
    return g_strdup (priv->name);
}

GtkWidget *
e_delegate_dialog_get_toplevel (EDelegateDialog *edd)
{
    g_return_val_if_fail (E_IS_DELEGATE_DIALOG (edd), NULL);

    return edd->priv->app;
}