atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 1 | /***************************************************************************
|
| 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 |
|
| 22 | using namespace Windows::Data::Json;
|
| 23 | using namespace Windows::Storage;
|
| 24 |
|
| 25 | using namespace RingClientUWP;
|
| 26 | using namespace Platform;
|
| 27 | using namespace Configuration;
|
| 28 |
|
| 29 | void
|
| 30 | UserPreferences::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 |
|
| 52 | void
|
| 53 | UserPreferences::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) {
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 62 | try {
|
| 63 | create_task(ApplicationData::Current->LocalFolder->GetFileAsync(preferencesFile))
|
| 64 | .then([this](StorageFile^ file)
|
| 65 | {
|
atraczyk | 1ddcb5a | 2016-09-07 16:18:30 -0400 | [diff] [blame] | 66 | 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 | }
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 81 | });
|
| 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 |
|
| 93 | String^
|
| 94 | UserPreferences::Stringify()
|
| 95 | {
|
| 96 | JsonObject^ preferencesObject = ref new JsonObject();
|
atraczyk | 2425ddd | 2016-09-01 13:16:22 -0400 | [diff] [blame] | 97 |
|
| 98 | preferencesObject->SetNamedValue("PREF_ACCOUNT_INDEX", JsonValue::CreateNumberValue( PREF_ACCOUNT_INDEX));
|
| 99 | preferencesObject->SetNamedValue("PREF_PROFILE_PHOTO", JsonValue::CreateBooleanValue( PREF_PROFILE_PHOTO));
|
| 100 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 101 | return preferencesObject->Stringify();
|
| 102 | }
|
| 103 |
|
| 104 | void
|
| 105 | UserPreferences::Destringify(String^ data)
|
| 106 | {
|
| 107 | JsonObject^ jsonObject = JsonObject::Parse(data);
|
atraczyk | 2425ddd | 2016-09-01 13:16:22 -0400 | [diff] [blame] | 108 |
|
| 109 | PREF_ACCOUNT_INDEX = static_cast<int>(jsonObject->GetNamedNumber( "PREF_ACCOUNT_INDEX" ));
|
| 110 | PREF_PROFILE_PHOTO = jsonObject->GetNamedBoolean( "PREF_PROFILE_PHOTO" );
|
| 111 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 112 | JsonArray^ preferencesList = jsonObject->GetNamedArray("Account.index", ref new JsonArray());
|
atraczyk | 49822a3 | 2016-08-26 17:18:44 -0400 | [diff] [blame] | 113 | } |