2 * Copyright (C) 2009 Intel Corporation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include <dbus/dbus-glib-bindings.h>
34 #ifdef USE_GNOME_KEYRING
35 // extern "C" is missing in 2.24.1, leading to
36 // link errors. Specifying it here is a workaround.
38 #include <gnome-keyring.h>
42 #include "EvolutionSyncSource.h"
43 #include "syncevo-dbus-server.h"
44 #include "syncevo-marshal.h"
46 static gboolean syncevo_start_sync (SyncevoDBusServer *obj, char *server, GPtrArray *sources, GError **error);
47 static gboolean syncevo_abort_sync (SyncevoDBusServer *obj, char *server, GError **error);
48 static gboolean syncevo_get_templates (SyncevoDBusServer *obj, GPtrArray **templates, GError **error);
49 static gboolean syncevo_get_template_config (SyncevoDBusServer *obj, char *templ, GPtrArray **options, GError **error);
50 static gboolean syncevo_get_servers (SyncevoDBusServer *obj, GPtrArray **servers, GError **error);
51 static gboolean syncevo_get_server_config (SyncevoDBusServer *obj, char *server, GPtrArray **options, GError **error);
52 static gboolean syncevo_set_server_config (SyncevoDBusServer *obj, char *server, GPtrArray *options, GError **error);
53 static gboolean syncevo_remove_server_config (SyncevoDBusServer *obj, char *server, GError **error);
54 static gboolean syncevo_get_sync_reports (SyncevoDBusServer *obj, char *server, int count, GPtrArray **reports, GError **error);
55 #include "syncevo-dbus-glue.h"
57 enum SyncevoDBusError{
58 SYNCEVO_DBUS_ERROR_GENERIC_ERROR,
59 SYNCEVO_DBUS_ERROR_NO_SUCH_SERVER,
60 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
61 SYNCEVO_DBUS_ERROR_INVALID_CALL, /* abort called when not syncing, or sync called when already syncing */
64 static GQuark syncevo_dbus_error_quark(void)
66 static GQuark quark = 0;
68 quark = g_quark_from_static_string("syncevo-dbus-server");
71 #define SYNCEVO_DBUS_ERROR (syncevo_dbus_error_quark())
74 syncevo_dbus_error_get_type (void)
76 static GType etype = 0;
77 if (G_UNLIKELY (etype == 0)) {
78 static const GEnumValue values[] = {
79 { SYNCEVO_DBUS_ERROR_GENERIC_ERROR, "SYNCEVO_DBUS_ERROR_GENERIC_ERROR", "GenericError" },
80 { SYNCEVO_DBUS_ERROR_NO_SUCH_SERVER, "SYNCEVO_DBUS_ERROR_NO_SUCH_SERVER", "NoSuchServer" },
81 { SYNCEVO_DBUS_ERROR_MISSING_ARGS, "SYNCEVO_DBUS_ERROR_MISSING_ARGS", "MissingArgs" },
82 { SYNCEVO_DBUS_ERROR_INVALID_CALL, "SYNCEVO_DBUS_ERROR_INVALID_CALL", "InvalidCall" },
85 etype = g_enum_register_static ("SyncevoDBusError", values);
89 #define SYNCEVO_DBUS_ERROR_TYPE (syncevo_dbus_error_get_type ())
92 #define SYNCEVO_SOURCE_TYPE (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_INT, G_TYPE_INVALID))
93 typedef GValueArray SyncevoSource;
94 #define SYNCEVO_OPTION_TYPE (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID))
95 typedef GValueArray SyncevoOption;
96 #define SYNCEVO_SERVER_TYPE (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INVALID))
97 typedef GValueArray SyncevoServer;
99 #define SYNCEVO_REPORT_TYPE (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INVALID))
100 typedef GValueArray SyncevoReport;
101 #define SYNCEVO_REPORT_ARRAY_TYPE (dbus_g_type_get_struct ("GValueArray", G_TYPE_INT, dbus_g_type_get_collection ("GPtrArray", SYNCEVO_REPORT_TYPE)))
102 typedef GValueArray SyncevoReportArray;
108 syncevo_source_new (char *name, int mode)
112 g_value_init (&val, SYNCEVO_SOURCE_TYPE);
113 g_value_take_boxed (&val, dbus_g_type_specialized_construct (SYNCEVO_SOURCE_TYPE));
114 dbus_g_type_struct_set (&val, 0, name, 1, mode, G_MAXUINT);
116 return (SyncevoSource*) g_value_get_boxed (&val);
120 syncevo_source_get (SyncevoSource *source, const char **name, int *mode)
123 *name = g_value_get_string (g_value_array_get_nth (source, 0));
126 *mode = g_value_get_int (g_value_array_get_nth (source, 1));
131 syncevo_source_add_to_map (SyncevoSource *source, map<string, int> source_map)
136 syncevo_source_get (source, &str, &mode);
137 source_map.insert (make_pair (str, mode));
141 syncevo_source_free (SyncevoSource *source)
144 g_boxed_free (SYNCEVO_SOURCE_TYPE, source);
149 syncevo_option_new (char *ns, char *key, char *value)
153 g_value_init (&val, SYNCEVO_OPTION_TYPE);
154 g_value_take_boxed (&val, dbus_g_type_specialized_construct (SYNCEVO_OPTION_TYPE));
155 dbus_g_type_struct_set (&val, 0, ns, 1, key, 2, value, G_MAXUINT);
157 return (SyncevoOption*) g_value_get_boxed (&val);
161 syncevo_option_get (SyncevoOption *option, const char **ns, const char **key, const char **value)
164 *ns = g_value_get_string (g_value_array_get_nth (option, 0));
167 *key = g_value_get_string (g_value_array_get_nth (option, 1));
170 *value = g_value_get_string (g_value_array_get_nth (option, 2));
175 syncevo_option_free (SyncevoOption *option)
178 g_boxed_free (SYNCEVO_OPTION_TYPE, option);
182 SyncevoServer* syncevo_server_new (char *name, char *url, char *icon, gboolean consumer_ready)
186 g_value_init (&val, SYNCEVO_SERVER_TYPE);
187 g_value_take_boxed (&val, dbus_g_type_specialized_construct (SYNCEVO_SERVER_TYPE));
188 dbus_g_type_struct_set (&val,
195 return (SyncevoServer*) g_value_get_boxed (&val);
198 void syncevo_server_get (SyncevoServer *server, const char **name, const char **url, const char **icon, gboolean *consumer_ready)
201 *name = g_value_get_string (g_value_array_get_nth (server, 0));
204 *url = g_value_get_string (g_value_array_get_nth (server, 1));
207 *icon = g_value_get_string (g_value_array_get_nth (server, 2));
209 if (consumer_ready) {
210 *consumer_ready = g_value_get_boolean (g_value_array_get_nth (server, 3));
214 void syncevo_server_free (SyncevoServer *server)
217 g_boxed_free (SYNCEVO_SERVER_TYPE, server);
222 syncevo_report_new (char *source)
226 g_value_init (&val, SYNCEVO_REPORT_TYPE);
227 g_value_take_boxed (&val, dbus_g_type_specialized_construct (SYNCEVO_REPORT_TYPE));
228 dbus_g_type_struct_set (&val,
232 return (SyncevoReport*) g_value_get_boxed (&val);
236 insert_int (SyncevoReport *report, int index, int value)
240 g_value_init (&val, G_TYPE_INT);
241 g_value_set_int (&val, value);
242 g_value_array_insert (report, index, &val);
246 syncevo_report_set_io (SyncevoReport *report,
247 int sent_bytes, int received_bytes)
249 g_return_if_fail (report);
251 insert_int (report, 1, sent_bytes);
252 insert_int (report, 2, received_bytes);
257 syncevo_report_set_local (SyncevoReport *report,
258 int adds, int updates, int removes, int rejects)
260 g_return_if_fail (report);
262 insert_int (report, 3, adds);
263 insert_int (report, 4, updates);
264 insert_int (report, 5, removes);
265 insert_int (report, 6, rejects);
269 syncevo_report_set_remote (SyncevoReport *report,
270 int adds, int updates, int removes, int rejects)
272 g_return_if_fail (report);
274 insert_int (report, 7, adds);
275 insert_int (report, 8, updates);
276 insert_int (report, 9, removes);
277 insert_int (report, 10, rejects);
281 syncevo_report_set_conflicts (SyncevoReport *report,
282 int local_won, int remote_won, int duplicated)
284 g_return_if_fail (report);
286 insert_int (report, 11, local_won);
287 insert_int (report, 12, remote_won);
288 insert_int (report, 13, duplicated);
292 syncevo_report_get_name (SyncevoReport *report)
294 g_return_val_if_fail (report, NULL);
296 return g_value_get_string (g_value_array_get_nth (report, 0));
301 syncevo_report_get_io (SyncevoReport *report,
302 int *bytes_sent, int *bytes_received)
304 g_return_if_fail (report);
307 *bytes_sent = g_value_get_int (g_value_array_get_nth (report, 1));
309 if (bytes_received) {
310 *bytes_received = g_value_get_int (g_value_array_get_nth (report, 2));
315 syncevo_report_get_local (SyncevoReport *report,
316 int *adds, int *updates, int *removes, int *rejects)
318 g_return_if_fail (report);
321 *adds = g_value_get_int (g_value_array_get_nth (report, 3));
324 *updates = g_value_get_int (g_value_array_get_nth (report, 4));
327 *removes = g_value_get_int (g_value_array_get_nth (report, 5));
330 *rejects = g_value_get_int (g_value_array_get_nth (report, 6));
335 syncevo_report_get_remote (SyncevoReport *report,
336 int *adds, int *updates, int *removes, int *rejects)
338 g_return_if_fail (report);
341 *adds = g_value_get_int (g_value_array_get_nth (report, 7));
344 *updates = g_value_get_int (g_value_array_get_nth (report, 8));
347 *removes = g_value_get_int (g_value_array_get_nth (report, 9));
350 *rejects = g_value_get_int (g_value_array_get_nth (report, 10));
355 syncevo_report_get_conflicts (SyncevoReport *report,
356 int *local_won, int *remote_won, int *duplicated)
358 g_return_if_fail (report);
361 *local_won = g_value_get_int (g_value_array_get_nth (report, 11));
364 *remote_won = g_value_get_int (g_value_array_get_nth (report, 12));
367 *duplicated = g_value_get_int (g_value_array_get_nth (report, 13));
372 syncevo_report_free (SyncevoReport *report)
375 g_boxed_free (SYNCEVO_REPORT_TYPE, report);
379 SyncevoReportArray* syncevo_report_array_new (int end_time, GPtrArray *reports)
383 g_value_init (&val, SYNCEVO_REPORT_ARRAY_TYPE);
384 g_value_take_boxed (&val, dbus_g_type_specialized_construct (SYNCEVO_REPORT_ARRAY_TYPE));
385 dbus_g_type_struct_set (&val,
389 return (SyncevoReportArray*) g_value_get_boxed (&val);
392 void syncevo_report_array_get (SyncevoReportArray *array, int *end_time, GPtrArray **reports)
394 g_return_if_fail (array);
397 *end_time = g_value_get_int (g_value_array_get_nth (array, 0));
400 *reports = (GPtrArray*)g_value_get_boxed (g_value_array_get_nth (array, 1));
405 syncevo_report_array_free (SyncevoReportArray *array)
408 g_boxed_free (SYNCEVO_REPORT_ARRAY_TYPE, array);
418 static guint signals[LAST_SIGNAL] = {0};
420 G_DEFINE_TYPE (SyncevoDBusServer, syncevo_dbus_server, G_TYPE_OBJECT);
425 g_main_loop_quit (loop);
431 update_shutdown_timer (SyncevoDBusServer *obj)
433 if (obj->shutdown_timeout_src > 0)
434 g_source_remove (obj->shutdown_timeout_src);
436 obj->shutdown_timeout_src = g_timeout_add_seconds (120,
437 (GSourceFunc)shutdown,
442 emit_progress (const char *source,
449 SyncevoDBusServer *obj = (SyncevoDBusServer *)data;
451 g_signal_emit (obj, signals[PROGRESS], 0,
461 emit_server_message (const char *message,
464 SyncevoDBusServer *obj = (SyncevoDBusServer *)data;
466 g_signal_emit (obj, signals[SERVER_MESSAGE], 0,
471 #ifdef USE_GNOME_KEYRING
473 need_password (const char *username,
474 const char *server_url,
477 char *password = NULL;
479 GnomeKeyringResult res;
481 server = strstr (server_url, "://");
488 res = gnome_keyring_find_password_sync (GNOME_KEYRING_NETWORK_PASSWORD,
495 case GNOME_KEYRING_RESULT_OK:
496 case GNOME_KEYRING_RESULT_NO_MATCH:
499 g_warning ("Failed to get password from keyring: %s",
500 gnome_keyring_result_to_message (res));
509 check_for_suspend (gpointer data)
511 SyncevoDBusServer *obj = (SyncevoDBusServer *)data;
517 do_sync (SyncevoDBusServer *obj)
523 ret = (*obj->client).sync(&report);
525 g_printerr ("sync returned error %d\n", ret);
528 g_printerr ("sync failed (non-existing server?)\n");
532 /* adding a progress signal on top of synthesis ones */
533 g_signal_emit (obj, signals[PROGRESS], 0,
542 g_free (obj->server);
546 /* shutdown after a moment of inactivity */
547 update_shutdown_timer (obj);
553 syncevo_start_sync (SyncevoDBusServer *obj,
559 *error = g_error_new (SYNCEVO_DBUS_ERROR,
560 SYNCEVO_DBUS_ERROR_INVALID_CALL,
561 "Sync already in progress. Concurrent syncs are currently not supported");
565 *error = g_error_new (SYNCEVO_DBUS_ERROR,
566 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
567 "Server argument must be set");
571 /* don't auto-shutdown while syncing */
572 if (obj->shutdown_timeout_src > 0) {
573 g_source_remove (obj->shutdown_timeout_src);
574 obj->shutdown_timeout_src = 0;
577 obj->aborted = FALSE;
578 obj->server = g_strdup (server);
580 map<string,int> source_map;
581 g_ptr_array_foreach (sources, (GFunc)syncevo_source_add_to_map, &source_map);
583 #ifdef USE_GNOME_KEYRING
584 obj->client = new DBusSyncClient (string (server), source_map,
585 emit_progress, emit_server_message, need_password, check_for_suspend,
588 obj->client = new DBusSyncClient (string (server), source_map,
589 emit_progress, emit_server_message, NULL, check_for_suspend,
593 g_idle_add ((GSourceFunc)do_sync, obj);
599 syncevo_abort_sync (SyncevoDBusServer *obj,
604 *error = g_error_new (SYNCEVO_DBUS_ERROR,
605 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
606 "Server variable must be set");
610 if ((!obj->server) || strcmp (server, obj->server) != 0) {
611 *error = g_error_new (SYNCEVO_DBUS_ERROR,
612 SYNCEVO_DBUS_ERROR_INVALID_CALL,
613 "Not syncing server '%s'", server);
623 syncevo_get_servers (SyncevoDBusServer *obj,
628 *error = g_error_new (SYNCEVO_DBUS_ERROR,
629 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
630 "servers argument must be set");
634 *servers = g_ptr_array_new ();
636 EvolutionSyncConfig::ServerList list = EvolutionSyncConfig::getServers();
638 BOOST_FOREACH(const EvolutionSyncConfig::ServerList::value_type &server,list) {
642 gboolean ready = TRUE;
645 boost::shared_ptr<EvolutionSyncConfig> config (EvolutionSyncConfig::createServerTemplate (server.first));
648 url = g_strdup (config->getWebURL().c_str());
649 icon = g_strdup (config->getIconURI().c_str());
650 ready = config->getConsumerReady();
652 name = g_strdup (server.first.c_str());
653 srv = syncevo_server_new (name, url, icon, ready);
655 g_ptr_array_add (*servers, srv);
658 update_shutdown_timer (obj);
664 syncevo_get_templates (SyncevoDBusServer *obj,
665 GPtrArray **templates,
669 *error = g_error_new (SYNCEVO_DBUS_ERROR,
670 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
671 "templates argument must be set");
675 *templates = g_ptr_array_new ();
677 EvolutionSyncConfig::ServerList list = EvolutionSyncConfig::getServerTemplates();
679 BOOST_FOREACH(const EvolutionSyncConfig::ServerList::value_type &server,list) {
680 char *name, *url, *icon;
684 boost::shared_ptr<EvolutionSyncConfig> config (EvolutionSyncConfig::createServerTemplate (server.first));
685 name = g_strdup (server.first.c_str());
686 url = g_strdup (config->getWebURL().c_str());
687 icon = g_strdup (config->getIconURI().c_str());
688 ready = config->getConsumerReady();
689 temp = syncevo_server_new (name, url, icon, ready);
691 g_ptr_array_add (*templates, temp);
694 update_shutdown_timer (obj);
700 syncevo_get_template_config (SyncevoDBusServer *obj,
705 SyncevoOption *option;
708 if (!templ || !options) {
712 *error = g_error_new (SYNCEVO_DBUS_ERROR,
713 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
714 "Template and options arguments must be given");
718 boost::shared_ptr<EvolutionSyncConfig> config (EvolutionSyncConfig::createServerTemplate (string (templ)));
721 *error = g_error_new (SYNCEVO_DBUS_ERROR,
722 SYNCEVO_DBUS_ERROR_NO_SUCH_SERVER,
723 "No template '%s' found", templ);
727 *options = g_ptr_array_new ();
728 option = syncevo_option_new (NULL, g_strdup ("syncURL"), g_strdup(config->getSyncURL()));
729 g_ptr_array_add (*options, option);
730 option = syncevo_option_new (NULL, g_strdup("username"), g_strdup(config->getUsername()));
731 g_ptr_array_add (*options, option);
732 option = syncevo_option_new (NULL, g_strdup("webURL"), g_strdup(config->getWebURL().c_str()));
733 g_ptr_array_add (*options, option);
734 option = syncevo_option_new (NULL, g_strdup("iconURI"), g_strdup(config->getIconURI().c_str()));
735 g_ptr_array_add (*options, option);
737 ready = (config->getConsumerReady() ? "yes" : "no");
738 option = syncevo_option_new (NULL, g_strdup("consumerReady"), g_strdup(ready));
739 g_ptr_array_add (*options, option);
741 option = syncevo_option_new (NULL, g_strdup("fromTemplate"), g_strdup("yes"));
742 g_ptr_array_add (*options, option);
744 list<string> sources = config->getSyncSources();
745 BOOST_FOREACH(const string &name, sources) {
748 boost::shared_ptr<EvolutionSyncSourceConfig> source_config = config->getSyncSourceConfig(name);
750 option = syncevo_option_new (g_strdup (name.c_str()), g_strdup ("sync"), g_strdup (source_config->getSync()));
751 g_ptr_array_add (*options, option);
752 option = syncevo_option_new (g_strdup (name.c_str()), g_strdup ("uri"), g_strdup (source_config->getURI()));
753 g_ptr_array_add (*options, option);
755 /* check whether we have support locally */
756 EvolutionSyncSourceParams params(name, config->getSyncSourceNodes(name), "");
757 auto_ptr<EvolutionSyncSource> syncSource(EvolutionSyncSource::createSource(params, false));
760 if (syncSource.get()) {
765 option = syncevo_option_new (g_strdup (name.c_str()),
766 g_strdup ("localDB"),
767 g_strdup_printf ("%d", local));
768 g_ptr_array_add (*options, option);
771 update_shutdown_timer (obj);
777 syncevo_get_server_config (SyncevoDBusServer *obj,
782 SyncevoOption *option;
784 if (!server || !options) {
787 *error = g_error_new (SYNCEVO_DBUS_ERROR,
788 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
789 "Server and options arguments must be given");
793 boost::shared_ptr<EvolutionSyncConfig> from(new EvolutionSyncConfig (string (server)));
794 /* if config does not exist, create from template */
795 if (!from->exists()) {
796 from = EvolutionSyncConfig::createServerTemplate( string (server));
799 *error = g_error_new (SYNCEVO_DBUS_ERROR,
800 SYNCEVO_DBUS_ERROR_NO_SUCH_SERVER,
801 "No server or template '%s' found", server);
806 *options = g_ptr_array_new ();
807 boost::shared_ptr<EvolutionSyncConfig> config(new EvolutionSyncConfig(string (server)));
808 config->copy(*from, NULL);
810 option = syncevo_option_new (NULL, g_strdup ("syncURL"), g_strdup(config->getSyncURL()));
811 g_ptr_array_add (*options, option);
812 option = syncevo_option_new (NULL, g_strdup("username"), g_strdup(config->getUsername()));
813 g_ptr_array_add (*options, option);
815 /* get template options if template exists */
816 boost::shared_ptr<EvolutionSyncConfig> templ = EvolutionSyncConfig::createServerTemplate( string (server));
820 option = syncevo_option_new (NULL, g_strdup("fromTemplate"), g_strdup("yes"));
821 g_ptr_array_add (*options, option);
822 option = syncevo_option_new (NULL, g_strdup("webURL"), g_strdup(templ->getWebURL().c_str()));
823 g_ptr_array_add (*options, option);
824 option = syncevo_option_new (NULL, g_strdup("iconURI"), g_strdup(templ->getIconURI().c_str()));
825 g_ptr_array_add (*options, option);
827 ready = templ->getConsumerReady() ? "yes" : "no";
828 option = syncevo_option_new (NULL, g_strdup("consumerReady"), g_strdup (ready));
829 g_ptr_array_add (*options, option);
832 list<string> sources = config->getSyncSources();
833 BOOST_FOREACH(const string &name, sources) {
836 boost::shared_ptr<EvolutionSyncSourceConfig> source_config = config->getSyncSourceConfig(name);
838 option = syncevo_option_new (g_strdup (name.c_str()), g_strdup ("sync"), g_strdup (source_config->getSync()));
839 g_ptr_array_add (*options, option);
840 option = syncevo_option_new (g_strdup (name.c_str()), g_strdup ("uri"), g_strdup (source_config->getURI()));
841 g_ptr_array_add (*options, option);
843 /* check whether we have support locally */
844 EvolutionSyncSourceParams params(name, config->getSyncSourceNodes(name), "");
845 auto_ptr<EvolutionSyncSource> syncSource(EvolutionSyncSource::createSource(params, false));
848 if (syncSource.get()) {
853 option = syncevo_option_new (g_strdup (name.c_str()),
854 g_strdup ("localDB"),
855 g_strdup_printf ("%d", local));
856 g_ptr_array_add (*options, option);
862 update_shutdown_timer (obj);
869 syncevo_set_server_config (SyncevoDBusServer *obj,
876 if (!server || !options) {
877 *error = g_error_new (SYNCEVO_DBUS_ERROR,
878 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
879 "Server and options parameters must be given");
884 *error = g_error_new (SYNCEVO_DBUS_ERROR,
885 SYNCEVO_DBUS_ERROR_GENERIC_ERROR,
886 "GetServers is currently not supported when a sync is in progress");
890 boost::shared_ptr<EvolutionSyncConfig> from(new EvolutionSyncConfig (string (server)));
891 /* if config does not exist, create from template */
892 if (!from->exists()) {
893 from = EvolutionSyncConfig::createServerTemplate( string (server));
895 from = EvolutionSyncConfig::createServerTemplate( string ("default"));
898 boost::shared_ptr<EvolutionSyncConfig> config(new EvolutionSyncConfig(string (server)));
899 config->copy(*from, NULL);
901 for (i = 0; i < (int)options->len; i++) {
902 const char *ns, *key, *value;
903 SyncevoOption *option = (SyncevoOption*)g_ptr_array_index (options, i);
905 syncevo_option_get (option, &ns, &key, &value);
907 if ((!ns || strlen (ns) == 0) && key) {
908 if (strcmp (key, "syncURL") == 0) {
909 config->setSyncURL (string (value));
910 } else if (strcmp (key, "username") == 0) {
911 config->setUsername (string (value));
912 } else if (strcmp (key, "password") == 0) {
913 config->setPassword (string (value));
914 } else if (strcmp (key, "webURL") == 0) {
915 config->setWebURL (string (value));
916 } else if (strcmp (key, "iconURI") == 0) {
917 config->setIconURI (string (value));
919 } else if (ns && key) {
920 boost::shared_ptr<EvolutionSyncSourceConfig> source_config = config->getSyncSourceConfig(ns);
921 if (strcmp (key, "sync") == 0) {
922 source_config->setSync (string (value));
923 } else if (strcmp (key, "uri") == 0) {
924 source_config->setURI (string (value));
930 update_shutdown_timer (obj);
936 syncevo_remove_server_config (SyncevoDBusServer *obj,
941 *error = g_error_new (SYNCEVO_DBUS_ERROR,
942 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
943 "Server argument must be given");
948 *error = g_error_new (SYNCEVO_DBUS_ERROR,
949 SYNCEVO_DBUS_ERROR_GENERIC_ERROR,
950 "RemoveServerConfig is not supported when a sync is in progress");
954 boost::shared_ptr<EvolutionSyncConfig> config(new EvolutionSyncConfig (string (server)));
955 if (!config->exists()) {
956 *error = g_error_new (SYNCEVO_DBUS_ERROR,
957 SYNCEVO_DBUS_ERROR_NO_SUCH_SERVER,
958 "No server '%s' found", server);
963 update_shutdown_timer (obj);
969 syncevo_get_sync_reports (SyncevoDBusServer *obj,
976 *error = g_error_new (SYNCEVO_DBUS_ERROR,
977 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
978 "Server argument must be given");
983 *error = g_error_new (SYNCEVO_DBUS_ERROR,
984 SYNCEVO_DBUS_ERROR_MISSING_ARGS,
985 "reports argument must be given");
989 EvolutionSyncClient client (string (server), false);
991 *reports = g_ptr_array_new ();
993 client.getSessions (dirs);
994 int start_from = dirs.size () - count;
997 BOOST_FOREACH (const string &dir, dirs) {
998 if (index < start_from) {
1001 SyncevoReportArray * session_report;
1002 GPtrArray *source_reports;
1005 client.readSessionInfo (dir, report);
1006 source_reports = g_ptr_array_new ();
1008 for (SyncReport::iterator it = report.begin(); it != report.end(); ++it) {
1009 SyncevoReport *source_report;
1011 SyncSourceReport srcrep = it->second;
1012 source_report = syncevo_report_new (g_strdup (it->first.c_str ()));
1014 syncevo_report_set_io (source_report,
1015 srcrep.getItemStat (SyncSourceReport::ITEM_LOCAL,
1016 SyncSourceReport::ITEM_ANY,
1017 SyncSourceReport::ITEM_SENT_BYTES),
1018 srcrep.getItemStat (SyncSourceReport::ITEM_LOCAL,
1019 SyncSourceReport::ITEM_ANY,
1020 SyncSourceReport::ITEM_RECEIVED_BYTES));
1021 syncevo_report_set_local (source_report,
1022 srcrep.getItemStat (SyncSourceReport::ITEM_LOCAL,
1023 SyncSourceReport::ITEM_ADDED,
1024 SyncSourceReport::ITEM_TOTAL),
1025 srcrep.getItemStat (SyncSourceReport::ITEM_LOCAL,
1026 SyncSourceReport::ITEM_UPDATED,
1027 SyncSourceReport::ITEM_TOTAL),
1028 srcrep.getItemStat (SyncSourceReport::ITEM_LOCAL,
1029 SyncSourceReport::ITEM_REMOVED,
1030 SyncSourceReport::ITEM_TOTAL),
1031 srcrep.getItemStat (SyncSourceReport::ITEM_LOCAL,
1032 SyncSourceReport::ITEM_ANY,
1033 SyncSourceReport::ITEM_REJECT));
1034 syncevo_report_set_remote (source_report,
1035 srcrep.getItemStat (SyncSourceReport::ITEM_REMOTE,
1036 SyncSourceReport::ITEM_ADDED,
1037 SyncSourceReport::ITEM_TOTAL),
1038 srcrep.getItemStat (SyncSourceReport::ITEM_REMOTE,
1039 SyncSourceReport::ITEM_UPDATED,
1040 SyncSourceReport::ITEM_TOTAL),
1041 srcrep.getItemStat (SyncSourceReport::ITEM_REMOTE,
1042 SyncSourceReport::ITEM_REMOVED,
1043 SyncSourceReport::ITEM_TOTAL),
1044 srcrep.getItemStat (SyncSourceReport::ITEM_REMOTE,
1045 SyncSourceReport::ITEM_ANY,
1046 SyncSourceReport::ITEM_REJECT));
1047 syncevo_report_set_conflicts (source_report,
1048 srcrep.getItemStat (SyncSourceReport::ITEM_REMOTE,
1049 SyncSourceReport::ITEM_ANY,
1050 SyncSourceReport::ITEM_CONFLICT_CLIENT_WON),
1051 srcrep.getItemStat (SyncSourceReport::ITEM_REMOTE,
1052 SyncSourceReport::ITEM_ANY,
1053 SyncSourceReport::ITEM_CONFLICT_SERVER_WON),
1054 srcrep.getItemStat (SyncSourceReport::ITEM_REMOTE,
1055 SyncSourceReport::ITEM_ANY,
1056 SyncSourceReport::ITEM_CONFLICT_DUPLICATED));
1057 g_ptr_array_add (source_reports, source_report);
1059 session_report = syncevo_report_array_new (report.getEnd (), source_reports);
1060 g_ptr_array_add (*reports, session_report);
1064 update_shutdown_timer (obj);
1070 syncevo_dbus_server_class_init(SyncevoDBusServerClass *klass)
1072 GError *error = NULL;
1074 klass->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
1075 if (klass->connection == NULL)
1077 g_warning("Unable to connect to dbus: %s", error->message);
1078 g_error_free (error);
1082 signals[PROGRESS] = g_signal_new ("progress",
1083 G_TYPE_FROM_CLASS (klass),
1084 (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE),
1085 G_STRUCT_OFFSET (SyncevoDBusServerClass, progress),
1087 syncevo_marshal_VOID__STRING_STRING_INT_INT_INT_INT,
1089 6, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT);
1090 signals[SERVER_MESSAGE] = g_signal_new ("server-message",
1091 G_TYPE_FROM_CLASS (klass),
1092 (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE),
1093 G_STRUCT_OFFSET (SyncevoDBusServerClass, server_message),
1095 syncevo_marshal_VOID__STRING_STRING,
1097 2, G_TYPE_STRING, G_TYPE_STRING);
1099 /* dbus_glib_syncevo_object_info is provided in the generated glue file */
1100 dbus_g_object_type_install_info (SYNCEVO_TYPE_DBUS_SERVER, &dbus_glib_syncevo_object_info);
1102 /* register error domain so clients get proper error names with dbus_g_error_get_name() */
1103 dbus_g_error_domain_register (SYNCEVO_DBUS_ERROR, NULL, SYNCEVO_DBUS_ERROR_TYPE);
1107 syncevo_dbus_server_init(SyncevoDBusServer *obj)
1109 GError *error = NULL;
1112 SyncevoDBusServerClass *klass = SYNCEVO_DBUS_SERVER_GET_CLASS (obj);
1114 dbus_g_connection_register_g_object (klass->connection,
1115 "/org/Moblin/SyncEvolution",
1118 proxy = dbus_g_proxy_new_for_name (klass->connection,
1121 DBUS_INTERFACE_DBUS);
1123 if(!org_freedesktop_DBus_request_name (proxy,
1124 "org.Moblin.SyncEvolution",
1127 g_warning("Unable to register service: %s", error->message);
1128 g_error_free (error);
1130 g_object_unref (proxy);
1132 update_shutdown_timer (obj);
1137 g_main_loop_quit (loop);
1142 SyncevoDBusServer *server;
1144 signal(SIGTERM, niam);
1145 signal(SIGINT, niam);
1148 g_thread_init (NULL);
1149 g_set_application_name ("SyncEvolution");
1151 server = (SyncevoDBusServer*)g_object_new (SYNCEVO_TYPE_DBUS_SERVER, NULL);
1153 loop = g_main_loop_new (NULL, FALSE);
1154 g_main_loop_run (loop);
1156 g_main_loop_unref (loop);
1157 g_object_unref (server);