nextcloud-desktop/src/mirall/application.cpp

224 lines
6.3 KiB
C++
Raw Normal View History

2011-04-06 13:48:02 +04:00
/*
* Copyright (C) by Duncan Mac-Vicar P. <duncan@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
2011-02-17 02:21:45 +03:00
#include <QDebug>
#include <QDesktopServices>
2011-02-17 02:21:45 +03:00
#include <QDir>
#include <QIcon>
#include <QMenu>
#include <QNetworkConfigurationManager>
#include <QSettings>
#include <QStringList>
2011-02-17 02:21:45 +03:00
#include <QSystemTrayIcon>
#include "mirall/constants.h"
#include "mirall/application.h"
#include "mirall/folder.h"
#include "mirall/folderwizard.h"
#include "mirall/unisonfolder.h"
#include "mirall/inotify.h"
2011-02-17 02:21:45 +03:00
namespace Mirall {
Application::Application(int argc, char **argv) :
QApplication(argc, argv),
2011-04-04 14:23:30 +04:00
_networkMgr(new QNetworkConfigurationManager(this)),
2011-04-06 12:56:20 +04:00
_folderSyncCount(0),
_contextMenu(0)
2011-02-17 02:21:45 +03:00
{
INotify::initialize();
2011-02-17 02:21:45 +03:00
setApplicationName("Mirall");
setQuitOnLastWindowClosed(false);
_folderWizard = new FolderWizard();
_folderConfigPath = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/folders";
2011-02-17 02:21:45 +03:00
setupActions();
setupSystemTray();
qDebug() << "* Network is" << (_networkMgr->isOnline() ? "online" : "offline");
foreach (QNetworkConfiguration netCfg, _networkMgr->allConfigurations(QNetworkConfiguration::Active)) {
//qDebug() << "Network:" << netCfg.identifier();
}
// if QDir::mkpath would not be so stupid, I would not need to have this
// duplication of folderConfigPath() here
QDir storageDir(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
storageDir.mkpath("folders");
setupKnownFolders();
2011-02-17 02:21:45 +03:00
setupContextMenu();
2011-02-17 02:21:45 +03:00
}
Application::~Application()
{
qDebug() << "* Mirall shutdown";
INotify::cleanup();
delete _networkMgr;
delete _tray;
foreach (Folder *folder, _folderMap) {
delete folder;
}
2011-02-17 02:21:45 +03:00
}
QString Application::folderConfigPath() const
{
return _folderConfigPath;
}
2011-02-17 02:21:45 +03:00
void Application::setupActions()
{
_actionAddFolder = new QAction(tr("Add folder"), this);
QObject::connect(_actionAddFolder, SIGNAL(triggered(bool)), SLOT(slotAddFolder()));
_actionQuit = new QAction(tr("Quit"), this);
QObject::connect(_actionQuit, SIGNAL(triggered(bool)), SLOT(quit()));
}
void Application::setupSystemTray()
{
_tray = new QSystemTrayIcon(this);
_tray->setIcon(QIcon(FOLDER_ICON));
_tray->show();
}
void Application::setupContextMenu()
{
2011-04-06 12:56:20 +04:00
delete _contextMenu;
_contextMenu = new QMenu();
_contextMenu->addAction(_actionAddFolder);
// here all folders should be added
foreach (Folder *folder, _folderMap) {
2011-04-06 12:56:20 +04:00
_contextMenu->addAction(folder->openAction());
}
2011-04-06 12:56:20 +04:00
_contextMenu->addSeparator();
2011-04-06 12:56:20 +04:00
_contextMenu->addAction(_actionQuit);
_tray->setContextMenu(_contextMenu);
2011-02-17 02:21:45 +03:00
}
void Application::slotAddFolder()
{
if (_folderWizard->exec() == QDialog::Accepted) {
qDebug() << "* Folder wizard completed";
QString alias = _folderWizard->field("alias").toString();
QSettings settings(folderConfigPath() + "/" + alias, QSettings::IniFormat);
settings.setValue("folder/backend", "unison");
settings.setValue("folder/path", _folderWizard->field("sourceFolder"));
if (_folderWizard->field("local?").toBool()) {
settings.setValue("backend:unison/secondPath", _folderWizard->field("targetLocalFolder"));
}
else if (_folderWizard->field("remote?").toBool()) {
settings.setValue("backend:unison/secondPath", _folderWizard->field("targetSSHFolder"));
}
else
{
qWarning() << "* Folder not local and note remote?";
return;
}
settings.sync();
setupFolderFromConfigFile(alias);
2011-04-06 12:56:20 +04:00
setupContextMenu();
}
else
qDebug() << "* Folder wizard cancelled";
2011-02-17 02:21:45 +03:00
}
void Application::setupKnownFolders()
{
qDebug() << "* Setup folders from " << folderConfigPath();
QDir dir(folderConfigPath());
dir.setFilter(QDir::Files);
QStringList list = dir.entryList();
foreach (QString file, list) {
setupFolderFromConfigFile(file);
}
}
// filename is the name of the file only, it does not include
// the configuration directory path
void Application::setupFolderFromConfigFile(const QString &file) {
2011-04-04 14:23:30 +04:00
Folder *folder = 0L;
qDebug() << " ` -> setting up:" << file;
QSettings settings(folderConfigPath() + "/" + file, QSettings::IniFormat);
if (!settings.contains("folder/path")) {
qWarning() << " `->" << file << "is not a valid folder configuration";
return;
}
QVariant path = settings.value("folder/path").toString();
if (path.isNull() || !QFileInfo(path.toString()).isDir()) {
qWarning() << " `->" << path.toString() << "does not exist. Skipping folder" << file;
_tray->showMessage(tr("Unknown folder"),
tr("Folder %1 does not exist").arg(path.toString()),
QSystemTrayIcon::Critical);
return;
}
QVariant backend = settings.value("folder/backend");
if (!backend.isNull()) {
if (backend.toString() == "unison") {
2011-04-06 11:52:02 +04:00
folder = new UnisonFolder(file,
path.toString(),
2011-04-04 14:23:30 +04:00
settings.value("backend:unison/secondPath").toString(),
this);
}
else {
qWarning() << "unknown backend" << backend;
return;
}
}
2011-04-04 14:23:30 +04:00
_folderMap[file] = folder;
QObject::connect(folder, SIGNAL(syncStarted()), SLOT(slotFolderSyncStarted()));
QObject::connect(folder, SIGNAL(syncFinished()), SLOT(slotFolderSyncFinished()));
}
void Application::slotFolderSyncStarted()
{
_folderSyncCount++;
if (_folderSyncCount > 0) {
_tray->setIcon(QIcon(FOLDER_SYNC_ICON));
}
}
void Application::slotFolderSyncFinished()
{
_folderSyncCount--;
if (_folderSyncCount < 1) {
_tray->setIcon(QIcon(FOLDER_ICON));
}
}
2011-02-17 02:21:45 +03:00
} // namespace Mirall
#include "application.moc"