ButeoSynchronizationFramework
synchronizer.h
1 /*
2  * This file is part of buteo-syncfw package
3  *
4  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5  * Copyright (C) 2014-2015 Jolla Ltd
6  *
7  * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 #ifndef SYNCHRONIZER_H
25 #define SYNCHRONIZER_H
26 
27 #include "SyncDBusInterface.h"
28 #include "SyncQueue.h"
29 #include "StorageBooker.h"
30 #include "SyncScheduler.h"
31 #include "SyncBackup.h"
32 #include "SyncOnChange.h"
33 #include "SyncOnChangeScheduler.h"
34 
35 #include "SyncCommonDefs.h"
36 #include "ProfileManager.h"
37 #include "PluginManager.h"
38 #include "PluginCbInterface.h"
39 #include "ClientPlugin.h"
40 
41 #include <QVector>
42 #include <QMutex>
43 #include <QCoreApplication>
44 #include <QMap>
45 #include <QString>
46 #include <QDBusInterface>
47 #include <QScopedPointer>
48 #include <QTimer>
49 
50 struct _GSettings;
51 
52 namespace Buteo {
53 
54 class PluginManager;
55 class ServerPluginRunner;
56 class NetworkManager;
57 class TransportTracker;
58 class ServerActivator;
59 class AccountsHelper;
60 class BatteryInfo;
61 
66 class Synchronizer : public SyncDBusInterface, // Derived from QObject
67  public PluginCbInterface
68 {
69  Q_OBJECT
70 public:
71 
73  Synchronizer(QCoreApplication *aApplication);
74 
76  virtual ~Synchronizer();
77 
80  bool initialize();
81 
83  void close();
84 
85 
86 // From PluginCbInterface
87 // ---------------------------------------------------------------------------
89  virtual bool requestStorage(const QString &aStorageName,
90  const SyncPluginBase *aCaller);
91 
93  virtual void releaseStorage(const QString &aStorageName,
94  const SyncPluginBase *aCaller);
95 
97  virtual StoragePlugin* createStorage(const QString &aPluginName);
98 
100  virtual void destroyStorage(StoragePlugin *aStorage);
101 
103  virtual bool isConnectivityAvailable( Sync::ConnectivityType aType );
104 
106  virtual Profile* getSyncProfileByRemoteAddress(const QString& aAddress);
107 
109  virtual QString getValue(const QString& aAddress, const QString& aKey);
110 
111 
112 // From SyncDBusInterface
113 // --------------------------------------------------------------------------
114 
115 public slots:
116 
118  virtual bool startSync(QString aProfileName);
119 
121  virtual void abortSync(QString aProfileName);
122 
124  virtual bool removeProfile(QString aProfileAsXml);
125 
127  virtual bool updateProfile(QString aProfileAsXml);
128 
130  virtual bool requestStorages(QStringList aStorageNames);
131 
133  virtual void releaseStorages(QStringList aStorageNames);
134 
136  virtual QStringList runningSyncs();
137 
139  virtual bool setSyncSchedule(QString aProfileId , QString aScheduleAsXml);
140 
142  virtual bool saveSyncResults(QString aProfileId,QString aSyncResults);
143 
145  virtual QString createSyncProfileForAccount(uint aAccountId);
146 
151  virtual QString getLastSyncResult(const QString &aProfileId);
152 
159  virtual QStringList allVisibleSyncProfiles();
160 
169  virtual QString syncProfile(const QString &aProfileId);
170  virtual QStringList syncProfilesByKey(const QString &aKey, const QString &aValue);
171  virtual QStringList syncProfilesByType(const QString &aType);
172 // --------------------------------------------------------------------------
173 
175  bool startScheduledSync(QString aProfileName);
176 
178  void backupStarts();
179 
181  void backupFinished();
182 
184  void restoreStarts();
185 
187  void restoreFinished();
188 
190  virtual bool getBackUpRestoreState();
191 
192  void start(unsigned int aAccountId);
193 
198  void stop(unsigned int aAccountId);
199 
205 
219  int status(unsigned int aAccountId, int &aFailedReason, qlonglong &aPrevSyncTime, qlonglong &aNextSyncTime);
220 
229  void isSyncedExternally(unsigned int aAccountId, const QString aClientProfileName);
230 
231 signals:
232 
234  void storageReleased();
235 
241  void syncDone(const QString &aProfileName);
242 
243 private slots:
244 
250  void onStorageReleased();
251 
252  void onTransferProgress( const QString &aProfileName,
253  Sync::TransferDatabase aDatabase, Sync::TransferType aType,
254  const QString &aMimeType, int aCommittedItems );
255 
256  void onSessionFinished( const QString &aProfileName,
257  Sync::SyncStatus aStatus, const QString &aMessage, int aErrorCode );
258 
259  void onStorageAccquired(const QString &aProfileName, const QString &aMimeType);
260 
261  void onSyncProgressDetail(const QString &aProfileName,int aProgressDetail);
262 
263  void onServerDone();
264 
265  void onNewSession(const QString &aDestination);
266 
267  void slotProfileChanged(QString aProfileName, int aChangeType , QString aProfileAsXml);
268 
273  void startServer(const QString &aProfileName);
274 
279  void stopServer(const QString &aProfileName);
280 
281  void onNetworkStateChanged(bool aState, Sync::InternetConnectionType type);
282 
289  void enableSOCSlot(const QString& aProfileName);
290 
295  void reschedule(const QString &aProfileName);
296 
304  void slotSyncStatus(QString aProfileName, int aStatus,
305  QString aMessage, int aMoreDetails);
306 
311  void removeScheduledSync(const QString &aProfileName);
312 
320  void externalSyncStatus(const SyncProfile *aProfile, bool aQuery=false);
321 
323  void profileChangeTriggerTimeout();
324 
325 private:
326 
327  bool startSync(const QString &aProfileName, bool aScheduled);
328 
334  bool startSyncNow(SyncSession *aSession);
335 
342  bool startNextSync();
343 
349  void cleanupSession(SyncSession *aSession, Sync::SyncStatus aStatus);
350 
355  void startServers( bool resume = false );
356 
361  void stopServers( bool suspend = false );
362 
366  void backupRestoreStarts ();
367 
371  void backupRestoreFinished();
372 
376  void initializeScheduler();
377 
378  bool isBackupRestoreInProgress ();
379 
385  bool cleanupProfile(const QString &profileId);
386 
387  bool clientProfileActive(const QString &clientProfileName);
388 
394  void removeExternalSyncStatus(const SyncProfile *aProfile);
395 
400  bool acceptScheduledSync(bool aConnected, Sync::InternetConnectionType aType) const;
401 
402  QMap<QString, SyncSession*> iActiveSessions;
403 
404  QMap<QString, bool> iExternalSyncProfileStatus;
405 
406  QList<QString> iProfilesToRemove;
407 
409 
410  QList<QString> iWaitingOnlineSyncs;
411 
412  NetworkManager *iNetworkManager;
413 
414  QMap<QString, int> iCountersStorage;
415 
416  PluginManager iPluginManager;
417 
418  ProfileManager iProfileManager;
419 
420  SyncQueue iSyncQueue;
421 
422  StorageBooker iStorageBooker;
423 
424  SyncScheduler *iSyncScheduler;
425 
426  SyncBackup *iSyncBackup;
427 
428  TransportTracker *iTransportTracker;
429 
430  ServerActivator *iServerActivator;
431 
432  AccountsHelper *iAccounts;
433 
434  bool iClosing;
435 
436  SyncOnChange iSyncOnChange;
437 
438  SyncOnChangeScheduler iSyncOnChangeScheduler;
439 
444  void saveProfileCounter(const SyncProfile* aProfile);
445 
450  void restoreProfileCounter(SyncProfile* aProfile);
451 
452  bool iSOCEnabled;
453 
454  QString iUUID;
455 
456  QString iRemoteName;
457 
458  /*
459  * Temporary, until we can clean up Buteo and properly implement the SyncOnChange
460  * queue to handle all of the required changes (account + profile + connectivity)
461  * in a sane manner (also taking into account BackupRestore status).
462  * However, that change will be far more invasive, so for now this is much simpler.
463  */
464  QList<QPair<QString, ProfileManager::ProfileChangeType> > iProfileChangeTriggerQueue;
465  QTimer iProfileChangeTriggerTimer;
466 
467 #ifdef SYNCFW_UNIT_TESTS
468  friend class SynchronizerTest;
469 #endif
470 
471  QDBusInterface *iSyncUIInterface;
472  _GSettings *iSettings;
473  BatteryInfo *iBatteryInfo;
474 };
475 
476 }
477 
478 #endif // SYNCHRONIZER_H
virtual QStringList runningSyncs()
Definition: synchronizer.cpp:1033
virtual Profile * getSyncProfileByRemoteAddress(const QString &aAddress)
Definition: synchronizer.cpp:2018
void restoreFinished()
Called when backup is restored.
Definition: synchronizer.cpp:1743
This class represents a single profile, a collection of settings or data releated to some entity...
Definition: Profile.h:52
virtual bool startSync(QString aProfileName)
Definition: synchronizer.cpp:316
Definition: SyncOnChangeScheduler.h:15
Manages plugins.
Definition: PluginManager.h:91
void isSyncedExternally(unsigned int aAccountId, const QString aClientProfileName)
Queries the sync externally status of a given account, &#39;syncedExternallyStatus&#39; signal is emitted wit...
Definition: synchronizer.cpp:2151
virtual void releaseStorages(QStringList aStorageNames)
Definition: synchronizer.cpp:1025
void close()
stops the daemon and unregisters the dbus object
Definition: synchronizer.cpp:268
this class initiates a sync if there are changes in storage(s) it&#39;s asked to monitor ...
Definition: SyncOnChange.h:19
virtual bool requestStorages(QStringList aStorageNames)
Definition: synchronizer.cpp:1018
int status(unsigned int aAccountId, int &aFailedReason, qlonglong &aPrevSyncTime, qlonglong &aNextSyncTime)
Returns the status of the sync for the given account Id.
Definition: synchronizer.cpp:1780
QList< unsigned int > syncingAccounts()
Returns the list of account IDs for which sync is ongoing.
Definition: synchronizer.cpp:1840
virtual QStringList allVisibleSyncProfiles()
Gets all visible sync profiles.
Definition: synchronizer.cpp:1901
Definition: AccountsHelper.h:31
Synchronizer(QCoreApplication *aApplication)
The contructor.
Definition: synchronizer.cpp:88
void storageReleased()
emitted by releaseStorages call
Definition: moc_synchronizer.cpp:420
Class for queuing sync sessions.
Definition: SyncQueue.h:37
void stop(unsigned int aAccountId)
Stops sync for all profiles matching the given account ID.
Definition: synchronizer.cpp:1768
virtual StoragePlugin * createStorage(const QString &aPluginName)
Definition: synchronizer.cpp:1117
virtual QString createSyncProfileForAccount(uint aAccountId)
Definition: synchronizer.cpp:376
Keeps track of which server plug-ins should be enabled.
Definition: ServerActivator.h:47
virtual bool setSyncSchedule(QString aProfileId, QString aScheduleAsXml)
Definition: synchronizer.cpp:352
virtual bool isConnectivityAvailable(Sync::ConnectivityType aType)
Definition: synchronizer.cpp:1163
void syncDone(const QString &aProfileName)
emit this signal when the sync session is completed, this is useful when the session status is not im...
Definition: moc_synchronizer.cpp:426
virtual bool requestStorage(const QString &aStorageName, const SyncPluginBase *aCaller)
Definition: synchronizer.cpp:1099
The main entry point to the synchronization framework.
Definition: synchronizer.h:66
Defines a D-Bus interface for the sync daemon.
Definition: msyncd/SyncDBusInterface.h:41
virtual ~Synchronizer()
Destructor.
Definition: synchronizer.cpp:109
Helper Class towards Accounts::Manager and various SSO related operations.
Definition: AccountsHelper.h:43
ProfileManager is responsible for storing and retrieving the profiles.
Definition: ProfileManager.h:45
virtual void releaseStorage(const QString &aStorageName, const SyncPluginBase *aCaller)
Definition: synchronizer.cpp:1108
virtual QString getLastSyncResult(const QString &aProfileId)
To get lastSyncResult.
Definition: synchronizer.cpp:1875
Class representing a single sync session.
Definition: SyncSession.h:43
virtual bool saveSyncResults(QString aProfileId, QString aSyncResults)
Definition: synchronizer.cpp:362
SyncScheduler Object to be used to set Schedule via the framework.
Definition: SyncScheduler.h:52
Interface which client and server plugins can use to communicate with synchronization daemon...
Definition: PluginCbInterface.h:38
virtual bool getBackUpRestoreState()
Called to get the current backup/restore state.
Definition: synchronizer.cpp:1750
Handles Sync requirements towards Backup.
Definition: SyncBackup.h:37
virtual QString syncProfile(const QString &aProfileId)
Gets a sync profile.
Definition: synchronizer.cpp:1922
virtual bool updateProfile(QString aProfileAsXml)
Definition: synchronizer.cpp:966
virtual void destroyStorage(StoragePlugin *aStorage)
Definition: synchronizer.cpp:1156
void backupFinished()
Called when backup is completed.
Definition: synchronizer.cpp:1729
Class for managing network sessions.
Definition: NetworkManager.h:43
void restoreStarts()
Called when starting to restore a backup.
Definition: synchronizer.cpp:1736
Base class for client and server plugins.
Definition: SyncPluginBase.h:45
A top level synchronization profile.
Definition: SyncProfile.h:47
bool initialize()
registers the dbus service and creates handlers for various tasks of the synchronizer ...
Definition: synchronizer.cpp:121
Class for tracking transport states.
Definition: TransportTracker.h:47
A helper class for managing storage reservations.
Definition: StorageBooker.h:36
Base class for storage plugins.
Definition: StoragePlugin.h:38
virtual bool removeProfile(QString aProfileAsXml)
Definition: synchronizer.cpp:945
bool startScheduledSync(QString aProfileName)
Called starts a schedule sync.
Definition: synchronizer.cpp:324
virtual void abortSync(QString aProfileName)
Definition: synchronizer.cpp:847
Definition: SyncBackupAdaptor.h:41
void backupStarts()
Called when backup starts.
Definition: synchronizer.cpp:1722
Definition: SyncBackupAdaptor.h:40
virtual QString getValue(const QString &aAddress, const QString &aKey)
Definition: synchronizer.cpp:2042