Make FolderMan a singleton

It doesn't "belong" to application (think about lib use cases).
This is better.
This commit is contained in:
Daniel Molkentin 2013-07-22 13:59:52 +02:00
parent 5fe4d2db2b
commit a5e7af6c1f
10 changed files with 90 additions and 77 deletions

View file

@ -37,10 +37,9 @@
namespace Mirall {
AccountSettings::AccountSettings(FolderMan *folderMan, QWidget *parent) :
AccountSettings::AccountSettings(QWidget *parent) :
QWidget(parent),
ui(new Ui::AccountSettings),
_folderMan(folderMan),
_item(0)
{
ui->setupUi(this);
@ -76,7 +75,7 @@ AccountSettings::AccountSettings(FolderMan *folderMan, QWidget *parent) :
ui->connectLabel->setWordWrap( true );
setFolderList(folderMan->map());
setFolderList(FolderMan::instance()->map());
slotCheckConnection();
}
@ -106,10 +105,11 @@ void AccountSettings::slotFolderActivated( const QModelIndex& indx )
void AccountSettings::slotAddFolder()
{
_folderMan->setSyncEnabled(false); // do not start more syncs.
FolderMan *folderMan = FolderMan::instance();
folderMan->setSyncEnabled(false); // do not start more syncs.
FolderWizard *folderWizard = new FolderWizard(this);
Folder::Map folderMap = _folderMan->map();
Folder::Map folderMap = folderMan->map();
folderWizard->setFolderMap( folderMap );
connect(folderWizard, SIGNAL(accepted()), SLOT(slotFolderWizardAccepted()));
@ -121,6 +121,7 @@ void AccountSettings::slotAddFolder()
void AccountSettings::slotFolderWizardAccepted()
{
FolderWizard *folderWizard = qobject_cast<FolderWizard*>(sender());
FolderMan *folderMan = FolderMan::instance();
qDebug() << "* Folder wizard completed";
@ -131,12 +132,12 @@ void AccountSettings::slotFolderWizardAccepted()
if (!FolderMan::ensureJournalGone( sourceFolder ))
return;
_folderMan->addFolderDefinition( backend, alias, sourceFolder, targetPath, false );
Folder *f = _folderMan->setupFolderFromConfigFile( alias );
folderMan->addFolderDefinition( backend, alias, sourceFolder, targetPath, false );
Folder *f = folderMan->setupFolderFromConfigFile( alias );
slotAddFolder( f );
_folderMan->setSyncEnabled(true);
folderMan->setSyncEnabled(true);
if( f ) {
_folderMan->slotScheduleAllFolders();
folderMan->slotScheduleAllFolders();
emit folderChanged();
}
}
@ -144,13 +145,14 @@ void AccountSettings::slotFolderWizardAccepted()
void AccountSettings::slotFolderWizardRejected()
{
qDebug() << "* Folder wizard cancelled";
_folderMan->setSyncEnabled(true);
_folderMan->slotScheduleAllFolders();
FolderMan *folderMan = FolderMan::instance();
folderMan->setSyncEnabled(true);
folderMan->slotScheduleAllFolders();
}
void AccountSettings::slotOpenAccountWizard()
{
OwncloudSetupWizard::runWizard(_folderMan, qApp, SLOT(slotownCloudWizardDone(int)), this);
OwncloudSetupWizard::runWizard(qApp, SLOT(slotownCloudWizardDone(int)), this);
}
void AccountSettings::slotAddFolder( Folder *folder )
@ -234,8 +236,9 @@ void AccountSettings::slotRemoveCurrentFolder()
if( ret == QMessageBox::No ) {
return;
}
_folderMan->slotRemoveFolder( alias );
setFolderList(_folderMan->map());
FolderMan *folderMan = FolderMan::instance();
folderMan->slotRemoveFolder( alias );
setFolderList(folderMan->map());
emit folderChanged();
slotCheckConnection();
}
@ -252,10 +255,11 @@ void AccountSettings::slotResetCurrentFolder()
"traffic and take several minutes to hours, depending on the size of the folder.</p>").arg(alias),
QMessageBox::Yes|QMessageBox::No );
if( ret == QMessageBox::Yes ) {
Folder *f = _folderMan->folder(alias);
FolderMan *folderMan = FolderMan::instance();
Folder *f = folderMan->folder(alias);
f->slotTerminateSync();
f->wipe();
_folderMan->slotScheduleAllFolders();
folderMan->slotScheduleAllFolders();
}
}
}
@ -304,7 +308,7 @@ void AccountSettings::setFolderList( const Folder::Map &folders )
// move from Application
void AccountSettings::slotFolderOpenAction( const QString& alias )
{
Folder *f = _folderMan->folder(alias);
Folder *f = FolderMan::instance()->folder(alias);
qDebug() << "opening local url " << f->path();
if( f ) {
QUrl url(f->path(), QUrl::TolerantMode);
@ -332,12 +336,13 @@ void AccountSettings::slotEnableCurrentFolder()
bool folderEnabled = _model->data( selected, FolderStatusDelegate::FolderSyncEnabled).toBool();
qDebug() << "Toggle enabled/disabled Folder alias " << alias << " - current state: " << folderEnabled;
if( !alias.isEmpty() ) {
FolderMan *folderMan = FolderMan::instance();
qDebug() << "Application: enable folder with alias " << alias;
bool terminate = false;
// this sets the folder status to disabled but does not interrupt it.
Folder *f = _folderMan->folder( alias );
Folder *f = folderMan->folder( alias );
if( f && !folderEnabled ) {
// check if a sync is still running and if so, ask if we should terminate.
if( f->isBusy() ) { // its still running
@ -354,9 +359,9 @@ void AccountSettings::slotEnableCurrentFolder()
// message box can return at any time while the thread keeps running,
// so better check again after the user has responded.
if ( f->isBusy() && terminate )
_folderMan->terminateSyncProcess( alias );
folderMan->terminateSyncProcess( alias );
_folderMan->slotEnableFolder( alias, !folderEnabled );
folderMan->slotEnableFolder( alias, !folderEnabled );
slotUpdateFolderState (f);
// set the button text accordingly.
slotFolderActivated( selected );
@ -383,7 +388,7 @@ void AccountSettings::slotUpdateFolderState( Folder *folder )
if( !_fileItemDialog.isNull() && _fileItemDialog->isVisible() ) {
_fileItemDialog->setSyncResult( _folderMan->syncResult(folder) );
_fileItemDialog->setSyncResult( FolderMan::instance()->syncResult(folder) );
}
if( item ) {
@ -530,7 +535,7 @@ void AccountSettings::slotSetProgress( Progress::Kind kind, const QString& folde
if(shortFile.startsWith(QLatin1String("ownclouds://")) ||
shortFile.startsWith(QLatin1String("owncloud://")) ) {
// rip off the whole ownCloud URL.
Folder *f = _folderMan->folder(folder);
Folder *f = FolderMan::instance()->folder(folder);
if( f ) {
QString regexp = QString("^owncloud[s]*://.*/remote.php/webdav/%1/").arg(f->secondPath());
QRegExp re( regexp );
@ -611,9 +616,7 @@ void AccountSettings::slotInfoAboutCurrentFolder()
Utility::raiseDialog( _fileItemDialog );
}
_fileItemDialog->setSyncResult( _folderMan->syncResult( alias ) );
_fileItemDialog->setSyncResult( FolderMan::instance()->syncResult( alias ) );
}
}
}

View file

@ -45,7 +45,7 @@ class AccountSettings : public QWidget
Q_OBJECT
public:
explicit AccountSettings(FolderMan *folderMan, QWidget *parent = 0);
explicit AccountSettings(QWidget *parent = 0);
~AccountSettings();
void setFolderList( const Folder::Map& );
@ -90,7 +90,6 @@ private:
Ui::AccountSettings *ui;
QPointer<FileItemDialog> _fileItemDialog;
QPointer<IgnoreListEditor> _ignoreEditor;
FolderMan *_folderMan;
QStandardItemModel *_model;
QListWidgetItem *_item;
QUrl _OCUrl;

View file

@ -19,6 +19,7 @@
#include "mirall/application.h"
#include "mirall/systray.h"
#include "mirall/folder.h"
#include "mirall/folderman.h"
#include "mirall/folderwatcher.h"
#include "mirall/folderwizard.h"
#include "mirall/networklocation.h"
@ -122,10 +123,10 @@ Application::Application(int &argc, char **argv) :
connect( Logger::instance(), SIGNAL(optionalGuiLog(QString,QString)),
this, SLOT(slotShowOptionalTrayMessage(QString,QString)));
// create folder manager for sync folder management
_folderMan = new FolderMan(this);
connect( _folderMan, SIGNAL(folderSyncStateChange(QString)),
FolderMan *folderMan = FolderMan::instance();
connect( folderMan, SIGNAL(folderSyncStateChange(QString)),
this,SLOT(slotSyncStateChange(QString)));
_folderMan->setSyncEnabled(false);
folderMan->setSyncEnabled(false);
/* use a signal mapper to map the open requests to the alias names */
_folderOpenActionMapper = new QSignalMapper(this);
@ -153,7 +154,7 @@ Application::Application(int &argc, char **argv) :
setupSystemTray();
slotSetupProxy();
int cnt = _folderMan->setupFolders();
int cnt = folderMan->setupFolders();
// startup procedure.
QTimer::singleShot( 0, this, SLOT( slotCheckConnection() ));
@ -198,17 +199,18 @@ void Application::slotConnectionValidatorResult(ConnectionValidator::Status stat
qDebug() << "Connection Validator Result: " << _conValidator->statusString(status);
if( status == ConnectionValidator::Connected ) {
FolderMan *folderMan = FolderMan::instance();
qDebug() << "######## Connection and Credentials are ok!";
_folderMan->setSyncEnabled(true);
folderMan->setSyncEnabled(true);
_tray->setIcon( _theme->syncStateIcon( SyncResult::NotYetStarted, true ) );
_tray->show();
int cnt = _folderMan->map().size();
int cnt = folderMan->map().size();
slotShowTrayMessage(tr("%1 Sync Started").arg(_theme->appNameGUI()),
tr("Sync started for %n configured sync folder(s).","", cnt));
// queue up the sync for all folders.
_folderMan->slotScheduleAllFolders();
folderMan->slotScheduleAllFolders();
computeOverallSyncStatus();
@ -257,15 +259,16 @@ void Application::slotSSLFailed( QNetworkReply *reply, QList<QSslError> errors )
void Application::slotownCloudWizardDone( int res )
{
FolderMan *folderMan = FolderMan::instance();
if( res == QDialog::Accepted ) {
int cnt = _folderMan->setupFolders();
int cnt = folderMan->setupFolders();
qDebug() << "Set up " << cnt << " folders.";
// We have some sort of configuration. Enable autostart
Utility::setLaunchOnStartup(_theme->appName(), _theme->appNameGUI(), true);
// FIXME!
// _statusDialog->setFolderList( _folderMan->map() );
// _statusDialog->setFolderList( folderMan->map() );
}
_folderMan->setSyncEnabled( true );
folderMan->setSyncEnabled( true );
if( res == QDialog::Accepted ) {
slotCheckConnection();
}
@ -304,6 +307,7 @@ void Application::setupSystemTray()
void Application::setupContextMenu()
{
bool isConfigured = ownCloudInfo::instance()->isConfigured();
FolderMan *folderMan = FolderMan::instance();
_actionOpenoC->setEnabled(isConfigured);
@ -318,13 +322,13 @@ void Application::setupContextMenu()
_contextMenu->setTitle(_theme->appNameGUI() );
_contextMenu->addAction(_actionOpenoC);
int folderCnt = _folderMan->map().size();
int folderCnt = folderMan->map().size();
// add open actions for all sync folders to the tray menu
if( _theme->singleSyncFolder() ) {
// there should be exactly one folder. No sync-folder add action will be shown.
QStringList li = _folderMan->map().keys();
QStringList li = folderMan->map().keys();
if( li.size() == 1 ) {
Folder *folder = _folderMan->map().value(li.first());
Folder *folder = folderMan->map().value(li.first());
if( folder ) {
// if there is singleFolder mode, a generic open action is displayed.
QAction *action = new QAction( tr("Open local folder '%1'").arg(_theme->appNameGUI()), this);
@ -341,7 +345,7 @@ void Application::setupContextMenu()
if ( folderCnt > 1) {
_contextMenu->addAction(tr("Managed Folders:"))->setDisabled(true);
}
foreach (Folder *folder, _folderMan->map() ) {
foreach (Folder *folder, folderMan->map() ) {
QAction *action = new QAction( tr("Open folder '%1'").arg(folder->alias()), this );
connect( action, SIGNAL(triggered()),_folderOpenActionMapper,SLOT(map()));
_folderOpenActionMapper->setMapping( action, folder->alias() );
@ -487,7 +491,7 @@ void Application::slotHelp()
*/
void Application::slotFolderOpenAction( const QString& alias )
{
Folder *f = _folderMan->folder(alias);
Folder *f = FolderMan::instance()->folder(alias);
qDebug() << "opening local url " << f->path();
if( f ) {
QUrl url(f->path(), QUrl::TolerantMode);
@ -534,7 +538,7 @@ void Application::slotCheckConfig()
slotSettings();
} else {
qDebug() << "No configured folders yet, starting setup wizard";
OwncloudSetupWizard::runWizard(_folderMan, this, SLOT(slotownCloudWizardDone(int)));
OwncloudSetupWizard::runWizard(this, SLOT(slotownCloudWizardDone(int)));
}
}
@ -585,9 +589,9 @@ void Application::slotShowOptionalTrayMessage(const QString &title, const QStrin
void Application::slotSyncStateChange( const QString& alias )
{
SyncResult result = _folderMan->syncResult( alias );
emit folderStateChanged( _folderMan->folder(alias) );
FolderMan *folderMan = FolderMan::instance();
SyncResult result = folderMan->syncResult( alias );
emit folderStateChanged( folderMan->folder(alias) );
computeOverallSyncStatus();
@ -652,7 +656,8 @@ void Application::computeOverallSyncStatus()
// display the info of the least successful sync (eg. not just display the result of the latest sync
QString trayMessage;
Folder::Map map = _folderMan->map();
FolderMan *folderMan = FolderMan::instance();
Folder::Map map = folderMan->map();
SyncResult overallResult = FolderMan::accountStatus(map.values());
// create the tray blob message, check if we have an defined state
@ -660,7 +665,7 @@ void Application::computeOverallSyncStatus()
QStringList allStatusStrings;
foreach(Folder* folder, map.values()) {
qDebug() << "Folder in overallStatus Message: " << folder << " with name " << folder->alias();
QString folderMessage = _folderMan->statusToString(folder->syncResult().status());
QString folderMessage = folderMan->statusToString(folder->syncResult().status());
allStatusStrings += tr("Folder %1: %2").arg(folder->alias(), folderMessage);
}

View file

@ -24,7 +24,6 @@
#include "mirall/syncresult.h"
#include "mirall/logbrowser.h"
#include "mirall/folderman.h"
#include "mirall/systray.h"
#include "mirall/connectionvalidator.h"
@ -48,7 +47,6 @@ class Application : public SharedTools::QtSingleApplication
{
Q_OBJECT
public:
friend class SettingsDialog;
explicit Application(int &argc, char **argv);
~Application();
@ -122,7 +120,6 @@ private:
// tray's menu
QMenu *_contextMenu;
FolderMan *_folderMan;
Theme *_theme;
QSignalMapper *_folderOpenActionMapper;
LogBrowser *_logBrowser;

View file

@ -33,6 +33,8 @@
namespace Mirall {
FolderMan* FolderMan::_instance = 0;
FolderMan::FolderMan(QObject *parent) :
QObject(parent),
_syncEnabled( true )
@ -49,6 +51,14 @@ FolderMan::FolderMan(QObject *parent) :
this, SIGNAL(folderSyncStateChange(const QString &)));
}
FolderMan *FolderMan::instance()
{
if(!_instance)
_instance = new FolderMan;
return _instance;
}
FolderMan::~FolderMan()
{
qDeleteAll(_folderMap);

View file

@ -34,7 +34,7 @@ class FolderMan : public QObject
{
Q_OBJECT
public:
explicit FolderMan(QObject *parent = 0);
static FolderMan* instance();
~FolderMan();
int setupFolders();
@ -153,6 +153,10 @@ private:
QString _currentSyncFolder;
QStringList _scheduleQueue;
bool _syncEnabled;
explicit FolderMan(QObject *parent = 0);
static FolderMan *_instance;
};
} // namespace Mirall

View file

@ -26,11 +26,10 @@
namespace Mirall {
OwncloudSetupWizard::OwncloudSetupWizard( FolderMan *folderMan, QObject *parent ) :
OwncloudSetupWizard::OwncloudSetupWizard(QObject *parent ) :
QObject( parent ),
_mkdirRequestReply(0),
_checkInstallationRequest(0),
_folderMan(folderMan)
_checkInstallationRequest(0)
{
_ocWizard = new OwncloudWizard();
@ -54,11 +53,11 @@ OwncloudWizard *OwncloudSetupWizard::wizard() {
return _ocWizard;
}
void OwncloudSetupWizard::runWizard(FolderMan *folderMan, QObject* obj, const char* amember, QWidget *parent)
void OwncloudSetupWizard::runWizard(QObject* obj, const char* amember, QWidget *parent)
{
OwncloudSetupWizard *wiz = new OwncloudSetupWizard(folderMan, parent);
OwncloudSetupWizard *wiz = new OwncloudSetupWizard(parent);
connect( wiz, SIGNAL(ownCloudWizardDone(int)), obj, amember);
folderMan->setSyncEnabled(false);
FolderMan::instance()->setSyncEnabled(false);
wiz->startWizard();
}
@ -97,7 +96,7 @@ void OwncloudSetupWizard::startWizard()
_ocWizard->restart();
// settings re-initialized in initPage must be set here after restart
_ocWizard->setMultipleFoldersExist(_folderMan->map().count() > 1);
_ocWizard->setMultipleFoldersExist(FolderMan::instance()->map().count() > 1);
_ocWizard->open();
_ocWizard->raise();
@ -109,7 +108,7 @@ void OwncloudSetupWizard::startWizard()
void OwncloudSetupWizard::slotAssistantFinished( int result )
{
MirallConfigFile cfg( _configHandle );
FolderMan *folderMan = FolderMan::instance();
if( result == QDialog::Rejected ) {
// the old config remains valid. Remove the temporary one.
@ -138,16 +137,16 @@ void OwncloudSetupWizard::slotAssistantFinished( int result )
if( urlHasChanged ) {
// first terminate sync jobs.
_folderMan->terminateSyncProcess();
folderMan->terminateSyncProcess();
_folderMan->unloadAllFolders();
folderMan->unloadAllFolders();
bool startFromScratch = _ocWizard->field( "OCSyncFromScratch" ).toBool();
if( startFromScratch ) {
// first try to rename (backup) the current local dir.
bool renameOk = false;
while( !renameOk ) {
renameOk = _folderMan->startFromScratch(localFolder);
renameOk = folderMan->startFromScratch(localFolder);
if( ! renameOk ) {
QMessageBox::StandardButton but;
but = QMessageBox::question( 0, tr("Folder rename failed"),
@ -168,8 +167,8 @@ void OwncloudSetupWizard::slotAssistantFinished( int result )
// Now write the resulting folder definition if folder names are set.
if( acceptCfg && urlHasChanged ) {
_folderMan->removeAllFolderDefinitions();
_folderMan->addFolderDefinition( QLatin1String("owncloud"), Theme::instance()->appName(),
folderMan->removeAllFolderDefinitions();
folderMan->addFolderDefinition( QLatin1String("owncloud"), Theme::instance()->appName(),
localFolder, _remoteFolder, false );
_ocWizard->appendToConfigurationLog(tr("<font color=\"green\"><b>Local sync folder %1 successfully created!</b></font>").arg(localFolder));
} else {
@ -302,8 +301,6 @@ void OwncloudSetupWizard::slotNoOwnCloudFound( QNetworkReply *err )
void OwncloudSetupWizard::setupLocalSyncFolder()
{
if( ! _folderMan ) return;
const QString localFolder = _ocWizard->property("localFolder").toString();
qDebug() << "Setup local sync folder for new oC connection " << localFolder;
QDir fi( localFolder );

View file

@ -29,13 +29,12 @@ namespace Mirall {
class SiteCopyFolder;
class SyncResult;
class ownCloudInfo;
class FolderMan;
class OwncloudSetupWizard : public QObject
{
Q_OBJECT
public:
explicit OwncloudSetupWizard( FolderMan *folderMan, QObject *parent = 0 );
explicit OwncloudSetupWizard(QObject *parent = 0 );
~OwncloudSetupWizard();
@ -60,7 +59,7 @@ public:
OwncloudWizard *wizard();
/** Run the wizard */
static void runWizard( FolderMan *folderMan, QObject *obj, const char* amember, QWidget *parent = 0 );
static void runWizard(QObject *obj, const char* amember, QWidget *parent = 0 );
signals:
// issued if the oC Setup process (owncloud-admin) is finished.
@ -95,8 +94,6 @@ private:
QPointer<QNetworkReply> _checkInstallationRequest;
QPointer<QNetworkReply> _checkRemoteFolderRequest;
FolderMan *_folderMan;
QString _configHandle;
QString _remoteFolder;
};

View file

@ -14,6 +14,7 @@
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "mirall/folderman.h"
#include "mirall/theme.h"
#include "mirall/generalsettings.h"
#include "mirall/accountsettings.h"
@ -39,7 +40,7 @@ QIcon createDummy() {
SettingsDialog::SettingsDialog(Application *app, QWidget *parent) :
QDialog(parent),
_ui(new Ui::SettingsDialog), _folderMan(app->_folderMan)
_ui(new Ui::SettingsDialog)
{
_ui->setupUi(this);
setObjectName("Settings"); // required as group for saveGeometry call
@ -52,12 +53,12 @@ SettingsDialog::SettingsDialog(Application *app, QWidget *parent) :
_ui->labelWidget->addItem(general);
GeneralSettings *generalSettings = new GeneralSettings;
connect(generalSettings, SIGNAL(proxySettingsChanged()), app, SLOT(slotSetupProxy()));
connect(generalSettings, SIGNAL(proxySettingsChanged()), app->_folderMan, SLOT(slotScheduleAllFolders()));
connect(generalSettings, SIGNAL(proxySettingsChanged()), FolderMan::instance(), SLOT(slotScheduleAllFolders()));
_ui->stack->addWidget(generalSettings);
//connect(generalSettings, SIGNAL(resizeToSizeHint()), SLOT(resizeToSizeHint()));
_accountSettings = new AccountSettings(app->_folderMan);
_accountSettings = new AccountSettings(this);
addAccount(tr("Account"), _accountSettings);
slotUpdateAccountState();
@ -112,7 +113,8 @@ void SettingsDialog::closeEvent(QCloseEvent *event)
void SettingsDialog::slotUpdateAccountState()
{
SyncResult state = _folderMan->accountStatus(_folderMan->map().values());
FolderMan *folderMan = FolderMan::instance();
SyncResult state = folderMan->accountStatus(folderMan->map().values());
_accountItem->setIcon(Theme::instance()->syncStateIcon(state.status()));
}

View file

@ -51,7 +51,6 @@ private:
Ui::SettingsDialog *_ui;
AccountSettings *_accountSettings;
QListWidgetItem *_accountItem;
FolderMan *_folderMan;
};