aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/dialogs/comp-editor.c
blob: ddf9f9a9aeeedc924ba77072bf1bb039fba48a82 (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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* Evolution calendar - Framework for a calendar component editor dialog
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

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

#include <gnome.h>
#include <gal/widgets/e-unicode.h>
#include "save-comp.h"
#include "comp-editor.h"



/* Private part of the CompEditor structure */
struct _CompEditorPrivate {
    /* Client to use */
    CalClient *client;

    /* Calendar object/uid we are editing; this is an internal copy */
    CalComponent *comp;

    /* The pages we have */
    GList *pages;

    /* Toplevel window for the dialog */
    GtkWidget *window;

    /* Notebook to hold the pages */
    GtkNotebook *notebook;

    gboolean changed;
};



static void comp_editor_class_init (CompEditorClass *class);
static void comp_editor_init (CompEditor *editor);
static void comp_editor_destroy (GtkObject *object);

static void page_summary_changed_cb (GtkWidget *widget, const char *summary, gpointer data);
static void page_dates_changed_cb (GtkWidget *widget, CompEditorPageDates *dates, gpointer data);
static void page_changed_cb (GtkWidget *widget, gpointer data);

static void save_clicked_cb (GtkWidget *widget, gpointer data);
static void close_clicked_cb (GtkWidget *widget, gpointer data);
static void help_clicked_cb (GtkWidget *widget, gpointer data);
static gint delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data);

static GtkObjectClass *parent_class;



GtkType
comp_editor_get_type (void)
{
    static GtkType comp_editor_type = 0;

    if (!comp_editor_type) {
        GtkTypeInfo comp_editor_info = {
            "CompEditor",
            sizeof (CompEditor),
            sizeof (CompEditorClass),
            (GtkClassInitFunc) comp_editor_class_init,
            (GtkObjectInitFunc) comp_editor_init,
            NULL, /* reserved_1 */
            NULL, /* reserved_2 */
            (GtkClassInitFunc) NULL
        };

        comp_editor_type = gtk_type_unique (GTK_TYPE_OBJECT,
                            &comp_editor_info); 
    }

    return comp_editor_type;
}

/* Class initialization function for the calendar component editor */
static void
comp_editor_class_init (CompEditorClass *class)
{
    GtkObjectClass *object_class;

    object_class = (GtkObjectClass *) class;

    parent_class = gtk_type_class (GTK_TYPE_OBJECT);

    object_class->destroy = comp_editor_destroy;
}

/* Creates the basic in the editor */
static void
setup_widgets (CompEditor *editor)
{
    CompEditorPrivate *priv;
    GtkWidget *vbox;
    GtkWidget *bbox;
    GtkWidget *pixmap;
    GtkWidget *button;

    priv = editor->priv;

    /* Window and basic vbox */

    priv->window = gtk_window_new (GTK_WINDOW_DIALOG);
    gtk_signal_connect (GTK_OBJECT (priv->window), "delete_event",
                GTK_SIGNAL_FUNC (delete_event_cb), editor);

    vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
    gtk_container_set_border_width (GTK_CONTAINER (vbox), GNOME_PAD_SMALL);
    gtk_container_add (GTK_CONTAINER (priv->window), vbox);

    /* Notebook */

    priv->notebook = GTK_NOTEBOOK (gtk_notebook_new ());
    gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->notebook),
                TRUE, TRUE, 0);

    /* Buttons */

    bbox = gtk_hbutton_box_new ();
    gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
    gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);

    pixmap = gnome_stock_pixmap_widget (NULL, GNOME_STOCK_PIXMAP_SAVE);
    button = gnome_pixmap_button (pixmap, _("Save"));
    gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
                GTK_SIGNAL_FUNC (save_clicked_cb), editor);
    
    button = gnome_stock_button (GNOME_STOCK_BUTTON_CLOSE);
    gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
                GTK_SIGNAL_FUNC (close_clicked_cb), editor);

    button = gnome_stock_button (GNOME_STOCK_BUTTON_HELP);
    gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
                GTK_SIGNAL_FUNC (help_clicked_cb), editor);
}

/* Object initialization function for the calendar component editor */
static void
comp_editor_init (CompEditor *editor)
{
    CompEditorPrivate *priv;

    priv = g_new0 (CompEditorPrivate, 1);
    editor->priv = priv;

    setup_widgets (editor);

    priv->pages = NULL;
    priv->changed = FALSE;
}

/* Destroy handler for the calendar component editor */
static void
comp_editor_destroy (GtkObject *object)
{
    CompEditor *editor;
    CompEditorPrivate *priv;

    editor = COMP_EDITOR (object);
    priv = editor->priv;

    if (priv->window) {
        gtk_widget_destroy (priv->window);
        priv->window = NULL;
    }

    g_free (priv);
    editor->priv = NULL;

    if (GTK_OBJECT_CLASS (parent_class)->destroy)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}



/**
 * comp_editor_append_page:
 * @editor: A component editor
 * @page: Top level widget of the page
 * @label: Label of the page
 * 
 * Appends a page to the editor notebook with the given label
 **/
void
comp_editor_append_page (CompEditor *editor,
             CompEditorPage *page,
             const char *label)
{
    CompEditorPrivate *priv;
    GtkWidget *page_widget;
    GtkWidget *label_widget;

    g_return_if_fail (editor != NULL);
    g_return_if_fail (IS_COMP_EDITOR (editor));
    g_return_if_fail (page != NULL);
    g_return_if_fail (IS_COMP_EDITOR_PAGE (page));
    g_return_if_fail (label != NULL);

    priv = editor->priv;

    /* Only allow adding the pages while a component has not been set */
    g_return_if_fail (priv->comp == NULL);

    page_widget = comp_editor_page_get_widget (page);
    g_assert (page_widget != NULL);
    
    label_widget = gtk_label_new (label);

    priv->pages = g_list_append (priv->pages, page);
    gtk_notebook_append_page (priv->notebook, page_widget, label_widget);

    /* Listen for things happening on the page */
    gtk_signal_connect (GTK_OBJECT (page), "summary_changed",
                GTK_SIGNAL_FUNC (page_summary_changed_cb), editor);
    gtk_signal_connect (GTK_OBJECT (page), "dates_changed",
                GTK_SIGNAL_FUNC (page_dates_changed_cb), editor);
    gtk_signal_connect (GTK_OBJECT (page), "changed",
                GTK_SIGNAL_FUNC (page_changed_cb), editor);
}

/**
 * comp_editor_set_cal_client:
 * @editor: A component editor
 * @client: The calendar client to use
 * 
 * Sets the calendar client used by the editor to update components
 **/
void
comp_editor_set_cal_client (CompEditor *editor, CalClient *client)
{
    CompEditorPrivate *priv;

    g_return_if_fail (editor != NULL);
    g_return_if_fail (IS_COMP_EDITOR (editor));

    priv = editor->priv;

    if (client == priv->client)
        return;

    if (client) {
        g_return_if_fail (IS_CAL_CLIENT (client));
        g_return_if_fail (cal_client_get_load_state (client) ==
                  CAL_CLIENT_LOAD_LOADED);
        gtk_object_ref (GTK_OBJECT (client));
    }
    
    if (priv->client) {
        gtk_signal_disconnect_by_data (GTK_OBJECT (priv->client), 
                           editor);
        gtk_object_unref (GTK_OBJECT (priv->client));
    }

    priv->client = client;
}

/**
 * comp_editor_get_cal_client:
 * @editor: A component editor
 * 
 * Returns the calendar client of the editor
 * 
 * Return value: The calendar client of the editor
 **/
CalClient *
comp_editor_get_cal_client (CompEditor *editor)
{
    CompEditorPrivate *priv;

    g_return_val_if_fail (editor != NULL, NULL);
    g_return_val_if_fail (IS_COMP_EDITOR (editor), NULL);

    priv = editor->priv;

    return priv->client;
}

/* Creates an appropriate title for the event editor dialog */
static char *
make_title_from_comp (CalComponent *comp)
{
    char *title;
    const char *type_string;
    CalComponentVType type;
    CalComponentText text;

    if (!comp)
        return g_strdup (_("Edit Appointment"));

    type = cal_component_get_vtype (comp);
    switch (type) {
    case CAL_COMPONENT_EVENT:
        type_string = _("Appointment - %s");
        break;
    case CAL_COMPONENT_TODO:
        type_string = _("Task - %s");
        break;
    case CAL_COMPONENT_JOURNAL:
        type_string = _("Journal entry - %s");
        break;
    default:
        g_message ("make_title_from_comp(): Cannot handle object of type %d", type);
        return NULL;
    }

    cal_component_get_summary (comp, &text);
    if (text.value) {
        char *summary;
        summary = e_utf8_to_locale_string (text.value);
        title = g_strdup_printf (type_string, summary);
        g_free (summary);
    } else
        title = g_strdup_printf (type_string, _("No summary"));

    return title;
}

/* Sets the event editor's window title from a calendar component */
static void
set_title_from_comp (CompEditor *editor)
{
    CompEditorPrivate *priv;
    char *title;

    priv = editor->priv;
    title = make_title_from_comp (priv->comp);
    gtk_window_set_title (GTK_WINDOW (priv->window), title);
    g_free (title);
}

static void
fill_widgets (CompEditor *editor) 
{
    CompEditorPrivate *priv;
    GList *l;
    
    priv = editor->priv;
    
    for (l = priv->pages; l != NULL; l = l->next)
        comp_editor_page_fill_widgets (l->data, priv->comp);
}

/**
 * comp_editor_edit_comp:
 * @editor: A component editor
 * @comp: A calendar component
 * 
 * Starts the editor editing the given component
 **/
void
comp_editor_edit_comp (CompEditor *editor, CalComponent *comp)
{
    CompEditorPrivate *priv;

    g_return_if_fail (editor != NULL);
    g_return_if_fail (IS_COMP_EDITOR (editor));

    priv = editor->priv;

    if (priv->comp) {
        gtk_object_unref (GTK_OBJECT (priv->comp));
        priv->comp = NULL;
    }

    if (comp)
        priv->comp = cal_component_clone (comp);

    set_title_from_comp (editor);
    fill_widgets (editor);
}


/* Brings attention to a window by raising it and giving it focus */
static void
raise_and_focus (GtkWidget *widget)
{
    g_assert (GTK_WIDGET_REALIZED (widget));
    gdk_window_show (widget->window);
    gtk_widget_grab_focus (widget);
}

/**
 * comp_editor_focus:
 * @editor: A component editor
 * 
 * Brings the editor window to the front and gives it focus
 **/
void
comp_editor_focus (CompEditor *editor)
{
    CompEditorPrivate *priv;

    g_return_if_fail (editor != NULL);
    g_return_if_fail (IS_COMP_EDITOR (editor));

    priv = editor->priv;

    gtk_widget_show_all (priv->window);
    raise_and_focus (priv->window);
}

static void
save_comp (CompEditor *editor) 
{
    CompEditorPrivate *priv;
    GList *l;
    
    priv = editor->priv;
    
    for (l = priv->pages; l != NULL; l = l->next)
        comp_editor_page_fill_component (l->data, priv->comp);

    if (!cal_client_update_object (priv->client, priv->comp))
        g_message ("save_comp (): Could not update the object!");
    else
        priv->changed = FALSE;
}

static gboolean
prompt_to_save_changes (CompEditor *editor)
{
    CompEditorPrivate *priv;

    priv = editor->priv;

    if (!priv->changed)
        return TRUE;

    switch (save_component_dialog (GTK_WINDOW (priv->window))) {
    case 0: /* Save */
        /* FIXME: If an error occurs here, we should popup a dialog
           and then return FALSE. */
        save_comp (editor);
        return TRUE;
    case 1: /* Discard */
        return TRUE;
    case 2: /* Cancel */
    default:
        return FALSE;
        break;
    }
}

/* Closes the dialog box and emits the appropriate signals */
static void
close_dialog (CompEditor *editor)
{
    CompEditorPrivate *priv;

    priv = editor->priv;

    g_assert (priv->window != NULL);

    gtk_object_destroy (GTK_OBJECT (editor));
}

static void
save_clicked_cb (GtkWidget *widget, gpointer data)
{
    CompEditor *editor = COMP_EDITOR (data);

    save_comp (editor);
    close_dialog (editor);
}

static void
close_clicked_cb (GtkWidget *widget, gpointer data)
{
    CompEditor *editor = COMP_EDITOR (data);

    if (prompt_to_save_changes (editor))
        close_dialog (editor);
}

static void
help_clicked_cb (GtkWidget *widget, gpointer data)
{
}

static void
page_summary_changed_cb (GtkWidget *widget, const char *summary, gpointer data)
{
    CompEditor *editor = COMP_EDITOR (data);
    CompEditorPrivate *priv;
    GList *l;
    
    priv = editor->priv;
    
    for (l = priv->pages; l != NULL; l = l->next)
        comp_editor_page_set_summary (l->data, summary);
    
    priv->changed = TRUE;
}

static void
page_dates_changed_cb (GtkWidget *widget,
               CompEditorPageDates *dates,
               gpointer data)
{
    CompEditor *editor = COMP_EDITOR (data);
    CompEditorPrivate *priv;
    GList *l;
    
    priv = editor->priv;

    for (l = priv->pages; l != NULL; l = l->next)
        comp_editor_page_set_dates (l->data, dates);

    priv->changed = TRUE;
}


static void
page_changed_cb (GtkWidget *widget, gpointer data)
{
    CompEditor *editor = COMP_EDITOR (data);
    CompEditorPrivate *priv;
    
    priv = editor->priv;
    
    priv->changed = TRUE;
}

static gint
delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
{
    CompEditor *editor = COMP_EDITOR (data);

    if (prompt_to_save_changes (editor))
        close_dialog (editor);

    return TRUE;
}