blob: 067f2954f4a52b61ea3c785d9bc3110557422bcd [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
atraczyk21e55dd2016-09-19 15:46:55 -040023#include "fileutils.h"
24
Nicolas Jager6e30ad82016-08-26 13:00:27 -040025using namespace Windows::ApplicationModel::Core;
atraczykb724d332016-08-30 15:25:59 -040026using namespace Windows::Data::Json;
27using namespace Windows::Storage;
Nicolas Jager6e30ad82016-08-26 13:00:27 -040028using namespace Windows::Storage::Streams;
29using namespace Windows::UI::Core;
30
atraczykb724d332016-08-30 15:25:59 -040031
32using namespace RingClientUWP;
33using namespace ViewModel;
34
35ContactsViewModel::ContactsViewModel()
36{
37 contactsList_ = ref new Vector<Contact^>();
38 openContactsFromFile();
Nicolas Jager6e30ad82016-08-26 13:00:27 -040039
40 /* connect delegates. */
41 RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
Nicolas Jagerc551c362016-10-01 19:24:50 -040042 String^ fromRingId, String^ payload) {
43 auto contact = findContactByName(fromRingId);
Nicolas Jager6e30ad82016-08-26 13:00:27 -040044
45 if (contact == nullptr)
Nicolas Jagerc551c362016-10-01 19:24:50 -040046 contact = addNewContact(fromRingId, fromRingId); // contact checked inside addNewContact.
Nicolas Jager6e30ad82016-08-26 13:00:27 -040047
Nicolas Jagerc551c362016-10-01 19:24:50 -040048 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
Nicolas Jager0788e962016-08-26 15:41:06 -040049
Nicolas Jager6e30ad82016-08-26 13:00:27 -040050 if (contact == nullptr) {
51 ERR_("contact not handled!");
52 return;
53 }
54
Nicolas Jager93abfea2016-08-30 12:33:07 -040055 contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_CONTACT, payload);
Nicolas Jager0788e962016-08-26 15:41:06 -040056
atraczykf5be5462016-08-31 14:23:06 -040057 /* save contacts conversation to disk */
58 contact->saveConversationToFile();
59
Nicolas Jagerc551c362016-10-01 19:24:50 -040060
61 auto selectedContact = (item) ? item->_contact : nullptr;
62
63 if (contact->ringID_ == fromRingId && contact != selectedContact) {
64 contact->_unreadMessages++;
65 /* saveContactsToFile used to save the notification */
66 saveContactsToFile();
atraczyka535ee12016-08-31 17:36:05 -040067 }
Nicolas Jager6e30ad82016-08-26 13:00:27 -040068 });
atraczykb724d332016-08-30 15:25:59 -040069}
70
Nicolas Jager7c409f32016-09-08 09:35:16 -040071Contact^ // refacto : remove "byName"
Nicolas Jager6e30ad82016-08-26 13:00:27 -040072ContactsViewModel::findContactByName(String^ name)
atraczykb724d332016-08-30 15:25:59 -040073{
atraczyk25608ed2016-09-15 11:12:16 -040074 auto trimmedName = Utils::Trim(name);
atraczykb724d332016-08-30 15:25:59 -040075 for each (Contact^ contact in contactsList_)
atraczyk25608ed2016-09-15 11:12:16 -040076 if (contact->name_ == trimmedName)
atraczykb724d332016-08-30 15:25:59 -040077 return contact;
78
79 return nullptr;
80}
81
82Contact^
83ContactsViewModel::addNewContact(String^ name, String^ ringId)
84{
atraczyk25608ed2016-09-15 11:12:16 -040085 auto trimmedName = Utils::Trim(name);
86 if (contactsList_ && !findContactByName(trimmedName)) {
87 Contact^ contact = ref new Contact(trimmedName, trimmedName, nullptr, 0);
atraczykb724d332016-08-30 15:25:59 -040088 contactsList_->Append(contact);
89 saveContactsToFile();
Nicolas Jager7c409f32016-09-08 09:35:16 -040090 contactAdded(contact);
atraczykb724d332016-08-30 15:25:59 -040091 return contact;
92 }
93
94 return nullptr;
95}
96
97void
98ContactsViewModel::saveContactsToFile()
99{
100 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
atraczyk21e55dd2016-09-19 15:46:55 -0400101 String^ contactsFile = localfolder->Path + "\\" + ".profile\\contacts.json";
atraczykb724d332016-08-30 15:25:59 -0400102
atraczyk21e55dd2016-09-19 15:46:55 -0400103 if (ring::fileutils::recursive_mkdir(Utils::toString(localfolder->Path + "\\" + ".profile\\").c_str())) {
104 std::ofstream file(Utils::toString(contactsFile).c_str());
105 if (file.is_open())
106 {
107 file << Utils::toString(Stringify());
108 file.close();
109 }
atraczykb724d332016-08-30 15:25:59 -0400110 }
111}
112
113void
114ContactsViewModel::openContactsFromFile()
115{
atraczyk21e55dd2016-09-19 15:46:55 -0400116 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
117 String^ contactsFile = localfolder->Path + "\\" + ".profile\\contacts.json";
atraczykb724d332016-08-30 15:25:59 -0400118
atraczyk21e55dd2016-09-19 15:46:55 -0400119 String^ fileContents = Utils::toPlatformString(Utils::getStringFromFile(Utils::toString(contactsFile)));
120
Nicolas Jager9edbea32016-10-03 09:13:53 -0400121 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
atraczyk21e55dd2016-09-19 15:46:55 -0400122 ref new DispatchedHandler([=]() {
123 if (fileContents != nullptr)
124 Destringify(fileContents);
125 }));
atraczykb724d332016-08-30 15:25:59 -0400126}
127
128String^
129ContactsViewModel::Stringify()
130{
131 JsonArray^ jsonArray = ref new JsonArray();
132
133 for (unsigned int i = 0; i < contactsList_->Size; i++) {
134 jsonArray->Append(contactsList_->GetAt(i)->ToJsonObject());
135 }
136
137 JsonObject^ jsonObject = ref new JsonObject();
138 jsonObject->SetNamedValue(contactListKey, jsonArray);
139
140 return jsonObject->Stringify();
141}
142
143void
144ContactsViewModel::Destringify(String^ data)
145{
atraczykcac45522016-08-31 18:42:36 -0400146 JsonObject^ jsonObject = JsonObject::Parse(data);
147 String^ name;
148 String^ ringid;
149 String^ guid;
150 unsigned int unreadmessages;
atraczykb724d332016-08-30 15:25:59 -0400151
152 JsonArray^ contactlist = jsonObject->GetNamedArray(contactListKey, ref new JsonArray());
153 for (unsigned int i = 0; i < contactlist->Size; i++) {
154 IJsonValue^ contact = contactlist->GetAt(i);
155 if (contact->ValueType == JsonValueType::Object) {
156 JsonObject^ jsonContactObject = contact->GetObject();
157 JsonObject^ contactObject = jsonContactObject->GetNamedObject(contactKey, nullptr);
158 if (contactObject != nullptr) {
atraczykcac45522016-08-31 18:42:36 -0400159 name = contactObject->GetNamedString(nameKey);
160 ringid = contactObject->GetNamedString(ringIDKey);
161 guid = contactObject->GetNamedString(GUIDKey);
162 unreadmessages = static_cast<uint16_t>(contactObject->GetNamedNumber(unreadMessagesKey));
atraczykb724d332016-08-30 15:25:59 -0400163 }
Nicolas Jager7c409f32016-09-08 09:35:16 -0400164 auto contact = ref new Contact(name, ringid, guid, unreadmessages);
165 contactsList_->Append(contact);
166 contactAdded(contact);
atraczykb724d332016-08-30 15:25:59 -0400167 }
168 }
169}