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 |
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 23 | #include "fileutils.h"
|
| 24 |
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 25 | using namespace Windows::ApplicationModel::Core;
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 26 | using namespace Windows::Data::Json;
|
| 27 | using namespace Windows::Storage;
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 28 | using namespace Windows::Storage::Streams;
|
| 29 | using namespace Windows::UI::Core;
|
| 30 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 31 |
|
| 32 | using namespace RingClientUWP;
|
| 33 | using namespace ViewModel;
|
| 34 |
|
| 35 | ContactsViewModel::ContactsViewModel()
|
| 36 | {
|
| 37 | contactsList_ = ref new Vector<Contact^>();
|
| 38 | openContactsFromFile();
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 39 |
|
| 40 | /* connect delegates. */
|
| 41 | RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 42 | String^ fromRingId, String^ payload) {
|
| 43 | auto contact = findContactByName(fromRingId);
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 44 |
|
| 45 | if (contact == nullptr)
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 46 | contact = addNewContact(fromRingId, fromRingId); // contact checked inside addNewContact.
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 47 |
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 48 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 49 |
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 50 | if (contact == nullptr) {
|
| 51 | ERR_("contact not handled!");
|
| 52 | return;
|
| 53 | }
|
| 54 |
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 55 | contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_CONTACT, payload);
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 56 |
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 57 | /* save contacts conversation to disk */
|
| 58 | contact->saveConversationToFile();
|
| 59 |
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 60 |
|
| 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();
|
atraczyk | a535ee1 | 2016-08-31 17:36:05 -0400 | [diff] [blame] | 67 | }
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 68 | });
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 69 | }
|
| 70 |
|
Nicolas Jager | 7c409f3 | 2016-09-08 09:35:16 -0400 | [diff] [blame] | 71 | Contact^ // refacto : remove "byName"
|
Nicolas Jager | 6e30ad8 | 2016-08-26 13:00:27 -0400 | [diff] [blame] | 72 | ContactsViewModel::findContactByName(String^ name)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 73 | {
|
atraczyk | 25608ed | 2016-09-15 11:12:16 -0400 | [diff] [blame] | 74 | auto trimmedName = Utils::Trim(name);
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 75 | for each (Contact^ contact in contactsList_)
|
atraczyk | 25608ed | 2016-09-15 11:12:16 -0400 | [diff] [blame] | 76 | if (contact->name_ == trimmedName)
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 77 | return contact;
|
| 78 |
|
| 79 | return nullptr;
|
| 80 | }
|
| 81 |
|
| 82 | Contact^
|
| 83 | ContactsViewModel::addNewContact(String^ name, String^ ringId)
|
| 84 | {
|
atraczyk | 25608ed | 2016-09-15 11:12:16 -0400 | [diff] [blame] | 85 | auto trimmedName = Utils::Trim(name);
|
| 86 | if (contactsList_ && !findContactByName(trimmedName)) {
|
| 87 | Contact^ contact = ref new Contact(trimmedName, trimmedName, nullptr, 0);
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 88 | contactsList_->Append(contact);
|
| 89 | saveContactsToFile();
|
Nicolas Jager | 7c409f3 | 2016-09-08 09:35:16 -0400 | [diff] [blame] | 90 | contactAdded(contact);
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 91 | return contact;
|
| 92 | }
|
| 93 |
|
| 94 | return nullptr;
|
| 95 | }
|
| 96 |
|
| 97 | void
|
| 98 | ContactsViewModel::saveContactsToFile()
|
| 99 | {
|
| 100 | StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 101 | String^ contactsFile = localfolder->Path + "\\" + ".profile\\contacts.json";
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 102 |
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 103 | 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 | }
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | void
|
| 114 | ContactsViewModel::openContactsFromFile()
|
| 115 | {
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 116 | StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
|
| 117 | String^ contactsFile = localfolder->Path + "\\" + ".profile\\contacts.json";
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 118 |
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 119 | String^ fileContents = Utils::toPlatformString(Utils::getStringFromFile(Utils::toString(contactsFile)));
|
| 120 |
|
Nicolas Jager | 9edbea3 | 2016-10-03 09:13:53 -0400 | [diff] [blame] | 121 | CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 122 | ref new DispatchedHandler([=]() {
|
| 123 | if (fileContents != nullptr)
|
| 124 | Destringify(fileContents);
|
| 125 | }));
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 126 | }
|
| 127 |
|
| 128 | String^
|
| 129 | ContactsViewModel::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 |
|
| 143 | void
|
| 144 | ContactsViewModel::Destringify(String^ data)
|
| 145 | {
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 146 | JsonObject^ jsonObject = JsonObject::Parse(data);
|
| 147 | String^ name;
|
| 148 | String^ ringid;
|
| 149 | String^ guid;
|
| 150 | unsigned int unreadmessages;
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 151 |
|
| 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) {
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 159 | name = contactObject->GetNamedString(nameKey);
|
| 160 | ringid = contactObject->GetNamedString(ringIDKey);
|
| 161 | guid = contactObject->GetNamedString(GUIDKey);
|
| 162 | unreadmessages = static_cast<uint16_t>(contactObject->GetNamedNumber(unreadMessagesKey));
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 163 | }
|
Nicolas Jager | 7c409f3 | 2016-09-08 09:35:16 -0400 | [diff] [blame] | 164 | auto contact = ref new Contact(name, ringid, guid, unreadmessages);
|
| 165 | contactsList_->Append(contact);
|
| 166 | contactAdded(contact);
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 167 | }
|
| 168 | }
|
| 169 | }
|