commit 885e3c5a862ca5ab1660f0355eb844b999b7adfd
parent 0c08e68c5fe503590b624eccb5d9d3cc93fb3290
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date: Tue, 7 Aug 2012 09:41:52 -0700
Add spek-preferences.cc
Diffstat:
8 files changed, 184 insertions(+), 155 deletions(-)
diff --git 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
@@ -20,7 +20,7 @@
#include <libavutil/mathematics.h>
-#include "spek-platform.h"
+#include "spek-platform.hh"
#include "spek-audio.h"
diff --git a/src/spek-platform.cc b/src/spek-platform.cc
@@ -22,18 +22,9 @@
#include <wx/stdpaths.h>
#include <wx/utils.h>
-#include "spek-platform.h"
+#include "spek-platform.hh"
-char * spek_platform_short_path(const char *path)
-{
-#ifdef OS_WIN
- wxFileName file_name(wxString(path, wxConvUTF8));
- return strdup(file_name.GetShortPath().char_str(wxConvFile));
-#endif
- return NULL;
-}
-
-char * spek_platform_config_dir(const char *app_name)
+wxString SpekPlatform::ConfigPath(const wxString& app_name)
{
#ifdef OS_WIN
wxFileName file_name(wxStandardPaths::Get().GetUserConfigDir());
@@ -41,7 +32,16 @@ char * spek_platform_config_dir(const char *app_name)
wxFileName file_name(wxGetHomeDir());
file_name.AppendDir(wxT(".config"));
#endif
- file_name.AppendDir(wxString(app_name, wxConvUTF8));
+ file_name.AppendDir(app_name);
file_name.SetFullName(wxT("preferences"));
- return strdup(file_name.GetFullPath().char_str(wxConvUTF8));
+ return file_name.GetFullPath();
+}
+
+char * spek_platform_short_path(const char *path)
+{
+#ifdef OS_WIN
+ wxFileName file_name(wxString(path, wxConvUTF8));
+ return strdup(file_name.GetShortPath().char_str(wxConvFile));
+#endif
+ return NULL;
}
diff --git a/src/spek-platform.h b/src/spek-platform.h
@@ -1,37 +0,0 @@
-/* spek-platform.h
- *
- * Copyright (C) 2010-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_PLATFORM_H_
-#define SPEK_PLATFORM_H_
-
-#ifdef __cplusplus
-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
-
-#endif
diff --git a/src/spek-platform.hh b/src/spek-platform.hh
@@ -0,0 +1,42 @@
+/* spek-platform.hh
+ *
+ * Copyright (C) 2010-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_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);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/spek-preferences.cc b/src/spek-preferences.cc
@@ -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();
+}
diff --git a/src/spek-preferences.hh b/src/spek-preferences.hh
@@ -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
diff --git a/src/spek-preferences.vala b/src/spek-preferences.vala
@@ -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);
- }
- }
- }
-}
-\ No newline at end of file