Commit 49822a3d authored by atraczyk's avatar atraczyk Committed by Andreas Traczyk

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
This diff is collapsed.
#pragma once
/**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@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/>. *
**************************************************************************/
using namespace Concurrency;
using namespace Platform::Collections;
namespace RingClientUWP
{
delegate void NewContactSelected();
delegate void NoContactSelected();
namespace ViewModel {
public ref class ContactsViewModel sealed
{
internal:
/* singleton */
static property ContactsViewModel^ instance
{
ContactsViewModel^ get()
{
static ContactsViewModel^ instance_ = ref new ContactsViewModel();
return instance_;
}
}
/* functions */
Contact^ findContactByName(String^ name);
Contact^ addNewContact(String^ name, String^ ringId);
void saveContactsToFile();
void openContactsFromFile();
String^ Stringify();
void Destringify(String^ data);
/* properties */
property Contact^ selectedContact
{
Contact^ get()
{
return currentItem_;
}
void set(Contact^ value)
{
oldItem_ = currentItem_;
currentItem_ = value;
if (value)
newContactSelected();
else
noContactSelected();
}
}
property Vector<Contact^>^ contactsList
{
Vector<Contact^>^ get()
{
return contactsList_;
}
}
/* events */
event NewContactSelected^ newContactSelected;
event NoContactSelected^ noContactSelected;
private:
ContactsViewModel(); // singleton
Vector<Contact^>^ contactsList_;
Contact^ currentItem_;
Contact^ oldItem_;
};
}
}
/***************************************************************************
* 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/>. *
**************************************************************************/
#pragma once
using namespace Platform::Collections;
using namespace Concurrency;
namespace RingClientUWP
{
delegate void NewContactSelected();
delegate void NoContactSelected();
namespace ViewModel {
public ref class ContactsViewModel sealed
{
internal:
/* singleton */
static property ContactsViewModel^ instance
{
ContactsViewModel^ get()
{
static ContactsViewModel^ instance_ = ref new ContactsViewModel();
return instance_;
}
}
/* functions */
Contact^ findContactByName(String^ name);
Contact^ addNewContact(String^ name, String^ ringId);
void saveContactsToFile();
void openContactsFromFile();
String^ Stringify();
void Destringify(String^ data);
/* properties */
property Contact^ selectedContact
{
Contact^ get()
{
return currentItem_;
}
void set(Contact^ value)
{
oldItem_ = currentItem_;
currentItem_ = value;
if (value)
newContactSelected();
else
noContactSelected();
}
}
property Vector<Contact^>^ contactsList
{
Vector<Contact^>^ get()
{
return contactsList_;
}
}
/* events */
event NewContactSelected^ newContactSelected;
event NoContactSelected^ noContactSelected;
private:
ContactsViewModel(); // singleton
Vector<Contact^>^ contactsList_;
Contact^ currentItem_;
Contact^ oldItem_;
};
}
}
This diff is collapsed.
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@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
using namespace concurrency;
namespace RingClientUWP
{
/* delegate */
delegate void IncomingCall(String^ accountId, String^ callId, String^ from);
delegate void StateChange(String^ callId, String^ state, int code);
public ref class RingD sealed
{
public:
/* functions */
/* properties */
static property RingD^ instance
{
RingD^ get()
{
static RingD^ instance_ = ref new RingD();
return instance_;
}
}
property bool daemonRunning
{
bool get()
{
return daemonRunning_;
}
}
internal:
/* functions */
void startDaemon();
/* TODO : move members */
bool hasConfig;
std::string accountName;
/* events */
event IncomingCall^ incomingCall;
event StateChange^ stateChange;
private:
/* sub classes */
enum class Request { None };
ref class Task
{
public:
property Request request;
};
/* functions */
RingD(); // singleton
void dequeueTasks();
/* members */
std::string localFolder_;
bool daemonRunning_ = false;
std::queue<Task^> tasksList_;
};
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@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
using namespace concurrency;
namespace RingClientUWP
{
/* delegate */
delegate void IncomingCall(String^ accountId, String^ callId, String^ from);
delegate void StateChange(String^ callId, String^ state, int code);
public ref class RingD sealed
{
public:
/* functions */
/* properties */
static property RingD^ instance
{
RingD^ get()
{
static RingD^ instance_ = ref new RingD();
return instance_;
}
}
property bool daemonRunning
{
bool get()
{
return daemonRunning_;
}
}
internal:
/* functions */
void startDaemon();
void reloadAccountList();
/* TODO : move members */
bool hasConfig;
std::string accountName;
/* events */
event IncomingCall^ incomingCall;
event StateChange^ stateChange;
private:
/* sub classes */
enum class Request { None };
ref class Task
{
public:
property Request request;
};
/* functions */
RingD(); // singleton
void dequeueTasks();
/* members */
std::string localFolder_;
bool daemonRunning_ = false;
std::queue<Task^> tasksList_;
};
}
\ No newline at end of file
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@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/>. *
**************************************************************************/
* 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/>. *
**************************************************************************/
/* client */
#include "pch.h"
......
This diff is collapsed.
/***************************************************************************
* 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 @@
#include "RingD.h"
#include "RingDebug.h"
#include "Utils.h"
#include "UserPreferences.h"
......@@ -137,7 +137,7 @@
<DisableSpecificWarnings>4453;28204;4267;4129;4973;4018;</DisableSpecificWarnings>
<AdditionalIncludeDirectories>..\ring-daemon\MSVC;..\ring-daemon\src\media;..\ring-daemon\src;..\ring-daemon\src\dring;..\ring-daemon\contrib\include;..\ring-daemon\contrib\include\pjlib;..\ring-daemon\contrib\pjproject\third_party\speex\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32_NATIVE;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<WholeProgramOptimization>false</WholeProgramOptimization>
<WholeProgramOptimization>false</WholeProgramOptimization>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(SolutionDir)..\ring-daemon\contrib\lib\x64;$(SolutionDir)..\ring-daemon\MSVC\x64\ReleaseLib\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
......@@ -189,6 +189,7 @@
<ClInclude Include="SmartPanel.xaml.h">
<DependentUpon>SmartPanel.xaml</DependentUpon>
</ClInclude>
<ClInclude Include="UserPreferences.h" />
<ClInclude Include="Utils.h" />
<ClInclude Include="VideoPage.xaml.h">
<DependentUpon>VideoPage.xaml</DependentUpon>
......@@ -293,6 +294,7 @@
<ClCompile Include="SmartPanel.xaml.cpp">
<DependentUpon>SmartPanel.xaml</DependentUpon>
</ClCompile>
<ClCompile Include="UserPreferences.cpp" />
<ClCompile Include="VideoPage.xaml.cpp">
<DependentUpon>VideoPage.xaml</DependentUpon>
</ClCompile>
......
......@@ -73,10 +73,13 @@
</ClCompile>
<ClCompile Include="Conversation.cpp">
<Filter>Model</Filter>
</ClCompile>
</ClCompile>
<ClCompile Include="CallsViewModel.cpp">
<Filter>ModelViews</Filter>
</ClCompile>
<ClCompile Include="UserPreferences.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
......@@ -115,10 +118,13 @@
</ClInclude>
<ClInclude Include="CallsViewModel.h">
<Filter>ModelViews</Filter>
</ClInclude>
</ClInclude>
<ClInclude Include="Conversation.h">
<Filter>Model</Filter>
</ClInclude>
<ClInclude Include="UserPreferences.h">
<Filter>Common</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="Assets\LockScreenLogo.scale-200.png">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment