Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / EvolutionSyncClient.h
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 #ifndef INCL_EVOLUTIONSYNCCLIENT
21 #define INCL_EVOLUTIONSYNCCLIENT
22
23 #include <config.h>
24
25 #include "EvolutionSmartPtr.h"
26 #include "SyncEvolutionConfig.h"
27 #include <client/SyncClient.h>
28 #include <spds/SyncManagerConfig.h>
29
30 #include <string>
31 #include <set>
32 using namespace std;
33
34 class SourceList;
35
36 /*
37  * This is the main class inside sync4jevolution which
38  * looks at the configuration, activates all enabled
39  * sources and executes the synchronization.
40  *
41  */
42 class EvolutionSyncClient : public SyncClient, public EvolutionSyncConfig, public ConfigUserInterface {
43     const string m_server;
44     const set<string> m_sources;
45     const bool m_doLogging;
46     SyncMode m_syncMode;
47     bool m_quiet;
48
49     /**
50      * a pointer to the active SourceList instance if one exists; 
51      * used for error handling in throwError() on the iPhone
52      */
53     static SourceList *m_sourceListPtr;
54
55   public:
56     /**
57      * @param server     identifies the server config to be used
58      * @param syncMode   setting this overrides the sync mode from the config
59      * @param doLogging  write additional log and datatbase files about the sync
60      */
61     EvolutionSyncClient(const string &server,
62                         bool doLogging = false,
63                         const set<string> &sources = set<string>());
64     ~EvolutionSyncClient();
65
66     /**
67      * A helper function which interactively asks the user for
68      * a certain password. May throw errors.
69      *
70      * The default implementation uses stdin/stdout to communicate
71      * with the user.
72      *
73      * @param descr     A simple string explaining what the password is needed for,
74      *                  e.g. "SyncML server". Has to be unique and understandable
75      *                  by the user.
76      * @return entered password
77      */
78     virtual string askPassword(const string &descr);
79
80     bool getQuiet() { return m_quiet; }
81     void setQuiet(bool quiet) { m_quiet = quiet; }
82     SyncMode getSyncMode() { return m_syncMode; }
83     void setSyncMode(SyncMode syncMode) { m_syncMode = syncMode; }
84
85     /**
86      * Executes the sync, throws an exception in case of failure.
87      * Handles automatic backups and report generation.
88      */
89     int sync();
90
91     /**
92      * Determines the log directory of the previous sync (either in
93      * temp or logdir) and shows changes since then.
94      */
95     void status();
96
97     /**
98      * throws a runtime_error with the given string
99      * or (on the iPhone, where exception handling is not
100      * supported by the toolchain) prints an error directly
101      * and aborts
102      *
103      * @param error     a string describing the error
104      */
105     static void throwError(const string &error);
106
107     /**
108      * An error handler which prints the error message and then
109      * stops the program. Never returns.
110      *
111      * The API was chosen so that it can be used as libebook/libecal
112      * "backend-dies" signal handler.
113      */
114     static void fatalError(void *object, const char *error);
115
116     /**
117      * When using Evolution this function starts a background thread
118      * which drives the default event loop. Without that loop
119      * "backend-died" signals are not delivered. The problem with
120      * the thread is that it seems to interfere with gconf startup
121      * when added to the main() function of syncevolution. Therefore
122      * it is started by EvolutionSyncSource::beginSync() (for unit
123      * testing of sync sources) and EvolutionSyncClient::sync() (for
124      * normal operation).
125      */
126     static void startLoopThread();
127
128
129     /* AbstractSyncConfig API */
130     virtual AbstractSyncSourceConfig* getAbstractSyncSourceConfig(const char* name) const;
131     virtual AbstractSyncSourceConfig* getAbstractSyncSourceConfig(unsigned int i) const;
132     virtual unsigned int getAbstractSyncSourceConfigsCount() const;
133
134   protected:
135     /**
136      * Callback for derived classes: called after setting up the client's
137      * and sources' configuration. Can be used to reconfigure before
138      * actually starting the synchronization.
139      *
140      * @param sources   a NULL terminated array of all active sources
141      */
142     virtual void prepare(SyncSource **sources);
143
144  private:
145     /**
146      * the code common to init() and status():
147      * populate source list with active sources and open
148      * them for reading without changing their state yet
149      */
150     void initSources(SourceList &sourceList);
151 };
152
153 #endif // INCL_EVOLUTIONSYNCCLIENT