From c937c9b2d60ec0d19658265936479ec524bf1933 Mon Sep 17 00:00:00 2001
From: Artem Prilutskiy <performer@aesyle>
Date: Fri, 30 Dec 2016 13:34:07 +0300
Subject: [PATCH] ...

---
 CallCapture/PatchCordProxy.cpp      | 44 ++++++++++++++++++++---------
 CallCapture/PatchCordProxy.h        |  1 +
 SVXLink/echolink/PatchCordProxy.cpp | 44 ++++++++++++++++++++---------
 SVXLink/echolink/PatchCordProxy.h   |  1 +
 4 files changed, 62 insertions(+), 28 deletions(-)

diff --git a/CallCapture/PatchCordProxy.cpp b/CallCapture/PatchCordProxy.cpp
index 2e0e94f..348c320 100644
--- a/CallCapture/PatchCordProxy.cpp
+++ b/CallCapture/PatchCordProxy.cpp
@@ -48,22 +48,13 @@ uint32_t PatchCordProxy::getTalkerID()
 
 void PatchCordProxy::setTalkerAlias(const char* value)
 {
-  size_t length = strlen(value);
-  const uint32_t* data = (const uint32_t*)value;
-
-  size_t index = VALUE_CORD_TALKER_ALIAS;
-  size_t count =
-     (length / sizeof(uint32_t)) +
-    ((length % sizeof(uint32_t)) > 0);
+  char* buffer;
+  asprintf(&buffer, "set alias %s", value);
 
   getContextBanner();
-  while (count > 0)
-  {
-    setSpecificValue(index, *data);
-    count --;
-    index ++;
-    data ++;
-  }
+  invokeCommand(buffer);
+
+  free(buffer);
 }
 
 void PatchCordProxy::getContextBanner()
@@ -101,6 +92,31 @@ void PatchCordProxy::getContextBanner()
   dbus_message_unref(message);
 }
 
+void PatchCordProxy::invokeCommand(const char* command)
+{
+  DBusMessage* message = dbus_message_new_method_call(
+    name, OBJECT_PATH, INTERFACE_NAME, "invokeCommand");
+
+  dbus_message_append_args(message,
+    DBUS_TYPE_STRING, &banner,
+    DBUS_TYPE_STRING, &command,
+    DBUS_TYPE_INVALID);
+  
+  DBusPendingCall* pending;
+  if (dbus_connection_send_with_reply(connection, message, &pending, -1))
+  {
+    dbus_connection_flush(connection);
+    dbus_message_unref(message);
+    dbus_pending_call_block(pending);
+    message = dbus_pending_call_steal_reply(pending);
+    dbus_pending_call_unref(pending);
+  }
+  dbus_message_unref(message);
+  
+  // Each call of dbus_message_unref removes banner from the heap
+  banner = NULL;
+}
+
 void PatchCordProxy::setSpecificValue(uint32_t key, uint32_t value)
 {
   DBusMessage* message = dbus_message_new_method_call(
diff --git a/CallCapture/PatchCordProxy.h b/CallCapture/PatchCordProxy.h
index d477135..b9f67c9 100644
--- a/CallCapture/PatchCordProxy.h
+++ b/CallCapture/PatchCordProxy.h
@@ -26,6 +26,7 @@ class PatchCordProxy
     char* banner;
 
     void getContextBanner();
+    void invokeCommand(const char* command);
     void setSpecificValue(uint32_t key, uint32_t value);
     uint32_t getSpecificValue(uint32_t key);
 
diff --git a/SVXLink/echolink/PatchCordProxy.cpp b/SVXLink/echolink/PatchCordProxy.cpp
index 2e0e94f..348c320 100644
--- a/SVXLink/echolink/PatchCordProxy.cpp
+++ b/SVXLink/echolink/PatchCordProxy.cpp
@@ -48,22 +48,13 @@ uint32_t PatchCordProxy::getTalkerID()
 
 void PatchCordProxy::setTalkerAlias(const char* value)
 {
-  size_t length = strlen(value);
-  const uint32_t* data = (const uint32_t*)value;
-
-  size_t index = VALUE_CORD_TALKER_ALIAS;
-  size_t count =
-     (length / sizeof(uint32_t)) +
-    ((length % sizeof(uint32_t)) > 0);
+  char* buffer;
+  asprintf(&buffer, "set alias %s", value);
 
   getContextBanner();
-  while (count > 0)
-  {
-    setSpecificValue(index, *data);
-    count --;
-    index ++;
-    data ++;
-  }
+  invokeCommand(buffer);
+
+  free(buffer);
 }
 
 void PatchCordProxy::getContextBanner()
@@ -101,6 +92,31 @@ void PatchCordProxy::getContextBanner()
   dbus_message_unref(message);
 }
 
+void PatchCordProxy::invokeCommand(const char* command)
+{
+  DBusMessage* message = dbus_message_new_method_call(
+    name, OBJECT_PATH, INTERFACE_NAME, "invokeCommand");
+
+  dbus_message_append_args(message,
+    DBUS_TYPE_STRING, &banner,
+    DBUS_TYPE_STRING, &command,
+    DBUS_TYPE_INVALID);
+  
+  DBusPendingCall* pending;
+  if (dbus_connection_send_with_reply(connection, message, &pending, -1))
+  {
+    dbus_connection_flush(connection);
+    dbus_message_unref(message);
+    dbus_pending_call_block(pending);
+    message = dbus_pending_call_steal_reply(pending);
+    dbus_pending_call_unref(pending);
+  }
+  dbus_message_unref(message);
+  
+  // Each call of dbus_message_unref removes banner from the heap
+  banner = NULL;
+}
+
 void PatchCordProxy::setSpecificValue(uint32_t key, uint32_t value)
 {
   DBusMessage* message = dbus_message_new_method_call(
diff --git a/SVXLink/echolink/PatchCordProxy.h b/SVXLink/echolink/PatchCordProxy.h
index d477135..b9f67c9 100644
--- a/SVXLink/echolink/PatchCordProxy.h
+++ b/SVXLink/echolink/PatchCordProxy.h
@@ -26,6 +26,7 @@ class PatchCordProxy
     char* banner;
 
     void getContextBanner();
+    void invokeCommand(const char* command);
     void setSpecificValue(uint32_t key, uint32_t value);
     uint32_t getSpecificValue(uint32_t key);