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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
/*
* e-memo-shell-backend.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/>
*
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#include "e-memo-shell-backend.h"
#include <string.h>
#include <glib/gi18n.h>
#include <libecal/e-cal.h>
#include <libedataserver/e-url.h>
#include <libedataserver/e-source.h>
#include <libedataserver/e-source-list.h>
#include <libedataserver/e-source-group.h>
#include "shell/e-shell.h"
#include "shell/e-shell-backend.h"
#include "shell/e-shell-window.h"
#include "calendar/common/authentication.h"
#include "calendar/gui/calendar-config.h"
#include "calendar/gui/comp-util.h"
#include "calendar/gui/dialogs/calendar-setup.h"
#include "calendar/gui/dialogs/memo-editor.h"
#include "e-memo-shell-migrate.h"
#include "e-memo-shell-view.h"
#define E_MEMO_SHELL_BACKEND_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_MEMO_SHELL_BACKEND, EMemoShellBackendPrivate))
#define WEB_BASE_URI "webcal://"
#define PERSONAL_RELATIVE_URI "system"
struct _EMemoShellBackendPrivate {
ESourceList *source_list;
};
enum {
PROP_0,
PROP_SOURCE_LIST
};
static gpointer parent_class;
static GType memo_shell_backend_type;
static void
memo_shell_backend_ensure_sources (EShellBackend *shell_backend)
{
/* XXX This is basically the same algorithm across all modules.
* Maybe we could somehow integrate this into EShellBackend? */
EMemoShellBackendPrivate *priv;
ESourceGroup *on_this_computer;
ESource *personal;
EShell *shell;
EShellSettings *shell_settings;
const gchar *data_dir;
const gchar *name;
gchar *base_uri, base_uri_seventh;
gchar *filename;
gboolean save_list = FALSE;
personal = NULL;
priv = E_MEMO_SHELL_BACKEND_GET_PRIVATE (shell_backend);
shell = e_shell_backend_get_shell (shell_backend);
shell_settings = e_shell_get_shell_settings (shell);
if (!e_cal_get_sources (&priv->source_list, E_CAL_SOURCE_TYPE_JOURNAL, NULL)) {
g_warning ("Could not get memo sources from GConf!");
return;
}
data_dir = e_shell_backend_get_data_dir (shell_backend);
filename = g_build_filename (data_dir, "local", NULL);
base_uri = g_filename_to_uri (filename, NULL, NULL);
g_free (filename);
if (strlen (base_uri) > 7) {
/* Compare only file:// part. If user home dir name
* changes we do not want to create one more group. */
base_uri_seventh = base_uri[7];
base_uri[7] = 0;
} else {
base_uri_seventh = -1;
}
on_this_computer = e_source_list_ensure_group (
priv->source_list, _("On This Computer"), base_uri, TRUE);
e_source_list_ensure_group (
priv->source_list, _("On The Web"), WEB_BASE_URI, FALSE);
if (base_uri_seventh != -1) {
base_uri[7] = base_uri_seventh;
}
if (on_this_computer != NULL) {
GSList *sources, *iter;
const gchar *group_base_uri;
sources = e_source_group_peek_sources (on_this_computer);
group_base_uri = e_source_group_peek_base_uri (on_this_computer);
/* Make sure this group includes a "Personal" source. */
for (iter = sources; iter != NULL; iter = iter->next) {
ESource *source = iter->data;
const gchar *relative_uri;
relative_uri = e_source_peek_relative_uri (source);
if (relative_uri == NULL)
continue;
if (strcmp (PERSONAL_RELATIVE_URI, relative_uri) != 0)
continue;
personal = source;
break;
}
/* Make sure we have the correct base URI. This can
* change when the user's home directory changes. */
if (strcmp (base_uri, group_base_uri) != 0) {
e_source_group_set_base_uri (
on_this_computer, base_uri);
/* XXX We shouldn't need this sync call here as
* set_base_uri() results in synching to GConf,
* but that happens in an idle loop and too late
* to prevent the user from seeing a "Cannot
* Open ... because of invalid URI" error. */
save_list = TRUE;
}
}
name = _("Personal");
if (personal == NULL) {
ESource *source;
GSList *selected;
gchar *primary;
source = e_source_new (name, PERSONAL_RELATIVE_URI);
e_source_set_color_spec (source, "#BECEDD");
e_source_group_add_source (on_this_computer, source, -1);
g_object_unref (source);
save_list = TRUE;
primary = e_shell_settings_get_string (
shell_settings, "cal-primary-memo-list");
selected = calendar_config_get_memos_selected ();
if (primary == NULL && selected == NULL) {
const gchar *uid;
uid = e_source_peek_uid (source);
selected = g_slist_prepend (NULL, g_strdup (uid));
e_shell_settings_set_string (
shell_settings, "cal-primary-memo-list", uid);
calendar_config_set_memos_selected (selected);
}
g_slist_foreach (selected, (GFunc) g_free, NULL);
g_slist_free (selected);
g_free (primary);
} else {
/* Force the source name to the current locale. */
e_source_set_name (personal, name);
}
g_object_unref (on_this_computer);
g_free (base_uri);
if (save_list)
e_source_list_sync (priv->source_list, NULL);
}
static void
memo_shell_backend_memo_new_cb (ECal *cal,
ECalendarStatus status,
EShell *shell)
{
ECalComponent *comp;
CompEditor *editor;
CompEditorFlags flags = 0;
/* XXX Handle errors better. */
if (status != E_CALENDAR_STATUS_OK)
return;
flags |= COMP_EDITOR_NEW_ITEM;
editor = memo_editor_new (cal, shell, flags);
comp = cal_comp_memo_new_with_defaults (cal);
comp_editor_edit_comp (editor, comp);
gtk_window_present (GTK_WINDOW (editor));
g_object_unref (comp);
g_object_unref (cal);
}
static void
memo_shell_backend_memo_shared_new_cb (ECal *cal,
ECalendarStatus status,
EShell *shell)
{
ECalComponent *comp;
CompEditor *editor;
CompEditorFlags flags = 0;
/* XXX Handle errors better. */
if (status != E_CALENDAR_STATUS_OK)
return;
flags |= COMP_EDITOR_NEW_ITEM;
flags |= COMP_EDITOR_IS_SHARED;
flags |= COMP_EDITOR_USER_ORG;
editor = memo_editor_new (cal, shell, flags);
comp = cal_comp_memo_new_with_defaults (cal);
comp_editor_edit_comp (editor, comp);
gtk_window_present (GTK_WINDOW (editor));
g_object_unref (comp);
g_object_unref (cal);
}
static void
action_memo_new_cb (GtkAction *action,
EShellWindow *shell_window)
{
ECal *cal = NULL;
ECalSourceType source_type;
ESourceList *source_list;
EShellSettings *shell_settings;
EShell *shell;
const gchar *action_name;
gchar *uid;
/* This callback is used for both memos and shared memos. */
source_type = E_CAL_SOURCE_TYPE_JOURNAL;
shell = e_shell_window_get_shell (shell_window);
shell_settings = e_shell_get_shell_settings (shell);
if (!e_cal_get_sources (&source_list, source_type, NULL)) {
g_warning ("Could not get memo sources from GConf!");
return;
}
uid = e_shell_settings_get_string (
shell_settings, "cal-primary-memo-list");
if (uid != NULL) {
ESource *source;
source = e_source_list_peek_source_by_uid (source_list, uid);
if (source != NULL)
cal = e_auth_new_cal_from_source (source, source_type);
g_free (uid);
}
if (cal == NULL)
cal = e_auth_new_cal_from_default (source_type);
g_return_if_fail (cal != NULL);
/* Connect the appropriate signal handler. */
action_name = gtk_action_get_name (action);
if (strcmp (action_name, "memo-shared-new") == 0)
g_signal_connect (
cal, "cal-opened",
G_CALLBACK (memo_shell_backend_memo_shared_new_cb),
shell);
else
g_signal_connect (
cal, "cal-opened",
G_CALLBACK (memo_shell_backend_memo_new_cb),
shell);
e_cal_open_async (cal, FALSE);
}
static void
action_memo_list_new_cb (GtkAction *action,
EShellWindow *shell_window)
{
calendar_setup_new_memo_list (GTK_WINDOW (shell_window));
}
static GtkActionEntry item_entries[] = {
{ "memo-new",
"stock_insert-note",
NC_("New", "Mem_o"),
"<Shift><Control>o",
N_("Create a new memo"),
G_CALLBACK (action_memo_new_cb) },
{ "memo-shared-new",
"stock_insert-note",
NC_("New", "_Shared Memo"),
"<Shift><Control>h",
N_("Create a new shared memo"),
G_CALLBACK (action_memo_new_cb) }
};
static GtkActionEntry source_entries[] = {
{ "memo-list-new",
"stock_notes",
NC_("New", "Memo Li_st"),
NULL,
N_("Create a new memo list"),
G_CALLBACK (action_memo_list_new_cb) }
};
static gboolean
memo_shell_backend_handle_uri_cb (EShellBackend *shell_backend,
const gchar *uri)
{
EShell *shell;
CompEditor *editor;
CompEditorFlags flags = 0;
ECal *client;
ECalComponent *comp;
ESource *source;
ESourceList *source_list;
ECalSourceType source_type;
EUri *euri;
icalcomponent *icalcomp;
const gchar *cp;
gchar *source_uid = NULL;
gchar *comp_uid = NULL;
gchar *comp_rid = NULL;
gboolean handled = FALSE;
GError *error = NULL;
source_type = E_CAL_SOURCE_TYPE_JOURNAL;
shell = e_shell_backend_get_shell (shell_backend);
if (strncmp (uri, "memo:", 5) != 0)
return FALSE;
euri = e_uri_new (uri);
cp = euri->query;
if (cp == NULL)
goto exit;
while (*cp != '\0') {
gchar *header;
gchar *content;
gsize header_len;
gsize content_len;
header_len = strcspn (cp, "=&");
/* If it's malformed, give up. */
if (cp[header_len] != '=')
break;
header = (gchar *) cp;
header[header_len] = '\0';
cp += header_len + 1;
content_len = strcspn (cp, "&");
content = g_strndup (cp, content_len);
if (g_ascii_strcasecmp (header, "source-uid") == 0)
source_uid = g_strdup (content);
else if (g_ascii_strcasecmp (header, "comp-uid") == 0)
comp_uid = g_strdup (content);
else if (g_ascii_strcasecmp (header, "comp-rid") == 0)
comp_rid = g_strdup (content);
g_free (content);
cp += content_len;
if (*cp == '&') {
cp++;
if (strcmp (cp, "amp;") == 0)
cp += 4;
}
}
if (source_uid == NULL || comp_uid == NULL)
goto exit;
/* URI is valid, so consider it handled. Whether
* we successfully open it is another matter... */
handled = TRUE;
if (!e_cal_get_sources (&source_list, source_type, NULL)) {
g_printerr ("Could not get memo sources from GConf!\n");
goto exit;
}
source = e_source_list_peek_source_by_uid (source_list, source_uid);
if (source == NULL) {
g_printerr ("No source for UID `%s'\n", source_uid);
g_object_unref (source_list);
goto exit;
}
client = e_auth_new_cal_from_source (source, source_type);
if (client == NULL || !e_cal_open (client, TRUE, &error)) {
if (error != NULL) {
g_printerr ("%s\n", error->message);
g_error_free (error);
}
g_object_unref (source_list);
goto exit;
}
/* XXX Copied from e_memo_shell_view_open_memo().
* Clearly a new utility function is needed. */
editor = comp_editor_find_instance (comp_uid);
if (editor != NULL)
goto present;
if (!e_cal_get_object (client, comp_uid, comp_rid, &icalcomp, &error)) {
g_printerr ("%s\n", error->message);
g_object_unref (source_list);
g_error_free (error);
goto exit;
}
comp = e_cal_component_new ();
e_cal_component_set_icalcomponent (comp, icalcomp);
if (e_cal_component_has_organizer (comp))
flags |= COMP_EDITOR_IS_SHARED;
if (itip_organizer_is_user (comp, client))
flags |= COMP_EDITOR_USER_ORG;
editor = memo_editor_new (client, shell, flags);
comp_editor_edit_comp (editor, comp);
g_object_unref (comp);
present:
gtk_window_present (GTK_WINDOW (editor));
g_object_unref (source_list);
g_object_unref (client);
exit:
g_free (source_uid);
g_free (comp_uid);
g_free (comp_rid);
e_uri_free (euri);
return handled;
}
static void
memo_shell_backend_window_created_cb (EShellBackend *shell_backend,
GtkWindow *window)
{
const gchar *module_name;
if (!E_IS_SHELL_WINDOW (window))
return;
module_name = E_SHELL_BACKEND_GET_CLASS (shell_backend)->name;
e_shell_window_register_new_item_actions (
E_SHELL_WINDOW (window), module_name,
item_entries, G_N_ELEMENTS (item_entries));
e_shell_window_register_new_source_actions (
E_SHELL_WINDOW (window), module_name,
source_entries, G_N_ELEMENTS (source_entries));
}
static void
memo_shell_backend_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_SOURCE_LIST:
g_value_set_object (
value,
e_memo_shell_backend_get_source_list (
E_MEMO_SHELL_BACKEND (object)));
return;
}
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
memo_shell_backend_dispose (GObject *object)
{
EMemoShellBackendPrivate *priv;
priv = E_MEMO_SHELL_BACKEND_GET_PRIVATE (object);
if (priv->source_list != NULL) {
g_object_unref (priv->source_list);
priv->source_list = NULL;
}
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
memo_shell_backend_constructed (GObject *object)
{
EShell *shell;
EShellBackend *shell_backend;
shell_backend = E_SHELL_BACKEND (object);
shell = e_shell_backend_get_shell (shell_backend);
memo_shell_backend_ensure_sources (shell_backend);
g_signal_connect_swapped (
shell, "handle-uri",
G_CALLBACK (memo_shell_backend_handle_uri_cb),
shell_backend);
g_signal_connect_swapped (
shell, "window-created",
G_CALLBACK (memo_shell_backend_window_created_cb),
shell_backend);
}
static void
memo_shell_backend_class_init (EMemoShellBackendClass *class)
{
GObjectClass *object_class;
EShellBackendClass *shell_backend_class;
parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (EMemoShellBackendPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->get_property = memo_shell_backend_get_property;
object_class->dispose = memo_shell_backend_dispose;
object_class->constructed = memo_shell_backend_constructed;
shell_backend_class = E_SHELL_BACKEND_CLASS (class);
shell_backend_class->shell_view_type = E_TYPE_MEMO_SHELL_VIEW;
shell_backend_class->name = "memos";
shell_backend_class->aliases = "";
shell_backend_class->schemes = "memo";
shell_backend_class->sort_order = 500;
shell_backend_class->preferences_page = "calendar-and-tasks";
shell_backend_class->start = NULL;
shell_backend_class->migrate = e_memo_shell_backend_migrate;
g_object_class_install_property (
object_class,
PROP_SOURCE_LIST,
g_param_spec_object (
"source-list",
_("Source List"),
_("The registry of memo lists"),
E_TYPE_SOURCE_LIST,
G_PARAM_READABLE));
}
static void
memo_shell_backend_init (EMemoShellBackend *memo_shell_backend)
{
memo_shell_backend->priv =
E_MEMO_SHELL_BACKEND_GET_PRIVATE (memo_shell_backend);
}
GType
e_memo_shell_backend_get_type (void)
{
return memo_shell_backend_type;
}
void
e_memo_shell_backend_register_type (GTypeModule *type_module)
{
const GTypeInfo type_info = {
sizeof (EMemoShellBackendClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) memo_shell_backend_class_init,
(GClassFinalizeFunc) NULL,
NULL, /* class_data */
sizeof (EMemoShellBackend),
0, /* n_preallocs */
(GInstanceInitFunc) memo_shell_backend_init,
NULL /* value_table */
};
memo_shell_backend_type = g_type_module_register_type (
type_module, E_TYPE_SHELL_BACKEND,
"EMemoShellBackend", &type_info, 0);
}
ESourceList *
e_memo_shell_backend_get_source_list (EMemoShellBackend *memo_shell_backend)
{
g_return_val_if_fail (
E_IS_MEMO_SHELL_BACKEND (memo_shell_backend), NULL);
return memo_shell_backend->priv->source_list;
}
|