blob: 96d3afdd593ea1785b860c6e820fc71cd65b2cf5 [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;
atraczyk21e55dd2016-09-19 15:46:55 -040024using namespace Windows::UI::Core;
25using namespace Windows::ApplicationModel::Core;
atraczykb724d332016-08-30 15:25:59 -040026
27using namespace RingClientUWP;
28using namespace Platform;
29using namespace Configuration;
30
31void
32UserPreferences::save()
33{
34 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
atraczyk21e55dd2016-09-19 15:46:55 -040035 String^ preferencesFile = localfolder->Path + "\\" + "preferences.json";
atraczykb724d332016-08-30 15:25:59 -040036
atraczyk21e55dd2016-09-19 15:46:55 -040037 std::ofstream file(Utils::toString(preferencesFile).c_str());
38 if (file.is_open())
39 {
40 file << Utils::toString(Stringify());
41 file.close();
atraczykb724d332016-08-30 15:25:59 -040042 }
43}
44
45void
46UserPreferences::load()
47{
atraczyk21e55dd2016-09-19 15:46:55 -040048 StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
49 String^ preferencesFile = localfolder->Path + "\\preferences.json";
atraczykb724d332016-08-30 15:25:59 -040050
atraczyk21e55dd2016-09-19 15:46:55 -040051 String^ fileContents = Utils::toPlatformString(Utils::getStringFromFile(Utils::toString(preferencesFile)));
52
Nicolas Jager9edbea32016-10-03 09:13:53 -040053 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
atraczyk21e55dd2016-09-19 15:46:55 -040054 ref new DispatchedHandler([=]() {
55 if (fileContents != nullptr) {
56 Destringify(fileContents);
57 selectIndex(PREF_ACCOUNT_INDEX);
58 if (PREF_PROFILE_PHOTO)
59 loadProfileImage();
atraczykb724d332016-08-30 15:25:59 -040060 }
atraczyk21e55dd2016-09-19 15:46:55 -040061 else
atraczykb724d332016-08-30 15:25:59 -040062 selectIndex(0);
atraczyk21e55dd2016-09-19 15:46:55 -040063 }));
atraczykb724d332016-08-30 15:25:59 -040064}
65
66String^
67UserPreferences::Stringify()
68{
69 JsonObject^ preferencesObject = ref new JsonObject();
atraczyk2425ddd2016-09-01 13:16:22 -040070
71 preferencesObject->SetNamedValue("PREF_ACCOUNT_INDEX", JsonValue::CreateNumberValue( PREF_ACCOUNT_INDEX));
72 preferencesObject->SetNamedValue("PREF_PROFILE_PHOTO", JsonValue::CreateBooleanValue( PREF_PROFILE_PHOTO));
73
atraczykb724d332016-08-30 15:25:59 -040074 return preferencesObject->Stringify();
75}
76
77void
78UserPreferences::Destringify(String^ data)
79{
80 JsonObject^ jsonObject = JsonObject::Parse(data);
atraczyk2425ddd2016-09-01 13:16:22 -040081
82 PREF_ACCOUNT_INDEX = static_cast<int>(jsonObject->GetNamedNumber( "PREF_ACCOUNT_INDEX" ));
83 PREF_PROFILE_PHOTO = jsonObject->GetNamedBoolean( "PREF_PROFILE_PHOTO" );
84
atraczykb724d332016-08-30 15:25:59 -040085 JsonArray^ preferencesList = jsonObject->GetNamedArray("Account.index", ref new JsonArray());
atraczyk49822a32016-08-26 17:18:44 -040086}