blob: cf24c9cde5def7619b2f5b42dd8c62cf6593e499 [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([&] () {
Nicolas Jager0788e962016-08-26 15:41:06 -040078 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 Jagerbff5fbb2016-08-18 08:58:56 -040087}
88
89void
atraczyka535ee12016-08-31 17:36:05 -040090Contact::addNotifyNewConversationMessage()
91{
92 notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
93 unreadMessages_++;
94 RingDebug::instance->print(Utils::toString(unreadMessages_.ToString()));
95}
96
97void
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040098Contact::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 Jagerbff5fbb2016-08-18 08:58:56 -0400106 }));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400107}
108
109JsonObject^
110Contact::ToJsonObject()
111{
112 JsonObject^ contactObject = ref new JsonObject();
113 contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
114 contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
atraczykf5be5462016-08-31 14:23:06 -0400115 contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400116
117 JsonObject^ jsonObject = ref new JsonObject();
118 jsonObject->SetNamedValue(contactKey, contactObject);
119
120 return jsonObject;
121}
atraczykf5be5462016-08-31 14:23:06 -0400122
123String^
124Contact::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
138void
139Contact::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
162void
163Contact::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}