aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-source-util.c
blob: 44a60bcee191d1d010db6d5a6db55092b63df2ae (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
/*
 * e-source-util.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/>
 *
 */

#include "e-source-util.h"

typedef struct _AsyncContext AsyncContext;

struct _AsyncContext {
    EActivity *activity;
};

static void
async_context_free (AsyncContext *async_context)
{
    if (async_context->activity != NULL)
        g_object_unref (async_context->activity);

    g_slice_free (AsyncContext, async_context);
}

static void
source_util_remove_cb (GObject *source_object,
                       GAsyncResult *result,
                       gpointer user_data)
{
    ESource *source;
    EActivity *activity;
    EAlertSink *alert_sink;
    AsyncContext *async_context;
    const gchar *display_name;
    GError *error = NULL;

    source = E_SOURCE (source_object);
    async_context = (AsyncContext *) user_data;

    activity = async_context->activity;
    alert_sink = e_activity_get_alert_sink (activity);
    display_name = e_source_get_display_name (source);

    e_source_remove_finish (source, result, &error);

    if (e_activity_handle_cancellation (activity, error)) {
        g_error_free (error);

    } else if (error != NULL) {
        e_alert_submit (
            alert_sink,
            "system:remove-source-fail",
            display_name, error->message, NULL);
        g_error_free (error);

    } else {
        e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
    }

    async_context_free (async_context);
}

/**
 * e_source_util_remove:
 * @source: the #ESource to be removed
 * @alert_sink: an #EAlertSink
 *
 * Requests the D-Bus service to delete the key files for @source and all of
 * its descendants and broadcast their removal to all clients.  If an error
 * occurs, an #EAlert will be posted to @alert_sink.
 *
 * This function does not block.  The returned #EActivity can either be
 * ignored or passed to something that can display activity status to the
 * user, such as e_shell_backend_add_activity().
 *
 * Returns: an #EActivity to track the operation
 **/
EActivity *
e_source_util_remove (ESource *source,
                      EAlertSink *alert_sink)
{
    AsyncContext *async_context;
    GCancellable *cancellable;

    g_return_val_if_fail (E_IS_SOURCE (source), NULL);
    g_return_val_if_fail (E_IS_ALERT_SINK (alert_sink), NULL);

    cancellable = g_cancellable_new ();

    async_context = g_slice_new0 (AsyncContext);
    async_context->activity = e_activity_new ();

    e_activity_set_alert_sink (async_context->activity, alert_sink);
    e_activity_set_cancellable (async_context->activity, cancellable);

    e_source_remove (
        source, cancellable,
        source_util_remove_cb,
        async_context);

    g_object_unref (cancellable);

    return async_context->activity;
}

static void
source_util_write_cb (GObject *source_object,
                      GAsyncResult *result,
                      gpointer user_data)
{
    ESource *source;
    EActivity *activity;
    EAlertSink *alert_sink;
    AsyncContext *async_context;
    const gchar *display_name;
    GError *error = NULL;

    source = E_SOURCE (source_object);
    async_context = (AsyncContext *) user_data;

    activity = async_context->activity;
    alert_sink = e_activity_get_alert_sink (activity);
    display_name = e_source_get_display_name (source);

    e_source_write_finish (source, result, &error);

    if (e_activity_handle_cancellation (activity, error)) {
        g_error_free (error);

    } else if (error != NULL) {
        e_alert_submit (
            alert_sink,
            "system:write-source-fail",
            display_name, error->message, NULL);
        g_error_free (error);

    } else {
        e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
    }

    async_context_free (async_context);
}

/**
 * e_source_util_write:
 * @source: an #ESource
 * @alert_sink: an #EAlertSink
 *
 * Submits the current contents of @source to the D-Bus service to be
 * written to disk and broadcast to other clients.  If an error occurs,
 * an #EAlert will be posted to @alert_sink.
 *
 * This function does not block.  The returned #EActivity can either be
 * ignored or passed to something that can display activity status to the
 * user, such as e_shell_backend_add_activity().
 *
 * Returns: an #EActivity to track the operation
 **/
EActivity *
e_source_util_write (ESource *source,
                     EAlertSink *alert_sink)
{
    AsyncContext *async_context;
    GCancellable *cancellable;

    g_return_val_if_fail (E_IS_SOURCE (source), NULL);
    g_return_val_if_fail (E_IS_ALERT_SINK (alert_sink), NULL);

    cancellable = g_cancellable_new ();

    async_context = g_slice_new0 (AsyncContext);
    async_context->activity = e_activity_new ();

    e_activity_set_alert_sink (async_context->activity, alert_sink);
    e_activity_set_cancellable (async_context->activity, cancellable);

    e_source_write (
        source, cancellable,
        source_util_write_cb,
        async_context);

    g_object_unref (cancellable);

    return async_context->activity;
}