blob: d714ad0e0c5f8cadf639032ab7f57aeb1a55c82d [file] [log] [blame]
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -04001/**************************************************************************
2* Copyright (C) 2016 by Savoir-faire Linux *
3* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
Nicolas Jager58c70b02016-08-26 09:50:45 -04004* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -04005* *
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
atraczykf5be5462016-08-31 14:23:06 -040023#include "ObjBase.h" // for CoCreateGuid
24
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040025using namespace Windows::ApplicationModel::Core;
26using namespace Platform;
Nicolas Jager2e6ab412016-08-26 11:12:00 -040027using namespace Windows::Data::Json;
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040028using namespace Windows::UI::Core;
29
30using namespace RingClientUWP;
Nicolas Jager0788e962016-08-26 15:41:06 -040031using namespace ViewModel;
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040032
33Contact::Contact(String^ name,
atraczykf5be5462016-08-31 14:23:06 -040034 String^ ringID,
35 String^ GUID)
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040036{
atraczykf5be5462016-08-31 14:23:06 -040037 name_ = name;
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040038 ringID_ = ringID;
atraczykf5be5462016-08-31 14:23:06 -040039 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 Jager58c70b02016-08-26 09:50:45 -040047 conversation_ = ref new Conversation();
atraczykf5be5462016-08-31 14:23:06 -040048 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 Jager0788e962016-08-26 15:41:06 -040073 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([&] () {
78 notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
79 unreadMessages_++;
80 PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
81 });
82 ContactsViewModel::instance->newContactSelected += ref new RingClientUWP::NewContactSelected([&]() {
83 if (ContactsViewModel::instance->selectedContact == this) {
84 PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
85 notificationNewMessage = Windows::UI::Xaml::Visibility::Collapsed;
86 unreadMessages_ = 0;
87 }
88 });
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040089}
90
91void
92Contact::NotifyPropertyChanged(String^ propertyName)
93{
94 CoreApplicationView^ view = CoreApplication::MainView;
95 view->CoreWindow->Dispatcher->RunAsync(
96 CoreDispatcherPriority::Normal,
97 ref new DispatchedHandler([this, propertyName]()
98 {
99 PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -0400100 }));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400101}
102
103JsonObject^
104Contact::ToJsonObject()
105{
106 JsonObject^ contactObject = ref new JsonObject();
107 contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
108 contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
atraczykf5be5462016-08-31 14:23:06 -0400109 contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400110
111 JsonObject^ jsonObject = ref new JsonObject();
112 jsonObject->SetNamedValue(contactKey, contactObject);
113
114 return jsonObject;
115}
atraczykf5be5462016-08-31 14:23:06 -0400116
117String^
118Contact::StringifyConversation()
119{
120 JsonArray^ jsonArray = ref new JsonArray();
121
122 for (unsigned int i = 0; i < conversation_->_messages->Size; i++) {
123 jsonArray->Append(conversation_->_messages->GetAt(i)->ToJsonObject());
124 }
125
126 JsonObject^ jsonObject = ref new JsonObject();
127 jsonObject->SetNamedValue(conversationKey, jsonArray);
128
129 return jsonObject->Stringify();
130}
131
132void
133Contact::DestringifyConversation(String^ data)
134{
135 JsonObject^ jsonObject = JsonObject::Parse(data);
136 String^ date;
137 bool fromContact;
138 String^ payload;
139
140 JsonArray^ messageList = jsonObject->GetNamedArray(conversationKey, ref new JsonArray());
141 for (unsigned int i = 0; i < messageList->Size; i++) {
142 IJsonValue^ message = messageList->GetAt(i);
143 if (message->ValueType == JsonValueType::Object) {
144 JsonObject^ jsonMessageObject = message->GetObject();
145 JsonObject^ messageObject = jsonMessageObject->GetNamedObject(messageKey, nullptr);
146 if (messageObject != nullptr) {
147 date = messageObject->GetNamedString(dateKey, "");
148 fromContact = messageObject->GetNamedBoolean(fromContactKey, "");
149 payload = messageObject->GetNamedString(payloadKey, "");
150 }
151 conversation_->addMessage(date, fromContact, payload);
152 }
153 }
154}
155
156void
157Contact::saveConversationToFile()
158{
159 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
160 String^ messagesFile = ".messages\\" + GUID_ + ".json";
161
162 try {
163 create_task(localfolder->CreateFileAsync(messagesFile
164 , Windows::Storage::CreationCollisionOption::ReplaceExisting))
165 .then([&](StorageFile^ file) {
166 try {
167 FileIO::WriteTextAsync(file, StringifyConversation());
168 }
169 catch (Exception^ e) {
170 RingDebug::instance->print("Exception while writing to conversation file");
171 }
172 });
173 }
174 catch (Exception^ e) {
175 RingDebug::instance->print("Exception while opening conversation file");
176 }
177}