Skip to content

Commit

Permalink
Report state changes for VPN connections
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvogt committed Nov 4, 2016
1 parent e334384 commit 08249e9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/vpnmodel.cpp
Expand Up @@ -181,6 +181,11 @@ QVariantMap propertiesToQml(const QVariantMap &fromDBus)
return rv;
}

int numericValue(VpnModel::ConnectionState state)
{
return (state == VpnModel::Ready ? 3 : (state == VpnModel::Configuration ? 2 : (state == VpnModel::Failure ? 1 : 0)));
}

}


Expand Down Expand Up @@ -260,6 +265,7 @@ VpnModel::VpnModel(QObject *parent)
: ObjectListModel(parent, true, false)
, connmanVpn_("net.connman.vpn", "/", QDBusConnection::systemBus(), this)
, tokenFiles_("/home/nemo/.local/share/system/vpn")
, bestState_(VpnModel::Idle)
{
qDBusRegisterMetaType<PathProperties>();
qDBusRegisterMetaType<PathPropertiesArray>();
Expand Down Expand Up @@ -318,6 +324,11 @@ VpnModel::~VpnModel()
deleteAll();
}

int VpnModel::bestState() const
{
return static_cast<int>(bestState_);
}

void VpnModel::createConnection(const QVariantMap &createProperties)
{
const QString path(createProperties.value(QString("path")).toString());
Expand Down Expand Up @@ -612,10 +623,30 @@ void VpnModel::updateConnection(VpnConnection *conn, const QVariantMap &updatePr
}
}

int oldState(conn->state());

if (updateItem(conn, properties)) {
itemChanged(conn);

const int itemCount(count());

if (conn->state() != oldState) {
emit connectionStateChanged(conn->path(), static_cast<int>(conn->state()));

// Check to see if the best state has changed
ConnectionState maxState = Idle;
for (int i = 0; i < itemCount; ++i) {
ConnectionState state(static_cast<ConnectionState>(get<VpnConnection>(i)->state()));
if (numericValue(state) > numericValue(maxState)) {
maxState = state;
}
}
if (bestState_ != maxState) {
bestState_ = maxState;
emit bestStateChanged();
}
}

if (itemCount > 1) {
// Keep the items sorted by name
int index = 0;
Expand Down Expand Up @@ -851,7 +882,7 @@ QVariantMap VpnModel::processOpenVpnProvisioningFile(QFile &provisioningFile)
VpnConnection::VpnConnection(const QString &path)
: QObject(0)
, path_(path)
, state_(static_cast<int>(VpnModel::Idle))
, state_(static_cast<int>(VpnModel::Disconnect))
, type_(static_cast<int>(VpnModel::OpenVPN))
{
}
Expand Down
9 changes: 9 additions & 0 deletions src/vpnmodel.h
Expand Up @@ -52,6 +52,8 @@ class SYSTEMSETTINGS_EXPORT VpnModel : public ObjectListModel
Q_ENUMS(ConnectionState)
Q_ENUMS(ConnectionType)

Q_PROPERTY(int bestState READ bestState NOTIFY bestStateChanged)

public:
enum ConnectionState {
Idle,
Expand All @@ -72,6 +74,8 @@ class SYSTEMSETTINGS_EXPORT VpnModel : public ObjectListModel
explicit VpnModel(QObject *parent = 0);
virtual ~VpnModel();

int bestState() const;

Q_INVOKABLE void createConnection(const QVariantMap &properties);
Q_INVOKABLE void modifyConnection(const QString &path, const QVariantMap &properties);
Q_INVOKABLE void deleteConnection(const QString &path);
Expand All @@ -85,6 +89,10 @@ class SYSTEMSETTINGS_EXPORT VpnModel : public ObjectListModel

Q_INVOKABLE QVariantMap processProvisioningFile(const QString &path, ConnectionType type);

signals:
void bestStateChanged();
void connectionStateChanged(const QString &path, int state);

private:
void fetchVpnList();

Expand Down Expand Up @@ -116,6 +124,7 @@ class SYSTEMSETTINGS_EXPORT VpnModel : public ObjectListModel
ConnmanVpnProxy connmanVpn_;
QHash<QString, ConnmanVpnConnectionProxy *> connections_;
TokenFileRepository tokenFiles_;
ConnectionState bestState_;
};

class SYSTEMSETTINGS_EXPORT VpnConnection : public QObject
Expand Down

0 comments on commit 08249e9

Please sign in to comment.