Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 1 | /**************************************************************************
|
| 2 | * Copyright (C) 2016 by Savoir-faire Linux *
|
| 3 | * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
|
Nicolas Jager | 58c70b0 | 2016-08-26 09:50:45 -0400 | [diff] [blame] | 4 | * Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 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 | **************************************************************************/
|
| 19 | #include "pch.h"
|
| 20 |
|
| 21 | #include "Contact.h"
|
| 22 |
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 23 | #include "ObjBase.h" // for CoCreateGuid
|
| 24 |
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 25 | using namespace Windows::ApplicationModel::Core;
|
| 26 | using namespace Platform;
|
Nicolas Jager | 2e6ab41 | 2016-08-26 11:12:00 -0400 | [diff] [blame] | 27 | using namespace Windows::Data::Json;
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 28 | using namespace Windows::UI::Core;
|
| 29 |
|
| 30 | using namespace RingClientUWP;
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 31 | using namespace ViewModel;
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 32 |
|
| 33 | Contact::Contact(String^ name,
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 34 | String^ ringID,
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 35 | String^ GUID,
|
| 36 | unsigned int unreadmessages)
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 37 | {
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 38 | name_ = name;
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 39 | ringID_ = ringID;
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 40 | GUID_ = GUID;
|
| 41 |
|
| 42 | if (GUID_ == nullptr)
|
| 43 | GUID_ = Utils::GetNewGUID();
|
| 44 |
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 45 | // load conversation from disk
|
Nicolas Jager | 58c70b0 | 2016-08-26 09:50:45 -0400 | [diff] [blame] | 46 | conversation_ = ref new Conversation();
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 47 | StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
|
| 48 | String^ messagesFile = ".messages\\" + GUID_ + ".json";
|
| 49 | Utils::fileExists(ApplicationData::Current->LocalFolder,
|
| 50 | messagesFile)
|
| 51 | .then([this,messagesFile](bool messages_file_exists)
|
| 52 | {
|
| 53 | if (messages_file_exists) {
|
| 54 | try {
|
| 55 | create_task(ApplicationData::Current->LocalFolder->GetFileAsync(messagesFile))
|
| 56 | .then([this](StorageFile^ file)
|
| 57 | {
|
| 58 | create_task(FileIO::ReadTextAsync(file))
|
| 59 | .then([this](String^ fileContents) {
|
| 60 | if (fileContents != nullptr)
|
| 61 | DestringifyConversation(fileContents);
|
| 62 | });
|
| 63 | });
|
| 64 | }
|
| 65 | catch (Exception^ e) {
|
| 66 | RingDebug::instance->print("Exception while opening messages file");
|
| 67 | }
|
| 68 | }
|
| 69 | });
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 70 |
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 71 | notificationNewMessage_ = Windows::UI::Xaml::Visibility::Collapsed;
|
Nicolas Jager | d76940f | 2016-08-31 14:44:04 -0400 | [diff] [blame] | 72 | unreadMessages_ = unreadmessages;
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 73 |
|
| 74 | if(unreadMessages_) {
|
| 75 | notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
|
| 76 | PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
|
| 77 | }
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 78 |
|
| 79 | /* connect to delegate */
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 80 | ContactsViewModel::instance->notifyNewConversationMessage += ref new NotifyNewConversationMessage([&] (
|
| 81 | bool isContactNotSelected) {
|
| 82 | if (isContactNotSelected)
|
| 83 | PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 84 | });
|
| 85 | ContactsViewModel::instance->newContactSelected += ref new RingClientUWP::NewContactSelected([&]() {
|
| 86 | if (ContactsViewModel::instance->selectedContact == this) {
|
| 87 | PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
|
| 88 | notificationNewMessage = Windows::UI::Xaml::Visibility::Collapsed;
|
| 89 | unreadMessages_ = 0;
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 90 | ContactsViewModel::instance->saveContactsToFile();
|
Nicolas Jager | 0788e96 | 2016-08-26 15:41:06 -0400 | [diff] [blame] | 91 | }
|
| 92 | });
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 93 | }
|
| 94 |
|
| 95 | void
|
atraczyk | a535ee1 | 2016-08-31 17:36:05 -0400 | [diff] [blame] | 96 | Contact::addNotifyNewConversationMessage()
|
| 97 | {
|
| 98 | notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
|
| 99 | unreadMessages_++;
|
atraczyk | a535ee1 | 2016-08-31 17:36:05 -0400 | [diff] [blame] | 100 | }
|
| 101 |
|
| 102 | void
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 103 | Contact::NotifyPropertyChanged(String^ propertyName)
|
| 104 | {
|
| 105 | CoreApplicationView^ view = CoreApplication::MainView;
|
| 106 | view->CoreWindow->Dispatcher->RunAsync(
|
| 107 | CoreDispatcherPriority::Normal,
|
| 108 | ref new DispatchedHandler([this, propertyName]()
|
| 109 | {
|
| 110 | PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
|
Nicolas Jager | bff5fbb | 2016-08-18 08:58:56 -0400 | [diff] [blame] | 111 | }));
|
Nicolas Jager | 2e6ab41 | 2016-08-26 11:12:00 -0400 | [diff] [blame] | 112 | }
|
| 113 |
|
| 114 | JsonObject^
|
| 115 | Contact::ToJsonObject()
|
| 116 | {
|
| 117 | JsonObject^ contactObject = ref new JsonObject();
|
| 118 | contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
|
| 119 | contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 120 | contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_));
|
atraczyk | cac4552 | 2016-08-31 18:42:36 -0400 | [diff] [blame] | 121 | contactObject->SetNamedValue(unreadMessagesKey, JsonValue::CreateNumberValue(unreadMessages_));
|
Nicolas Jager | 2e6ab41 | 2016-08-26 11:12:00 -0400 | [diff] [blame] | 122 |
|
| 123 | JsonObject^ jsonObject = ref new JsonObject();
|
| 124 | jsonObject->SetNamedValue(contactKey, contactObject);
|
| 125 |
|
| 126 | return jsonObject;
|
| 127 | }
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 128 |
|
| 129 | String^
|
| 130 | Contact::StringifyConversation()
|
| 131 | {
|
| 132 | JsonArray^ jsonArray = ref new JsonArray();
|
| 133 |
|
| 134 | for (unsigned int i = 0; i < conversation_->_messages->Size; i++) {
|
| 135 | jsonArray->Append(conversation_->_messages->GetAt(i)->ToJsonObject());
|
| 136 | }
|
| 137 |
|
| 138 | JsonObject^ jsonObject = ref new JsonObject();
|
| 139 | jsonObject->SetNamedValue(conversationKey, jsonArray);
|
| 140 |
|
| 141 | return jsonObject->Stringify();
|
| 142 | }
|
| 143 |
|
| 144 | void
|
| 145 | Contact::DestringifyConversation(String^ data)
|
| 146 | {
|
| 147 | JsonObject^ jsonObject = JsonObject::Parse(data);
|
| 148 | String^ date;
|
| 149 | bool fromContact;
|
| 150 | String^ payload;
|
| 151 |
|
| 152 | JsonArray^ messageList = jsonObject->GetNamedArray(conversationKey, ref new JsonArray());
|
| 153 | for (unsigned int i = 0; i < messageList->Size; i++) {
|
| 154 | IJsonValue^ message = messageList->GetAt(i);
|
| 155 | if (message->ValueType == JsonValueType::Object) {
|
| 156 | JsonObject^ jsonMessageObject = message->GetObject();
|
| 157 | JsonObject^ messageObject = jsonMessageObject->GetNamedObject(messageKey, nullptr);
|
| 158 | if (messageObject != nullptr) {
|
| 159 | date = messageObject->GetNamedString(dateKey, "");
|
| 160 | fromContact = messageObject->GetNamedBoolean(fromContactKey, "");
|
| 161 | payload = messageObject->GetNamedString(payloadKey, "");
|
| 162 | }
|
| 163 | conversation_->addMessage(date, fromContact, payload);
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | void
|
| 169 | Contact::saveConversationToFile()
|
| 170 | {
|
| 171 | StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
|
| 172 | String^ messagesFile = ".messages\\" + GUID_ + ".json";
|
| 173 |
|
| 174 | try {
|
| 175 | create_task(localfolder->CreateFileAsync(messagesFile
|
| 176 | , Windows::Storage::CreationCollisionOption::ReplaceExisting))
|
| 177 | .then([&](StorageFile^ file) {
|
| 178 | try {
|
| 179 | FileIO::WriteTextAsync(file, StringifyConversation());
|
| 180 | }
|
| 181 | catch (Exception^ e) {
|
| 182 | RingDebug::instance->print("Exception while writing to conversation file");
|
| 183 | }
|
| 184 | });
|
| 185 | }
|
| 186 | catch (Exception^ e) {
|
| 187 | RingDebug::instance->print("Exception while opening conversation file");
|
| 188 | }
|
| 189 | } |