OpenAB  1.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TwoWaySync.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  */
10 #ifndef TWO_WAY_SYNC_HPP
11 #define TWO_WAY_SYNC_HPP
12 
13 #include <pthread.h>
14 #include "plugin/sync/Sync.hpp"
15 
60 struct OpenAB_Sync_params
61 {
62  std::string local_plugin;
66  std::string remote_plugin;
71  enum SyncType
72  {
74  eSyncAll,
75  eSyncText
76  } sync_type;
77  float sync_progress_time;
78  unsigned int batch_size;
79  std::string metadata;
80 };
81 
86 {
87  public:
89  ~TwoWaySync();
90 
92 
93  void synchronize();
94 
96 
98 
100 
101  void getStats(unsigned int& locallyAdded,
102  unsigned int& locallyModified,
103  unsigned int& locallyRemoved,
104  unsigned int& remotelyAdded,
105  unsigned int& remotelyModified,
106  unsigned int& remotelyRemoved);
107 
108  private:
112  TwoWaySync(TwoWaySync const &other);
113 
117  TwoWaySync& operator=(TwoWaySync const &other);
118 
119  void buildLocalIndexDB();
120  void cleanLocalIndexDB();
121 
122  void initMetadata();
123  void saveMetadata();
124 
125 
126  bool checkRemoteChanges();
127  bool checkLocalChanges();
128 
129  void firstTimeSync();
130  void fullSync();
131 
132  void addLocalItem(const OpenAB::SmartPtr<OpenAB::PIMItem>& item);
133  void modifyLocalItem(const std::string& id, const OpenAB::SmartPtr<OpenAB::PIMItem>& item);
134  void removeLocalItem(const std::string& id);
135 
136  void addRemoteItem(const OpenAB::SmartPtr<OpenAB::PIMItem>& item);
137  void modifyRemoteItem(const std::string& id, const OpenAB::SmartPtr<OpenAB::PIMItem>& item);
138  void removeRemoteItem(const std::string& id);
139 
140  bool flushLocalInsertions();
141  bool flushLocalModifications();
142  bool flushLocalRemovals();
143 
144  bool flushRemoteInsertions();
145  bool flushRemoteModifications();
146  bool flushRemoteRemovals();
147 
148  enum eSync doSynchronize();
149  static void* threadSync(void*);
150 
151  struct SyncStats {
152  void clean(){
153  locallyAdded = 0;
154  locallyModified = 0;
155  locallyRemoved = 0;
156 
157  remotelyAdded = 0;
158  remotelyModified = 0;
159  remotelyRemoved = 0;
160  };
161  unsigned int locallyAdded;
162  unsigned int locallyModified;
163  unsigned int locallyRemoved;
164 
165  unsigned int remotelyAdded;
166  unsigned int remotelyModified;
167  unsigned int remotelyRemoved;
168 
169  pthread_mutex_t mutex;
170 
171  };
172 
173  SyncStats globalStats;
174 
175  OpenAB_Sync_params params;
176 
177  OpenAB_Storage::Storage* localStorage;
178 
179  OpenAB_Storage::Storage* remoteStorage;
180 
181  typedef std::vector< OpenAB::SmartPtr<OpenAB_Storage::StorageItem> > vectorElem;
182  typedef std::map< OpenAB::SmartPtr<OpenAB::PIMItemIndex> , vectorElem > dbIndexElem;
183  dbIndexElem indexDB;
184 
185  //typedef std::map<std::string, OpenAB::SmartPtr<OpenAB_Storage::StorageItem> LocalIndex;
186  //LocalIndex localIndex;
187  // std::vector<OpenAB::SmartPtr<OpenAB_Storage::StorageItem> > localChanges;
188 
189  struct ItemDesc
190  {
191  ItemDesc(const std::string& _id, OpenAB::SmartPtr<OpenAB::PIMItem> vcard) :
192  id (_id),
193  item (vcard){}
194 
195  std::string id;
197  };
198 
199  std::vector<ItemDesc> localItemsToBeAdded;
200  std::vector<ItemDesc> localItemsToBeModified;
201  std::vector<std::string> localItemsToBeRemoved;
202 
203  std::vector<ItemDesc> remoteItemsToBeAdded;
204  std::vector<ItemDesc> remoteItemsToBeModified;
205  std::vector<std::string> remoteItemsToBeRemoved;
206 
207  std::vector<OpenAB::SmartPtr<OpenAB::PIMItem> > localyAddedItems;
208  std::map<std::string, OpenAB::SmartPtr<OpenAB::PIMItem> > localyModifiedItems;
209 
210  std::vector<OpenAB::SmartPtr<OpenAB::PIMItem> > remotelyAddedItems;
211  std::map<std::string, OpenAB::SmartPtr<OpenAB::PIMItem> > remotelyModifiedItems;
212 
213  bool dbError;
214  bool inputError;
215 
216  pthread_t syncThread;
217  pthread_mutex_t syncMutex;
218  bool threadCreated;
219  bool syncInProgress;
220  eSync lastSyncResult;
221  bool cancelSync;
222 
223  OpenAB_Sync::SyncMetadata metadata;
224 };
225 
226 #endif /* TWO_WAY_SYNC_HPP */
eInit
Definition: Sync.hpp:142
Definition: OneWaySync.hpp:114
std::string local_plugin
The name of the Input plugin.
Definition: TwoWaySync.hpp:62
Definition: OneWaySync.hpp:112
enum OpenAB_Sync::Sync::eSuspend suspend()
Suspends synchronization (if used OpenAB_Source::Source plugin supports it)
Definition: TwoWaySync.cpp:178
OpenAB_Source::Parameters remote_input_params
The parameters required by the Input plugin used.
Definition: TwoWaySync.hpp:67
float sync_progress_time
Definition: OneWaySync.hpp:116
OpenAB_Storage::Parameters local_storage_params
The parameters required by the Input plugin used.
Definition: TwoWaySync.hpp:64
std::string metadata
Definition: TwoWaySync.hpp:79
enum OpenAB_Sync_params::SyncType sync_type
enum OpenAB_Sync::Sync::eCancel cancel()
Cancels synchronization.
Definition: TwoWaySync.cpp:158
SyncType
Definition: OneWaySync.hpp:110
enum OpenAB_Sync::Sync::eResume resume()
Resumes synchronization (if used OpenAB_Source::Source plugin supports it)
Definition: TwoWaySync.cpp:198
Documentation for Storage plugin interface. Generic interface is modeled over Sync Plugin specific re...
Definition: Storage.hpp:106
eCancel
Definition: Sync.hpp:172
unsigned int batch_size
Definition: OneWaySync.hpp:117
void getStats(unsigned int &locallyAdded, unsigned int &locallyModified, unsigned int &locallyRemoved, unsigned int &remotelyAdded, unsigned int &remotelyModified, unsigned int &remotelyRemoved)
Returns statistics of synchronization.
Definition: TwoWaySync.cpp:218
~TwoWaySync()
Definition: TwoWaySync.cpp:56
Set of parameters required to perform the synchronization.
Definition: OneWaySync.hpp:103
eResume
Definition: Sync.hpp:202
TwoWaySync(OpenAB_Sync_params &)
Definition: TwoWaySync.cpp:39
Virtual class that provide all the callback routines to control the synchronization.
Definition: Sync.hpp:249
OpenAB_Sync::Sync::SyncCallback * cb
The Callback object.
Definition: OneWaySync.hpp:109
enum OpenAB_Sync::Sync::eInit init()
Initializes Sync.
Definition: TwoWaySync.cpp:82
Documentation for class SyncMetadata. This class is designed to store and keep track of synchronizati...
Definition: Sync.hpp:322
OpenAB_Source::Parameters local_input_params
The parameters required by the Input plugin used.
Definition: TwoWaySync.hpp:63
OpenAB_Storage::Parameters remote_storage_params
The parameters required by the Input plugin used.
Definition: TwoWaySync.hpp:68
Definition: OneWaySync.hpp:113
Documentation for class Sync plugin interface.
Definition: Sync.hpp:126
eSuspend
Definition: Sync.hpp:187
std::string remote_plugin
The name of the AB (Address Book) plugin.
Definition: TwoWaySync.hpp:66
void synchronize()
Synchronizes data.
Definition: TwoWaySync.cpp:125
Generic storage for plugin parameters. Allows to store map of parameters with different types (OpenAB...
Definition: GenericParameters.hpp:25
eSync
Definition: Sync.hpp:156
Sync Class providing all the Sync controls.
Definition: TwoWaySync.hpp:85