Add spek-preferences.cc

This commit is contained in:
Alexander Kojevnikov 2012-08-07 09:41:52 -07:00
parent 0c08e68c5f
commit 885e3c5a86
7 changed files with 155 additions and 125 deletions

View File

@ -7,7 +7,9 @@ spek_SOURCES = \
spek-fft.c \
spek-fft.h \
spek-platform.cc \
spek-platform.h \
spek-platform.hh \
spek-preferences.cc \
spek-preferences.hh \
spek-window.cc \
spek-window.hh

View File

@ -20,7 +20,7 @@
#include <libavutil/mathematics.h>
#include "spek-platform.h"
#include "spek-platform.hh"
#include "spek-audio.h"

View File

@ -22,7 +22,20 @@
#include <wx/stdpaths.h>
#include <wx/utils.h>
#include "spek-platform.h"
#include "spek-platform.hh"
wxString SpekPlatform::ConfigPath(const wxString& app_name)
{
#ifdef OS_WIN
wxFileName file_name(wxStandardPaths::Get().GetUserConfigDir());
#else
wxFileName file_name(wxGetHomeDir());
file_name.AppendDir(wxT(".config"));
#endif
file_name.AppendDir(app_name);
file_name.SetFullName(wxT("preferences"));
return file_name.GetFullPath();
}
char * spek_platform_short_path(const char *path)
{
@ -32,16 +45,3 @@ char * spek_platform_short_path(const char *path)
#endif
return NULL;
}
char * spek_platform_config_dir(const char *app_name)
{
#ifdef OS_WIN
wxFileName file_name(wxStandardPaths::Get().GetUserConfigDir());
#else
wxFileName file_name(wxGetHomeDir());
file_name.AppendDir(wxT(".config"));
#endif
file_name.AppendDir(wxString(app_name, wxConvUTF8));
file_name.SetFullName(wxT("preferences"));
return strdup(file_name.GetFullPath().char_str(wxConvUTF8));
}

View File

@ -1,4 +1,4 @@
/* spek-platform.h
/* spek-platform.hh
*
* Copyright (C) 2010-2012 Alexander Kojevnikov <alexander@kojevnikov.com>
*
@ -16,20 +16,25 @@
* along with Spek. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPEK_PLATFORM_H_
#define SPEK_PLATFORM_H_
#ifndef SPEK_PLATFORM_HH_
#define SPEK_PLATFORM_HH_
#ifdef __cplusplus
#include <wx/string.h>
class SpekPlatform
{
public:
// Not quite XDG-compatible, but close enough.
static wxString ConfigPath(const wxString& app_name);
};
extern "C" {
#endif
// Returns a 8.3 version of the UTF8-encoded path on Windows and NULL on other platforms.
char * spek_platform_short_path(const char *path);
// Not quite XDG-compatible, but close enough.
// TODO: implement XDG spec in wxWidgets.
char * spek_platform_config_dir(const char *app_name);
#ifdef __cplusplus
}
#endif

81
src/spek-preferences.cc Normal file
View File

@ -0,0 +1,81 @@
/* spek-preferences.cc
*
* Copyright (C) 2011-2012 Alexander Kojevnikov <alexander@kojevnikov.com>
*
* Spek 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.
*
* Spek 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 Spek. If not, see <http://www.gnu.org/licenses/>.
*/
#include <wx/string.h>
#include "spek-platform.hh"
#include "spek-preferences.hh"
SpekPreferences& SpekPreferences::Get()
{
static SpekPreferences instance;
return instance;
}
SpekPreferences::SpekPreferences()
{
wxString path = SpekPlatform::ConfigPath(wxT("spek"));
this->config = new wxFileConfig(
wxEmptyString,
wxEmptyString,
path,
wxEmptyString,
wxCONFIG_USE_LOCAL_FILE,
wxConvUTF8
);
}
bool SpekPreferences::GetCheckUpdate()
{
bool result = true;
this->config->Read(wxT("/update/check"), &result);
return result;
}
void SpekPreferences::SetCheckUpdate(bool value)
{
this->config->Write(wxT("/update/check"), value);
this->config->Flush();
}
long SpekPreferences::GetLastUpdate()
{
long result = 0;
this->config->Read(wxT("/update/last"), &result);
return result;
}
void SpekPreferences::SetLastUpdate(long value)
{
this->config->Write(wxT("/update/last"), value);
this->config->Flush();
}
wxString SpekPreferences::GetLanguage()
{
wxString result(wxT(""));
this->config->Read(wxT("/general/language"), &result);
return result;
}
void SpekPreferences::SetLanguage(const wxString& value)
{
this->config->Write(wxT("/general/language"), value);
this->config->Flush();
}

44
src/spek-preferences.hh Normal file
View File

@ -0,0 +1,44 @@
/* spek-preferences.hh
*
* Copyright (C) 2011-2012 Alexander Kojevnikov <alexander@kojevnikov.com>
*
* Spek 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.
*
* Spek 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 Spek. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPEK_PREFERENCES_HH_
#define SPEK_PREFERENCES_HH_
#include <wx/fileconf.h>
class SpekPreferences
{
public:
static SpekPreferences& Get();
bool GetCheckUpdate();
void SetCheckUpdate(bool value);
long GetLastUpdate();
void SetLastUpdate(long value);
wxString GetLanguage();
void SetLanguage(const wxString& value);
private:
SpekPreferences();
SpekPreferences(const SpekPreferences&);
void operator=(const SpekPreferences&);
wxFileConfig *config;
};
#endif

View File

@ -1,102 +0,0 @@
/* spek-preferences.vala
*
* Copyright (C) 2011 Alexander Kojevnikov <alexander@kojevnikov.com>
*
* Spek 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.
*
* Spek 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 Spek. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Spek {
public class Preferences {
private KeyFile key_file;
private string file_name;
private bool can_save = true;
private Preferences () {
file_name = Path.build_filename (Environment.get_user_config_dir (), "spek");
if (DirUtils.create_with_parents (file_name, 0755) != 0) {
this.can_save = false;
}
file_name = Path.build_filename (file_name, "preferences");
this.key_file = new KeyFile ();
try {
key_file.load_from_file (file_name, KeyFileFlags.NONE);
} catch (KeyFileError e) {
} catch (FileError e) {
}
}
~Preferences () {
save ();
}
private static Preferences _instance;
public static Preferences instance {
get {
if (_instance == null) {
_instance = new Preferences ();
}
return _instance;
}
}
public void save () {
if (!can_save) {
return;
}
var output = FileStream.open (file_name, "w+");
if (output != null) {
output.puts (key_file.to_data ());
}
}
public bool check_update {
get {
try {
return key_file.get_boolean ("update", "check");
} catch (KeyFileError e) {
}
return true;
}
set {
key_file.set_boolean ("update", "check", value);
}
}
public int last_update {
get {
try {
return key_file.get_integer ("update", "last");
} catch (KeyFileError e) {
}
return 0;
}
set {
key_file.set_integer ("update", "last", value);
}
}
public string language {
owned get {
try {
return key_file.get_string ("general", "language");
} catch (KeyFileError e) {
}
return "";
}
set {
key_file.set_string ("general", "language", value);
}
}
}
}