2011-02-17 02:21:45 +03:00
|
|
|
#include <QDebug>
|
2011-04-04 13:36:44 +04:00
|
|
|
#include <QDesktopServices>
|
2011-02-17 02:21:45 +03:00
|
|
|
#include <QDir>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QMenu>
|
2011-04-04 13:36:44 +04:00
|
|
|
#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"
|
2011-04-05 20:31:14 +04:00
|
|
|
#include "mirall/folderwizard.h"
|
2011-03-23 01:03:43 +03:00
|
|
|
#include "mirall/unisonfolder.h"
|
2011-03-16 16:50:34 +03:00
|
|
|
#include "mirall/inotify.h"
|
2011-02-17 02:21:45 +03:00
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
|
|
|
Application::Application(int argc, char **argv) :
|
2011-04-04 13:36:44 +04:00
|
|
|
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
|
|
|
{
|
2011-03-16 16:50:34 +03:00
|
|
|
INotify::initialize();
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
setApplicationName("Mirall");
|
2011-04-05 20:31:14 +04:00
|
|
|
setQuitOnLastWindowClosed(false);
|
|
|
|
|
|
|
|
_folderWizard = new FolderWizard();
|
2011-04-04 13:36:44 +04:00
|
|
|
|
|
|
|
_folderConfigPath = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/folders";
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
setupActions();
|
|
|
|
setupSystemTray();
|
2011-04-04 13:36:44 +04:00
|
|
|
|
|
|
|
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-04-04 13:36:44 +04:00
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Application::~Application()
|
|
|
|
{
|
2011-04-05 20:31:19 +04:00
|
|
|
qDebug() << "* Mirall shutdown";
|
2011-03-16 16:50:34 +03:00
|
|
|
INotify::cleanup();
|
2011-04-05 20:31:19 +04:00
|
|
|
|
|
|
|
delete _networkMgr;
|
|
|
|
delete _tray;
|
|
|
|
|
|
|
|
foreach (Folder *folder, _folderMap) {
|
|
|
|
delete folder;
|
|
|
|
}
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
2011-04-04 13:36:44 +04: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);
|
2011-02-17 17:10:06 +03:00
|
|
|
|
|
|
|
// here all folders should be added
|
2011-04-04 13:36:44 +04:00
|
|
|
foreach (Folder *folder, _folderMap) {
|
2011-04-06 12:56:20 +04:00
|
|
|
_contextMenu->addAction(folder->openAction());
|
2011-04-04 13:36:44 +04:00
|
|
|
}
|
2011-02-17 17:10:06 +03:00
|
|
|
|
2011-04-06 12:56:20 +04:00
|
|
|
_contextMenu->addSeparator();
|
2011-02-17 17:10:06 +03:00
|
|
|
|
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()
|
|
|
|
{
|
2011-04-05 20:31:14 +04:00
|
|
|
if (_folderWizard->exec() == QDialog::Accepted) {
|
|
|
|
qDebug() << "* Folder wizard completed";
|
2011-04-06 12:40:15 +04:00
|
|
|
|
|
|
|
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();
|
2011-04-05 20:31:14 +04:00
|
|
|
}
|
2011-04-06 12:40:15 +04:00
|
|
|
else
|
|
|
|
qDebug() << "* Folder wizard cancelled";
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
2011-04-04 13:36:44 +04: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;
|
|
|
|
|
2011-04-04 13:36:44 +04:00
|
|
|
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);
|
2011-04-04 13:36:44 +04:00
|
|
|
}
|
|
|
|
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-04-04 13:36:44 +04:00
|
|
|
}
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
|
|
|
|
} // namespace Mirall
|
|
|
|
|
|
|
|
#include "application.moc"
|