aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/select-names/e-select-names-manager.c
blob: 51a4d889c1c9d5d5c0c39db5886a54381ddb4ff0 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Authors: 
 *   Chris Lahey     <clahey@helixcode.com>
 *
 * Copyright (C) 2000 Helix Code, Inc.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

#include "e-select-names-manager.h"
#include "e-select-names-model.h"
#include "e-select-names-text-model.h"
#include "e-select-names.h"
#include "widgets/e-text/e-entry.h"

/* Object argument IDs */
enum {
    ARG_0,
    ARG_CARD,
};


typedef struct {
    char *id;
    char *title;
    ESelectNamesModel *model;
} ESelectNamesManagerSection;

static void e_select_names_manager_init (ESelectNamesManager *manager);
static void e_select_names_manager_class_init (ESelectNamesManagerClass *klass);

static void e_select_names_manager_destroy (GtkObject *object);
static void e_select_names_manager_set_arg (GtkObject *object, GtkArg *arg, guint arg_id);
static void e_select_names_manager_get_arg (GtkObject *object, GtkArg *arg, guint arg_id);

/**
 * e_select_names_manager_get_type:
 * @void: 
 * 
 * Registers the &ESelectNamesManager class if necessary, and returns the type ID
 * associated to it.
 * 
 * Return value: The type ID of the &ESelectNamesManager class.
 **/
GtkType
e_select_names_manager_get_type (void)
{
    static GtkType manager_type = 0;

    if (!manager_type) {
        GtkTypeInfo manager_info = {
            "ESelectNamesManager",
            sizeof (ESelectNamesManager),
            sizeof (ESelectNamesManagerClass),
            (GtkClassInitFunc) e_select_names_manager_class_init,
            (GtkObjectInitFunc) e_select_names_manager_init,
            NULL, /* reserved_1 */
            NULL, /* reserved_2 */
            (GtkClassInitFunc) NULL
        };

        manager_type = gtk_type_unique (gtk_object_get_type (), &manager_info);
    }

    return manager_type;
}

/**
 * e_select_names_manager_new:
 * @VCard: a string in vCard format
 *
 * Returns: a new #ESelectNamesManager that wraps the @VCard.
 */
ESelectNamesManager *
e_select_names_manager_new (void)
{
    ESelectNamesManager *manager = E_SELECT_NAMES_MANAGER(gtk_type_new(e_select_names_manager_get_type()));
    return manager;
}

static void
e_select_names_manager_class_init (ESelectNamesManagerClass *klass)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS(klass);

    gtk_object_add_arg_type ("ESelectNamesManager::card",
                 GTK_TYPE_OBJECT, GTK_ARG_READWRITE, ARG_CARD);

    object_class->destroy = e_select_names_manager_destroy;
    object_class->get_arg = e_select_names_manager_get_arg;
    object_class->set_arg = e_select_names_manager_set_arg;
}

/*
 * ESelectNamesManager lifecycle management and vcard loading/saving.
 */

static void
e_select_names_manager_destroy (GtkObject *object)
{
    ESelectNamesManager *manager;
    
    manager = E_SELECT_NAMES_MANAGER (object);

    gtk_object_unref(GTK_OBJECT(manager->sections));
}


/* Set_arg handler for the manager */
static void
e_select_names_manager_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESelectNamesManager *manager;
    
    manager = E_SELECT_NAMES_MANAGER (object);

    switch (arg_id) {
    case ARG_CARD:
        break;
    default:
        return;
    }
}

/* Get_arg handler for the manager */
static void
e_select_names_manager_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESelectNamesManager *manager;

    manager = E_SELECT_NAMES_MANAGER (object);

    switch (arg_id) {
    case ARG_CARD:
        break;
    default:
        arg->type = GTK_TYPE_INVALID;
        break;
    }
}

static void *
section_copy(const void *sec, void *data)
{
    const ESelectNamesManagerSection *section = sec;
    ESelectNamesManagerSection *newsec;
    
    newsec = g_new(ESelectNamesManagerSection, 1);
    newsec->id = g_strdup(section->id);
    newsec->title = g_strdup(section->title);
    newsec->model = section->model;
    if (newsec->model)
        gtk_object_ref(GTK_OBJECT(newsec->model));
    return newsec;
}

static void
section_free(void *sec, void *data)
{
    ESelectNamesManagerSection *section = sec;
    g_free(section->id);
    g_free(section->title);
    if (section->model)
        gtk_object_unref(GTK_OBJECT(section->model));
    g_free(section);
}

/**
 * e_select_names_manager_init:
 */
static void
e_select_names_manager_init (ESelectNamesManager *manager)
{
    manager->sections = e_list_new(section_copy, section_free, manager);
}

void                          e_select_names_manager_add_section               (ESelectNamesManager *manager,
                                        char *id,
                                        char *title)
{
    ESelectNamesManagerSection *section;
    
    section = g_new(ESelectNamesManagerSection, 1);
    section->id = g_strdup(id);
    section->title = g_strdup(title);
    section->model = e_select_names_model_new();
    e_list_append(manager->sections, section);
    section_free(section, manager);
}

GtkWidget                    *e_select_names_manager_create_entry              (ESelectNamesManager *manager,
                                        char *id)
{
    GtkWidget *entry;
    ETextModel *model;
    EIterator *iterator;
    iterator = e_list_get_iterator(manager->sections);
    for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
        const ESelectNamesManagerSection *section = e_iterator_get(iterator);
        if (!strcmp(section->id, id)) {
            entry = GTK_WIDGET(e_entry_new());
            model = e_select_names_text_model_new(section->model);
            gtk_object_set(GTK_OBJECT(entry),
                       "model", model,
                       "editable", TRUE,
                       NULL);
            return entry;
        }
    }
    return NULL;
}

void                          e_select_names_manager_activate_dialog           (ESelectNamesManager *manager,
                                        char *id)
{
    ESelectNames *names = E_SELECT_NAMES(e_select_names_new());
    EIterator *iterator;
    iterator = e_list_get_iterator(manager->sections);
    for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
        const ESelectNamesManagerSection *section = e_iterator_get(iterator);
        e_select_names_add_section(names, section->id, section->title, section->model);
    }
    gtk_widget_show(GTK_WIDGET(names));
}

/* Of type ECard */
EList                    *e_select_names_manager_get_cards                 (ESelectNamesManager *manager,
                                        char *id)
{
    EIterator *iterator;
    iterator = e_list_get_iterator(manager->sections);
    for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
        const ESelectNamesManagerSection *section = e_iterator_get(iterator);
        if (!strcmp(section->id, id)) {
            return e_select_names_model_get_cards(section->model);
        }
    }
    return NULL;
}