blob: d42a84bf31e699a82490d05ab6b7f6ef09158e75 [file] [log] [blame]
atraczykb724d332016-08-30 15:25:59 -04001/***************************************************************************
2 * Copyright (C) 2016 by Savoir-faire Linux *
3 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
4 * Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 **************************************************************************/
atraczykb724d332016-08-30 15:25:59 -040019#include "pch.h"
Nicolas Jager6e30ad82016-08-26 13:00:27 -040020
atraczykb724d332016-08-30 15:25:59 -040021#include "ContactsViewModel.h"
22
Nicolas Jager6e30ad82016-08-26 13:00:27 -040023using namespace Windows::ApplicationModel::Core;
atraczykb724d332016-08-30 15:25:59 -040024using namespace Windows::Data::Json;
25using namespace Windows::Storage;
Nicolas Jager6e30ad82016-08-26 13:00:27 -040026using namespace Windows::Storage::Streams;
27using namespace Windows::UI::Core;
28
atraczykb724d332016-08-30 15:25:59 -040029
30using namespace RingClientUWP;
31using namespace ViewModel;
32
33ContactsViewModel::ContactsViewModel()
34{
35 contactsList_ = ref new Vector<Contact^>();
36 openContactsFromFile();
Nicolas Jager6e30ad82016-08-26 13:00:27 -040037
38 /* connect delegates. */
39 RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
40 String^ from, String^ payload) {
41 auto contact = findContactByName(from);
42
43 if (contact == nullptr)
44 contact = addNewContact(from, from); // contact checked inside addNewContact.
45
Nicolas Jager0788e962016-08-26 15:41:06 -040046 bool isNotSelected = (contact != ContactsViewModel::instance->selectedContact) ? true : false;
47
Nicolas Jager6e30ad82016-08-26 13:00:27 -040048 if (contact == nullptr) {
49 ERR_("contact not handled!");
50 return;
51 }
52
Nicolas Jager93abfea2016-08-30 12:33:07 -040053 contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_CONTACT, payload);
Nicolas Jager0788e962016-08-26 15:41:06 -040054
atraczykf5be5462016-08-31 14:23:06 -040055 /* save contacts conversation to disk */
56 contact->saveConversationToFile();
57
atraczyka535ee12016-08-31 17:36:05 -040058 if (contact->ringID_ == from && isNotSelected) {
59 // increment contact's unread message count
60 contact->addNotifyNewConversationMessage();
61 // update the xaml for all
Nicolas Jager0788e962016-08-26 15:41:06 -040062 notifyNewConversationMessage();
atraczyka535ee12016-08-31 17:36:05 -040063 }
Nicolas Jager6e30ad82016-08-26 13:00:27 -040064 });
65
atraczykb724d332016-08-30 15:25:59 -040066}
67
68Contact^
Nicolas Jager6e30ad82016-08-26 13:00:27 -040069ContactsViewModel::findContactByName(String^ name)
atraczykb724d332016-08-30 15:25:59 -040070{
71 for each (Contact^ contact in contactsList_)
72 if (contact->name_ == name)
73 return contact;
74
75 return nullptr;
76}
77
78Contact^
79ContactsViewModel::addNewContact(String^ name, String^ ringId)
80{
Nicolas Jager2647eaa2016-08-31 12:08:15 -040081 auto trimedName = Utils::Trim(name);
82 if (contactsList_ && !findContactByName(trimedName)) {
atraczykf5be5462016-08-31 14:23:06 -040083 Contact^ contact = ref new Contact(trimedName, trimedName, nullptr);
atraczykb724d332016-08-30 15:25:59 -040084 contactsList_->Append(contact);
85 saveContactsToFile();
86 return contact;
87 }
88
89 return nullptr;
90}
91
92void
93ContactsViewModel::saveContactsToFile()
94{
95 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
96 String^ contactsFile = ".profile\\contacts.json";
97
98 try {
99 create_task(localfolder->CreateFileAsync(contactsFile
100 , Windows::Storage::CreationCollisionOption::ReplaceExisting))
101 .then([&](StorageFile^ newFile) {
102 try {
103 FileIO::WriteTextAsync(newFile, Stringify());
104 }
105 catch (Exception^ e) {
106 RingDebug::instance->print("Exception while writing to contacts file");
107 }
108 });
109 }
110 catch (Exception^ e) {
111 RingDebug::instance->print("Exception while opening contacts file");
112 }
113}
114
115void
116ContactsViewModel::openContactsFromFile()
117{
118 String^ contactsFile = ".profile\\contacts.json";
119
120 Utils::fileExists(ApplicationData::Current->LocalFolder,
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400121 contactsFile)
122 .then([this,contactsFile](bool contacts_file_exists)
atraczykb724d332016-08-30 15:25:59 -0400123 {
124 if (contacts_file_exists) {
125 try {
126 create_task(ApplicationData::Current->LocalFolder->GetFileAsync(contactsFile))
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400127 .then([this](StorageFile^ file)
atraczykb724d332016-08-30 15:25:59 -0400128 {
129 create_task(FileIO::ReadTextAsync(file))
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400130 .then([this](String^ fileContents) {
atraczykb724d332016-08-30 15:25:59 -0400131 if (fileContents != nullptr)
132 Destringify(fileContents);
133 });
134 });
135 }
136 catch (Exception^ e) {
137 RingDebug::instance->print("Exception while opening contacts file");
138 }
139 }
140 });
141}
142
143String^
144ContactsViewModel::Stringify()
145{
146 JsonArray^ jsonArray = ref new JsonArray();
147
148 for (unsigned int i = 0; i < contactsList_->Size; i++) {
149 jsonArray->Append(contactsList_->GetAt(i)->ToJsonObject());
150 }
151
152 JsonObject^ jsonObject = ref new JsonObject();
153 jsonObject->SetNamedValue(contactListKey, jsonArray);
154
155 return jsonObject->Stringify();
156}
157
158void
159ContactsViewModel::Destringify(String^ data)
160{
161 JsonObject^ jsonObject = JsonObject::Parse(data);
162 String^ name;
163 String^ ringid;
atraczykf5be5462016-08-31 14:23:06 -0400164 String^ guid;
atraczykb724d332016-08-30 15:25:59 -0400165
166 JsonArray^ contactlist = jsonObject->GetNamedArray(contactListKey, ref new JsonArray());
167 for (unsigned int i = 0; i < contactlist->Size; i++) {
168 IJsonValue^ contact = contactlist->GetAt(i);
169 if (contact->ValueType == JsonValueType::Object) {
170 JsonObject^ jsonContactObject = contact->GetObject();
171 JsonObject^ contactObject = jsonContactObject->GetNamedObject(contactKey, nullptr);
172 if (contactObject != nullptr) {
173 name = contactObject->GetNamedString(nameKey, "");
174 ringid = contactObject->GetNamedString(ringIDKey, "");
atraczykf5be5462016-08-31 14:23:06 -0400175 guid = contactObject->GetNamedString(GUIDKey, "");
atraczykb724d332016-08-30 15:25:59 -0400176 }
atraczykf5be5462016-08-31 14:23:06 -0400177 contactsList_->Append(ref new Contact(name, ringid, guid));
atraczykb724d332016-08-30 15:25:59 -0400178 }
179 }
180}