23 #include "connection-manager.h"
25 #include "libsignoncommon.h"
26 #include "signond/signoncommon.h"
28 #include <QDBusConnectionInterface>
30 #include <QDBusPendingCallWatcher>
32 #include <QProcessEnvironment>
33 #include <QStandardPaths>
37 static QPointer<ConnectionManager> connectionInstance = 0;
39 ConnectionManager::ConnectionManager(QObject *parent):
41 m_connection(QLatin1String(
"libsignon-qt-invalid")),
42 m_serviceStatus(ServiceStatusUnknown)
44 if (connectionInstance == 0) {
46 connectionInstance =
this;
48 BLAME() <<
"SignOn::ConnectionManager instantiated more than once!";
52 ConnectionManager::~ConnectionManager()
58 if (connectionInstance == 0) {
61 return connectionInstance;
64 void ConnectionManager::connect()
66 if (m_connection.isConnected()) {
67 Q_EMIT connected(m_connection);
73 bool ConnectionManager::hasConnection()
const
75 return m_connection.isConnected();
78 ConnectionManager::SocketConnectionStatus
79 ConnectionManager::setupSocketConnection()
81 QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
82 QLatin1String one(
"1");
83 if (environment.value(QLatin1String(
"SSO_USE_PEER_BUS"), one) != one) {
84 return SocketConnectionUnavailable;
88 QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
89 if (runtimeDir.isEmpty())
return SocketConnectionUnavailable;
91 QString socketFileName =
92 QString::fromLatin1(
"unix:path=%1/" SIGNOND_SOCKET_FILENAME).arg(runtimeDir);
95 QDBusConnection connection =
96 QDBusConnection::connectToPeer(socketFileName,
97 QString(QLatin1String(
"libsignon-qt%1")).arg(count++));
98 if (!connection.isConnected()) {
99 QDBusError error = connection.lastError();
100 QString name = error.name();
101 TRACE() <<
"p2p error:" << error << error.type();
102 if (name == QLatin1String(
"org.freedesktop.DBus.Error.FileNotFound") &&
103 m_serviceStatus != ServiceActivated) {
104 return SocketConnectionNoService;
106 return SocketConnectionUnavailable;
110 m_connection = connection;
111 m_connection.connect(QString(),
112 QLatin1String(
"/org/freedesktop/DBus/Local"),
113 QLatin1String(
"org.freedesktop.DBus.Local"),
114 QLatin1String(
"Disconnected"),
115 this, SLOT(onDisconnected()));
117 return SocketConnectionOk;
120 void ConnectionManager::init()
122 if (m_serviceStatus == ServiceActivating)
return;
124 SocketConnectionStatus status = setupSocketConnection();
126 if (status == SocketConnectionNoService) {
127 TRACE() <<
"Peer connection unavailable, activating service";
128 QDBusConnectionInterface *
interface =
129 QDBusConnection::sessionBus().interface();
130 QDBusPendingCall call =
131 interface->asyncCall(QLatin1String(
"StartServiceByName"),
132 SIGNOND_SERVICE, uint(0));
133 m_serviceStatus = ServiceActivating;
134 QDBusPendingCallWatcher *watcher =
135 new QDBusPendingCallWatcher(call,
this);
136 QObject::connect(watcher,
137 SIGNAL(finished(QDBusPendingCallWatcher*)),
139 SLOT(onActivationDone(QDBusPendingCallWatcher*)));
140 }
else if (status == SocketConnectionUnavailable) {
141 m_connection = SIGNOND_BUS;
144 if (m_connection.isConnected()) {
145 TRACE() <<
"Connected to" << m_connection.name();
146 Q_EMIT connected(m_connection);
150 void ConnectionManager::onActivationDone(QDBusPendingCallWatcher *watcher)
152 QDBusPendingReply<> reply(*watcher);
153 watcher->deleteLater();
155 if (!reply.isError()) {
156 m_serviceStatus = ServiceActivated;
160 BLAME() << reply.error();
164 void ConnectionManager::onDisconnected()
166 TRACE() <<
"Disconnected from daemon";
167 m_serviceStatus = ServiceStatusUnknown;
168 Q_EMIT disconnected();