~ubuntu-branches/ubuntu/raring/almanah/raring-proposed

« back to all changes in this revision

Viewing changes to src/export-operation.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-04-18 16:21:36 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110418162136-l0ik4snrl420srer
Tags: 0.8.0-1
* New upstream release
* Temporarily remove evolution support in unstable
* Disable AM_GCONF_SOURCE_2 macro to fix ftbfs in unstable
  - debian/patches/disable_am_gconf_source_2
  - Build-Depends on dh-autoreconf
  - Use --with autoreconf in rules
* debian/control:
  - Build-Depends on libgtk-3-dev
* debian/rules:
  - Remove unnecessary override_dh_auto_install
* debian/copyright:
  - Update copyright years
  - Update license stanza
* Bump Standards-Version to 3.9.1 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
 * Almanah
 
4
 * Copyright (C) Philip Withnall 2010 <philip@tecnocode.co.uk>
 
5
 *
 
6
 * Almanah is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * Almanah is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with Almanah.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
#include <glib.h>
 
22
#include <glib/gi18n.h>
 
23
#include <gtk/gtk.h>
 
24
 
 
25
#include "export-operation.h"
 
26
#include "entry.h"
 
27
#include "storage-manager.h"
 
28
#include "main.h"
 
29
 
 
30
typedef gboolean (*ExportFunc) (AlmanahExportOperation *self, GFile *destination, AlmanahExportProgressCallback progress_callback,
 
31
                                gpointer progress_user_data, GCancellable *cancellable, GError **error);
 
32
 
 
33
typedef struct {
 
34
        const gchar *name; /* translatable */
 
35
        const gchar *description; /* translatable */
 
36
        GtkFileChooserAction action;
 
37
        ExportFunc export_func;
 
38
} ExportModeDetails;
 
39
 
 
40
static gboolean export_text_files (AlmanahExportOperation *self, GFile *destination, AlmanahExportProgressCallback progress_callback,
 
41
                                   gpointer progress_user_data, GCancellable *cancellable, GError **error);
 
42
static gboolean export_database (AlmanahExportOperation *self, GFile *destination, AlmanahExportProgressCallback progress_callback,
 
43
                                 gpointer progress_user_data, GCancellable *cancellable, GError **error);
 
44
 
 
45
static const ExportModeDetails export_modes[] = {
 
46
        { N_("Text Files"),
 
47
          N_("Select a _folder to export the entries to as text files, one per entry, with names in the format 'yyyy-mm-dd', and no extension. "
 
48
             "All entries will be exported, unencrypted in plain text format."),
 
49
          GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
 
50
          export_text_files },
 
51
        { N_("Database"),
 
52
          N_("Select a _filename for a complete copy of the unencrypted Almanah Diary database to be given."),
 
53
          GTK_FILE_CHOOSER_ACTION_SAVE,
 
54
          export_database }
 
55
};
 
56
 
 
57
static void almanah_export_operation_dispose (GObject *object);
 
58
 
 
59
struct _AlmanahExportOperationPrivate {
 
60
        gint current_mode; /* index into export_modes */
 
61
        GFile *destination;
 
62
};
 
63
 
 
64
G_DEFINE_TYPE (AlmanahExportOperation, almanah_export_operation, G_TYPE_OBJECT)
 
65
#define ALMANAH_EXPORT_OPERATION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ALMANAH_TYPE_EXPORT_OPERATION, AlmanahExportOperationPrivate))
 
66
 
 
67
static void
 
68
almanah_export_operation_class_init (AlmanahExportOperationClass *klass)
 
69
{
 
70
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
71
 
 
72
        g_type_class_add_private (klass, sizeof (AlmanahExportOperationPrivate));
 
73
 
 
74
        gobject_class->dispose = almanah_export_operation_dispose;
 
75
}
 
76
 
 
77
static void
 
78
almanah_export_operation_init (AlmanahExportOperation *self)
 
79
{
 
80
        self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_EXPORT_OPERATION, AlmanahExportOperationPrivate);
 
81
        self->priv->current_mode = -1; /* no mode selected */
 
82
}
 
83
 
 
84
static void
 
85
almanah_export_operation_dispose (GObject *object)
 
86
{
 
87
        AlmanahExportOperationPrivate *priv = ALMANAH_EXPORT_OPERATION (object)->priv;
 
88
 
 
89
        if (priv->destination != NULL)
 
90
                g_object_unref (priv->destination);
 
91
        priv->destination = NULL;
 
92
 
 
93
        /* Chain up to the parent class */
 
94
        G_OBJECT_CLASS (almanah_export_operation_parent_class)->dispose (object);
 
95
}
 
96
 
 
97
AlmanahExportOperation *
 
98
almanah_export_operation_new (AlmanahExportOperationType type_id, GFile *destination)
 
99
{
 
100
        AlmanahExportOperation *export_operation = g_object_new (ALMANAH_TYPE_EXPORT_OPERATION, NULL);
 
101
        export_operation->priv->current_mode = type_id;
 
102
        export_operation->priv->destination = g_object_ref (destination);
 
103
 
 
104
        return export_operation;
 
105
}
 
106
 
 
107
typedef struct {
 
108
        AlmanahExportProgressCallback callback;
 
109
        gpointer user_data;
 
110
        GDate *date;
 
111
} ProgressData;
 
112
 
 
113
static gboolean
 
114
progress_idle_callback_cb (ProgressData *data)
 
115
{
 
116
        g_assert (data->callback != NULL);
 
117
        data->callback (data->date, data->user_data);
 
118
 
 
119
        /* Free the data */
 
120
        g_date_free (data->date);
 
121
        g_slice_free (ProgressData, data);
 
122
 
 
123
        return FALSE;
 
124
}
 
125
 
 
126
static void
 
127
progress_idle_callback (AlmanahExportProgressCallback callback, gpointer user_data, const GDate *date)
 
128
{
 
129
        GSource *source;
 
130
        ProgressData *data;
 
131
 
 
132
        data = g_slice_new (ProgressData);
 
133
        data->callback = callback;
 
134
        data->user_data = user_data;
 
135
        data->date = g_date_new_dmy (g_date_get_day (date), g_date_get_month (date), g_date_get_year (date));
 
136
 
 
137
        /* We can't just use g_idle_add() here, since GSimpleAsyncResult uses default priority, so the finished callback will skip any outstanding
 
138
         * progress callbacks in the main loop's priority queue, causing Bad Things to happen. We need to guarantee that no more progress callbacks
 
139
         * will occur after the finished callback has been called; this is one hacky way of achieving that. */
 
140
        source = g_idle_source_new ();
 
141
        g_source_set_priority (source, G_PRIORITY_DEFAULT);
 
142
 
 
143
        g_source_set_callback (source, (GSourceFunc) progress_idle_callback_cb, data, NULL);
 
144
        g_source_attach (source, NULL);
 
145
 
 
146
        g_source_unref (source);
 
147
}
 
148
 
 
149
static gboolean
 
150
export_text_files (AlmanahExportOperation *self, GFile *destination, AlmanahExportProgressCallback progress_callback, gpointer progress_user_data,
 
151
                   GCancellable *cancellable, GError **error)
 
152
{
 
153
        AlmanahStorageManagerIter iter;
 
154
        AlmanahEntry *entry;
 
155
        GtkTextBuffer *buffer;
 
156
        gboolean success = FALSE;
 
157
        GError *child_error = NULL;
 
158
 
 
159
        /* Build a text buffer to use when getting all the entries */
 
160
        buffer = gtk_text_buffer_new (NULL);
 
161
 
 
162
        /* Iterate through the entries */
 
163
        almanah_storage_manager_iter_init (&iter);
 
164
        while ((entry = almanah_storage_manager_get_entries (almanah->storage_manager, &iter)) != NULL) {
 
165
                GDate date;
 
166
                gchar *filename, *content;
 
167
                GFile *file;
 
168
                GtkTextIter start_iter, end_iter;
 
169
 
 
170
                /* Get the filename */
 
171
                almanah_entry_get_date (entry, &date);
 
172
                filename = g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (&date), g_date_get_month (&date), g_date_get_day (&date));
 
173
                file = g_file_get_child (destination, filename);
 
174
                g_free (filename);
 
175
 
 
176
                /* Get the entry contents */
 
177
                if (almanah_entry_get_content (entry, buffer, TRUE, &child_error) == FALSE) {
 
178
                        /* Error */
 
179
                        g_object_unref (file);
 
180
                        g_object_unref (entry);
 
181
                        break;
 
182
                }
 
183
 
 
184
                g_object_unref (entry);
 
185
 
 
186
                gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
 
187
                content = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
 
188
 
 
189
                /* Create the file */
 
190
                if (g_file_replace_contents (file, content, strlen (content), NULL, FALSE,
 
191
                                             G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL, &child_error) == FALSE) {
 
192
                        /* Error */
 
193
                        g_object_unref (file);
 
194
                        g_free (content);
 
195
                        break;
 
196
                }
 
197
 
 
198
                /* Progress callback */
 
199
                progress_idle_callback (progress_callback, progress_user_data, &date);
 
200
 
 
201
                g_object_unref (file);
 
202
                g_free (content);
 
203
 
 
204
                /* Check for cancellation */
 
205
                if (cancellable != NULL && g_cancellable_set_error_if_cancelled (cancellable, &child_error) == TRUE)
 
206
                        break;
 
207
        }
 
208
 
 
209
        /* Check if the loop was broken due to an error */
 
210
        if (child_error != NULL) {
 
211
                g_propagate_error (error, child_error);
 
212
                goto finish;
 
213
        }
 
214
 
 
215
        success = TRUE;
 
216
 
 
217
finish:
 
218
        g_object_unref (buffer);
 
219
 
 
220
        return success;
 
221
}
 
222
 
 
223
static gboolean
 
224
export_database (AlmanahExportOperation *self, GFile *destination, AlmanahExportProgressCallback progress_callback, gpointer progress_user_data,
 
225
                 GCancellable *cancellable, GError **error)
 
226
{
 
227
        GFile *source;
 
228
        gboolean success;
 
229
 
 
230
        /* We ignore the progress callbacks, since this is a fairly fast operation, and it exports all the entries at once. */
 
231
 
 
232
        /* Get the input file (current unencrypted database) */
 
233
        source = g_file_new_for_path (almanah_storage_manager_get_filename (almanah->storage_manager, TRUE));
 
234
 
 
235
        /* Copy the current database to that location */
 
236
        success = g_file_copy (source, destination, G_FILE_COPY_OVERWRITE, cancellable, NULL, NULL, error);
 
237
 
 
238
        g_object_unref (source);
 
239
 
 
240
        return success;
 
241
}
 
242
 
 
243
typedef struct {
 
244
        AlmanahExportProgressCallback progress_callback;
 
245
        gpointer progress_user_data;
 
246
} ExportData;
 
247
 
 
248
static void
 
249
export_data_free (ExportData *data)
 
250
{
 
251
        g_slice_free (ExportData, data);
 
252
}
 
253
 
 
254
static void
 
255
export_thread (GSimpleAsyncResult *result, AlmanahExportOperation *operation, GCancellable *cancellable)
 
256
{
 
257
        GError *error = NULL;
 
258
        ExportData *data = g_simple_async_result_get_op_res_gpointer (result);
 
259
 
 
260
        /* Check to see if the operation's been cancelled already */
 
261
        if (g_cancellable_set_error_if_cancelled (cancellable, &error) == TRUE) {
 
262
                g_simple_async_result_set_from_error (result, error);
 
263
                g_error_free (error);
 
264
                return;
 
265
        }
 
266
 
 
267
        /* Export and return */
 
268
        if (export_modes[operation->priv->current_mode].export_func (operation, operation->priv->destination, data->progress_callback,
 
269
            data->progress_user_data, cancellable, &error) == FALSE) {
 
270
                g_simple_async_result_set_from_error (result, error);
 
271
                g_error_free (error);
 
272
        }
 
273
}
 
274
 
 
275
void
 
276
almanah_export_operation_run (AlmanahExportOperation *self, GCancellable *cancellable, AlmanahExportProgressCallback progress_callback,
 
277
                              gpointer progress_user_data,GAsyncReadyCallback callback, gpointer user_data)
 
278
{
 
279
        GSimpleAsyncResult *result;
 
280
        ExportData *data;
 
281
 
 
282
        g_return_if_fail (ALMANAH_IS_EXPORT_OPERATION (self));
 
283
        g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
284
 
 
285
        data = g_slice_new (ExportData);
 
286
        data->progress_callback = progress_callback;
 
287
        data->progress_user_data = progress_user_data;
 
288
 
 
289
        result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, almanah_export_operation_run);
 
290
        g_simple_async_result_set_op_res_gpointer (result, data, (GDestroyNotify) export_data_free);
 
291
        g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) export_thread, G_PRIORITY_DEFAULT, cancellable);
 
292
        g_object_unref (result);
 
293
}
 
294
 
 
295
gboolean
 
296
almanah_export_operation_finish (AlmanahExportOperation *self, GAsyncResult *async_result, GError **error)
 
297
{
 
298
        GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (async_result);
 
299
 
 
300
        g_return_val_if_fail (ALMANAH_IS_EXPORT_OPERATION (self), FALSE);
 
301
        g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), FALSE);
 
302
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
303
 
 
304
        g_warn_if_fail (g_simple_async_result_get_source_tag (result) == almanah_export_operation_run);
 
305
 
 
306
        if (g_simple_async_result_propagate_error (result, error) == TRUE)
 
307
                return FALSE;
 
308
 
 
309
        return TRUE;
 
310
}
 
311
 
 
312
void
 
313
almanah_export_operation_populate_model (GtkListStore *store, guint type_id_column, guint name_column, guint description_column, guint action_column)
 
314
{
 
315
        guint i;
 
316
 
 
317
        for (i = 0; i < G_N_ELEMENTS (export_modes); i++) {
 
318
                GtkTreeIter iter;
 
319
 
 
320
                gtk_list_store_append (store, &iter);
 
321
                gtk_list_store_set (store, &iter,
 
322
                                    type_id_column, i,
 
323
                                    name_column, _(export_modes[i].name),
 
324
                                    description_column, _(export_modes[i].description),
 
325
                                    action_column, export_modes[i].action,
 
326
                                    -1);
 
327
        }
 
328
}