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-2019 Jolla Ltd.
6  * Copyright (C) 2020 Open Mobile Platform LLC.
7  *
8  * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * version 2.1 as published by the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  */
25 #ifndef SYNCHRONIZER_H
26 #define SYNCHRONIZER_H
27 
28 #include "SyncDBusInterface.h"
29 #include "SyncQueue.h"
30 #include "StorageBooker.h"
31 #include "SyncScheduler.h"
32 #include "SyncBackup.h"
33 #include "SyncOnChange.h"
34 #include "SyncOnChangeScheduler.h"
35 
36 #include "SyncCommonDefs.h"
37 #include "ProfileManager.h"
38 #include "PluginManager.h"
39 #include "PluginCbInterface.h"
40 #include "ClientPlugin.h"
41 
42 #include <QVector>
43 #include <QMutex>
44 #include <QCoreApplication>
45 #include <QMap>
46 #include <QString>
47 #include <QDBusInterface>
48 #include <QScopedPointer>
49 #include <QTimer>
50 
51 struct _GSettings;
52 
53 namespace Buteo {
54 
55 class PluginManager;
56 class ServerPluginRunner;
57 class NetworkManager;
58 class TransportTracker;
59 class ServerActivator;
60 class AccountsHelper;
61 class BatteryInfo;
62 
67 class Synchronizer : public SyncDBusInterface, // Derived from QObject
68  public PluginCbInterface
69 {
70  Q_OBJECT
71 public:
72 
74  Synchronizer(QCoreApplication *aApplication);
75 
77  virtual ~Synchronizer();
78 
81  bool initialize();
82 
84  void close();
85 
86 
87 // From PluginCbInterface
88 // ---------------------------------------------------------------------------
90  virtual bool requestStorage(const QString &aStorageName,
91  const SyncPluginBase *aCaller);
92 
94  virtual void releaseStorage(const QString &aStorageName,
95  const SyncPluginBase *aCaller);
96 
98  virtual StoragePlugin* createStorage(const QString &aPluginName);
99 
101  virtual void destroyStorage(StoragePlugin *aStorage);
102 
104  virtual bool isConnectivityAvailable( Sync::ConnectivityType aType );
105 
107  virtual Profile* getSyncProfileByRemoteAddress(const QString& aAddress);
108 
110  virtual QString getValue(const QString& aAddress, const QString& aKey);
111 
112 
113 // From SyncDBusInterface
114 // --------------------------------------------------------------------------
115 
116 public slots:
117 
119  virtual bool startSync(QString aProfileName);
120 
122  virtual void abortSync(QString aProfileName);
123 
125  virtual bool removeProfile(QString aProfileAsXml);
126 
128  virtual bool updateProfile(QString aProfileAsXml);
129 
131  virtual bool requestStorages(QStringList aStorageNames);
132 
134  virtual void releaseStorages(QStringList aStorageNames);
135 
137  virtual QStringList runningSyncs();
138 
140  virtual bool setSyncSchedule(QString aProfileId , QString aScheduleAsXml);
141 
143  virtual bool saveSyncResults(QString aProfileId,QString aSyncResults);
144 
146  virtual QString createSyncProfileForAccount(uint aAccountId);
147 
152  virtual QString getLastSyncResult(const QString &aProfileId);
153 
160  virtual QStringList allVisibleSyncProfiles();
161 
170  virtual QString syncProfile(const QString &aProfileId);
171  virtual QStringList syncProfilesByKey(const QString &aKey, const QString &aValue);
172  virtual QStringList syncProfilesByType(const QString &aType);
173 // --------------------------------------------------------------------------
174 
176  bool startScheduledSync(QString aProfileName);
177 
179  void backupStarts();
180 
182  void backupFinished();
183 
185  void restoreStarts();
186 
188  void restoreFinished();
189 
191  virtual bool getBackUpRestoreState();
192 
193  void start(unsigned int aAccountId);
194 
199  void stop(unsigned int aAccountId);
200 
206 
220  int status(unsigned int aAccountId, int &aFailedReason, qlonglong &aPrevSyncTime, qlonglong &aNextSyncTime);
221 
230  void isSyncedExternally(unsigned int aAccountId, const QString aClientProfileName);
231 
232 signals:
233 
235  void storageReleased();
236 
242  void syncDone(const QString &aProfileName);
243 
244 private slots:
245 
251  void onStorageReleased();
252 
253  void onTransferProgress( const QString &aProfileName,
254  Sync::TransferDatabase aDatabase, Sync::TransferType aType,
255  const QString &aMimeType, int aCommittedItems );
256 
257  void onSessionFinished( const QString &aProfileName,
258  Sync::SyncStatus aStatus, const QString &aMessage, int aErrorCode );
259 
260  void onStorageAccquired(const QString &aProfileName, const QString &aMimeType);
261 
262  void onSyncProgressDetail(const QString &aProfileName,int aProgressDetail);
263 
264  void onServerDone();
265 
266  void onNewSession(const QString &aDestination);
267 
268  void slotProfileChanged(QString aProfileName, int aChangeType , QString aProfileAsXml);
269 
274  void startServer(const QString &aProfileName);
275 
280  void stopServer(const QString &aProfileName);
281 
282  void onNetworkStateChanged(bool aState, Sync::InternetConnectionType type);
283 
290  void enableSOCSlot(const QString& aProfileName);
291 
296  void reschedule(const QString &aProfileName);
297 
305  void slotSyncStatus(QString aProfileName, int aStatus,
306  QString aMessage, int aMoreDetails);
307 
312  void removeScheduledSync(const QString &aProfileName);
313 
319  void externalSyncStatus(const QString &aProfileName, bool aQuery);
320 
322  void profileChangeTriggerTimeout();
323 
324 private:
325 
326  bool startSync(const QString &aProfileName, bool aScheduled);
327 
333  bool startSyncNow(SyncSession *aSession);
334 
341  bool startNextSync();
342 
348  void cleanupSession(SyncSession *aSession, Sync::SyncStatus aStatus);
349 
354  void startServers( bool resume = false );
355 
360  void stopServers( bool suspend = false );
361 
365  void backupRestoreStarts ();
366 
370  void backupRestoreFinished();
371 
375  void initializeScheduler();
376 
377  bool isBackupRestoreInProgress ();
378 
384  bool cleanupProfile(const QString &profileId);
385 
386  bool clientProfileActive(const QString &clientProfileName);
387 
393  void removeExternalSyncStatus(const SyncProfile *aProfile);
394 
399  bool acceptScheduledSync(bool aConnected, Sync::InternetConnectionType aType, SyncProfile *profile) const;
400 
408  void externalSyncStatus(const SyncProfile *aProfile, bool aQuery=false);
409 
410  QMap<QString, SyncSession*> iActiveSessions;
411 
412  QMap<QString, bool> iExternalSyncProfileStatus;
413 
414  QList<QString> iProfilesToRemove;
415 
417 
418  QList<QString> iWaitingOnlineSyncs;
419 
420  NetworkManager *iNetworkManager;
421 
422  QMap<QString, int> iCountersStorage;
423 
424  PluginManager iPluginManager;
425 
426  ProfileManager iProfileManager;
427 
428  SyncQueue iSyncQueue;
429 
430  StorageBooker iStorageBooker;
431 
432  SyncScheduler *iSyncScheduler;
433 
434  SyncBackup *iSyncBackup;
435 
436  TransportTracker *iTransportTracker;
437 
438  ServerActivator *iServerActivator;
439 
440  AccountsHelper *iAccounts;
441 
442  bool iClosing;
443 
444  SyncOnChange iSyncOnChange;
445 
446  SyncOnChangeScheduler iSyncOnChangeScheduler;
447 
452  void saveProfileCounter(const SyncProfile* aProfile);
453 
458  void restoreProfileCounter(SyncProfile* aProfile);
459 
460  bool iSOCEnabled;
461 
462  QString iUUID;
463 
464  QString iRemoteName;
465 
466  /*
467  * Temporary, until we can clean up Buteo and properly implement the SyncOnChange
468  * queue to handle all of the required changes (account + profile + connectivity)
469  * in a sane manner (also taking into account BackupRestore status).
470  * However, that change will be far more invasive, so for now this is much simpler.
471  */
472  QList<QPair<QString, ProfileManager::ProfileChangeType> > iProfileChangeTriggerQueue;
473  QTimer iProfileChangeTriggerTimer;
474 
475 #ifdef SYNCFW_UNIT_TESTS
476  friend class SynchronizerTest;
477 #endif
478 
479  QDBusInterface *iSyncUIInterface;
480  _GSettings *iSettings;
481  BatteryInfo *iBatteryInfo;
482 };
483 
484 }
485 
486 #endif // SYNCHRONIZER_H
Buteo::AccountsHelper
Helper Class towards Accounts::Manager and various SSO related operations.
Definition: AccountsHelper.h:43
Buteo::Synchronizer::createStorage
virtual StoragePlugin * createStorage(const QString &aPluginName)
Definition: synchronizer.cpp:1156
Buteo::SyncDBusInterface
Defines a D-Bus interface for the sync daemon.
Definition: msyncd/SyncDBusInterface.h:41
Buteo::Synchronizer::setSyncSchedule
virtual bool setSyncSchedule(QString aProfileId, QString aScheduleAsXml)
Definition: synchronizer.cpp:379
Buteo::Synchronizer::~Synchronizer
virtual ~Synchronizer()
Destructor.
Definition: synchronizer.cpp:95
Buteo::SyncOnChange
this class initiates a sync if there are changes in storage(s) it's asked to monitor
Definition: SyncOnChange.h:19
Buteo::TransportTracker
Class for tracking transport states.
Definition: TransportTracker.h:44
Buteo::Synchronizer::createSyncProfileForAccount
virtual QString createSyncProfileForAccount(uint aAccountId)
Definition: synchronizer.cpp:403
Buteo::Synchronizer::isSyncedExternally
void isSyncedExternally(unsigned int aAccountId, const QString aClientProfileName)
Queries the sync externally status of a given account, 'syncedExternallyStatus' signal is emitted wit...
Definition: synchronizer.cpp:2242
Buteo::Synchronizer::isConnectivityAvailable
virtual bool isConnectivityAvailable(Sync::ConnectivityType aType)
Definition: synchronizer.cpp:1202
Buteo::PluginManager
Manages plugins.
Definition: PluginManager.h:67
Buteo::Synchronizer::getBackUpRestoreState
virtual bool getBackUpRestoreState()
Called to get the current backup/restore state.
Definition: synchronizer.cpp:1802
QList
Definition: SyncBackupAdaptor.h:40
Buteo::SyncQueue
Class for queuing sync sessions.
Definition: SyncQueue.h:37
Buteo::Synchronizer::syncProfile
virtual QString syncProfile(const QString &aProfileId)
Gets a sync profile.
Definition: synchronizer.cpp:1974
Buteo::Synchronizer::requestStorages
virtual bool requestStorages(QStringList aStorageNames)
Definition: synchronizer.cpp:1057
Buteo::StoragePlugin
Base class for storage plugins.
Definition: StoragePlugin.h:38
Buteo::Synchronizer
The main entry point to the synchronization framework.
Definition: synchronizer.h:67
Buteo::Synchronizer::syncDone
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
QMap
Definition: SyncBackupAdaptor.h:41
Buteo::Synchronizer::getValue
virtual QString getValue(const QString &aAddress, const QString &aKey)
Definition: synchronizer.cpp:2098
Buteo::SyncProfile
A top level synchronization profile.
Definition: SyncProfile.h:48
Buteo::Synchronizer::initialize
bool initialize()
registers the dbus service and creates handlers for various tasks of the synchronizer
Definition: synchronizer.cpp:107
Buteo::Synchronizer::startScheduledSync
bool startScheduledSync(QString aProfileName)
Called starts a schedule sync.
Definition: synchronizer.cpp:312
Buteo::Synchronizer::removeProfile
virtual bool removeProfile(QString aProfileAsXml)
Definition: synchronizer.cpp:984
Buteo::ServerActivator
Keeps track of which server plug-ins should be enabled.
Definition: ServerActivator.h:47
Buteo::Synchronizer::backupFinished
void backupFinished()
Called when backup is completed.
Definition: synchronizer.cpp:1781
Buteo::Synchronizer::backupStarts
void backupStarts()
Called when backup starts.
Definition: synchronizer.cpp:1774
Buteo::Synchronizer::Synchronizer
Synchronizer(QCoreApplication *aApplication)
The contructor.
Definition: synchronizer.cpp:74
Buteo::Synchronizer::releaseStorages
virtual void releaseStorages(QStringList aStorageNames)
Definition: synchronizer.cpp:1064
Buteo::Synchronizer::getSyncProfileByRemoteAddress
virtual Profile * getSyncProfileByRemoteAddress(const QString &aAddress)
Definition: synchronizer.cpp:2074
Buteo::SyncPluginBase
Base class for client and server plugins.
Definition: SyncPluginBase.h:45
Buteo::StorageBooker
A helper class for managing storage reservations.
Definition: StorageBooker.h:36
Buteo::Synchronizer::allVisibleSyncProfiles
virtual QStringList allVisibleSyncProfiles()
Gets all visible sync profiles.
Definition: synchronizer.cpp:1953
Buteo::ProfileManager
ProfileManager is responsible for storing and retrieving the profiles.
Definition: ProfileManager.h:45
Buteo::Profile
This class represents a single profile, a collection of settings or data releated to some entity.
Definition: Profile.h:52
Buteo::Synchronizer::getLastSyncResult
virtual QString getLastSyncResult(const QString &aProfileId)
To get lastSyncResult.
Definition: synchronizer.cpp:1927
Buteo::Synchronizer::abortSync
virtual void abortSync(QString aProfileName)
Definition: synchronizer.cpp:886
Buteo::Synchronizer::storageReleased
void storageReleased()
emitted by releaseStorages call
Definition: moc_synchronizer.cpp:420
Buteo::Synchronizer::stop
void stop(unsigned int aAccountId)
Stops sync for all profiles matching the given account ID.
Definition: synchronizer.cpp:1820
Buteo::Synchronizer::updateProfile
virtual bool updateProfile(QString aProfileAsXml)
Definition: synchronizer.cpp:1005
Buteo::Synchronizer::status
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:1832
Buteo::Synchronizer::close
void close()
stops the daemon and unregisters the dbus object
Definition: synchronizer.cpp:256
Buteo::Synchronizer::requestStorage
virtual bool requestStorage(const QString &aStorageName, const SyncPluginBase *aCaller)
Definition: synchronizer.cpp:1138
Buteo::SyncScheduler
SyncScheduler Object to be used to set Schedule via the framework.
Definition: SyncScheduler.h:54
Buteo::Synchronizer::saveSyncResults
virtual bool saveSyncResults(QString aProfileId, QString aSyncResults)
Definition: synchronizer.cpp:389
Buteo::Synchronizer::runningSyncs
virtual QStringList runningSyncs()
Definition: synchronizer.cpp:1072
Buteo::Synchronizer::syncingAccounts
QList< unsigned int > syncingAccounts()
Returns the list of account IDs for which sync is ongoing.
Definition: synchronizer.cpp:1892
Buteo::Synchronizer::restoreFinished
void restoreFinished()
Called when backup is restored.
Definition: synchronizer.cpp:1795
Buteo::Synchronizer::startSync
virtual bool startSync(QString aProfileName)
Definition: synchronizer.cpp:304
Buteo::SyncBackup
Handles Sync requirements towards Backup.
Definition: SyncBackup.h:37
Buteo::Synchronizer::releaseStorage
virtual void releaseStorage(const QString &aStorageName, const SyncPluginBase *aCaller)
Definition: synchronizer.cpp:1147
Buteo::Synchronizer::destroyStorage
virtual void destroyStorage(StoragePlugin *aStorage)
Definition: synchronizer.cpp:1195
Buteo::SyncOnChangeScheduler
Definition: SyncOnChangeScheduler.h:15
Buteo::SyncSession
Class representing a single sync session.
Definition: SyncSession.h:43
Buteo::Synchronizer::restoreStarts
void restoreStarts()
Called when starting to restore a backup.
Definition: synchronizer.cpp:1788
Buteo::NetworkManager
Class for managing network sessions.
Definition: NetworkManager.h:43
Buteo::PluginCbInterface
Interface which client and server plugins can use to communicate with synchronization daemon.
Definition: PluginCbInterface.h:38