aboutsummaryrefslogtreecommitdiffstats
path: root/modules/composer-autosave/e-composer-autosave.c
blob: e24b1640c0077577442431e5f4771f264d373532 (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
/*
 * e-composer-autosave.c
 *
 * 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/>
 *
 */

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

#include <libebackend/libebackend.h>

#include <libevolution-utils/e-alert-dialog.h>
#include <composer/e-msg-composer.h>

#include "e-autosave-utils.h"

/* Standard GObject macros */
#define E_TYPE_COMPOSER_AUTOSAVE \
    (e_composer_autosave_get_type ())
#define E_COMPOSER_AUTOSAVE(obj) \
    (G_TYPE_CHECK_INSTANCE_CAST \
    ((obj), E_TYPE_COMPOSER_AUTOSAVE, EComposerAutosave))

#define AUTOSAVE_INTERVAL   60 /* seconds */

typedef struct _EComposerAutosave EComposerAutosave;
typedef struct _EComposerAutosaveClass EComposerAutosaveClass;

struct _EComposerAutosave {
    EExtension parent;

    GCancellable *cancellable;
    guint timeout_id;

    /* Composer contents have changed since
     * the last auto-save or explicit save. */
    gboolean changed;

    /* Prevent error dialogs from piling up. */
    gboolean error_shown;
};

struct _EComposerAutosaveClass {
    EExtensionClass parent_class;
};

/* Forward Declarations */
GType e_composer_autosave_get_type (void);
void e_composer_autosave_type_register (GTypeModule *type_module);

G_DEFINE_DYNAMIC_TYPE (
    EComposerAutosave,
    e_composer_autosave,
    E_TYPE_EXTENSION)

static void
composer_autosave_finished_cb (EMsgComposer *composer,
                               GAsyncResult *result,
                               EComposerAutosave *autosave)
{
    GFile *snapshot_file;
    GError *error = NULL;

    snapshot_file = e_composer_get_snapshot_file (composer);
    e_composer_save_snapshot_finish (composer, result, &error);

    /* Return silently if we were cancelled. */
    if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_error_free (error);

    else if (error != NULL) {
        gchar *basename;

        if (G_IS_FILE (snapshot_file))
            basename = g_file_get_basename (snapshot_file);
        else
            basename = g_strdup (" ");

        /* Only show one error dialog at a time. */
        if (!autosave->error_shown) {
            autosave->error_shown = TRUE;
            e_alert_run_dialog_for_args (
                GTK_WINDOW (composer),
                "mail-composer:no-autosave",
                basename, error->message, NULL);
            autosave->error_shown = FALSE;
        } else
            g_warning ("%s: %s", basename, error->message);

        g_free (basename);
        g_error_free (error);
    }

    g_object_unref (autosave);
}

static gboolean
composer_autosave_timeout_cb (EComposerAutosave *autosave)
{
    EExtensible *extensible;

    extensible = e_extension_get_extensible (E_EXTENSION (autosave));

    /* User may have reverted or explicitly saved
     * the changes since the timeout was scheduled. */
    if (autosave->changed) {

        /* Cancel the previous snapshot if it's still in
         * progress and start a new snapshot operation. */
        g_cancellable_cancel (autosave->cancellable);
        g_object_unref (autosave->cancellable);
        autosave->cancellable = g_cancellable_new ();

        e_composer_save_snapshot (
            E_MSG_COMPOSER (extensible),
            autosave->cancellable,
            (GAsyncReadyCallback)
            composer_autosave_finished_cb,
            g_object_ref (autosave));
    }

    autosave->timeout_id = 0;
    autosave->changed = FALSE;

    return FALSE;
}

static void
composer_autosave_changed_cb (EComposerAutosave *autosave)
{
    GtkhtmlEditor *editor;
    EExtensible *extensible;

    extensible = e_extension_get_extensible (E_EXTENSION (autosave));

    editor = GTKHTML_EDITOR (extensible);
    autosave->changed = gtkhtml_editor_get_changed (editor);

    if (autosave->changed && autosave->timeout_id == 0)
        autosave->timeout_id = g_timeout_add_seconds (
            AUTOSAVE_INTERVAL, (GSourceFunc)
            composer_autosave_timeout_cb, autosave);
}

static void
composer_autosave_dispose (GObject *object)
{
    EComposerAutosave *autosave;
    GObjectClass *parent_class;

    autosave = E_COMPOSER_AUTOSAVE (object);

    /* Cancel any snapshots in progress. */
    if (autosave->cancellable != NULL) {
        g_cancellable_cancel (autosave->cancellable);
        g_object_unref (autosave->cancellable);
        autosave->cancellable = NULL;
    }

    if (autosave->timeout_id > 0) {
        g_source_remove (autosave->timeout_id);
        autosave->timeout_id = 0;
    }

    /* Chain up to parent's dispose() method. */
    parent_class = G_OBJECT_CLASS (e_composer_autosave_parent_class);
    parent_class->dispose (object);
}

static void
composer_autosave_constructed (GObject *object)
{
    EExtensible *extensible;
    GObjectClass *parent_class;

    /* Chain up to parent's constructed() method. */
    parent_class = G_OBJECT_CLASS (e_composer_autosave_parent_class);
    parent_class->constructed (object);

    extensible = e_extension_get_extensible (E_EXTENSION (object));

    g_signal_connect_swapped (
        extensible, "notify::changed",
        G_CALLBACK (composer_autosave_changed_cb), object);
}

static void
e_composer_autosave_class_init (EComposerAutosaveClass *class)
{
    GObjectClass *object_class;
    EExtensionClass *extension_class;

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = composer_autosave_dispose;
    object_class->constructed = composer_autosave_constructed;

    extension_class = E_EXTENSION_CLASS (class);
    extension_class->extensible_type = E_TYPE_MSG_COMPOSER;
}

static void
e_composer_autosave_class_finalize (EComposerAutosaveClass *class)
{
}

static void
e_composer_autosave_init (EComposerAutosave *autosave)
{
    autosave->cancellable = g_cancellable_new ();
}

void
e_composer_autosave_type_register (GTypeModule *type_module)
{
    /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
     *     function, so we have to wrap it with a public function in
     *     order to register types from a separate compilation unit. */
    e_composer_autosave_register_type (type_module);
}