diff --git a/contourd/location/LocationManager.cpp b/contourd/location/LocationManager.cpp index 18eac85a..f26d752f 100644 --- a/contourd/location/LocationManager.cpp +++ b/contourd/location/LocationManager.cpp @@ -1,300 +1,300 @@ /* * Copyright (C) 2011 Ivan Cukic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * or (at your option) any later version, as published by the Free * Software Foundation * * 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 * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "LocationManager.h" #include "LocationManager_p.h" #include #include #include #include #include #include "network-engines/NetworkNotifier.h" #include "locationmanageradaptor.h" namespace Contour { LocationManager::LocationManager(QObject * parent) : QObject(parent), d(new Private()) { kDebug() << "Starting the location manager"; (void) new LocationManagerAdaptor(this); QDBusConnection::sessionBus().registerObject( QLatin1String("/LocationManager"), this); QDBusConnection::sessionBus().registerService("org.kde.LocationManager"); foreach (const QString & id, d->locationNames.keyList()) { const QString & name = d->locationNames.readEntry(id, QString()); d->knownLocationIds[name] = id; d->knownLocationInfos[id].name = name; d->knownLocationInfos[id].networks = d->locationNetworks.readEntry(id, QStringList()).toSet(); d->knownLocationInfos[id].networkRoots = d->locationNetworkRoots.readEntry(id, QStringList()).toSet(); } - connect(NetworkNotifierLoader::self(), SIGNAL(activeAccessPointChanged(QString, QString)), - this, SLOT(setActiveAccessPoint(QString, QString))); + connect(NetworkNotifierLoader::self(), SIGNAL(activeAccessPointChanged(QString,QString)), + this, SLOT(setActiveAccessPoint(QString,QString))); NetworkNotifierLoader::self()->init(); } LocationManager::~LocationManager() { delete d; } QString LocationManager::addLocation(const QString & name) { if (name.isEmpty()) { return QString(); } QString id = d->knownLocationIds.value(name); if (id.isEmpty()) { // We don't have a location with that name // Checking whether the name is an UUID. It shouldn't be if (!QUuid(name).isNull()) { return QString(); } id = QUuid::createUuid(); d->knownLocationIds[name] = id; d->knownLocationInfos[id].name = name; d->locationNames.writeEntry(id, name); d->scheduleConfigSync(); } return id; } QString LocationManager::currentLocationId() const { return d->currentLocationId; } QString LocationManager::currentLocationName() const { if (d->currentLocationId.isEmpty()) return QString(); return d->knownLocationInfos[d->currentLocationId].name; } QString LocationManager::setCurrentLocation(const QString & location) { if (location.isEmpty()) { - d->currentLocationId = QString(); + d->currentLocationId.clear(); emit currentLocationChanged(d->currentLocationId, d->currentLocationId); return d->currentLocationId; } kDebug() << "Setting the current location to" << location; if (QUuid(location).isNull()) { // We got passed a name for the location, not an id // addLocation will not create a new location if already exists: d->currentLocationId = addLocation(location); } else { // We got an UUID if (d->knownLocationInfos.contains(location)) { d->currentLocationId = location; } else { - d->currentLocationId = QString(); + d->currentLocationId.clear(); } } if (!d->currentNetworkName.isEmpty()) { kDebug() << "Current network name is" << d->currentNetworkName; d->addNetworkToLocation(d->currentLocationId, d->currentNetworkName); } emit currentLocationChanged(d->currentLocationId, d->knownLocationInfos[d->currentLocationId].name); return d->currentLocationId; } QStringList LocationManager::knownLocations() const { return d->knownLocationInfos.keys(); } void LocationManager::resetCurrentLocation() { setCurrentLocation(QString()); } void LocationManager::setActiveAccessPoint(const QString & accessPoint, const QString & backend) { kDebug() << accessPoint << backend; d->currentNetworkName = accessPoint; // TODO: do stuff :) // Checking whether we already have this access point // tied to a location kDebug() << "Checking whether we already have this access point tied to a location"; QHashIterator item(d->knownLocationInfos); while (item.hasNext()) { item.next(); kDebug() << item.key() << "has networks" << item.value().networks; if (item.value().networks.contains(accessPoint)) { setCurrentLocation(item.key()); return; } } // Checking whether we have a location that was tied // to a similarly named access point const QString & accessPointRoot = d->networkRoot(accessPoint); item.toFront(); while (item.hasNext()) { item.next(); if (item.value().networkRoots.contains(accessPointRoot)) { setCurrentLocation(item.key()); return; } } // Nothing found resetCurrentLocation(); } LocationManager::Private::Private() : config("contourrc"), locationNames(&config, "LocationManager-Location-Names"), locationNetworks(&config, "LocationManager-Location-Networks"), locationNetworkRoots(&config, "LocationManager-Location-NetworkRoots"), currentLocationId() { // Config syncing connect(&configSyncTimer, SIGNAL(timeout()), this, SLOT(configSync())); configSyncTimer.setSingleShot(true); configSyncTimer.setInterval(2 * /*60 **/ 1000); } LocationManager::Private::~Private() { configSync(); } void LocationManager::Private::scheduleConfigSync() { if (!configSyncTimer.isActive()) { configSyncTimer.start(); } } void LocationManager::Private::configSync() { configSyncTimer.stop(); config.sync(); } void LocationManager::Private::addNetworkToLocation(const QString & location, const QString & network) { if (!knownLocationInfos.contains(location) || network.isEmpty()) return; knownLocationInfos[location].networks << network; knownLocationInfos[location].networkRoots << networkRoot(network); kDebug() << "Setting networks for" << location << knownLocationInfos[location].name << knownLocationInfos[location].networks << knownLocationInfos[location].networkRoots ; locationNetworks.writeEntry(location, knownLocationInfos[location].networks.toList()); locationNetworkRoots.writeEntry(location, knownLocationInfos[location].networkRoots.toList()); scheduleConfigSync(); } QString LocationManager::Private::networkRoot(const QString & name) { // We are going to try to strip all the suffix data from // the network name QString result = name.toLower(); int lastDash = -1; int lastLetter = -1; for (int i = 0; i < name.size(); i++) { if (name[i] == '-' || name[i] == '_') { lastDash = i; } else if (name[i] > '9') { lastLetter = i; } } if (lastLetter == name.size() - 1) { // Letters are till the end of the name if (lastDash > name.size() / 2) { // The last dash is in the second half of the name, // considering it and the rest of the name as a suffix return result.left(lastDash); } else { // The letters are going to the end of the name, and // there are no dashes or we are ignoring them return result; } } else { // We want to remove the end of the name int last = qMin(lastDash, lastLetter); if (last <= name.size()) { last = qMax(lastDash, lastLetter); } if (last > name.size() / 2) { return result.left(last); } return result; } } } // namespace Contour diff --git a/contourd/location/LocationManager.h b/contourd/location/LocationManager.h index 9a033398..9e1eb9f6 100644 --- a/contourd/location/LocationManager.h +++ b/contourd/location/LocationManager.h @@ -1,115 +1,115 @@ /* * Copyright (C) 2011 Ivan Cukic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * or (at your option) any later version, as published by the Free * Software Foundation * * 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 * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef LOCATION_MANAGER_H_ -#define LOCATION_MANAGER_H_ +#ifndef LOCATION_MANAGER_H +#define LOCATION_MANAGER_H #include #include #include namespace Contour { /** * LocationManager */ class LocationManager: public QObject { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.contour.LocationManager") public: LocationManager(QObject * parent); virtual ~LocationManager(); public Q_SLOTS: /** * @returns the list of previously saved locations */ QStringList knownLocations() const; /** * Adds a new location to the list of known locations * @param name the name of the location * @returns the id of the newly created location, or the * id of the location with the specified name if it * existed previously. */ QString addLocation(const QString & name); /** * Sets the current location. * * If the function is invoked with an UUID, and the location * with that UUID doesn't exist, it will not change the current * location and will return the id of the old location instead. * * If the function is invoked with a name instead of an UUID, * and the location with that name doesn't exist, it will create a new * location and return the id of the newly created location. * @see addLocation * * If an empty string is passed, the location is considered unknown. * @see resetCurrentLocation * * @param id or the name of the location * @returns the id of the location */ QString setCurrentLocation(const QString & id); /** * Sets the location to unknown. */ void resetCurrentLocation(); /** * @returns the id of the current location. Empty if the location is not set. */ QString currentLocationId() const; /** * The names of locations are unique, but the user can change them * over time. If you need to save the current location to a configuration * file or somewhere else, don't save the name, but use the id. * * Otherwise, you can use the name. * * @returns the name of the current location. Empty if the location is not set. */ QString currentLocationName() const; Q_SIGNALS: /** * Emitted when the current location is changed * @param id the id of the new location. Empty if the location is not set. * @param name the name of the new location. Empty if the location is not set. */ void currentLocationChanged(const QString & id, const QString & name); protected Q_SLOTS: void setActiveAccessPoint(const QString & accessPoint, const QString & backend); private: class Private; Private * const d; }; } // namespace Contour -#endif // LOCATION_MANAGER_H_ +#endif // LOCATION_MANAGER_H diff --git a/contourd/location/LocationManager_p.h b/contourd/location/LocationManager_p.h index 576600ca..535e2d19 100644 --- a/contourd/location/LocationManager_p.h +++ b/contourd/location/LocationManager_p.h @@ -1,75 +1,75 @@ /* * Copyright (C) 2011 Ivan Cukic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * or (at your option) any later version, as published by the Free * Software Foundation * * 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 * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef LOCATION_MANAGER_P_H_ -#define LOCATION_MANAGER_P_H_ +#ifndef LOCATION_MANAGER_P_H +#define LOCATION_MANAGER_P_H #include "LocationManager.h" #include #include #include #include #include #include "network-engines/NetworkNotifier.h" #include "locationmanageradaptor.h" namespace Contour { class LocationManager::Private: public QObject { Q_OBJECT public: Private(); virtual ~Private(); void addNetworkToLocation(const QString & location, const QString & network); QString networkRoot(const QString & name); struct LocationInfo { QString name; QSet networks; QSet networkRoots; }; public Q_SLOTS: void scheduleConfigSync(); void configSync(); public: QTimer configSyncTimer; QHash knownLocationInfos; QHash knownLocationIds; KConfig config; KConfigGroup locationNames; KConfigGroup locationNetworks; KConfigGroup locationNetworkRoots; QString currentLocationId; QString currentNetworkName; }; } // namespace Contour -#endif // LOCATION_MANAGER_P_H_ +#endif // LOCATION_MANAGER_P_H diff --git a/contourd/location/network-engines/NetworkNotifier.h b/contourd/location/network-engines/NetworkNotifier.h index de36764f..9266bd85 100644 --- a/contourd/location/network-engines/NetworkNotifier.h +++ b/contourd/location/network-engines/NetworkNotifier.h @@ -1,75 +1,75 @@ /* * Copyright (C) 2011 Ivan Cukic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * or (at your option) any later version, as published by the Free * Software Foundation * * 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 * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef NETWORK_NOTIFIER_H_ -#define NETWORK_NOTIFIER_H_ +#ifndef NETWORK_NOTIFIER_H +#define NETWORK_NOTIFIER_H #include class NetworkNotifier: public QObject { Q_OBJECT public: NetworkNotifier(QObject * parent = NULL); virtual ~NetworkNotifier(); Q_SIGNALS: void activeAccessPointChanged(const QString & ssid); protected Q_SLOTS: void setActiveAccessPoint(const QString & ssid); virtual void init() = 0; friend class NetworkNotifierLoader; }; class NetworkNotifierLoader: public QObject { Q_OBJECT public: static NetworkNotifierLoader * self(); void registerNetworkNotifier(const QString & name, NetworkNotifier * nn); void init(); Q_SIGNALS: void activeAccessPointChanged(const QString & accessPoint, const QString & backend); protected Q_SLOTS: void setActiveAccessPoint(const QString & accessPoint); private: NetworkNotifierLoader(); ~NetworkNotifierLoader(); static NetworkNotifierLoader * s_instance; private: class Private; Private * const d; }; #define REGISTER_NETWORK_NOTIFIER(Name) \ static class Name##StaticInit { public: \ Name##StaticInit() { NetworkNotifierLoader::self()->registerNetworkNotifier(#Name, new Name()); } \ } Name##_static_init; -#endif // NETWORK_NOTIFIER_H_ +#endif // NETWORK_NOTIFIER_H diff --git a/contourd/location/network-engines/connman/ConnmanNetworkNotifier.h b/contourd/location/network-engines/connman/ConnmanNetworkNotifier.h index c46c96c0..9cfe2bff 100644 --- a/contourd/location/network-engines/connman/ConnmanNetworkNotifier.h +++ b/contourd/location/network-engines/connman/ConnmanNetworkNotifier.h @@ -1,51 +1,51 @@ /* * Copyright (C) 2011 Ivan Cukic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * or (at your option) any later version, as published by the Free * Software Foundation * * 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 * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef DUMMY_NETWORK_NOTIFIER_H_ -#define DUMMY_NETWORK_NOTIFIER_H_ +#ifndef CONNMAN_NETWORK_NOTIFIER_H +#define CONNMAN_NETWORK_NOTIFIER_H #include "../NetworkNotifier.h" /** * ConnmanNetworkNotifier */ class ConnmanNetworkNotifier: public NetworkNotifier { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.LocationManager.ConnmanNetworkNotifier") public: ConnmanNetworkNotifier(QObject * parent = NULL); virtual ~ConnmanNetworkNotifier(); public Q_SLOTS: void setWifiName(const QString & accessPoint); protected Q_SLOTS: void enable(); protected: void init(); private: class Private; Private * const d; }; -#endif // DUMMYNETWORK_NOTIFIER_H_ +#endif // CONNMAN_NETWORK_NOTIFIER_H diff --git a/contourd/location/network-engines/dummy/DummyNetworkNotifier.h b/contourd/location/network-engines/dummy/DummyNetworkNotifier.h index 72d89c6c..859d02f0 100644 --- a/contourd/location/network-engines/dummy/DummyNetworkNotifier.h +++ b/contourd/location/network-engines/dummy/DummyNetworkNotifier.h @@ -1,48 +1,48 @@ /* * Copyright (C) 2011 Ivan Cukic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * or (at your option) any later version, as published by the Free * Software Foundation * * 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 * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef CONNMAN_NETWORK_NOTIFIER_H_ -#define CONNMAN_NETWORK_NOTIFIER_H_ +#ifndef DUMMY_NETWORK_NOTIFIER_H +#define DUMMY_NETWORK_NOTIFIER_H #include "../NetworkNotifier.h" /** * DummyNetworkNotifier */ class DummyNetworkNotifier: public NetworkNotifier { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.LocationManager.DummyNetworkNotifier") public: DummyNetworkNotifier(QObject * parent = NULL); virtual ~DummyNetworkNotifier(); public Q_SLOTS: void setWifiName(const QString & accessPoint); protected: void init(); private: class Private; Private * const d; }; -#endif // CONNMANNETWORK_NOTIFIER_H_ +#endif // DUMMY_NETWORK_NOTIFIER_H diff --git a/contourd/location/network-engines/solid/SolidNetworkNotifier.h b/contourd/location/network-engines/solid/SolidNetworkNotifier.h index e7f0507d..e34b4978 100644 --- a/contourd/location/network-engines/solid/SolidNetworkNotifier.h +++ b/contourd/location/network-engines/solid/SolidNetworkNotifier.h @@ -1,44 +1,44 @@ /* * Copyright (C) 2011 Ivan Cukic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * or (at your option) any later version, as published by the Free * Software Foundation * * 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 * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef SOLID_NETWORK_NOTIFIER_H_ -#define SOLID_NETWORK_NOTIFIER_H_ +#ifndef SOLID_NETWORK_NOTIFIER_H +#define SOLID_NETWORK_NOTIFIER_H #include "../NetworkNotifier.h" /** * SolidNetworkNotifier */ class SolidNetworkNotifier: public NetworkNotifier { Q_OBJECT public: SolidNetworkNotifier(QObject * parent = NULL); virtual ~SolidNetworkNotifier(); protected: void init(); private: class Private; Private * const d; }; -#endif // SOLIDNETWORK_NOTIFIER_H_ +#endif // SOLIDNETWORK_NOTIFIER_H