Contact.cpp 7.05 KB
Newer Older
Nicolas Jager's avatar
Nicolas Jager committed
1
2
3
/**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux                                *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com>              *
4
* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com>          *
Nicolas Jager's avatar
Nicolas Jager committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*                                                                         *
* This program is free software; you can redistribute it and/or modify    *
* it under the terms of the GNU General Public License as published by    *
* the Free Software Foundation; either version 3 of the License, or       *
* (at your option) any later version.                                     *
*                                                                         *
* This program is distributed in the hope that it will be useful,         *
* but WITHOUT ANY WARRANTY; without even the implied warranty of          *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
* GNU General Public License for more details.                            *
*                                                                         *
* You should have received a copy of the GNU General Public License       *
* along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
**************************************************************************/
#include "pch.h"

#include "Contact.h"

23
#include <ObjBase.h> // for CoCreateGuid
24

25
#include "fileutils.h"
Nicolas Jager's avatar
Nicolas Jager committed
26
#include "direct.h"
27

Nicolas Jager's avatar
Nicolas Jager committed
28
29
using namespace Windows::ApplicationModel::Core;
using namespace Platform;
30
using namespace Windows::Data::Json;
Nicolas Jager's avatar
Nicolas Jager committed
31
32
33
using namespace Windows::UI::Core;

using namespace RingClientUWP;
34
using namespace ViewModel;
Nicolas Jager's avatar
Nicolas Jager committed
35

36
37
38
39
40
41
Contact::Contact(   String^ accountId,
                    String^ name,
                    String^ ringID,
                    String^ GUID,
                    unsigned int unreadmessages,
                    ContactStatus contactStatus)
Nicolas Jager's avatar
Nicolas Jager committed
42
{
43
    vCard_ = ref new VCardUtils::VCard(this, accountId);
atraczyk's avatar
atraczyk committed
44

45
    name_   = name;
Nicolas Jager's avatar
Nicolas Jager committed
46
    ringID_ = ringID;
47
48
49
50
51
    GUID_   = GUID;

    if (GUID_ == nullptr)
        GUID_ = Utils::GetNewGUID();

52
    conversation_ = ref new Conversation();
53
54

    // load conversation from disk
55
    StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
56
57
58
59
    String^ messagesFile = localfolder->Path + "\\" + ".messages\\" + GUID_ + ".json";

    String^ fileContents = Utils::toPlatformString(Utils::getStringFromFile(Utils::toString(messagesFile)));

60
    CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
61
62
63
64
    ref new DispatchedHandler([=]() {
        if (fileContents != nullptr)
            DestringifyConversation(fileContents);
    }));
65

66
    notificationNewMessage_ = Windows::UI::Xaml::Visibility::Collapsed;
67
    unreadMessages_ = unreadmessages;
68
69
70

    if(unreadMessages_) {
        notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
71
        NotifyPropertyChanged("unreadMessages");
72
    }
73

74
    _accountIdAssociated = accountId;
atraczyk's avatar
atraczyk committed
75
76
77
    _vcardUID = "";
    _avatarImage = ref new String(L"ms-appx:///Assets/TESTS/contactAvatar.png");
    _displayName = "";
Nicolas Jager's avatar
Nicolas Jager committed
78
79

    contactStatus_ = contactStatus;
80
    lastTime_ = "never called.";
81
82
}

Nicolas Jager's avatar
Nicolas Jager committed
83
84
85
86
87
void
Contact::NotifyPropertyChanged(String^ propertyName)
{
    CoreApplicationView^ view = CoreApplication::MainView;
    view->CoreWindow->Dispatcher->RunAsync(
88
        CoreDispatcherPriority::High,
Nicolas Jager's avatar
Nicolas Jager committed
89
90
91
92
        ref new DispatchedHandler([this, propertyName]()
    {
        PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
    }));
93
94
95
96
97
98
99
}

JsonObject^
Contact::ToJsonObject()
{
    JsonObject^ contactObject = ref new JsonObject();
    contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
atraczyk's avatar
atraczyk committed
100
    contactObject->SetNamedValue(displayNameKey, JsonValue::CreateStringValue(displayName_));
101
    contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
102
    contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_));
103
    contactObject->SetNamedValue(unreadMessagesKey, JsonValue::CreateNumberValue(unreadMessages_));
104
    contactObject->SetNamedValue(accountIdAssociatedKey, JsonValue::CreateStringValue(_accountIdAssociated));
atraczyk's avatar
atraczyk committed
105
    contactObject->SetNamedValue(vcardUIDKey, JsonValue::CreateStringValue(_vcardUID));
Nicolas Jager's avatar
Nicolas Jager committed
106
    contactObject->SetNamedValue(lastTimeKey, JsonValue::CreateStringValue(_lastTime));
107
108
109
110
111
112

    JsonObject^ jsonObject = ref new JsonObject();
    jsonObject->SetNamedValue(contactKey, contactObject);

    return jsonObject;
}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

String^
Contact::StringifyConversation()
{
    JsonArray^ jsonArray = ref new JsonArray();

    for (unsigned int i = 0; i < conversation_->_messages->Size; i++) {
        jsonArray->Append(conversation_->_messages->GetAt(i)->ToJsonObject());
    }

    JsonObject^ jsonObject = ref new JsonObject();
    jsonObject->SetNamedValue(conversationKey, jsonArray);

    return jsonObject->Stringify();
}

void
Contact::DestringifyConversation(String^ data)
{
    JsonObject^ jsonObject = JsonObject::Parse(data);
    String^     date;
    bool        fromContact;
    String^     payload;

    JsonArray^ messageList = jsonObject->GetNamedArray(conversationKey, ref new JsonArray());
    for (unsigned int i = 0; i < messageList->Size; i++) {
        IJsonValue^ message = messageList->GetAt(i);
        if (message->ValueType == JsonValueType::Object) {
            JsonObject^ jsonMessageObject = message->GetObject();
            JsonObject^ messageObject = jsonMessageObject->GetNamedObject(messageKey, nullptr);
            if (messageObject != nullptr) {
                date = messageObject->GetNamedString(dateKey, "");
                fromContact = messageObject->GetNamedBoolean(fromContactKey, "");
                payload = messageObject->GetNamedString(payloadKey, "");
            }
            conversation_->addMessage(date, fromContact, payload);
        }
    }
}

153
154
155
156
157
158
159
160
161
162
163
164
165
void RingClientUWP::Contact::deleteConversationFile()
{
    StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
    String^ messagesFile = ".messages\\" + GUID_ + ".json";

    // refacto : Utils::fileExists fails here if the file doesn't exist, code below should replace fileExist everywhere
    create_task(localfolder->TryGetItemAsync(messagesFile)).then([](IStorageItem^ storageFile) {
        if (storageFile)
            storageFile->DeleteAsync();
    });

}

166
167
168
169
void
Contact::saveConversationToFile()
{
    StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
170
171
    String^ messagesFile = localfolder->Path + "\\" + ".messages\\" + GUID_ + ".json";

Nicolas Jager's avatar
Nicolas Jager committed
172
    if (_mkdir(Utils::toString(localfolder->Path + "\\" + ".messages\\").c_str())) {
173
        std::ofstream file(Utils::toString(messagesFile).c_str());
atraczyk's avatar
atraczyk committed
174
        if (file.is_open()) {
175
176
177
            file << Utils::toString(StringifyConversation());
            file.close();
        }
178
    }
Nicolas Jager's avatar
Nicolas Jager committed
179
180
}

atraczyk's avatar
atraczyk committed
181
182
183
184
VCardUtils::VCard^
Contact::getVCard()
{
    return vCard_;
185
186
187
188
189
190
}

void
Contact::notifyPropertyChanged(String^ propertyName)
{
    NotifyPropertyChanged(propertyName);
atraczyk's avatar
atraczyk committed
191
}