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