atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 1 | /***************************************************************************
|
| 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 | **************************************************************************/
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 19 | #include "pch.h"
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 20 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 21 | #include "ContactsViewModel.h"
|
| 22 |
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 23 | using namespace Windows::ApplicationModel::Core;
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 24 | using namespace Windows::Data::Json;
|
| 25 | using namespace Windows::Storage;
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 26 | using namespace Windows::Storage::Streams;
|
| 27 | using namespace Windows::UI::Core;
|
| 28 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 29 |
|
| 30 | using namespace RingClientUWP;
|
| 31 | using namespace ViewModel;
|
| 32 |
|
| 33 | ContactsViewModel::ContactsViewModel()
|
| 34 | {
|
| 35 | contactsList_ = ref new Vector<Contact^>();
|
| 36 | openContactsFromFile();
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 37 |
|
| 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 Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 46 | bool isNotSelected = (contact != ContactsViewModel::instance->selectedContact) ? true : false;
|
| 47 |
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 48 | if (contact == nullptr) {
|
| 49 | ERR_("contact not handled!");
|
| 50 | return;
|
| 51 | }
|
| 52 |
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 53 | contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_CONTACT, payload);
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 54 |
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 55 | /* save contacts conversation to disk */
|
| 56 | contact->saveConversationToFile();
|
| 57 |
|
atraczyk | a535ee1 | 2016-08-31 17:36:05 -0400 | [diff] [blame] | 58 | if (contact->ringID_ == from && isNotSelected) {
|
| 59 | // increment contact's unread message count
|
| 60 | contact->addNotifyNewConversationMessage();
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 61 | // update the xaml for all contacts
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 62 | notifyNewConversationMessage();
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 63 | // save to disk
|
| 64 | saveContactsToFile();
|
atraczyk | a535ee1 | 2016-08-31 17:36:05 -0400 | [diff] [blame] | 65 | }
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 66 | });
|
Nicolas Jager | d76940f | 2016-08-31 14:44:04 -0400 | [diff] [blame] | 67 | CallsViewModel::instance->callRecieved += ref new RingClientUWP::CallRecieved([&](
|
| 68 | Call^ call) {
|
| 69 | auto from = call->from;
|
| 70 | auto contact = findContactByName(from);
|
| 71 |
|
| 72 | if (contact == nullptr)
|
| 73 | contact = addNewContact(from, from); // contact checked inside addNewContact.
|
| 74 |
|
| 75 | bool isNotSelected = (contact != ContactsViewModel::instance->selectedContact) ? true : false;
|
| 76 |
|
| 77 | if (contact == nullptr) {
|
| 78 | ERR_("contact not handled!");
|
| 79 | return;
|
| 80 | }
|
| 81 | contact->_call = call;
|
Nicolas Jager | f6a1032 | 2016-09-06 08:17:49 -0400 | [diff] [blame] | 82 | contact->_contactBarHeight = 50;
|
| 83 |
|
Nicolas Jager | d76940f | 2016-08-31 14:44:04 -0400 | [diff] [blame] | 84 | });
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 85 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 86 | }
|
| 87 |
|
| 88 | Contact^
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 89 | ContactsViewModel::findContactByName(String^ name)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 90 | {
|
| 91 | for each (Contact^ contact in contactsList_)
|
| 92 | if (contact->name_ == name)
|
| 93 | return contact;
|
| 94 |
|
| 95 | return nullptr;
|
| 96 | }
|
| 97 |
|
| 98 | Contact^
|
| 99 | ContactsViewModel::addNewContact(String^ name, String^ ringId)
|
| 100 | {
|
Nicolas Jager | 2647eaa | 2016-08-31 12:08:15 -0400 | [diff] [blame] | 101 | auto trimedName = Utils::Trim(name);
|
| 102 | if (contactsList_ && !findContactByName(trimedName)) {
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 103 | Contact^ contact = ref new Contact(trimedName, trimedName, nullptr, 0);
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 104 | contactsList_->Append(contact);
|
| 105 | saveContactsToFile();
|
| 106 | return contact;
|
| 107 | }
|
| 108 |
|
| 109 | return nullptr;
|
| 110 | }
|
| 111 |
|
| 112 | void
|
| 113 | ContactsViewModel::saveContactsToFile()
|
| 114 | {
|
| 115 | StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
|
| 116 | String^ contactsFile = ".profile\\contacts.json";
|
| 117 |
|
| 118 | try {
|
| 119 | create_task(localfolder->CreateFileAsync(contactsFile
|
| 120 | , Windows::Storage::CreationCollisionOption::ReplaceExisting))
|
| 121 | .then([&](StorageFile^ newFile) {
|
| 122 | try {
|
| 123 | FileIO::WriteTextAsync(newFile, Stringify());
|
| 124 | }
|
| 125 | catch (Exception^ e) {
|
| 126 | RingDebug::instance->print("Exception while writing to contacts file");
|
| 127 | }
|
| 128 | });
|
| 129 | }
|
| 130 | catch (Exception^ e) {
|
| 131 | RingDebug::instance->print("Exception while opening contacts file");
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | void
|
| 136 | ContactsViewModel::openContactsFromFile()
|
| 137 | {
|
| 138 | String^ contactsFile = ".profile\\contacts.json";
|
| 139 |
|
| 140 | Utils::fileExists(ApplicationData::Current->LocalFolder,
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 141 | contactsFile)
|
| 142 | .then([this,contactsFile](bool contacts_file_exists)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 143 | {
|
| 144 | if (contacts_file_exists) {
|
| 145 | try {
|
| 146 | create_task(ApplicationData::Current->LocalFolder->GetFileAsync(contactsFile))
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 147 | .then([this](StorageFile^ file)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 148 | {
|
| 149 | create_task(FileIO::ReadTextAsync(file))
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 150 | .then([this](String^ fileContents) {
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 151 | if (fileContents != nullptr)
|
| 152 | Destringify(fileContents);
|
| 153 | });
|
| 154 | });
|
| 155 | }
|
| 156 | catch (Exception^ e) {
|
| 157 | RingDebug::instance->print("Exception while opening contacts file");
|
| 158 | }
|
| 159 | }
|
| 160 | });
|
| 161 | }
|
| 162 |
|
| 163 | String^
|
| 164 | ContactsViewModel::Stringify()
|
| 165 | {
|
| 166 | JsonArray^ jsonArray = ref new JsonArray();
|
| 167 |
|
| 168 | for (unsigned int i = 0; i < contactsList_->Size; i++) {
|
| 169 | jsonArray->Append(contactsList_->GetAt(i)->ToJsonObject());
|
| 170 | }
|
| 171 |
|
| 172 | JsonObject^ jsonObject = ref new JsonObject();
|
| 173 | jsonObject->SetNamedValue(contactListKey, jsonArray);
|
| 174 |
|
| 175 | return jsonObject->Stringify();
|
| 176 | }
|
| 177 |
|
| 178 | void
|
| 179 | ContactsViewModel::Destringify(String^ data)
|
| 180 | {
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 181 | JsonObject^ jsonObject = JsonObject::Parse(data);
|
| 182 | String^ name;
|
| 183 | String^ ringid;
|
| 184 | String^ guid;
|
| 185 | unsigned int unreadmessages;
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 186 |
|
| 187 | JsonArray^ contactlist = jsonObject->GetNamedArray(contactListKey, ref new JsonArray());
|
| 188 | for (unsigned int i = 0; i < contactlist->Size; i++) {
|
| 189 | IJsonValue^ contact = contactlist->GetAt(i);
|
| 190 | if (contact->ValueType == JsonValueType::Object) {
|
| 191 | JsonObject^ jsonContactObject = contact->GetObject();
|
| 192 | JsonObject^ contactObject = jsonContactObject->GetNamedObject(contactKey, nullptr);
|
| 193 | if (contactObject != nullptr) {
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 194 | name = contactObject->GetNamedString(nameKey);
|
| 195 | ringid = contactObject->GetNamedString(ringIDKey);
|
| 196 | guid = contactObject->GetNamedString(GUIDKey);
|
| 197 | unreadmessages = static_cast<uint16_t>(contactObject->GetNamedNumber(unreadMessagesKey));
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 198 | }
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 199 | contactsList_->Append(ref new Contact(name, ringid, guid, unreadmessages));
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 200 | }
|
| 201 | }
|
| 202 | }
|