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;
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 24 | using namespace Windows::UI::Core;
|
| 25 | using namespace Windows::ApplicationModel::Core;
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 26 |
|
| 27 | using namespace RingClientUWP;
|
| 28 | using namespace Platform;
|
| 29 | using namespace Configuration;
|
| 30 |
|
| 31 | void
|
| 32 | UserPreferences::save()
|
| 33 | {
|
| 34 | StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 35 | String^ preferencesFile = localfolder->Path + "\\" + "preferences.json";
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 36 |
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 37 | std::ofstream file(Utils::toString(preferencesFile).c_str());
|
| 38 | if (file.is_open())
|
| 39 | {
|
| 40 | file << Utils::toString(Stringify());
|
| 41 | file.close();
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | void
|
| 46 | UserPreferences::load()
|
| 47 | {
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 48 | StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
|
| 49 | String^ preferencesFile = localfolder->Path + "\\preferences.json";
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 50 |
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 51 | String^ fileContents = Utils::toPlatformString(Utils::getStringFromFile(Utils::toString(preferencesFile)));
|
| 52 |
|
Nicolas Jager | 9edbea3 | 2016-10-03 09:13:53 -0400 | [diff] [blame] | 53 | CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 54 | ref new DispatchedHandler([=]() {
|
| 55 | if (fileContents != nullptr) {
|
| 56 | Destringify(fileContents);
|
| 57 | selectIndex(PREF_ACCOUNT_INDEX);
|
| 58 | if (PREF_PROFILE_PHOTO)
|
| 59 | loadProfileImage();
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 60 | }
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 61 | else
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 62 | selectIndex(0);
|
atraczyk | 21e55dd | 2016-09-19 15:46:55 -0400 | [diff] [blame] | 63 | }));
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 64 | }
|
| 65 |
|
| 66 | String^
|
| 67 | UserPreferences::Stringify()
|
| 68 | {
|
| 69 | JsonObject^ preferencesObject = ref new JsonObject();
|
atraczyk | 2425ddd | 2016-09-01 13:16:22 -0400 | [diff] [blame] | 70 |
|
| 71 | preferencesObject->SetNamedValue("PREF_ACCOUNT_INDEX", JsonValue::CreateNumberValue( PREF_ACCOUNT_INDEX));
|
| 72 | preferencesObject->SetNamedValue("PREF_PROFILE_PHOTO", JsonValue::CreateBooleanValue( PREF_PROFILE_PHOTO));
|
| 73 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 74 | return preferencesObject->Stringify();
|
| 75 | }
|
| 76 |
|
| 77 | void
|
| 78 | UserPreferences::Destringify(String^ data)
|
| 79 | {
|
| 80 | JsonObject^ jsonObject = JsonObject::Parse(data);
|
atraczyk | 2425ddd | 2016-09-01 13:16:22 -0400 | [diff] [blame] | 81 |
|
| 82 | PREF_ACCOUNT_INDEX = static_cast<int>(jsonObject->GetNamedNumber( "PREF_ACCOUNT_INDEX" ));
|
| 83 | PREF_PROFILE_PHOTO = jsonObject->GetNamedBoolean( "PREF_PROFILE_PHOTO" );
|
| 84 |
|
atraczyk | b724d33 | 2016-08-30 15:25:59 -0400 | [diff] [blame] | 85 | JsonArray^ preferencesList = jsonObject->GetNamedArray("Account.index", ref new JsonArray());
|
atraczyk | 49822a3 | 2016-08-26 17:18:44 -0400 | [diff] [blame] | 86 | } |