aboutsummaryrefslogtreecommitdiffstats
path: root/modules/composer-autosave/e-composer-registry.c
blob: db951d324aa990c6e3300cdb8712f921d3c63c57 (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
/*
 * e-composer-registry.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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "e-composer-registry.h"

#include <config.h>
#include <glib/gstdio.h>
#include <libebackend/libebackend.h>

#include <shell/e-shell.h>
#include <shell/e-shell-window.h>
#include <composer/e-msg-composer.h>

#include "e-autosave-utils.h"

#define E_COMPOSER_REGISTRY_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_COMPOSER_REGISTRY, EComposerRegistryPrivate))

struct _EComposerRegistryPrivate {
    GQueue composers;
    gboolean orphans_restored;
    gulong map_event_handler_id;
};

G_DEFINE_DYNAMIC_TYPE (
    EComposerRegistry,
    e_composer_registry,
    E_TYPE_EXTENSION)

static void
composer_registry_recovered_cb (GObject *source_object,
                                GAsyncResult *result,
                                gpointer user_data)
{
    EMsgComposer *composer;
    EComposerRegistry *registry;
    GError *local_error = NULL;

    registry = E_COMPOSER_REGISTRY (user_data);

    composer = e_composer_load_snapshot_finish (
        E_SHELL (source_object), result, &local_error);

    if (local_error != NULL) {
        /* FIXME Show an alert dialog here explaining
         *       why we could not recover the message.
         *       Will need a new error XML entry. */
        g_warn_if_fail (composer == NULL);
        g_warning ("%s", local_error->message);
        g_error_free (local_error);
        goto exit;
    }

    gtk_widget_show (GTK_WIDGET (composer));

    g_object_unref (composer);

exit:
    g_object_unref (registry);
}

static void
composer_registry_restore_orphans (EComposerRegistry *registry,
                                   GtkWindow *parent)
{
    EExtensible *extensible;
    GList *orphans;
    gint response;
    GError *local_error = NULL;

    extensible = e_extension_get_extensible (E_EXTENSION (registry));

    /* Look for orphaned auto-save files. */
    orphans = e_composer_find_orphans (
        &registry->priv->composers, &local_error);
    if (orphans == NULL) {
        if (local_error != NULL) {
            g_warning ("%s", local_error->message);
            g_error_free (local_error);
        }
        return;
    }

    /* Ask if the user wants to recover the orphaned files. */
    response = e_alert_run_dialog_for_args (
        parent, "mail-composer:recover-autosave", NULL);

    /* Based on the user's reponse, recover or delete them. */
    while (orphans != NULL) {
        GFile *file = orphans->data;

        if (response == GTK_RESPONSE_YES)
            e_composer_load_snapshot (
                E_SHELL (extensible),
                file, NULL,
                composer_registry_recovered_cb,
                g_object_ref (registry));
        else
            g_file_delete (file, NULL, NULL);

        g_object_unref (file);

        orphans = g_list_delete_link (orphans, orphans);
    }
}

static gboolean
composer_registry_map_event_cb (GtkWindow *parent,
                                GdkEvent *event,
                                EComposerRegistry *registry)
{
    composer_registry_restore_orphans (registry, parent);
    registry->priv->orphans_restored = TRUE;

    /* This is a one-time-only signal handler.
     * Disconnect from subsequent map events. */
    g_signal_handler_disconnect (
        parent, registry->priv->map_event_handler_id);
    registry->priv->map_event_handler_id = 0;

    return FALSE;
}

static void
composer_registry_notify_cb (EComposerRegistry *registry,
                             GObject *where_the_object_was)
{
    /* Remove the finalized composer from the registry. */
    g_queue_remove (&registry->priv->composers, where_the_object_was);

    g_object_unref (registry);
}

static void
composer_registry_window_added_cb (GtkApplication *application,
                                   GtkWindow *window,
                                   EComposerRegistry *registry)
{
    /* Offer to restore any orphaned auto-save files from the
     * previous session once the first EShellWindow is mapped. */
    if (E_IS_SHELL_WINDOW (window) && !registry->priv->orphans_restored) {
        gulong handler_id;

        handler_id = g_signal_connect (
            window, "map-event",
            G_CALLBACK (composer_registry_map_event_cb),
            registry);
        registry->priv->map_event_handler_id = handler_id;

    /* Track the new composer window. */
    } else if (E_IS_MSG_COMPOSER (window)) {
        g_queue_push_tail (&registry->priv->composers, window);
        g_object_weak_ref (
            G_OBJECT (window), (GWeakNotify)
            composer_registry_notify_cb,
            g_object_ref (registry));
    }
}

static void
composer_registry_finalize (GObject *object)
{
    EComposerRegistryPrivate *priv;

    priv = E_COMPOSER_REGISTRY_GET_PRIVATE (object);

    /* All composers should have been finalized by now. */
    g_warn_if_fail (g_queue_is_empty (&priv->composers));

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_composer_registry_parent_class)->finalize (object);
}

static void
composer_registry_constructed (GObject *object)
{
    EExtensible *extensible;

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

    extensible = e_extension_get_extensible (E_EXTENSION (object));

    /* Listen for new watched windows. */
    g_signal_connect (
        extensible, "window-added",
        G_CALLBACK (composer_registry_window_added_cb),
        object);
}

static void
e_composer_registry_class_init (EComposerRegistryClass *class)
{
    GObjectClass *object_class;
    EExtensionClass *extension_class;

    g_type_class_add_private (class, sizeof (EComposerRegistryPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = composer_registry_finalize;
    object_class->constructed = composer_registry_constructed;

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

static void
e_composer_registry_class_finalize (EComposerRegistryClass *class)
{
}

static void
e_composer_registry_init (EComposerRegistry *registry)
{
    registry->priv = E_COMPOSER_REGISTRY_GET_PRIVATE (registry);
}

void
e_composer_registry_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_registry_register_type (type_module);
}