Commit 6e30ad83 authored by Nicolas Jager's avatar Nicolas Jager

text message : add new contact to contacts list when

a message arrived from a new contact.

Change-Id: I82545d852c7d880e2ee1fd5bc479765091b6a830
Tuleap: #941
parent b724d332
......@@ -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);
});
......
/***************************************************************************
/**************************************************************************
* 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]()
......
/***************************************************************************
/**************************************************************************
* 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 */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment