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