Skip to content
Snippets Groups Projects
Commit 2e6ab41d authored by Nicolas Jager's avatar Nicolas Jager
Browse files

contact : (re)store contacts list to/from a Json file

- Uses Json windows API to store and restore contacts list on the disk.

Change-Id: I0091019c1184af9fc686aebce2e5223aed8b4d6a
Tuleap: #949
parent a6467168
No related branches found
No related tags found
No related merge requests found
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
using namespace Windows::ApplicationModel::Core; using namespace Windows::ApplicationModel::Core;
using namespace Platform; using namespace Platform;
using namespace Windows::Data::Json;
using namespace Windows::UI::Core; using namespace Windows::UI::Core;
using namespace RingClientUWP; using namespace RingClientUWP;
...@@ -46,3 +47,16 @@ Contact::NotifyPropertyChanged(String^ propertyName) ...@@ -46,3 +47,16 @@ Contact::NotifyPropertyChanged(String^ propertyName)
})); }));
} }
JsonObject^
Contact::ToJsonObject()
{
JsonObject^ contactObject = ref new JsonObject();
contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
JsonObject^ jsonObject = ref new JsonObject();
jsonObject->SetNamedValue(contactKey, contactObject);
return jsonObject;
}
#pragma once #pragma once
/************************************************************************** /**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * 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> * * Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
...@@ -18,8 +18,15 @@ ...@@ -18,8 +18,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/ **************************************************************************/
using namespace Platform; using namespace Platform;
using namespace Windows::Data::Json;
using namespace Windows::UI::Xaml::Data; using namespace Windows::UI::Xaml::Data;
/* strings required by Windows::Data::Json. Defined here on puprose */
String^ nameKey = "name";
String^ ringIDKey = "ringid";
String^ contactKey = "contact";
String^ contactListKey = "contactlist";
namespace RingClientUWP namespace RingClientUWP
{ {
ref class Conversation; ref class Conversation;
...@@ -27,6 +34,7 @@ public ref class Contact sealed : public INotifyPropertyChanged ...@@ -27,6 +34,7 @@ public ref class Contact sealed : public INotifyPropertyChanged
{ {
public: public:
Contact(String^ name, String^ ringID); Contact(String^ name, String^ ringID);
JsonObject^ ToJsonObject();
virtual event PropertyChangedEventHandler^ PropertyChanged; virtual event PropertyChangedEventHandler^ PropertyChanged;
......
/*************************************************************************** /***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
...@@ -18,35 +18,16 @@ ...@@ -18,35 +18,16 @@
#include "pch.h" #include "pch.h"
#include "ContactsViewModel.h" #include "ContactsViewModel.h"
using namespace Windows::Data::Json;
using namespace Windows::Storage;
using namespace RingClientUWP; using namespace RingClientUWP;
using namespace ViewModel; using namespace ViewModel;
ContactsViewModel::ContactsViewModel() ContactsViewModel::ContactsViewModel()
{ {
contactsList_ = ref new Vector<Contact^>(); contactsList_ = ref new Vector<Contact^>();
openContactsFromFile();
contactsList_->Append(ref new Contact("Homer Simpson", "356373d4fh3d2032d2961f4cbd4e1b46"));
contactsList_->Append(ref new Contact("Marge Simpson", "b430222a5219a4cb119607f1cdae900e"));
contactsList_->Append(ref new Contact("Marilyn Manson", "9f9a25b6925b1244f863966f4e33798f"));
contactsList_->Append(ref new Contact("Jesus Christ", "d1da438329d38517d85d5a523b82ffa8"));
contactsList_->Append(ref new Contact("Vladimir Lenin", "e38943ae33c7c9cbd8c6512476927ba7"));
contactsList_->Append(ref new Contact("(de)-crypt master", "45527ef8d4d7b0ba2c3b66342ea0279a"));
contactsList_->Append(ref new Contact("some people", "784fe73c815b58233ba020e7ee766911"));
contactsList_->Append(ref new Contact("some people with a very very very very long name", "356373d4f63d2032d2961f4cbd4e1b46"));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
contactsList_->Append(ref new Contact("some people", ""));
} }
Contact^ Contact^
...@@ -65,8 +46,97 @@ RingClientUWP::ViewModel::ContactsViewModel::addNewContact(String^ name, String^ ...@@ -65,8 +46,97 @@ RingClientUWP::ViewModel::ContactsViewModel::addNewContact(String^ name, String^
if (contactsList_ && !findContactByName(name)) { if (contactsList_ && !findContactByName(name)) {
Contact^ contact = ref new Contact(name, ringId); Contact^ contact = ref new Contact(name, ringId);
contactsList_->Append(contact); contactsList_->Append(contact);
saveContactsToFile();
return contact; return contact;
} }
return nullptr; return nullptr;
} }
void
ContactsViewModel::saveContactsToFile()
{
StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
String^ contactsFile = ".profile\\contacts.json";
try {
create_task(localfolder->CreateFileAsync(contactsFile
, Windows::Storage::CreationCollisionOption::ReplaceExisting))
.then([&](StorageFile^ newFile) {
try {
FileIO::WriteTextAsync(newFile, Stringify());
}
catch (Exception^ e) {
RingDebug::instance->print("Exception while writing to contacts file");
}
});
}
catch (Exception^ e) {
RingDebug::instance->print("Exception while opening contacts file");
}
}
void
ContactsViewModel::openContactsFromFile()
{
String^ contactsFile = ".profile\\contacts.json";
Utils::fileExists(ApplicationData::Current->LocalFolder,
contactsFile)
.then([this, contactsFile](bool contacts_file_exists)
{
if (contacts_file_exists) {
try {
create_task(ApplicationData::Current->LocalFolder->GetFileAsync(contactsFile))
.then([this](StorageFile^ file)
{
create_task(FileIO::ReadTextAsync(file))
.then([this](String^ fileContents) {
if (fileContents != nullptr)
Destringify(fileContents);
});
});
}
catch (Exception^ e) {
RingDebug::instance->print("Exception while opening contacts file");
}
}
});
}
String^
ContactsViewModel::Stringify()
{
JsonArray^ jsonArray = ref new JsonArray();
for (unsigned int i = 0; i < contactsList_->Size; i++) {
jsonArray->Append(contactsList_->GetAt(i)->ToJsonObject());
}
JsonObject^ jsonObject = ref new JsonObject();
jsonObject->SetNamedValue(contactListKey, jsonArray);
return jsonObject->Stringify();
}
void
ContactsViewModel::Destringify(String^ data)
{
JsonObject^ jsonObject = JsonObject::Parse(data);
String^ name;
String^ ringid;
JsonArray^ contactlist = jsonObject->GetNamedArray(contactListKey, ref new JsonArray());
for (unsigned int i = 0; i < contactlist->Size; i++) {
IJsonValue^ contact = contactlist->GetAt(i);
if (contact->ValueType == JsonValueType::Object) {
JsonObject^ jsonContactObject = contact->GetObject();
JsonObject^ contactObject = jsonContactObject->GetNamedObject(contactKey, nullptr);
if (contactObject != nullptr) {
name = contactObject->GetNamedString(nameKey, "");
ringid = contactObject->GetNamedString(ringIDKey, "");
}
contactsList_->Append(ref new Contact(name, ringid));
}
}
}
\ No newline at end of file
#pragma once #pragma once
/************************************************************************** /**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/ **************************************************************************/
using namespace Concurrency;
using namespace Platform::Collections; using namespace Platform::Collections;
namespace RingClientUWP namespace RingClientUWP
...@@ -40,8 +41,11 @@ internal: ...@@ -40,8 +41,11 @@ internal:
/* functions */ /* functions */
Contact^ findContactByName(String^ name); Contact^ findContactByName(String^ name);
Contact^ addNewContact(String^ name, String^ ringId); Contact^ addNewContact(String^ name, String^ ringId);
void saveContactsToFile();
void openContactsFromFile();
String^ Stringify();
void Destringify(String^ data);
/* properties */ /* properties */
property Contact^ selectedContact property Contact^ selectedContact
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment