Contact.cpp 6.28 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 24
#include "ObjBase.h" // for CoCreateGuid

25 26
#include "fileutils.h"

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

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

Contact::Contact(String^ name,
36
                 String^ ringID,
37 38
                 String^ GUID,
                 unsigned int unreadmessages)
Nicolas Jager's avatar
Nicolas Jager committed
39
{
40
    name_   = name;
Nicolas Jager's avatar
Nicolas Jager committed
41
    ringID_ = ringID;
42 43 44 45 46
    GUID_   = GUID;

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

47
    conversation_ = ref new Conversation();
48 49

    // load conversation from disk
50
    StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
51 52 53 54
    String^ messagesFile = localfolder->Path + "\\" + ".messages\\" + GUID_ + ".json";

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

55
    CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
56 57 58 59
    ref new DispatchedHandler([=]() {
        if (fileContents != nullptr)
            DestringifyConversation(fileContents);
    }));
60

61
    notificationNewMessage_ = Windows::UI::Xaml::Visibility::Collapsed;
62
    unreadMessages_ = unreadmessages;
63 64 65

    if(unreadMessages_) {
        notificationNewMessage = Windows::UI::Xaml::Visibility::Visible;
66
        NotifyPropertyChanged("unreadMessages");
67
    }
68 69

    _accountIdAssociated = "";
70 71
}

Nicolas Jager's avatar
Nicolas Jager committed
72 73 74 75 76
void
Contact::NotifyPropertyChanged(String^ propertyName)
{
    CoreApplicationView^ view = CoreApplication::MainView;
    view->CoreWindow->Dispatcher->RunAsync(
77
        CoreDispatcherPriority::High,
Nicolas Jager's avatar
Nicolas Jager committed
78 79 80 81
        ref new DispatchedHandler([this, propertyName]()
    {
        PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
    }));
82 83 84 85 86 87 88 89
}

JsonObject^
Contact::ToJsonObject()
{
    JsonObject^ contactObject = ref new JsonObject();
    contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_));
    contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_));
90
    contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_));
91
    contactObject->SetNamedValue(unreadMessagesKey, JsonValue::CreateNumberValue(unreadMessages_));
92
    contactObject->SetNamedValue(accountIdAssociatedKey, JsonValue::CreateStringValue(_accountIdAssociated));
93 94 95 96 97 98

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

    return jsonObject;
}
99 100 101 102 103 104 105 106 107 108 109 110 111 112 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

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);
        }
    }
}

139 140 141 142 143 144 145 146 147 148 149 150 151
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();
    });

}

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

    if (ring::fileutils::recursive_mkdir(Utils::toString(localfolder->Path + "\\" + ".messages\\").c_str())) {
        std::ofstream file(Utils::toString(messagesFile).c_str());
        if (file.is_open())
        {
            file << Utils::toString(StringifyConversation());
            file.close();
        }
165
    }
Nicolas Jager's avatar
Nicolas Jager committed
166 167 168 169 170
}