diff --git a/src/Makefile.am b/src/Makefile.am index 38108a1..90efb29 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/spek-audio.c b/src/spek-audio.c index 0c189d3..5012572 100644 --- a/src/spek-audio.c +++ b/src/spek-audio.c @@ -20,7 +20,7 @@ #include -#include "spek-platform.h" +#include "spek-platform.hh" #include "spek-audio.h" diff --git a/src/spek-platform.cc b/src/spek-platform.cc index 8e2e43f..cafd050 100644 --- a/src/spek-platform.cc +++ b/src/spek-platform.cc @@ -22,7 +22,20 @@ #include #include -#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)); -} diff --git a/src/spek-platform.h b/src/spek-platform.hh similarity index 79% rename from src/spek-platform.h rename to src/spek-platform.hh index eb0eb10..703b14f 100644 --- a/src/spek-platform.h +++ b/src/spek-platform.hh @@ -1,4 +1,4 @@ -/* spek-platform.h +/* spek-platform.hh * * Copyright (C) 2010-2012 Alexander Kojevnikov * @@ -16,20 +16,25 @@ * along with Spek. If not, see . */ -#ifndef SPEK_PLATFORM_H_ -#define SPEK_PLATFORM_H_ +#ifndef SPEK_PLATFORM_HH_ +#define SPEK_PLATFORM_HH_ #ifdef __cplusplus +#include + +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 diff --git a/src/spek-preferences.cc b/src/spek-preferences.cc new file mode 100644 index 0000000..cd9f0d5 --- /dev/null +++ b/src/spek-preferences.cc @@ -0,0 +1,81 @@ +/* spek-preferences.cc + * + * Copyright (C) 2011-2012 Alexander Kojevnikov + * + * 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 . + */ + +#include + +#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(); +} diff --git a/src/spek-preferences.hh b/src/spek-preferences.hh new file mode 100644 index 0000000..41c52f3 --- /dev/null +++ b/src/spek-preferences.hh @@ -0,0 +1,44 @@ +/* spek-preferences.hh + * + * Copyright (C) 2011-2012 Alexander Kojevnikov + * + * 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 . + */ + +#ifndef SPEK_PREFERENCES_HH_ +#define SPEK_PREFERENCES_HH_ + +#include + +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 diff --git a/src/spek-preferences.vala b/src/spek-preferences.vala deleted file mode 100644 index ad8e62a..0000000 --- a/src/spek-preferences.vala +++ /dev/null @@ -1,102 +0,0 @@ -/* spek-preferences.vala - * - * Copyright (C) 2011 Alexander Kojevnikov - * - * 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 . - */ - -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); - } - } - } -} \ No newline at end of file