Commit ad4971f5 authored by Pekka Vuorela's avatar Pekka Vuorela

Remove singleton usage of QConnectioAgent

Code only used ::instance() in one place so the singleton was just
overhead. Furthermore, the instance wasn't deleted when closing the app
and registration had a race condition.
parent a340fb3d
......@@ -18,7 +18,6 @@
#include <QTimer>
#include <QtGlobal>
#include <QDebug>
#include <QDBusConnection>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
......@@ -79,13 +78,13 @@ static void daemonize(void)
}
static QtMessageHandler previousMessageHandler;
bool toggleDebug;
static bool toggleDebug;
void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &str)
{
{
if (toggleDebug)
previousMessageHandler(type,context,str);
}
}
Q_DECL_EXPORT int main(int argc, char *argv[])
{
......@@ -104,13 +103,13 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
QCoreApplication::setApplicationVersion("1.0");
QCoreApplication a(argc, argv);
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("com.jolla.Connectiond")) {
qDebug() << "Connectionagent service is already running. Exiting now";
QConnectionAgent agent;
if (!agent.isValid()) {
qDebug() << "Connectionagent service failed to register. Exiting now";
return 1;
}
QConnectionAgent::instance();
return a.exec();
}
......@@ -31,8 +31,6 @@
#define CONNMAN_1_21
QConnectionAgent* QConnectionAgent::self = NULL;
#define CONND_SERVICE "com.jolla.Connectiond"
#define CONND_PATH "/Connectiond"
#define CONND_SESSION_PATH = "/ConnectionSession"
......@@ -48,20 +46,22 @@ QConnectionAgent::QConnectionAgent(QObject *parent) :
tetheringEnabled(false),
flightModeSuppression(false),
scanTimeoutInterval(1),
delayedTethering(false)
delayedTethering(false),
valid(true)
{
qDebug() << Q_FUNC_INFO;
new ConnAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
if (!dbus.registerService(CONND_SERVICE)) {
qDebug() << "XXXXXXXXXXX could not register service XXXXXXXXXXXXXXXXXX";
valid = false;
}
if (!dbus.registerObject(CONND_PATH, this)) {
qDebug() << "XXXXXXXXXXX could not register object XXXXXXXXXXXXXXXXXX";
valid = false;
}
connect(this,SIGNAL(configurationNeeded(QString)),this,SLOT(openConnectionDialog(QString)));
......@@ -97,23 +97,17 @@ QConnectionAgent::QConnectionAgent(QObject *parent) :
scanTimer = new QTimer(this);
connect(scanTimer,SIGNAL(timeout()),this,SLOT(scanTimeout()));
scanTimer->setSingleShot(true);
if (connmanAvailable)
if (connmanAvailable && valid)
setup();
}
QConnectionAgent::~QConnectionAgent()
{
delete self;
}
QConnectionAgent & QConnectionAgent::instance()
bool QConnectionAgent::isValid() const
{
qDebug() << Q_FUNC_INFO;
if (!self) {
self = new QConnectionAgent;
}
return *self;
return valid;
}
// from useragent
......
......@@ -42,9 +42,10 @@ class QConnectionAgent : public QObject
Q_OBJECT
public:
explicit QConnectionAgent(QObject *parent = 0);
~QConnectionAgent();
static QConnectionAgent &instance();
bool isValid() const;
Q_SIGNALS:
......@@ -112,7 +113,6 @@ private:
}
};
explicit QConnectionAgent(QObject *parent = 0);
void setup();
void updateServices();
bool isStateOnline(const QString &state);
......@@ -120,7 +120,6 @@ private:
QString findBestConnectableService();
void removeAllTypes(const QString &type);
static QConnectionAgent *self;
UserAgent *ua;
NetworkManager *netman;
......@@ -144,6 +143,7 @@ private:
QStringList knownTechnologies;
bool tetheringStarted;
bool delayedTethering;
bool valid;
private slots:
void onScanFinished();
......
......@@ -33,45 +33,23 @@ class Tst_connectionagent : public QObject
{
Q_OBJECT
public:
Tst_connectionagent();
private Q_SLOTS:
void tst_networkInstance();
void tst_restartconnman();
void tst_onUserInputRequested();
void tst_onUserInputCanceled();
void tst_onErrorReported();
void tst_onConnectionRequest();
void tst_sendConnectReply();
void tst_sendUserReply();
void tst_connectToType();
};
Tst_connectionagent::Tst_connectionagent()
{
}
void Tst_connectionagent::tst_networkInstance()
{
NetworkManager *netman = NetworkManagerFactory::createInstance();
QString currentState = netman->state();
QConnectionAgent::instance();
QVERIFY(currentState == netman->state());
}
void Tst_connectionagent::tst_restartconnman()
{
}
private:
QConnectionAgent agent;
};
void Tst_connectionagent::tst_onUserInputRequested()
{
QSignalSpy spy(&QConnectionAgent::instance(), SIGNAL(userInputRequested(QString,QVariantMap)));
QSignalSpy spy(&agent, SIGNAL(userInputRequested(QString,QVariantMap)));
QVariantMap map;
map.insert("test",true);
QConnectionAgent::instance().onUserInputRequested(QLatin1String("test_path"), map);
agent.onUserInputRequested(QLatin1String("test_path"), map);
QCOMPARE(spy.count(),1);
QList<QVariant> arguments;
arguments = spy.takeFirst();
......@@ -83,15 +61,15 @@ void Tst_connectionagent::tst_onUserInputRequested()
void Tst_connectionagent::tst_onUserInputCanceled()
{
QSignalSpy spy(&QConnectionAgent::instance(), SIGNAL(userInputCanceled()));
QConnectionAgent::instance().onUserInputCanceled();
QSignalSpy spy(&agent, SIGNAL(userInputCanceled()));
agent.onUserInputCanceled();
QCOMPARE(spy.count(),1);
}
void Tst_connectionagent::tst_onErrorReported()
{
QSignalSpy spy(&QConnectionAgent::instance(), SIGNAL(errorReported(QString,QString)));
QConnectionAgent::instance().onErrorReported("test_path","Test error");
QSignalSpy spy(&agent, SIGNAL(errorReported(QString,QString)));
agent.onErrorReported("test_path","Test error");
QCOMPARE(spy.count(),1);
QList<QVariant> arguments;
......@@ -99,7 +77,7 @@ void Tst_connectionagent::tst_onErrorReported()
QCOMPARE(arguments.at(0).toString(), QString("test_path"));
QCOMPARE(arguments.at(1).toString(), QString("Test error"));
QConnectionAgent::instance().connectToType("test");
agent.connectToType("test");
QCOMPARE(spy.count(),1);
arguments = spy.takeFirst();
QCOMPARE(arguments.at(0).toString(), QString(""));
......@@ -116,8 +94,8 @@ void Tst_connectionagent::tst_onConnectionRequest()
service->requestDisconnect();
// service->requestConnect();
}
QSignalSpy spy(&QConnectionAgent::instance(), SIGNAL(connectionRequest()));
QConnectionAgent::instance().onConnectionRequest();
QSignalSpy spy(&agent, SIGNAL(connectionRequest()));
agent.onConnectionRequest();
if (currentState == "online")
QCOMPARE(spy.count(),0);
......@@ -126,19 +104,6 @@ void Tst_connectionagent::tst_onConnectionRequest()
}
void Tst_connectionagent::tst_sendConnectReply()
{
}
void Tst_connectionagent::tst_sendUserReply()
{
}
void Tst_connectionagent::tst_connectToType()
{
}
QTEST_APPLESS_MAIN(Tst_connectionagent)
#include "tst_connectionagent.moc"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment