aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/save-calendar/save-calendar.c
blob: b7872a68d483da478717732a801179016b030cb9 (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
/*
 * 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:
 *      Rodrigo Moya <rodrigo@novell.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

/* This is prototype code only, this may, or may not, use undocumented
 * unstable or private internal function calls. */

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

#include <gio/gio.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libedataserver/e-source.h>
#include <libedataserverui/e-source-selector.h>
#include <libecal/e-cal.h>
#include <e-util/e-alert-dialog.h>
#include <e-util/e-plugin.h>
#include <string.h>

#include <shell/e-shell-sidebar.h>
#include <shell/e-shell-view.h>
#include <shell/e-shell-window.h>

#include "format-handler.h"

/* Plugin entry points */
gboolean    calendar_save_as_init       (GtkUIManager *ui_manager,
                         EShellView *shell_view);
gboolean    memo_list_save_as_init      (GtkUIManager *ui_manager,
                         EShellView *shell_view);
gboolean    task_list_save_as_init      (GtkUIManager *ui_manager,
                         EShellView *shell_view);

gint e_plugin_lib_enable (EPlugin *ep, gint enable);

gint
e_plugin_lib_enable (EPlugin *ep, gint enable)
{
    return 0;
}

enum {  /* GtkComboBox enum */
    DEST_NAME_COLUMN,
    DEST_HANDLER,
    N_DEST_COLUMNS

};

static void
extra_widget_foreach_hide (GtkWidget *widget, gpointer data)
{
    if (widget != data)
        gtk_widget_hide (widget);
}

static void
on_type_combobox_changed (GtkComboBox *combobox, gpointer data)
{
    FormatHandler *handler = NULL;
    GtkWidget *extra_widget = data;
    GtkTreeIter iter;
    GtkTreeModel *model = gtk_combo_box_get_model (combobox);

    gtk_container_foreach (GTK_CONTAINER (extra_widget),
        extra_widget_foreach_hide, g_object_get_data (G_OBJECT (combobox), "format-box"));

    gtk_combo_box_get_active_iter (combobox, &iter);

    gtk_tree_model_get (model, &iter,
        DEST_HANDLER, &handler, -1);

    if (handler->options_widget)
    {
        gtk_widget_show (handler->options_widget);
    }

}

static void
format_handlers_foreach_free (gpointer data, gpointer user_data)
{
    FormatHandler *handler = data;

    if (handler->options_widget)
        gtk_widget_destroy (handler->options_widget);

    if (handler->data)
        g_free (handler->data);

    g_free (data);
}

static void
ask_destination_and_save (ESourceSelector *selector, ECalSourceType type)
{
    FormatHandler *handler = NULL;

    GtkWidget *extra_widget = gtk_vbox_new (FALSE, 0);
    GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
    GtkLabel *label = GTK_LABEL (gtk_label_new_with_mnemonic (_("_Format:")));
    GtkComboBox *combo = GTK_COMBO_BOX(gtk_combo_box_new ());
    GtkTreeModel *model = GTK_TREE_MODEL (gtk_list_store_new
        (N_DEST_COLUMNS, G_TYPE_STRING, G_TYPE_POINTER));
    GtkCellRenderer *renderer=NULL;
    GtkListStore *store = GTK_LIST_STORE (model);
    GtkTreeIter iter;
    GtkWidget *dialog = NULL;
    gchar *dest_uri = NULL;

    GList *format_handlers = NULL;

    /* The available formathandlers */
    format_handlers = g_list_append (format_handlers,
        ical_format_handler_new ());
    format_handlers = g_list_append (format_handlers,
        csv_format_handler_new ());
    format_handlers = g_list_append (format_handlers,
        rdf_format_handler_new ());

    gtk_box_pack_start (GTK_BOX (extra_widget), GTK_WIDGET (hbox), FALSE, FALSE, 0);
    gtk_label_set_mnemonic_widget (label, GTK_WIDGET (combo));

    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (label), FALSE, FALSE, 0);

    /* The Type GtkComboBox */
    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (combo), TRUE, TRUE, 0);
    gtk_combo_box_set_model (combo, model);

    gtk_list_store_clear (store);
    renderer = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
    gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
            renderer, "text", DEST_NAME_COLUMN, NULL);

    while (format_handlers) {
        handler = format_handlers->data;
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, DEST_NAME_COLUMN,
            handler->combo_label, -1);
        gtk_list_store_set (store, &iter, DEST_HANDLER, handler, -1);

        if (handler->options_widget) {
            gtk_box_pack_start (GTK_BOX (extra_widget),
                GTK_WIDGET (handler->options_widget), TRUE, TRUE, 0);
            gtk_widget_hide (handler->options_widget);
        }

        if (handler->isdefault) {
            gtk_combo_box_set_active_iter (combo, &iter);
            if (handler->options_widget)
                gtk_widget_show (handler->options_widget);
        }

        format_handlers = g_list_next (format_handlers);
    }

    g_signal_connect (G_OBJECT(combo), "changed",
        G_CALLBACK (on_type_combobox_changed), extra_widget);
    g_object_set_data (G_OBJECT (combo), "format-box", hbox);

    dialog = gtk_file_chooser_dialog_new (_("Select destination file"),
                          NULL,
                          GTK_FILE_CHOOSER_ACTION_SAVE,
                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                          GTK_STOCK_SAVE_AS, GTK_RESPONSE_OK,
                          NULL);

    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
    gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), extra_widget);
    gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), FALSE);
    gtk_widget_show (hbox);
    gtk_widget_show (GTK_WIDGET (label));
    gtk_widget_show (GTK_WIDGET (combo));
    gtk_widget_show (extra_widget);

    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
        gchar *tmp = NULL;

        gtk_combo_box_get_active_iter (combo, &iter);
        gtk_tree_model_get (model, &iter,
            DEST_HANDLER, &handler, -1);

           dest_uri = gtk_file_chooser_get_uri
            (GTK_FILE_CHOOSER (dialog));

        tmp = strstr (dest_uri, handler->filename_ext);

        if (!(tmp && *(tmp + strlen (handler->filename_ext)) == '\0')) {

            gchar *temp;
            temp = g_strconcat (dest_uri, handler->filename_ext, NULL);
            g_free (dest_uri);
            dest_uri = temp;
        }

        handler->save (handler, selector, type, dest_uri);
    }

    /* Free the handlers */
    g_list_foreach (format_handlers, format_handlers_foreach_free, NULL);
    g_list_free (format_handlers);

    /* Now we can destroy it */
    gtk_widget_destroy (dialog);
    g_free (dest_uri);

}

/* Returns output stream for the uri, or NULL on any error.
   When done with the stream, just g_output_stream_close and g_object_unref it.
   It will ask for overwrite if file already exists.
*/
GOutputStream *
open_for_writing (GtkWindow *parent, const gchar *uri, GError **error)
{
    GFile *file;
    GFileOutputStream *fostream;
    GError *err = NULL;

    g_return_val_if_fail (uri != NULL, NULL);

    file = g_file_new_for_uri (uri);

    g_return_val_if_fail (file != NULL, NULL);

    fostream = g_file_create (file, G_FILE_CREATE_NONE, NULL, &err);

    if (err && err->code == G_IO_ERROR_EXISTS) {
        gint response;
        g_clear_error (&err);

        response = e_alert_run_dialog_for_args (
            parent, E_ALERT_ASK_FILE_EXISTS_OVERWRITE,
            uri, NULL);
        if (response == GTK_RESPONSE_OK) {
            fostream = g_file_replace (
                file, NULL, FALSE, G_FILE_CREATE_NONE,
                NULL, &err);

            if (err && fostream) {
                g_object_unref (fostream);
                fostream = NULL;
            }
        } else if (fostream) {
            g_object_unref (fostream);
            fostream = NULL;
        }
    }

    g_object_unref (file);

    if (error && err)
        *error = err;
    else if (err)
        g_error_free (err);

    if (fostream)
        return G_OUTPUT_STREAM (fostream);

    return NULL;
}

static void
save_general (EShellView *shell_view,
              ECalSourceType type)
{
    EShellSidebar *shell_sidebar;
    ESourceSelector *selector = NULL;

    shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);
    g_object_get (shell_sidebar, "selector", &selector, NULL);
    g_return_if_fail (selector != NULL);

    ask_destination_and_save (selector, type);

    g_object_unref (selector);
}

static void
action_calendar_save_as_cb (GtkAction *action,
                            EShellView *shell_view)
{
    save_general (shell_view, E_CAL_SOURCE_TYPE_EVENT);
}

static void
action_memo_list_save_as_cb (GtkAction *action,
                             EShellView *shell_view)
{
    save_general (shell_view, E_CAL_SOURCE_TYPE_JOURNAL);
}

static void
action_task_list_save_as_cb (GtkAction *action,
                             EShellView *shell_view)
{
    save_general (shell_view, E_CAL_SOURCE_TYPE_TODO);
}

gboolean
calendar_save_as_init (GtkUIManager *ui_manager,
                       EShellView *shell_view)
{
    EShellWindow *shell_window;
    GtkActionGroup *action_group;
    GtkAction *action;
    const gchar *tooltip;
    const gchar *stock_id;
    const gchar *name;

    shell_window = e_shell_view_get_shell_window (shell_view);

    name = "calendar-save-as";
    tooltip = _("Save the selected calendar to disk");
    stock_id = GTK_STOCK_SAVE_AS;
    action = gtk_action_new (name, NULL, tooltip, stock_id);

    name = "lockdown-save-to-disk";
    action_group = e_shell_window_get_action_group (shell_window, name);
    gtk_action_group_add_action (action_group, action);

    g_signal_connect (
        action, "activate",
        G_CALLBACK (action_calendar_save_as_cb), shell_view);

    g_object_unref (action);

    return TRUE;
}

gboolean
memo_list_save_as_init (GtkUIManager *ui_manager,
                        EShellView *shell_view)
{
    EShellWindow *shell_window;
    GtkActionGroup *action_group;
    GtkAction *action;
    const gchar *tooltip;
    const gchar *stock_id;
    const gchar *name;

    shell_window = e_shell_view_get_shell_window (shell_view);

    name = "memo-list-save-as";
    tooltip = _("Save the selected memo list to disk");
    stock_id = GTK_STOCK_SAVE_AS;
    action = gtk_action_new (name, NULL, tooltip, stock_id);

    name = "lockdown-save-to-disk";
    action_group = e_shell_window_get_action_group (shell_window, name);
    gtk_action_group_add_action (action_group, action);

    g_signal_connect (
        action, "activate",
        G_CALLBACK (action_memo_list_save_as_cb), shell_view);

    g_object_unref (action);

    return TRUE;
}

gboolean
task_list_save_as_init (GtkUIManager *ui_manager,
                        EShellView *shell_view)
{
    EShellWindow *shell_window;
    GtkActionGroup *action_group;
    GtkAction *action;
    const gchar *tooltip;
    const gchar *stock_id;
    const gchar *name;

    shell_window = e_shell_view_get_shell_window (shell_view);

    name = "task-list-save-as";
    tooltip = _("Save the selected task list to disk");
    stock_id = GTK_STOCK_SAVE_AS;
    action = gtk_action_new (name, NULL, tooltip, stock_id);

    name = "lockdown-save-to-disk";
    action_group = e_shell_window_get_action_group (shell_window, name);
    gtk_action_group_add_action (action_group, action);

    g_signal_connect (
        action, "activate",
        G_CALLBACK (action_task_list_save_as_cb), shell_view);

    g_object_unref (action);

    return TRUE;
}