From 02d9b5893ef441656d70cbe702e0d2339ae5c6de Mon Sep 17 00:00:00 2001
From: Javier <dev.git@javispedro.com>
Date: Mon, 23 Mar 2015 03:57:44 +0100
Subject: use profiled via proxy

---
 saltoqd/com.nokia.profiled.xml | 19 +++++++++++++++++++
 saltoqd/saltoqd.pro            |  2 +-
 saltoqd/toqmanager.cpp         | 10 ++++++----
 saltoqd/toqmanager.h           |  2 ++
 saltoqd/voicecallmanager.cpp   | 34 ++++++++++++++++++++++++----------
 saltoqd/voicecallmanager.h     |  3 +++
 6 files changed, 55 insertions(+), 15 deletions(-)
 create mode 100644 saltoqd/com.nokia.profiled.xml

diff --git a/saltoqd/com.nokia.profiled.xml b/saltoqd/com.nokia.profiled.xml
new file mode 100644
index 0000000..d90247e
--- /dev/null
+++ b/saltoqd/com.nokia.profiled.xml
@@ -0,0 +1,19 @@
+<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
+<node>
+   <interface name="com.nokia.profiled">
+      <signal name="profile_changed">
+         <arg name="changed" type="b" direction="out"/>
+         <arg name="active" type="b" direction="out"/>
+         <arg name="profile" type="s" direction="out"/>
+         <!-- TODO <arg type="a(sss)" direction="out"/> -->
+      </signal>
+      <method name="get_profile">
+         <arg name="profile" type="s" direction="out"/>
+      </method>
+      <method name="set_profile">
+         <arg name="profile" type="s" direction="in"/>
+         <arg name="success" type="b" direction="out"/>
+      </method>
+   </interface>
+</node>
diff --git a/saltoqd/saltoqd.pro b/saltoqd/saltoqd.pro
index 2c50db5..d0efe94 100644
--- a/saltoqd/saltoqd.pro
+++ b/saltoqd/saltoqd.pro
@@ -32,7 +32,7 @@ HEADERS += \
 
 DBUS_ADAPTORS += org.freedesktop.Notifications.xml
 
-DBUS_INTERFACES += org.nemomobile.voicecall.VoiceCallManager.xml
+DBUS_INTERFACES += com.nokia.profiled.xml org.nemomobile.voicecall.VoiceCallManager.xml
 
 target.path = /usr/bin
 INSTALLS += target
diff --git a/saltoqd/toqmanager.cpp b/saltoqd/toqmanager.cpp
index 2e0d967..fa8f7c6 100644
--- a/saltoqd/toqmanager.cpp
+++ b/saltoqd/toqmanager.cpp
@@ -5,6 +5,7 @@
 #include "storagemanager.h"
 #include "musicmanager.h"
 #include "commmanager.h"
+#include "voicecallmanager.h"
 
 ToqManager::ToqManager(const QBluetoothAddress &address, QObject *parent) :
 	QObject(parent),
@@ -13,7 +14,8 @@ ToqManager::ToqManager(const QBluetoothAddress &address, QObject *parent) :
 	_systemManager(new SystemManager(this)),
 	_storageManager(new StorageManager(this)),
 	_musicManager(new MusicManager(this)),
-	_commManager(new CommManager(_storageManager, this))
+	_commManager(new CommManager(_storageManager, this)),
+	_voiceCallManager(new VoiceCallManager(this))
 {
 	connect(_conn, &ToqConnection::messageReceived,
 			this, &ToqManager::handleToqMessage);
@@ -38,7 +40,7 @@ void ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endp
 {
 	QJsonDocument doc(payload);
 	ToqConnection::Message msg(source, destination, transactionId, type, doc);
-	_conn->sendMessage(msg);
+	sendMessage(msg);
 }
 
 quint16 ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endpoint destination, quint32 type, const QJsonObject &payload)
@@ -46,14 +48,14 @@ quint16 ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::E
 	QJsonDocument doc(payload);
 	quint16 transactionId = _conn->nextTransactionId();
 	ToqConnection::Message msg(source, destination, transactionId, type, doc);
-	_conn->sendMessage(msg);
+	sendMessage(msg);
 	return transactionId;
 }
 
 void ToqManager::sendReply(const ToqConnection::Message &msg, quint32 type, const QJsonObject &payload)
 {
 	ToqConnection::Message reply(msg.destination, msg.source, msg.transactionId, type, QJsonDocument(payload));
-	_conn->sendMessage(reply);
+	sendMessage(reply);
 }
 
 void ToqManager::handleToqMessage(const ToqConnection::Message &msg)
diff --git a/saltoqd/toqmanager.h b/saltoqd/toqmanager.h
index 0edd6ec..c1675ac 100644
--- a/saltoqd/toqmanager.h
+++ b/saltoqd/toqmanager.h
@@ -11,6 +11,7 @@ class SystemManager;
 class StorageManager;
 class MusicManager;
 class CommManager;
+class VoiceCallManager;
 
 class ToqManager : public QObject
 {
@@ -44,6 +45,7 @@ private:
 	StorageManager *_storageManager;
 	MusicManager *_musicManager;
 	CommManager *_commManager;
+	VoiceCallManager *_voiceCallManager;
 };
 
 inline bool ToqManager::isConnected() const
diff --git a/saltoqd/voicecallmanager.cpp b/saltoqd/voicecallmanager.cpp
index 3ef9223..8820dd6 100644
--- a/saltoqd/voicecallmanager.cpp
+++ b/saltoqd/voicecallmanager.cpp
@@ -1,18 +1,29 @@
 #include <QtDBus/QDBusConnection>
 #include "voicecallmanager.h"
 #include "voicecallmanager_interface.h"
+#include "profiled_interface.h"
 
+static ComNokiaProfiledInterface *profiled = NULL;
 static OrgNemomobileVoicecallVoiceCallManagerInterface *vcm = NULL;
 
 VoiceCallManager::VoiceCallManager(ToqManager *toq) :
 	QObject(toq), _toq(toq)
 {
+	if (!profiled) {
+		profiled = new ComNokiaProfiledInterface("com.nokia.profiled",
+												 "/com/nokia/profiled",
+												 QDBusConnection::sessionBus());
+	}
 	if (!vcm) {
 		vcm = new OrgNemomobileVoicecallVoiceCallManagerInterface("org.nemomobile.voicecall",
 																  "/",
 																  QDBusConnection::sessionBus());
 	}
 
+	// TODO Fix .xml to catch this signal
+	connect(profiled, SIGNAL(profile_changed(bool,bool,QString)),
+			this, SLOT(handleProfileChanged(bool,bool,QString)));
+
 	_toq->setEndpointListener(ToqConnection::VoiceCallEndpoint, this);
 }
 
@@ -47,11 +58,8 @@ void VoiceCallManager::handleGetPhoneStatusMessage(const ToqConnection::Message
 
 QString VoiceCallManager::getCurrentProfile()
 {
-	QDBusConnection bus = QDBusConnection::sessionBus();
-	QDBusReply<QString> resp = bus.call(QDBusMessage::createMethodCall("com.nokia.profiled",
-																		  "/com/nokia/profiled",
-																		  "com.nokia.profiled",
-																		  "get_profile"));
+	QDBusReply<QString> resp = profiled->get_profile();
+
 	if (resp.isValid()) {
 		return resp.value();
 	} else {
@@ -62,12 +70,18 @@ QString VoiceCallManager::getCurrentProfile()
 
 void VoiceCallManager::setProfile(const QString &name)
 {
-	QDBusConnection bus = QDBusConnection::sessionBus();
-	QDBusReply<bool> resp = bus.call(QDBusMessage::createMethodCall("com.nokia.profiled",
-																   "/com/nokia/profiled",
-																   "com.nokia.profiled",
-																   "set_profile") << name);
+	QDBusReply<bool> resp = profiled->set_profile(name);
+
 	if (!resp.isValid()) {
 		qWarning() << resp.error().message();
 	}
 }
+
+void VoiceCallManager::handleProfileChanged(bool changed, bool active, const QString &profile)
+{
+	Q_UNUSED(changed);
+	Q_UNUSED(active);
+	qDebug() << "Profile changed to" << profile;
+	// TODO
+}
+
diff --git a/saltoqd/voicecallmanager.h b/saltoqd/voicecallmanager.h
index eb9e38e..8a7bd85 100644
--- a/saltoqd/voicecallmanager.h
+++ b/saltoqd/voicecallmanager.h
@@ -19,6 +19,9 @@ private:
 	static QString getCurrentProfile();
 	static void setProfile(const QString &name);
 
+private slots:
+	void handleProfileChanged(bool changed, bool active, const QString &profile);
+
 private:
 	ToqManager *_toq;
 };
-- 
cgit v1.2.3