aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/contact-editor/e-contact-editor-categories.c
blob: db85466e874f3be305e1c8de7b5d87d4d9713f12 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-contact-editor-categories.c
 * Copyright (C) 2000  Helix Code, Inc.
 * Author: Chris Lahey <clahey@helixcode.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <gnome.h>
#include <e-contact-editor-categories.h>
#include <gal/e-table/e-table-scrolled.h>
#include <gal/e-table/e-table.h>
#include <gal/e-table/e-table-simple.h>
#include <gal/widgets/e-unicode.h>

static void e_contact_editor_categories_init        (EContactEditorCategories        *card);
static void e_contact_editor_categories_class_init  (EContactEditorCategoriesClass   *klass);
static void e_contact_editor_categories_set_arg         (GtkObject *o, GtkArg *arg, guint arg_id);
static void e_contact_editor_categories_get_arg         (GtkObject *object, GtkArg *arg, guint arg_id);
static void e_contact_editor_categories_destroy         (GtkObject *object);
static int e_contact_editor_categories_col_count (ETableModel *etc, gpointer data);
static int e_contact_editor_categories_row_count (ETableModel *etc, gpointer data);
static void *e_contact_editor_categories_value_at (ETableModel *etc, int col, int row, gpointer data);
static void e_contact_editor_categories_set_value_at (ETableModel *etc, int col, int row, const void *val, gpointer data);
static gboolean e_contact_editor_categories_is_cell_editable (ETableModel *etc, int col, int row, gpointer data);
static void *e_contact_editor_categories_duplicate_value (ETableModel *etc, int col, const void *value, gpointer data);
static void e_contact_editor_categories_free_value (ETableModel *etc, int col, void *value, gpointer data);
static void *e_contact_editor_categories_initialize_value (ETableModel *etc, int col, gpointer data);
static gboolean e_contact_editor_categories_value_is_empty (ETableModel *etc, int col, const void *value, gpointer data);
static char * e_contact_editor_categories_value_to_string (ETableModel *etc, int col, const void *value, gpointer data);

static GnomeDialogClass *parent_class = NULL;

/* The arguments we take */
enum {
    ARG_0,
    ARG_CATEGORIES
};

GtkType
e_contact_editor_categories_get_type (void)
{
    static GtkType contact_editor_categories_type = 0;

    if (!contact_editor_categories_type)
        {
            static const GtkTypeInfo contact_editor_categories_info =
            {
                "EContactEditorCategories",
                sizeof (EContactEditorCategories),
                sizeof (EContactEditorCategoriesClass),
                (GtkClassInitFunc) e_contact_editor_categories_class_init,
                (GtkObjectInitFunc) e_contact_editor_categories_init,
                /* reserved_1 */ NULL,
                /* reserved_2 */ NULL,
                (GtkClassInitFunc) NULL,
            };

            contact_editor_categories_type = gtk_type_unique (gnome_dialog_get_type (), &contact_editor_categories_info);
        }

    return contact_editor_categories_type;
}

static void
e_contact_editor_categories_class_init (EContactEditorCategoriesClass *klass)
{
    GtkObjectClass *object_class;
    GnomeDialogClass *dialog_class;

    object_class = (GtkObjectClass*) klass;
    dialog_class = (GnomeDialogClass *) klass;

    parent_class = gtk_type_class (gnome_dialog_get_type ());

    gtk_object_add_arg_type ("EContactEditorCategories::categories", GTK_TYPE_STRING,
                 GTK_ARG_READWRITE, ARG_CATEGORIES);
 
    object_class->set_arg = e_contact_editor_categories_set_arg;
    object_class->get_arg = e_contact_editor_categories_get_arg;
    object_class->destroy = e_contact_editor_categories_destroy;
}

gchar *builtin_categories[] = {
    "Business",
    "Competition",
    "Favorites",
    "Gifts",
    "Goals/Objectives",
    "Holiday",
    "Holiday Cards",
    "Hot Contacts",
    "Ideas",
    "International",
    "Key Customer",
    "Miscellaneous",
    "Personal",
    "Phone Calls",
    "Status",
    "Strategies",
    "Suppliers",
    "Time & Expenses",
    "VIP",
    "Waiting",
};

#define BUILTIN_CATEGORY_COUNT (sizeof(builtin_categories) / sizeof(builtin_categories[0]))

static void
add_list_unique(EContactEditorCategories *categories, char *string)
{
    int k;
    char *temp = e_strdup_strip(string);
    char **list = categories->category_list;

    if (!*temp) {
        g_free(temp);
        return;
    }
    for (k = 0; k < categories->list_length; k++) {
        if (!strcmp(list[k], temp)) {
            categories->selected_list[k] = TRUE;
            break;
        }
    }
    if (k == categories->list_length) {
        categories->selected_list[categories->list_length] = TRUE;
        list[categories->list_length++] = temp;
    } else {
        g_free(temp);
    }
}

static void
do_parse_categories(EContactEditorCategories *categories)
{
    char *str = categories->categories;
    int length = strlen(str);
    char *copy = g_new(char, length + 1);
    int i, j;
    char **list;
    int count = 1;

    e_table_model_pre_change(categories->model);

    for (i = 0; i < categories->list_length; i++)
        g_free(categories->category_list[i]);
    g_free(categories->category_list);
    g_free(categories->selected_list);

    for (i = 0; str[i]; i++) {
        switch (str[i]) {
        case '\\':
            i++;
            if (!str[i])
                i--;
            break;
        case ',':
            count ++;
            break;
        }
    }
    list = g_new(char *, count + 1 + BUILTIN_CATEGORY_COUNT);
    categories->category_list = list;

    categories->selected_list = g_new(gboolean, count + 1 + BUILTIN_CATEGORY_COUNT);

    for (count = 0; count < BUILTIN_CATEGORY_COUNT; count++) {
        list[count] = g_strdup(builtin_categories[count]);
        categories->selected_list[count] = 0;
    }

    categories->list_length = count;

    for (i = 0, j = 0; str[i]; i++, j++) {
        switch (str[i]) {
        case '\\':
            i++;
            if (str[i]) {
                copy[j] = str[i];
            } else
                i--;
            break;
        case ',':
            copy[j] = 0;
            add_list_unique(categories, copy);
            j = -1;
            break;
        default:
            copy[j] = str[i];
            break;
        }
    }
    copy[j] = 0;
    add_list_unique(categories, copy);
    g_free(copy);
    e_table_model_changed(categories->model);
}

static void
e_contact_editor_categories_entry_change (GtkWidget *entry, 
                      EContactEditorCategories *categories)
{
    g_free(categories->categories);
    categories->categories = e_utf8_gtk_entry_get_text(GTK_ENTRY(entry));
    do_parse_categories(categories);
}

#define INITIAL_SPEC "<ETableSpecification no-header=\"1\" draw-grid=\"true\">\
  <ETableColumn model_col=\"0\" _title=\" \" expansion=\"0.0\" minimum_width=\"20\" resizable=\"false\" cell=\"checkbox\"       compare=\"integer\"/> \
  <ETableColumn model_col=\"1\" _title=\"Category\" expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
        <ETableState> \
        <column> 0 </column>                       \
        <column> 1 </column>                       \
            <grouping> <leaf column=\"1\" ascending=\"1\"/> </grouping>    \
        </ETableState> \
</ETableSpecification>"

static void
e_contact_editor_categories_init (EContactEditorCategories *categories)
{
    GladeXML *gui;
    GtkWidget *table;
    GtkWidget *e_table;

    categories->list_length = 0;
    categories->category_list = NULL;
    categories->selected_list = NULL;
    categories->categories = NULL;

    gnome_dialog_append_button ( GNOME_DIALOG(categories),
                     GNOME_STOCK_BUTTON_OK);
    
    gnome_dialog_append_button ( GNOME_DIALOG(categories),
                     GNOME_STOCK_BUTTON_CANCEL);

    gtk_window_set_policy(GTK_WINDOW(categories), FALSE, TRUE, FALSE);

    gui = glade_xml_new (EVOLUTION_GLADEDIR "/categories.glade", NULL);
    categories->gui = gui;

    table = glade_xml_get_widget(gui, "table-categories");
    gtk_widget_ref(table);
    gtk_container_remove(GTK_CONTAINER(table->parent), table);
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (categories)->vbox), table, TRUE, TRUE, 0);
    gtk_widget_unref(table);

    categories->entry = glade_xml_get_widget(gui, "entry-categories");
    
    gtk_signal_connect(GTK_OBJECT(categories->entry), "changed",
               GTK_SIGNAL_FUNC(e_contact_editor_categories_entry_change), categories);

    categories->model = e_table_simple_new(e_contact_editor_categories_col_count, 
                           e_contact_editor_categories_row_count,
                           e_contact_editor_categories_value_at,
                           e_contact_editor_categories_set_value_at,
                           e_contact_editor_categories_is_cell_editable,
                           e_contact_editor_categories_duplicate_value,
                           e_contact_editor_categories_free_value,
                           e_contact_editor_categories_initialize_value,
                           e_contact_editor_categories_value_is_empty,
                           e_contact_editor_categories_value_to_string,
                           categories);

    e_table = e_table_scrolled_new (categories->model, NULL, INITIAL_SPEC, NULL);

    gtk_object_sink(GTK_OBJECT(categories->model));
    
    gtk_widget_show(e_table);

    gtk_table_attach_defaults(GTK_TABLE(table),
                  e_table, 
                  0, 1,
                  3, 4);
}

void
e_contact_editor_categories_destroy (GtkObject *object)
{
    EContactEditorCategories *categories = E_CONTACT_EDITOR_CATEGORIES(object);
    int i;

    if (categories->gui)
        gtk_object_unref(GTK_OBJECT(categories->gui));

    g_free(categories->categories);
    for (i = 0; i < categories->list_length; i++)
        g_free(categories->category_list[i]);
    g_free(categories->category_list);
    g_free(categories->selected_list);
}

GtkWidget*
e_contact_editor_categories_new (char *categories)
{
    GtkWidget *widget = GTK_WIDGET (gtk_type_new (e_contact_editor_categories_get_type ()));
    gtk_object_set (GTK_OBJECT(widget),
            "categories", categories,
            NULL);
    return widget;
}

static void
e_contact_editor_categories_set_arg (GtkObject *o, GtkArg *arg, guint arg_id)
{
    EContactEditorCategories *e_contact_editor_categories;

    e_contact_editor_categories = E_CONTACT_EDITOR_CATEGORIES (o);
    
    switch (arg_id){
    case ARG_CATEGORIES:
        e_utf8_gtk_entry_set_text(GTK_ENTRY(e_contact_editor_categories->entry), GTK_VALUE_STRING (*arg));
        break;
    }
}

static void
e_contact_editor_categories_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    EContactEditorCategories *e_contact_editor_categories;

    e_contact_editor_categories = E_CONTACT_EDITOR_CATEGORIES (object);

    switch (arg_id) {
    case ARG_CATEGORIES:
        GTK_VALUE_STRING (*arg) = g_strdup(e_contact_editor_categories->categories);
        break;
    default:
        arg->type = GTK_TYPE_INVALID;
        break;
    }
}

/* This function returns the number of columns in our ETableModel. */
static int
e_contact_editor_categories_col_count (ETableModel *etc, gpointer data)
{
    return 2;
}

/* This function returns the number of rows in our ETableModel. */
static int
e_contact_editor_categories_row_count (ETableModel *etc, gpointer data)
{
    EContactEditorCategories *categories = E_CONTACT_EDITOR_CATEGORIES(data);
    return categories->list_length;
}

/* This function returns the value at a particular point in our ETableModel. */
static void *
e_contact_editor_categories_value_at (ETableModel *etc, int col, int row, gpointer data)
{
    EContactEditorCategories *categories = E_CONTACT_EDITOR_CATEGORIES(data);
    if (col == 0)
        return (void *) categories->selected_list[row];
    else
        return categories->category_list[row];
}

/* This function sets the value at a particular point in our ETableModel. */
static void
e_contact_editor_categories_set_value_at (ETableModel *etc, int col, int row, const void *val, gpointer data)
{
    EContactEditorCategories *categories = E_CONTACT_EDITOR_CATEGORIES(data);
    if ( col == 0 ) {
        char **strs;
        int i, j;
        char *string;
        categories->selected_list[row] = (gboolean) val;
        strs = g_new(char *, categories->list_length + 1);
        for (i = 0, j = 0; i < categories->list_length; i++) {
            if (categories->selected_list[i])
                strs[j++] = categories->category_list[i];
        }
        strs[j] = 0;
        string = g_strjoinv(", ", strs);
        e_utf8_gtk_entry_set_text(GTK_ENTRY(categories->entry), string);
        g_free(string);
        g_free(strs);
    }
    if ( col == 1 )
        return;
}

/* This function returns whether a particular cell is editable. */
static gboolean
e_contact_editor_categories_is_cell_editable (ETableModel *etc, int col, int row, gpointer data)
{
    return col == 0;
}

/* This function duplicates the value passed to it. */
static void *
e_contact_editor_categories_duplicate_value (ETableModel *etc, int col, const void *value, gpointer data)
{
    if (col == 0)
        return (void *)value;
    else
        return g_strdup(value);
}

/* This function frees the value passed to it. */
static void
e_contact_editor_categories_free_value (ETableModel *etc, int col, void *value, gpointer data)
{
    if (col == 0)
        return;
    else
        g_free(value);
}

static void *
e_contact_editor_categories_initialize_value (ETableModel *etc, int col, gpointer data)
{
    if (col == 0)
        return NULL;
    else
        return g_strdup("");
}

static gboolean
e_contact_editor_categories_value_is_empty (ETableModel *etc, int col, const void *value, gpointer data)
{
    if (col == 0)
        return value == NULL;
    else
        return !(value && *(char *)value);
}

static char *
e_contact_editor_categories_value_to_string (ETableModel *etc, int col, const void *value, gpointer data)
{
    if (col == 0)
        return g_strdup_printf("%d", (int) value);
    else
        return g_strdup(value);
}