diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index db17b3f03..5d1f96dcd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,6 +17,7 @@ mirall/folderwatcher.cpp mirall/folderwizard.cpp mirall/gitfolder.cpp mirall/inotify.cpp +mirall/networklocation.cpp mirall/temporarydir.cpp mirall/unisonfolder.cpp ) diff --git a/src/mirall/application.cpp b/src/mirall/application.cpp index 7e664e8ea..7386ecf96 100644 --- a/src/mirall/application.cpp +++ b/src/mirall/application.cpp @@ -26,6 +26,7 @@ #include "mirall/application.h" #include "mirall/folder.h" #include "mirall/folderwizard.h" +#include "mirall/networklocation.h" #include "mirall/unisonfolder.h" #include "mirall/inotify.h" @@ -63,6 +64,7 @@ Application::Application(int argc, char **argv) : setupContextMenu(); + qDebug() << NetworkLocation::currentLocation().encoded(); } Application::~Application() @@ -131,12 +133,26 @@ void Application::slotAddFolder() } else if (_folderWizard->field("remote?").toBool()) { settings.setValue("backend:unison/secondPath", _folderWizard->field("targetSSHFolder")); + bool onlyOnline = _folderWizard->field("onlyOnline?").toBool(); + settings.setValue("folder/onlyOnline", onlyOnline); + + if (onlyOnline) { + bool onlyThisLAN = _folderWizard->field("onlyThisLAN?").toBool(); + settings.setValue("folder/onlyThisLAN", onlyThisLAN); + if (onlyThisLAN) { + settings.setValue("folder/onlyOnline", true); + if (_folderWizard->field("onlyThisLAN?").toBool()) { + + } + } + } } else { qWarning() << "* Folder not local and note remote?"; return; } + settings.sync(); setupFolderFromConfigFile(alias); setupContextMenu(); @@ -193,6 +209,8 @@ void Application::setupFolderFromConfigFile(const QString &file) { return; } } + folder->setOnlyOnlineEnabled(settings.value("folder/onlyOnline").toBool()); + folder->setOnlyThisLANEnabled(settings.value("folder/onlyThisLAN").toBool()); _folderMap[file] = folder; QObject::connect(folder, SIGNAL(syncStarted()), SLOT(slotFolderSyncStarted())); diff --git a/src/mirall/folder.cpp b/src/mirall/folder.cpp index 09543e963..fe45743a9 100644 --- a/src/mirall/folder.cpp +++ b/src/mirall/folder.cpp @@ -33,7 +33,9 @@ Folder::Folder(const QString &alias, const QString &path, QObject *parent) _path(path), _pollTimer(new QTimer(this)), _pollInterval(DEFAULT_POLL_INTERVAL_SEC), - _alias(alias) + _alias(alias), + _onlyOnlineEnabled(false), + _onlyThisLANEnabled(false) { _openAction = new QAction(QIcon::fromTheme(FOLDER_ICON), path, this); _openAction->setIconVisibleInMenu(true); @@ -76,6 +78,26 @@ QString Folder::path() const return _path; } +bool Folder::onlyOnlineEnabled() const +{ + return _onlyOnlineEnabled; +} + +void Folder::setOnlyOnlineEnabled(bool enabled) +{ + _onlyOnlineEnabled = enabled; +} + +bool Folder::onlyThisLANEnabled() const +{ + return _onlyThisLANEnabled; +} + +void Folder::setOnlyThisLANEnabled(bool enabled) +{ + _onlyThisLANEnabled = enabled; +} + int Folder::pollInterval() const { return _pollInterval; diff --git a/src/mirall/folder.h b/src/mirall/folder.h index eb98567af..2201dc4a0 100644 --- a/src/mirall/folder.h +++ b/src/mirall/folder.h @@ -63,6 +63,28 @@ public: */ virtual bool isBusy() const = 0; + /** + * only sync when online in the network + */ + bool onlyOnlineEnabled() const; + + /** + * @see onlyOnlineEnabled + */ + void setOnlyOnlineEnabled(bool enabled); + + /** + * only sync when online in the same LAN + * as the one used during setup + */ + bool onlyThisLANEnabled() const; + + /** + * @see onlyThisLANEnabled + */ + void setOnlyThisLANEnabled(bool enabled); + + protected: /** * The minimum amounts of seconds to wait before @@ -92,6 +114,8 @@ private: QTimer *_pollTimer; int _pollInterval; QString _alias; + bool _onlyOnlineEnabled; + bool _onlyThisLANEnabled; protected slots: diff --git a/src/mirall/folderwizard.cpp b/src/mirall/folderwizard.cpp index 8242ca2b0..196b17c7d 100644 --- a/src/mirall/folderwizard.cpp +++ b/src/mirall/folderwizard.cpp @@ -65,6 +65,9 @@ FolderWizardTargetPage::FolderWizardTargetPage() registerField("targetLocalFolder", _ui.localFolder2LineEdit); registerField("targetSSHFolder", _ui.sshFolderLineEdit); + + registerField("onlyOnline?", _ui.checkBoxOnlyOnline); + registerField("onlyThisLAN?", _ui.checkBoxOnlyThisLAN); } FolderWizardTargetPage::~FolderWizardTargetPage() diff --git a/src/mirall/networklocation.cpp b/src/mirall/networklocation.cpp new file mode 100644 index 000000000..6cb330e63 --- /dev/null +++ b/src/mirall/networklocation.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) by Duncan Mac-Vicar P. + * + * 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. + */ + +#include "mirall/networklocation.h" + +#include + +namespace Mirall +{ + +NetworkLocation::NetworkLocation(const QString &encoded) + : _encoded(encoded) +{ +} + +NetworkLocation::NetworkLocation() +{ +} + +NetworkLocation::~NetworkLocation() +{ +} + +/** + * for now our data is just the MAC address of the default gateway + */ +NetworkLocation NetworkLocation::currentLocation() +{ + QProcess ip; + ip.start("/sbin/ip", QStringList() << "route"); + + if (!ip.waitForStarted()) + return NetworkLocation(); + + if (!ip.waitForFinished()) + return NetworkLocation(); + + QByteArray gwIp; + while (ip.canReadLine()) { + QByteArray line = ip.readLine(); + if (line.startsWith("default")) { + QList parts = line.split(' '); + gwIp = parts[2]; + break; + } + } + if (gwIp.isEmpty()) + return NetworkLocation(); + + QProcess arp; + arp.start("/sbin/arp", QStringList() << "-a"); + + if (!arp.waitForStarted()) + return NetworkLocation(); + + if (!arp.waitForFinished()) + return NetworkLocation(); + + QByteArray gwMAC; + while (arp.canReadLine()) { + QByteArray line = arp.readLine(); + if (line.contains(gwIp)) { + QList parts = line.split(' '); + gwMAC = parts[3]; + break; + } + } + if (gwMAC.isEmpty()) + return NetworkLocation(); + + return NetworkLocation(gwMAC); +} + + +NetworkLocation::Proximity NetworkLocation::compareWith(const NetworkLocation &location) const +{ + if (location.encoded().isEmpty() || encoded().isEmpty()) + return Unknown; + if (location.encoded() == encoded()) + return Same; + return Different; +} + +QString NetworkLocation::encoded() const +{ + return _encoded; +} + +} + diff --git a/src/mirall/networklocation.h b/src/mirall/networklocation.h new file mode 100644 index 000000000..b4bd08b11 --- /dev/null +++ b/src/mirall/networklocation.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) by Duncan Mac-Vicar P. + * + * 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. + */ + +#ifndef MIRALL_NETWORK_LOCATION_H +#define MIRALL_NETWORK_LOCATION_H + +#include + +namespace Mirall { + +class NetworkLocation +{ +public: + + enum Proximity { + Unknown, + Same, + Different + }; + + /** + * constructs a location from its encoded + * form + */ + NetworkLocation(const QString &encoded); + + /** + * Unknown location + */ + NetworkLocation(); + + ~NetworkLocation(); + + QString encoded() const; + + static NetworkLocation currentLocation(); + + Proximity compareWith(const NetworkLocation &location) const; +private: + QString _encoded; +}; + +} + + +#endif