0c23ade14dd239dabc926c5bf2bac2db26be5537
[platform/upstream/syncevolution.git] / src / syncevolution.cpp
1 /*
2  * Copyright (C) 2005-2006 Patrick Ohly
3  * Copyright (C) 2007 Funambol
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <config.h>
21 #include <stddef.h>
22
23 #include <base/Log.h>
24 #include <posix/base/posixlog.h>
25 #include <spds/spdsutils.h>
26
27 #include <iostream>
28 #include <memory>
29 using namespace std;
30
31 #include <libgen.h>
32 #ifdef HAVE_GLIB
33 #include <glib-object.h>
34 #endif
35
36 #include "SyncEvolutionCmdline.h"
37 #include "EvolutionSyncSource.h"
38 #include "EvolutionSyncClient.h"
39
40 #if defined(ENABLE_MAEMO) && defined (ENABLE_EBOOK)
41
42 #include <dlfcn.h>
43
44 extern "C" EContact *e_contact_new_from_vcard(const char *vcard)
45 {
46     static typeof(e_contact_new_from_vcard) *impl;
47
48     if (!impl) {
49         impl = (typeof(impl))dlsym(RTLD_NEXT, "e_contact_new_from_vcard");
50     }
51
52     // Old versions of EDS-DBus parse_changes_array() call
53     // e_contact_new_from_vcard() with a pointer which starts
54     // with a line break; Evolution is not happy with that and
55     // refuses to parse it. This code forwards until it finds
56     // the first non-whitespace, presumably the BEGIN:VCARD.
57     while (*vcard && isspace(*vcard)) {
58         vcard++;
59     }
60
61     return impl ? impl(vcard) : NULL;
62 }
63 #endif
64
65 #ifdef LOG_HAVE_SET_LOGGER
66 class CmdLineLogger : public POSIXLog {
67 protected:
68     virtual void printLine(bool firstLine,
69                            time_t time,
70                            const char *fullTime,
71                            const char *shortTime,
72                            const char *utcTime,
73                            LogLevel level,
74                            const char *levelPrefix,
75                            const char *line) {
76         POSIXLog::printLine(firstLine,
77                             time,
78                             fullTime,
79                             shortTime,
80                             utcTime,
81                             level,
82                             levelPrefix,
83                             line);
84         if (level <= LOG_LEVEL_INFO &&
85             getLogFile()) {
86             /* POSIXLog is printing to file, therefore print important lines to stdout */
87             fprintf(stdout, "%s [%s] %s\n",
88                     shortTime,
89                     levelPrefix,
90                     line);
91         }
92     }
93 };
94 #endif
95
96 int main( int argc, char **argv )
97 {
98 #ifdef ENABLE_MAEMO
99     // EDS-DBus uses potentially long-running calls which may fail due
100     // to the default 25s timeout. Some of these can be replaced by
101     // their async version, but e_book_async_get_changes() still
102     // triggered it.
103     //
104     // The workaround for this is to link the binary against a libdbus
105     // which has the dbus-timeout.patch and thus let's users and
106     // the application increase the default timeout.
107     setenv("DBUS_DEFAULT_TIMEOUT", "600000", 0);
108 #endif
109     
110 #if defined(HAVE_GLIB) && defined(HAVE_EDS)
111     // this is required on Maemo and does not harm either on a normal
112     // desktop system with Evolution
113     g_type_init();
114 #endif
115
116 #ifdef LOG_HAVE_SET_LOGGER
117     static CmdLineLogger logger;
118     Log::setLogger(&logger);
119 #endif
120
121 #ifdef POSIX_LOG
122     POSIX_LOG.
123 #endif
124         setLogFile(NULL, "-");
125     LOG.reset();
126     LOG.setLevel(LOG_LEVEL_INFO);
127     resetError();
128     setvbuf(stderr, NULL, _IONBF, 0);
129     setvbuf(stdout, NULL, _IONBF, 0);
130
131     // Expand PATH to cover the directory we were started from?
132     // This might be needed to find normalize_vcard.
133     char *exe = strdup(argv[0]);
134     if (strchr(exe, '/') ) {
135         char *dir = dirname(exe);
136         string path;
137         char *oldpath = getenv("PATH");
138         if (oldpath) {
139             path += oldpath;
140             path += ":";
141         }
142         path += dir;
143         setenv("PATH", path.c_str(), 1);
144     }
145     free(exe);
146
147     try {
148         SyncEvolutionCmdline cmdline(argc, argv, cout, cerr);
149         if (cmdline.parse() &&
150             cmdline.run()) {
151             return 0;
152         } else {
153             return 1;
154         }
155     } catch ( const std::exception &ex ) {
156         LOG.error( "%s", ex.what() );
157     } catch (...) {
158         LOG.error( "unknown error" );
159     }
160
161     return 1;
162 }