add simple network location infrastructure

This commit is contained in:
Duncan Mac-Vicar P 2011-04-06 15:22:40 +02:00
parent 5162b9def2
commit 65c41b6368
7 changed files with 227 additions and 1 deletions

View file

@ -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
)

View file

@ -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()));

View file

@ -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;

View file

@ -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:

View file

@ -65,6 +65,9 @@ FolderWizardTargetPage::FolderWizardTargetPage()
registerField("targetLocalFolder", _ui.localFolder2LineEdit);
registerField("targetSSHFolder", _ui.sshFolderLineEdit);
registerField("onlyOnline?", _ui.checkBoxOnlyOnline);
registerField("onlyThisLAN?", _ui.checkBoxOnlyThisLAN);
}
FolderWizardTargetPage::~FolderWizardTargetPage()

View file

@ -0,0 +1,101 @@
/*
* 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.
*/
#include "mirall/networklocation.h"
#include <QProcess>
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<QByteArray> 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<QByteArray> 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;
}
}

View file

@ -0,0 +1,57 @@
/*
* 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.
*/
#ifndef MIRALL_NETWORK_LOCATION_H
#define MIRALL_NETWORK_LOCATION_H
#include <QString>
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