blob: 6415b335b6c0b462a4ec6bb0aa7d2c2875739eb6 [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,
atraczykcac45522016-08-31 18:42:36 -040035 String^ GUID,
36 unsigned int unreadmessages)
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040037{
atraczykf5be5462016-08-31 14:23:06 -040038 name_ = name;
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040039 ringID_ = ringID;
atraczykf5be5462016-08-31 14:23:06 -040040 GUID_ = GUID;
41
42 if (GUID_ == nullptr)
43 GUID_ = Utils::GetNewGUID();
44
45 //RingDebug::instance->print(Utils::toString(GUID_).c_str());
46
47 // load conversation from disk
Nicolas Jager58c70b02016-08-26 09:50:45 -040048 conversation_ = ref new Conversation();
atraczykf5be5462016-08-31 14:23:06 -040049 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
50 String^ messagesFile = ".messages\\" + GUID_ + ".json";
51 Utils::fileExists(ApplicationData::Current->LocalFolder,
52 messagesFile)
53 .then([this,messagesFile](bool messages_file_exists)
54 {
55 if (messages_file_exists) {
56 try {
57 create_task(ApplicationData::Current->LocalFolder->GetFileAsync(messagesFile))
58 .then([this](StorageFile^ file)
59 {
60 create_task(FileIO::ReadTextAsync(file))
61 .then([this](String^ fileContents) {
62 if (fileContents != nullptr)
63 DestringifyConversation(fileContents);
64 });
65 });
66 }
67 catch (Exception^ e) {
68 RingDebug::instance->print("Exception while opening messages file");
69 }
70 }
71 });
atraczykf5be5462016-08-31 14:23:06 -040072
Nicolas Jager0788e962016-08-26 15:41:06 -040073 notificationNewMessage_ = Windows::UI::Xaml::Visibility::Collapsed;
atraczykcac45522016-08-31 18:42:36 -040074 unreadMessages_ = unreadmessages; // not saved on disk yet (TO DO)
75
76 if(unreadMessages_) {
77 notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
78 PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
79 }
Nicolas Jager0788e962016-08-26 15:41:06 -040080
81 /* connect to delegate */
82 ContactsViewModel::instance->notifyNewConversationMessage += ref new NotifyNewConversationMessage([&] () {
Nicolas Jager0788e962016-08-26 15:41:06 -040083 PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
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;
atraczykcac45522016-08-31 18:42:36 -040090 ContactsViewModel::instance->saveContactsToFile();
Nicolas Jager0788e962016-08-26 15:41:06 -040091 }
92 });
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040093}
94
95void
atraczyka535ee12016-08-31 17:36:05 -040096Contact::addNotifyNewConversationMessage()
97{
98 notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
99 unreadMessages_++;
atraczyka535ee12016-08-31 17:36:05 -0400100}
101
102void
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -0400103Contact::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 Jagerbff5fbb2016-08-18 08:58:56 -0400111 }));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400112}
113
114JsonObject^
115Contact::ToJsonObject()
116{
117 JsonObject^ contactObject = ref new JsonObject();
118 contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
119 contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
atraczykf5be5462016-08-31 14:23:06 -0400120 contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_));
atraczykcac45522016-08-31 18:42:36 -0400121 contactObject->SetNamedValue(unreadMessagesKey, JsonValue::CreateNumberValue(unreadMessages_));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400122
123 JsonObject^ jsonObject = ref new JsonObject();
124 jsonObject->SetNamedValue(contactKey, contactObject);
125
126 return jsonObject;
127}
atraczykf5be5462016-08-31 14:23:06 -0400128
129String^
130Contact::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
144void
145Contact::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
168void
169Contact::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}