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();
|
| 61 | // update the xaml for all
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 62 | notifyNewConversationMessage();
|
atraczyk | a535ee1 | 2016-08-31 17:36:05 -0400 | [diff] [blame^] | 63 | }
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 64 | });
|
| 65 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 66 | }
|
| 67 |
|
| 68 | Contact^
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 69 | ContactsViewModel::findContactByName(String^ name)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 70 | {
|
| 71 | for each (Contact^ contact in contactsList_)
|
| 72 | if (contact->name_ == name)
|
| 73 | return contact;
|
| 74 |
|
| 75 | return nullptr;
|
| 76 | }
|
| 77 |
|
| 78 | Contact^
|
| 79 | ContactsViewModel::addNewContact(String^ name, String^ ringId)
|
| 80 | {
|
Nicolas Jager | 2647eaa | 2016-08-31 12:08:15 -0400 | [diff] [blame] | 81 | auto trimedName = Utils::Trim(name);
|
| 82 | if (contactsList_ && !findContactByName(trimedName)) {
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 83 | Contact^ contact = ref new Contact(trimedName, trimedName, nullptr);
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 84 | contactsList_->Append(contact);
|
| 85 | saveContactsToFile();
|
| 86 | return contact;
|
| 87 | }
|
| 88 |
|
| 89 | return nullptr;
|
| 90 | }
|
| 91 |
|
| 92 | void
|
| 93 | ContactsViewModel::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 |
|
| 115 | void
|
| 116 | ContactsViewModel::openContactsFromFile()
|
| 117 | {
|
| 118 | String^ contactsFile = ".profile\\contacts.json";
|
| 119 |
|
| 120 | Utils::fileExists(ApplicationData::Current->LocalFolder,
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 121 | contactsFile)
|
| 122 | .then([this,contactsFile](bool contacts_file_exists)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 123 | {
|
| 124 | if (contacts_file_exists) {
|
| 125 | try {
|
| 126 | create_task(ApplicationData::Current->LocalFolder->GetFileAsync(contactsFile))
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 127 | .then([this](StorageFile^ file)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 128 | {
|
| 129 | create_task(FileIO::ReadTextAsync(file))
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 130 | .then([this](String^ fileContents) {
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 131 | 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 |
|
| 143 | String^
|
| 144 | ContactsViewModel::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 |
|
| 158 | void
|
| 159 | ContactsViewModel::Destringify(String^ data)
|
| 160 | {
|
| 161 | JsonObject^ jsonObject = JsonObject::Parse(data);
|
| 162 | String^ name;
|
| 163 | String^ ringid;
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 164 | String^ guid;
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 165 |
|
| 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, "");
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 175 | guid = contactObject->GetNamedString(GUIDKey, "");
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 176 | }
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 177 | contactsList_->Append(ref new Contact(name, ringid, guid));
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 178 | }
|
| 179 | }
|
| 180 | }
|