Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pvuorela committed Jul 7, 2015
1 parent a340fb3 commit ad4971f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 71 deletions.
15 changes: 7 additions & 8 deletions connd/main.cpp
Expand Up @@ -18,7 +18,6 @@
#include <QTimer>
#include <QtGlobal>
#include <QDebug>
#include <QDBusConnection>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
Expand Down Expand Up @@ -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[])
{
Expand All @@ -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();
}

20 changes: 7 additions & 13 deletions connd/qconnectionagent.cpp
Expand Up @@ -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"
Expand All @@ -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)));
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions connd/qconnectionagent.h
Expand Up @@ -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:

Expand Down Expand Up @@ -112,15 +113,13 @@ public Q_SLOTS:
}
};

explicit QConnectionAgent(QObject *parent = 0);
void setup();
void updateServices();
bool isStateOnline(const QString &state);
bool isBestService(NetworkService *service);
QString findBestConnectableService();
void removeAllTypes(const QString &type);

static QConnectionAgent *self;
UserAgent *ua;

NetworkManager *netman;
Expand All @@ -144,6 +143,7 @@ public Q_SLOTS:
QStringList knownTechnologies;
bool tetheringStarted;
bool delayedTethering;
bool valid;

private slots:
void onScanFinished();
Expand Down
59 changes: 12 additions & 47 deletions test/auto/tst_connectionagent/tst_connectionagent.cpp
Expand Up @@ -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();
Expand All @@ -83,23 +61,23 @@ 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;
arguments = spy.takeFirst();
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(""));
Expand All @@ -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);
Expand All @@ -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"

0 comments on commit ad4971f

Please sign in to comment.