blob: 2307c7839732ce8c5956b5782a3dd104f0693124 [file] [log] [blame]
atraczykb724d332016-08-30 15:25:59 -04001/***************************************************************************
2 * Copyright (C) 2016 by Savoir-faire Linux *
3 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
4 * Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
5 * *
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
20#include "pch.h"
21
22using namespace Windows::Data::Json;
23using namespace Windows::Storage;
24
25using namespace RingClientUWP;
26using namespace Platform;
27using namespace Configuration;
28
29void
30UserPreferences::save()
31{
32 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
33 String^ preferencesFile = "preferences.json";
34
35 try {
36 create_task(localfolder->CreateFileAsync(preferencesFile
37 ,Windows::Storage::CreationCollisionOption::ReplaceExisting))
38 .then([&](StorageFile^ newFile){
39 try {
40 FileIO::WriteTextAsync(newFile,Stringify());
41 }
42 catch (Exception^ e) {
43 RingDebug::instance->print("Exception while writing to preferences file");
44 }
45 });
46 }
47 catch (Exception^ e) {
48 RingDebug::instance->print("Exception while opening preferences file");
49 }
50}
51
52void
53UserPreferences::load()
54{
55 String^ preferencesFile = "preferences.json";
56
57 Utils::fileExists(ApplicationData::Current->LocalFolder,
58 preferencesFile)
59 .then([this,preferencesFile](bool contacts_file_exists)
60 {
61 if (contacts_file_exists) {
atraczykb724d332016-08-30 15:25:59 -040062 try {
63 create_task(ApplicationData::Current->LocalFolder->GetFileAsync(preferencesFile))
64 .then([this](StorageFile^ file)
65 {
atraczyk1ddcb5a2016-09-07 16:18:30 -040066 try {
67 create_task(FileIO::ReadTextAsync(file))
68 .then([this](String^ fileContents){
69 if (fileContents != nullptr) {
70 Destringify(fileContents);
71 // select account index after loading preferences
72 selectIndex(PREF_ACCOUNT_INDEX);
73 if (PREF_PROFILE_PHOTO)
74 loadProfileImage();
75 }
76 });
77 }
78 catch (Exception^ e) {
79 RingDebug::instance->print("Exception while reading preferences file");
80 }
atraczykb724d332016-08-30 15:25:59 -040081 });
82 }
83 catch (Exception^ e) {
84 RingDebug::instance->print("Exception while opening preferences file");
85 }
86 }
87 else {
88 selectIndex(0);
89 }
90 });
91}
92
93String^
94UserPreferences::Stringify()
95{
96 JsonObject^ preferencesObject = ref new JsonObject();
atraczyk2425ddd2016-09-01 13:16:22 -040097
98 preferencesObject->SetNamedValue("PREF_ACCOUNT_INDEX", JsonValue::CreateNumberValue( PREF_ACCOUNT_INDEX));
99 preferencesObject->SetNamedValue("PREF_PROFILE_PHOTO", JsonValue::CreateBooleanValue( PREF_PROFILE_PHOTO));
100
atraczykb724d332016-08-30 15:25:59 -0400101 return preferencesObject->Stringify();
102}
103
104void
105UserPreferences::Destringify(String^ data)
106{
107 JsonObject^ jsonObject = JsonObject::Parse(data);
atraczyk2425ddd2016-09-01 13:16:22 -0400108
109 PREF_ACCOUNT_INDEX = static_cast<int>(jsonObject->GetNamedNumber( "PREF_ACCOUNT_INDEX" ));
110 PREF_PROFILE_PHOTO = jsonObject->GetNamedBoolean( "PREF_PROFILE_PHOTO" );
111
atraczykb724d332016-08-30 15:25:59 -0400112 JsonArray^ preferencesList = jsonObject->GetNamedArray("Account.index", ref new JsonArray());
atraczyk49822a32016-08-26 17:18:44 -0400113}