From 6e30ad83fd2129fbef3ca92da193fe3171293b76 Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Fri, 26 Aug 2016 13:00:27 -0400
Subject: [PATCH] text message : add new contact to contacts list when a
 message arrived from a new contact.

Change-Id: I82545d852c7d880e2ee1fd5bc479765091b6a830
Tuleap: #941
---
 ContactsViewModel.cpp | 34 +++++++++++++++++++++++++++-------
 RingD.cpp             | 12 ++++++++++--
 RingD.h               |  9 ++++++---
 3 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/ContactsViewModel.cpp b/ContactsViewModel.cpp
index 9b5d6da..80799ff 100644
--- a/ContactsViewModel.cpp
+++ b/ContactsViewModel.cpp
@@ -16,12 +16,16 @@
  * You should have received a copy of the GNU General Public License       *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
  **************************************************************************/
-
 #include "pch.h"
+
 #include "ContactsViewModel.h"
 
+using namespace Windows::ApplicationModel::Core;
 using namespace Windows::Data::Json;
 using namespace Windows::Storage;
+using namespace Windows::Storage::Streams;
+using namespace Windows::UI::Core;
+
 
 using namespace RingClientUWP;
 using namespace ViewModel;
@@ -30,10 +34,26 @@ ContactsViewModel::ContactsViewModel()
 {
     contactsList_ = ref new Vector<Contact^>();
     openContactsFromFile();
+
+    /* connect delegates. */
+    RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
+    String^ from, String^ payload) {
+        auto contact = findContactByName(from);
+
+        if (contact == nullptr)
+            contact = addNewContact(from, from); // contact checked inside addNewContact.
+
+        if (contact == nullptr) {
+            ERR_("contact not handled!");
+            return;
+        }
+
+    });
+
 }
 
 Contact^
-ContactsViewModel::findContactByName(String ^ name)
+ContactsViewModel::findContactByName(String^ name)
 {
     for each (Contact^ contact in contactsList_)
         if (contact->name_ == name)
@@ -46,7 +66,7 @@ Contact^
 ContactsViewModel::addNewContact(String^ name, String^ ringId)
 {
     if (contactsList_ && !findContactByName(name)) {
-        Contact^ contact = ref new Contact(name, ringId);
+        Contact^ contact = ref new Contact(name, name);
         contactsList_->Append(contact);
         saveContactsToFile();
         return contact;
@@ -84,16 +104,16 @@ ContactsViewModel::openContactsFromFile()
     String^ contactsFile = ".profile\\contacts.json";
 
     Utils::fileExists(ApplicationData::Current->LocalFolder,
-        contactsFile)
-        .then([this,contactsFile](bool contacts_file_exists)
+                      contactsFile)
+    .then([this,contactsFile](bool contacts_file_exists)
     {
         if (contacts_file_exists) {
             try {
                 create_task(ApplicationData::Current->LocalFolder->GetFileAsync(contactsFile))
-                    .then([this](StorageFile^ file)
+                .then([this](StorageFile^ file)
                 {
                     create_task(FileIO::ReadTextAsync(file))
-                        .then([this](String^ fileContents){
+                    .then([this](String^ fileContents) {
                         if (fileContents != nullptr)
                             Destringify(fileContents);
                     });
diff --git a/RingD.cpp b/RingD.cpp
index c2c0511..e714fc3 100644
--- a/RingD.cpp
+++ b/RingD.cpp
@@ -1,4 +1,4 @@
-/***************************************************************************
+/**************************************************************************
 * Copyright (C) 2016 by Savoir-faire Linux                                *
 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com>              *
 * Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com>          *
@@ -105,7 +105,7 @@ RingClientUWP::RingD::startDaemon()
                 stateChange(callId2, state2, code);
 
             }),
-            DRing::exportable_callback<DRing::ConfigurationSignal::IncomingAccountMessage>([this](
+            DRing::exportable_callback<DRing::ConfigurationSignal::IncomingAccountMessage>([&](
                 const std::string& accountId,
                 const std::string& from,
                 const std::map<std::string, std::string>& payloads)
@@ -114,9 +114,17 @@ RingClientUWP::RingD::startDaemon()
                 MSG_("accountId = " + accountId);
                 MSG_("from = " + from);
 
+                auto accountId2 = toPlatformString(accountId);
+                auto from2 = toPlatformString(from);
+
                 for (auto i : payloads) {
                     MSG_("payload = " + i.second);
                     auto payload = Utils::toPlatformString(i.second);
+                    CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
+                        CoreDispatcherPriority::Low, ref new DispatchedHandler([=]()
+                    {
+                        incomingAccountMessage(accountId2, from2, payload);
+                    }));
                 }
             }),
             DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>([this]()
diff --git a/RingD.h b/RingD.h
index b610fc5..2ee7908 100644
--- a/RingD.h
+++ b/RingD.h
@@ -1,6 +1,7 @@
-/***************************************************************************
+/**************************************************************************
 * Copyright (C) 2016 by Savoir-faire Linux                                *
-* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com>              *
+* Author: J�ger Nicolas <nicolas.jager@savoirfairelinux.com>              *
+* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com>          *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
@@ -15,7 +16,6 @@
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/
-#pragma once
 
 using namespace concurrency;
 
@@ -25,6 +25,8 @@ namespace RingClientUWP
 /* delegate */
 delegate void IncomingCall(String^ accountId, String^ callId, String^ from);
 delegate void StateChange(String^ callId, String^ state, int code);
+delegate void IncomingAccountMessage(String^ accountId, String^ from, String^ payload);
+
 
 public ref class RingD sealed
 {
@@ -61,6 +63,7 @@ internal:
     /* events */
     event IncomingCall^ incomingCall;
     event StateChange^ stateChange;
+    event IncomingAccountMessage^ incomingAccountMessage;
 
 private:
     /* sub classes */
-- 
GitLab