aboutsummaryrefslogtreecommitdiffstats
path: root/composer/e-composer-name-header.c
blob: 5aab95d7765a5ac2e433debd1032823245bdda53 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 *
 * 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/>  
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-composer-name-header.h"

#include <glib/gi18n.h>

/* XXX Temporary kludge */
#include "addressbook/gui/contact-editor/e-contact-editor.h"
#include "addressbook/gui/contact-list-editor/e-contact-list-editor.h"

#define E_COMPOSER_NAME_HEADER_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_COMPOSER_NAME_HEADER, EComposerNameHeaderPrivate))

/* Convenience macro */
#define E_COMPOSER_NAME_HEADER_GET_ENTRY(header) \
    (E_NAME_SELECTOR_ENTRY (E_COMPOSER_HEADER (header)->input_widget))

enum {
    PROP_0,
    PROP_NAME_SELECTOR
};

struct _EComposerNameHeaderPrivate {
    ENameSelector *name_selector;
    guint destination_index;
};

static gpointer parent_class;

static void
composer_name_header_clicked_cb (EComposerNameHeader *header)
{
    ENameSelectorDialog *dialog;

    dialog = e_name_selector_peek_dialog (header->priv->name_selector);
    e_name_selector_dialog_set_destination_index (
        dialog, header->priv->destination_index);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_hide (GTK_WIDGET (dialog));
}

static void
composer_name_header_entry_changed_cb (ENameSelectorEntry *entry,
                                       EComposerNameHeader *header)
{
    g_signal_emit_by_name (header, "changed");
}

static gboolean
composer_name_header_entry_query_tooltip_cb (GtkEntry *entry,
                                             gint x,
                                             gint y,
                                             gboolean keyboard_mode,
                                             GtkTooltip *tooltip)
{
    const gchar *text;

    text = gtk_entry_get_text (entry);

    if (keyboard_mode || text == NULL || *text == '\0')
        return FALSE;

    gtk_tooltip_set_text (tooltip, text);

    return TRUE;
}

static GObject *
composer_name_header_constructor (GType type,
                                  guint n_construct_properties,
                                  GObjectConstructParam *construct_properties)
{
    EComposerNameHeaderPrivate *priv;
    ENameSelectorModel *model;
    ENameSelectorEntry *entry;
    GObject *object;
    gchar *label;

    /* Chain up to parent's constructor() method. */
    object = G_OBJECT_CLASS (parent_class)->constructor (
        type, n_construct_properties, construct_properties);

    priv = E_COMPOSER_NAME_HEADER_GET_PRIVATE (object);
    g_assert (E_IS_NAME_SELECTOR (priv->name_selector));

    model = e_name_selector_peek_model (priv->name_selector);
    label = e_composer_header_get_label (E_COMPOSER_HEADER (object));
    g_assert (label != NULL);

    /* XXX Peeking at private data. */
    priv->destination_index = model->sections->len;
    e_name_selector_model_add_section (model, label, label, NULL);

    e_composer_header_set_title_tooltip (
        E_COMPOSER_HEADER (object),
        _("Click here for the address book"));

    entry = E_NAME_SELECTOR_ENTRY (
        e_name_selector_peek_section_list (
        priv->name_selector, label));
    e_name_selector_entry_set_contact_editor_func (
        entry, e_contact_editor_new);
    e_name_selector_entry_set_contact_list_editor_func (
        entry, e_contact_list_editor_new);
    g_signal_connect (
        entry, "changed",
        G_CALLBACK (composer_name_header_entry_changed_cb), object);
    g_signal_connect (
        entry, "query-tooltip",
        G_CALLBACK (composer_name_header_entry_query_tooltip_cb),
        NULL);
    E_COMPOSER_HEADER (object)->input_widget = g_object_ref_sink (entry);

    g_signal_connect (
        object, "clicked",
        G_CALLBACK (composer_name_header_clicked_cb), NULL);

    return object;
}

static void
composer_name_header_set_property (GObject *object,
                                   guint property_id,
                                   const GValue *value,
                                   GParamSpec *pspec)
{
    EComposerNameHeaderPrivate *priv;

    priv = E_COMPOSER_NAME_HEADER_GET_PRIVATE (object);

    switch (property_id) {
        case PROP_NAME_SELECTOR:    /* construct only */
            g_assert (priv->name_selector == NULL);
            priv->name_selector = g_value_dup_object (value);
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
composer_name_header_get_property (GObject *object,
                                   guint property_id,
                                   GValue *value,
                                   GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_NAME_SELECTOR:    /* construct only */
            g_value_set_object (
                value,
                e_composer_name_header_get_name_selector (
                E_COMPOSER_NAME_HEADER (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
composer_name_header_dispose (GObject *object)
{
    EComposerNameHeaderPrivate *priv;

    priv = E_COMPOSER_NAME_HEADER_GET_PRIVATE (object);

    if (priv->name_selector != NULL) {
        g_object_unref (priv->name_selector);
        priv->name_selector = NULL;
    }

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

static void
composer_name_header_class_init (EComposerNameHeaderClass *class)
{
    GObjectClass *object_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EComposerNameHeaderPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->constructor = composer_name_header_constructor;
    object_class->set_property = composer_name_header_set_property;
    object_class->get_property = composer_name_header_get_property;
    object_class->dispose = composer_name_header_dispose;

    g_object_class_install_property (
        object_class,
        PROP_NAME_SELECTOR,
        g_param_spec_object (
            "name-selector",
            NULL,
            NULL,
            E_TYPE_NAME_SELECTOR,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY));
}

static void
composer_name_header_init (EComposerNameHeader *header)
{
    header->priv = E_COMPOSER_NAME_HEADER_GET_PRIVATE (header);
}

GType
e_composer_name_header_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EComposerNameHeaderClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) composer_name_header_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EComposerNameHeader),
            0,     /* n_preallocs */
            (GInstanceInitFunc) composer_name_header_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            E_TYPE_COMPOSER_HEADER, "EComposerNameHeader",
            &type_info, 0);
    }

    return type;
}

EComposerHeader *
e_composer_name_header_new (const gchar *label,
                            ENameSelector *name_selector)
{
    g_return_val_if_fail (E_IS_NAME_SELECTOR (name_selector), NULL);

    return g_object_new (
        E_TYPE_COMPOSER_NAME_HEADER, "label", label,
        "button", TRUE, "name-selector", name_selector, NULL);
}

ENameSelector *
e_composer_name_header_get_name_selector (EComposerNameHeader *header)
{
    g_return_val_if_fail (E_IS_COMPOSER_NAME_HEADER (header), NULL);

    return header->priv->name_selector;
}

EDestination **
e_composer_name_header_get_destinations (EComposerNameHeader *header)
{
    EDestinationStore *store;
    EDestination **destinations;
    ENameSelectorEntry *entry;
    GList *list, *iter;
    gint ii = 0;

    g_return_val_if_fail (E_IS_COMPOSER_NAME_HEADER (header), NULL);

    entry = E_COMPOSER_NAME_HEADER_GET_ENTRY (header);
    store = e_name_selector_entry_peek_destination_store (entry);

    list = e_destination_store_list_destinations (store);
    destinations = g_new0 (EDestination *, g_list_length (list) + 1);

    for (iter = list; iter != NULL; iter = iter->next)
        destinations[ii++] = g_object_ref (iter->data);

    g_list_free (list);

    return destinations;  /* free with e_destination_freev() */
}

void
e_composer_name_header_add_destinations (EComposerNameHeader *header,
                                         EDestination **destinations)
{
    EDestinationStore *store;
    ENameSelectorEntry *entry;
    gint ii;

    g_return_if_fail (E_IS_COMPOSER_NAME_HEADER (header));

    entry = E_COMPOSER_NAME_HEADER_GET_ENTRY (header);
    store = e_name_selector_entry_peek_destination_store (entry);

    if (destinations == NULL)
        return;

    for (ii = 0; destinations[ii] != NULL; ii++)
        e_destination_store_append_destination (
            store, destinations[ii]);
}

void
e_composer_name_header_set_destinations (EComposerNameHeader *header,
                                         EDestination **destinations)
{
    EDestinationStore *store;
    ENameSelectorEntry *entry;
    GList *list, *iter;

    g_return_if_fail (E_IS_COMPOSER_NAME_HEADER (header));

    entry = E_COMPOSER_NAME_HEADER_GET_ENTRY (header);
    store = e_name_selector_entry_peek_destination_store (entry);

    /* Clear the destination store. */
    list = e_destination_store_list_destinations (store);
    for (iter = list; iter != NULL; iter = iter->next)
        e_destination_store_remove_destination (store, iter->data);
    g_list_free (list);

    e_composer_name_header_add_destinations (header, destinations);
}