blob: fd463afff2ff5e05ea5d6d0e9231fcdea3a87481 [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
atraczyk21e55dd2016-09-19 15:46:55 -040025#include "fileutils.h"
26
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040027using namespace Windows::ApplicationModel::Core;
28using namespace Platform;
Nicolas Jager2e6ab412016-08-26 11:12:00 -040029using namespace Windows::Data::Json;
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040030using namespace Windows::UI::Core;
31
32using namespace RingClientUWP;
Nicolas Jager0788e962016-08-26 15:41:06 -040033using namespace ViewModel;
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040034
35Contact::Contact(String^ name,
atraczykf5be5462016-08-31 14:23:06 -040036 String^ ringID,
atraczykcac45522016-08-31 18:42:36 -040037 String^ GUID,
38 unsigned int unreadmessages)
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040039{
atraczykf5be5462016-08-31 14:23:06 -040040 name_ = name;
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040041 ringID_ = ringID;
atraczykf5be5462016-08-31 14:23:06 -040042 GUID_ = GUID;
43
44 if (GUID_ == nullptr)
45 GUID_ = Utils::GetNewGUID();
46
Nicolas Jager58c70b02016-08-26 09:50:45 -040047 conversation_ = ref new Conversation();
atraczyk21e55dd2016-09-19 15:46:55 -040048
49 // load conversation from disk
atraczykf5be5462016-08-31 14:23:06 -040050 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
atraczyk21e55dd2016-09-19 15:46:55 -040051 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 }));
atraczykf5be5462016-08-31 14:23:06 -040060
Nicolas Jager0788e962016-08-26 15:41:06 -040061 notificationNewMessage_ = Windows::UI::Xaml::Visibility::Collapsed;
Nicolas Jagerd76940f2016-08-31 14:44:04 -040062 unreadMessages_ = unreadmessages;
atraczykcac45522016-08-31 18:42:36 -040063
64 if(unreadMessages_) {
65 notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
66 PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
67 }
Nicolas Jager0788e962016-08-26 15:41:06 -040068
69 /* connect to delegate */
Nicolas Jager7bef1492016-09-14 11:23:29 -040070 ContactsViewModel::instance->notifyNewConversationMessage += ref new NotifyNewConversationMessage([&] (
71 bool isContactNotSelected) {
72 if (isContactNotSelected)
73 PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages"));
Nicolas Jager0788e962016-08-26 15:41:06 -040074 });
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;
atraczykcac45522016-08-31 18:42:36 -040080 ContactsViewModel::instance->saveContactsToFile();
Nicolas Jager0788e962016-08-26 15:41:06 -040081 }
82 });
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040083}
84
85void
atraczyka535ee12016-08-31 17:36:05 -040086Contact::addNotifyNewConversationMessage()
87{
88 notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
89 unreadMessages_++;
atraczyka535ee12016-08-31 17:36:05 -040090}
91
92void
Nicolas Jagerbff5fbb2016-08-18 08:58:56 -040093Contact::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 Jagerbff5fbb2016-08-18 08:58:56 -0400101 }));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400102}
103
104JsonObject^
105Contact::ToJsonObject()
106{
107 JsonObject^ contactObject = ref new JsonObject();
108 contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
109 contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
atraczykf5be5462016-08-31 14:23:06 -0400110 contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_));
atraczykcac45522016-08-31 18:42:36 -0400111 contactObject->SetNamedValue(unreadMessagesKey, JsonValue::CreateNumberValue(unreadMessages_));
Nicolas Jager2e6ab412016-08-26 11:12:00 -0400112
113 JsonObject^ jsonObject = ref new JsonObject();
114 jsonObject->SetNamedValue(contactKey, contactObject);
115
116 return jsonObject;
117}
atraczykf5be5462016-08-31 14:23:06 -0400118
119String^
120Contact::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
134void
135Contact::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
158void
159Contact::saveConversationToFile()
160{
161 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
atraczyk21e55dd2016-09-19 15:46:55 -0400162 String^ messagesFile = localfolder->Path + "\\" + ".messages\\" + GUID_ + ".json";
atraczykf5be5462016-08-31 14:23:06 -0400163
atraczyk21e55dd2016-09-19 15:46:55 -0400164 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 }
atraczykf5be5462016-08-31 14:23:06 -0400171 }
172}