Skip to content
Snippets Groups Projects
Commit 49822a3d authored by Andreas Traczyk's avatar Andreas Traczyk Committed by Andreas Traczyk
Browse files

preferences: use a json file to store preferences

- Add the storing and loading of a json file for
  storing the clients user preferences starting
  with the default selected account

Change-Id: If0afcf537731a951ef3be58881ec01ad8b14a347
Tuleap: #974
parent b059a66c
Branches
No related tags found
No related merge requests found
/*************************************************************************** /***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/ **************************************************************************/
#include "pch.h" #include "pch.h"
#include "ContactsViewModel.h" #include "ContactsViewModel.h"
...@@ -31,7 +33,7 @@ ContactsViewModel::ContactsViewModel() ...@@ -31,7 +33,7 @@ ContactsViewModel::ContactsViewModel()
} }
Contact^ Contact^
RingClientUWP::ViewModel::ContactsViewModel::findContactByName(String ^ name) ContactsViewModel::findContactByName(String ^ name)
{ {
for each (Contact^ contact in contactsList_) for each (Contact^ contact in contactsList_)
if (contact->name_ == name) if (contact->name_ == name)
...@@ -41,7 +43,7 @@ RingClientUWP::ViewModel::ContactsViewModel::findContactByName(String ^ name) ...@@ -41,7 +43,7 @@ RingClientUWP::ViewModel::ContactsViewModel::findContactByName(String ^ name)
} }
Contact^ Contact^
RingClientUWP::ViewModel::ContactsViewModel::addNewContact(String^ name, String^ ringId) ContactsViewModel::addNewContact(String^ name, String^ ringId)
{ {
if (contactsList_ && !findContactByName(name)) { if (contactsList_ && !findContactByName(name)) {
Contact^ contact = ref new Contact(name, ringId); Contact^ contact = ref new Contact(name, ringId);
......
#pragma once /***************************************************************************
/**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
...@@ -16,8 +16,11 @@ ...@@ -16,8 +16,11 @@
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/ **************************************************************************/
using namespace Concurrency;
#pragma once
using namespace Platform::Collections; using namespace Platform::Collections;
using namespace Concurrency;
namespace RingClientUWP namespace RingClientUWP
{ {
......
/*************************************************************************** /***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> * * Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
...@@ -43,7 +43,7 @@ debugOutputWrapper(const std::string& str) ...@@ -43,7 +43,7 @@ debugOutputWrapper(const std::string& str)
} }
void void
reloadAccountList() RingClientUWP::RingD::reloadAccountList()
{ {
RingClientUWP::ViewModel::AccountsViewModel::instance->clearAccountList(); RingClientUWP::ViewModel::AccountsViewModel::instance->clearAccountList();
std::vector<std::string> accountList = DRing::getAccountList(); std::vector<std::string> accountList = DRing::getAccountList();
...@@ -58,6 +58,8 @@ reloadAccountList() ...@@ -58,6 +58,8 @@ reloadAccountList()
ringID, //ringid ringID, //ringid
accountDetails.find(ring::Conf::CONFIG_ACCOUNT_TYPE)->second); //type accountDetails.find(ring::Conf::CONFIG_ACCOUNT_TYPE)->second); //type
} }
// load user preferences
Configuration::UserPreferences::instance->load();
} }
void void
...@@ -119,7 +121,10 @@ RingClientUWP::RingD::startDaemon() ...@@ -119,7 +121,10 @@ RingClientUWP::RingD::startDaemon()
}), }),
DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>([this]() DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>([this]()
{ {
CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
ref new DispatchedHandler([=]() {
reloadAccountList(); reloadAccountList();
}));
}) })
}; };
...@@ -155,10 +160,12 @@ RingClientUWP::RingD::startDaemon() ...@@ -155,10 +160,12 @@ RingClientUWP::RingD::startDaemon()
ringAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_TYPE,"RING")); ringAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_TYPE,"RING"));
DRing::addAccount(ringAccountDetails); DRing::addAccount(ringAccountDetails);
} }
else {
CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
ref new DispatchedHandler([=]() { ref new DispatchedHandler([=]() {
reloadAccountList(); reloadAccountList();
})); }));
}
while (true) { while (true) {
DRing::pollEvents(); DRing::pollEvents();
Sleep(1000); Sleep(1000);
......
/*************************************************************************** /***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
...@@ -52,6 +52,7 @@ public: ...@@ -52,6 +52,7 @@ public:
internal: internal:
/* functions */ /* functions */
void startDaemon(); void startDaemon();
void reloadAccountList();
/* TODO : move members */ /* TODO : move members */
bool hasConfig; bool hasConfig;
......
/*************************************************************************** /***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
......
/*************************************************************************** /***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux * * Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> * * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
...@@ -37,6 +38,10 @@ SmartPanel::SmartPanel() ...@@ -37,6 +38,10 @@ SmartPanel::SmartPanel()
{ {
InitializeComponent(); InitializeComponent();
Configuration::UserPreferences::instance->selectIndex += ref new SelectIndex([this](int index) {
_accountsList_->SelectedIndex = index;
});
_accountsList_->ItemsSource = AccountsViewModel::instance->accountsList; _accountsList_->ItemsSource = AccountsViewModel::instance->accountsList;
_smartList_->ItemsSource = ContactsViewModel::instance->contactsList; _smartList_->ItemsSource = ContactsViewModel::instance->contactsList;
} }
...@@ -48,6 +53,8 @@ RingClientUWP::Views::SmartPanel::updatePageContent() ...@@ -48,6 +53,8 @@ RingClientUWP::Views::SmartPanel::updatePageContent()
if (!account) if (!account)
return; return;
Configuration::UserPreferences::instance->PREF_ACCOUNT_INDEX = _accountsList_->SelectedIndex;
Configuration::UserPreferences::instance->save();
_selectedAccountName_->Text = account->name_; _selectedAccountName_->Text = account->name_;
} }
...@@ -108,22 +115,26 @@ void RingClientUWP::Views::SmartPanel::_shareMenuButton__Unchecked(Platform::Obj ...@@ -108,22 +115,26 @@ void RingClientUWP::Views::SmartPanel::_shareMenuButton__Unchecked(Platform::Obj
_shareMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed; _shareMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
} }
void RingClientUWP::Views::SmartPanel::_addAccountBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) void RingClientUWP::Views::SmartPanel::_addAccountBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{ {
_accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed; _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
_accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible; _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
} }
void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{ {
} }
void RingClientUWP::Views::SmartPanel::_createAccountNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) void RingClientUWP::Views::SmartPanel::_createAccountNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{ {
} }
void RingClientUWP::Views::SmartPanel::_avatarWebcamCaptureBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) void RingClientUWP::Views::SmartPanel::_avatarWebcamCaptureBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{ {
CameraCaptureUI^ cameraCaptureUI = ref new CameraCaptureUI(); CameraCaptureUI^ cameraCaptureUI = ref new CameraCaptureUI();
...@@ -153,6 +164,7 @@ void RingClientUWP::Views::SmartPanel::_avatarWebcamCaptureBtn__Click(Platform:: ...@@ -153,6 +164,7 @@ void RingClientUWP::Views::SmartPanel::_avatarWebcamCaptureBtn__Click(Platform::
} }
void void
SmartPanel::_smartList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e) SmartPanel::_smartList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{ {
......
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
* *
* 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"
using namespace Windows::Data::Json;
using namespace Windows::Storage;
using namespace RingClientUWP;
using namespace Platform;
using namespace Configuration;
void
UserPreferences::save()
{
StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
String^ preferencesFile = "preferences.json";
try {
create_task(localfolder->CreateFileAsync(preferencesFile
,Windows::Storage::CreationCollisionOption::ReplaceExisting))
.then([&](StorageFile^ newFile){
try {
FileIO::WriteTextAsync(newFile,Stringify());
}
catch (Exception^ e) {
RingDebug::instance->print("Exception while writing to preferences file");
}
});
}
catch (Exception^ e) {
RingDebug::instance->print("Exception while opening preferences file");
}
}
void
UserPreferences::load()
{
String^ preferencesFile = "preferences.json";
Utils::fileExists(ApplicationData::Current->LocalFolder,
preferencesFile)
.then([this,preferencesFile](bool contacts_file_exists)
{
if (contacts_file_exists) {
RingDebug::instance->print("opened preferences file");
try {
create_task(ApplicationData::Current->LocalFolder->GetFileAsync(preferencesFile))
.then([this](StorageFile^ file)
{
create_task(FileIO::ReadTextAsync(file))
.then([this](String^ fileContents){
RingDebug::instance->print("reading preferences file");
if (fileContents != nullptr) {
Destringify(fileContents);
// select account index after loading preferences
selectIndex(PREF_ACCOUNT_INDEX);
}
});
});
}
catch (Exception^ e) {
RingDebug::instance->print("Exception while opening preferences file");
}
}
else {
selectIndex(0);
}
});
}
String^
UserPreferences::Stringify()
{
JsonObject^ preferencesObject = ref new JsonObject();
preferencesObject->SetNamedValue("PREF_ACCOUNT_INDEX", JsonValue::CreateNumberValue(PREF_ACCOUNT_INDEX));
return preferencesObject->Stringify();
}
void
UserPreferences::Destringify(String^ data)
{
JsonObject^ jsonObject = JsonObject::Parse(data);
PREF_ACCOUNT_INDEX = static_cast<int>(jsonObject->GetNamedNumber("PREF_ACCOUNT_INDEX"));
JsonArray^ preferencesList = jsonObject->GetNamedArray("Account.index", ref new JsonArray());
}
\ No newline at end of file
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
* *
* 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/>. *
**************************************************************************/
#pragma once
namespace RingClientUWP
{
delegate void SelectIndex(int index);
namespace Configuration
{
/* delegates */
public ref class UserPreferences sealed
{
public:
/* singleton */
static property UserPreferences^ instance
{
UserPreferences^ get()
{
static UserPreferences^ instance_ = ref new UserPreferences();
return instance_;
}
}
/* properties */
property int PREF_ACCOUNT_INDEX;
/* functions */
void save();
void load();
String^ Stringify();
void Destringify(String^ data);
internal:
/* events */
event SelectIndex^ selectIndex;
private:
UserPreferences() { };
};
}
}
\ No newline at end of file
...@@ -35,3 +35,4 @@ ...@@ -35,3 +35,4 @@
#include "RingD.h" #include "RingD.h"
#include "RingDebug.h" #include "RingDebug.h"
#include "Utils.h" #include "Utils.h"
#include "UserPreferences.h"
...@@ -189,6 +189,7 @@ ...@@ -189,6 +189,7 @@
<ClInclude Include="SmartPanel.xaml.h"> <ClInclude Include="SmartPanel.xaml.h">
<DependentUpon>SmartPanel.xaml</DependentUpon> <DependentUpon>SmartPanel.xaml</DependentUpon>
</ClInclude> </ClInclude>
<ClInclude Include="UserPreferences.h" />
<ClInclude Include="Utils.h" /> <ClInclude Include="Utils.h" />
<ClInclude Include="VideoPage.xaml.h"> <ClInclude Include="VideoPage.xaml.h">
<DependentUpon>VideoPage.xaml</DependentUpon> <DependentUpon>VideoPage.xaml</DependentUpon>
...@@ -293,6 +294,7 @@ ...@@ -293,6 +294,7 @@
<ClCompile Include="SmartPanel.xaml.cpp"> <ClCompile Include="SmartPanel.xaml.cpp">
<DependentUpon>SmartPanel.xaml</DependentUpon> <DependentUpon>SmartPanel.xaml</DependentUpon>
</ClCompile> </ClCompile>
<ClCompile Include="UserPreferences.cpp" />
<ClCompile Include="VideoPage.xaml.cpp"> <ClCompile Include="VideoPage.xaml.cpp">
<DependentUpon>VideoPage.xaml</DependentUpon> <DependentUpon>VideoPage.xaml</DependentUpon>
</ClCompile> </ClCompile>
......
...@@ -77,6 +77,9 @@ ...@@ -77,6 +77,9 @@
<ClCompile Include="CallsViewModel.cpp"> <ClCompile Include="CallsViewModel.cpp">
<Filter>ModelViews</Filter> <Filter>ModelViews</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="UserPreferences.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="pch.h" /> <ClInclude Include="pch.h" />
...@@ -119,6 +122,9 @@ ...@@ -119,6 +122,9 @@
<ClInclude Include="Conversation.h"> <ClInclude Include="Conversation.h">
<Filter>Model</Filter> <Filter>Model</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="UserPreferences.h">
<Filter>Common</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Image Include="Assets\LockScreenLogo.scale-200.png"> <Image Include="Assets\LockScreenLogo.scale-200.png">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment