00001 /* 00002 * Copyright (C) 2005-2008 Patrick Ohly 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #ifndef INCL_EVOLUTIONSYNCCLIENT 00020 #define INCL_EVOLUTIONSYNCCLIENT 00021 00022 #include <config.h> 00023 00024 #include "EvolutionSmartPtr.h" 00025 #include "SyncEvolutionConfig.h" 00026 #include <client/SyncClient.h> 00027 #include <spds/SyncManagerConfig.h> 00028 00029 #include <string> 00030 #include <set> 00031 using namespace std; 00032 00033 class SourceList; 00034 00035 /* 00036 * This is the main class inside sync4jevolution which 00037 * looks at the configuration, activates all enabled 00038 * sources and executes the synchronization. 00039 * 00040 */ 00041 class EvolutionSyncClient : public SyncClient, public EvolutionSyncConfig, public ConfigUserInterface { 00042 const string m_server; 00043 const set<string> m_sources; 00044 const bool m_doLogging; 00045 SyncMode m_syncMode; 00046 bool m_quiet; 00047 00048 /** 00049 * a pointer to the active SourceList instance if one exists; 00050 * used for error handling in throwError() on the iPhone 00051 */ 00052 static SourceList *m_sourceListPtr; 00053 00054 public: 00055 /** 00056 * @param server identifies the server config to be used 00057 * @param syncMode setting this overrides the sync mode from the config 00058 * @param doLogging write additional log and datatbase files about the sync 00059 */ 00060 EvolutionSyncClient(const string &server, 00061 bool doLogging = false, 00062 const set<string> &sources = set<string>()); 00063 ~EvolutionSyncClient(); 00064 00065 /** 00066 * A helper function which interactively asks the user for 00067 * a certain password. May throw errors. 00068 * 00069 * The default implementation uses stdin/stdout to communicate 00070 * with the user. 00071 * 00072 * @param descr A simple string explaining what the password is needed for, 00073 * e.g. "SyncML server". Has to be unique and understandable 00074 * by the user. 00075 * @return entered password 00076 */ 00077 virtual string askPassword(const string &descr); 00078 00079 bool getQuiet() { return m_quiet; } 00080 void setQuiet(bool quiet) { m_quiet = quiet; } 00081 SyncMode getSyncMode() { return m_syncMode; } 00082 void setSyncMode(SyncMode syncMode) { m_syncMode = syncMode; } 00083 00084 /** 00085 * Executes the sync, throws an exception in case of failure. 00086 * Handles automatic backups and report generation. 00087 */ 00088 int sync(); 00089 00090 /** 00091 * Determines the log directory of the previous sync (either in 00092 * temp or logdir) and shows changes since then. 00093 */ 00094 void status(); 00095 00096 /** 00097 * throws a runtime_error with the given string 00098 * or (on the iPhone, where exception handling is not 00099 * supported by the toolchain) prints an error directly 00100 * and aborts 00101 * 00102 * output format: <error> 00103 * 00104 * @param error a string describing the error 00105 */ 00106 static void throwError(const string &error); 00107 00108 /** 00109 * throw an exception after an operation failed and 00110 * remember that this instance has failed 00111 * 00112 * output format: <action>: <error string> 00113 * 00114 * @Param action a string describing the operation or object involved 00115 * @param error the errno error code for the failure 00116 */ 00117 static void throwError(const string &action, int error); 00118 00119 /** 00120 * An error handler which prints the error message and then 00121 * stops the program. Never returns. 00122 * 00123 * The API was chosen so that it can be used as libebook/libecal 00124 * "backend-dies" signal handler. 00125 */ 00126 static void fatalError(void *object, const char *error); 00127 00128 /** 00129 * When using Evolution this function starts a background thread 00130 * which drives the default event loop. Without that loop 00131 * "backend-died" signals are not delivered. The problem with 00132 * the thread is that it seems to interfere with gconf startup 00133 * when added to the main() function of syncevolution. Therefore 00134 * it is started by EvolutionSyncSource::beginSync() (for unit 00135 * testing of sync sources) and EvolutionSyncClient::sync() (for 00136 * normal operation). 00137 */ 00138 static void startLoopThread(); 00139 00140 00141 /* AbstractSyncConfig API */ 00142 virtual AbstractSyncSourceConfig* getAbstractSyncSourceConfig(const char* name) const; 00143 virtual AbstractSyncSourceConfig* getAbstractSyncSourceConfig(unsigned int i) const; 00144 virtual unsigned int getAbstractSyncSourceConfigsCount() const; 00145 00146 /** 00147 * intercept config filters 00148 * 00149 * This call removes the "sync" source property and remembers 00150 * it separately because it has to be applied to only the active 00151 * sync sources; the generic config handling code would apply 00152 * it to all sources. 00153 */ 00154 virtual void setConfigFilter(bool sync, const FilterConfigNode::ConfigFilter &filter); 00155 00156 protected: 00157 /** 00158 * Callback for derived classes: called after setting up the client's 00159 * and sources' configuration. Can be used to reconfigure before 00160 * actually starting the synchronization. 00161 * 00162 * @param sources a NULL terminated array of all active sources 00163 */ 00164 virtual void prepare(SyncSource **sources); 00165 00166 private: 00167 /** 00168 * the code common to init() and status(): 00169 * populate source list with active sources and open 00170 * them for reading without changing their state yet 00171 */ 00172 void initSources(SourceList &sourceList); 00173 00174 /** 00175 * override sync mode of all active sync sources if set 00176 */ 00177 string m_overrideMode; 00178 }; 00179 00180 #endif // INCL_EVOLUTIONSYNCCLIENT