spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit 6b9ce121161a9e985be4fe293b5875444b04ff0a
parent 0180b76f3c7c040db9f88fcb7741c357d259b750
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Wed,  2 Mar 2011 20:17:20 +0800

Add a class for app preferences

Diffstat:
Msrc/Makefile.am | 1+
Asrc/spek-preferences.vala | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/spek-window.vala | 35+++++++----------------------------
3 files changed, 75 insertions(+), 28 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am @@ -7,6 +7,7 @@ spek_SOURCES = \ spek-message-bar.vala \ spek-pipeline.vala \ spek-platform.c \ + spek-preferences.vala \ spek-ruler.vala \ spek-spectrogram.vala \ spek-window.vala diff --git a/src/spek-preferences.vala b/src/spek-preferences.vala @@ -0,0 +1,66 @@ +/* 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; + public Preferences () { + file_name = Path.build_filename (Environment.get_user_config_dir (), "spek"); + DirUtils.create_with_parents (file_name, 0755); + file_name = Path.build_filename (file_name, "config.ini"); + this.key_file = new KeyFile (); + try { + key_file.load_from_file (file_name, KeyFileFlags.NONE); + } catch (KeyFileError e) { + } catch (FileError e) { + } + } + + ~Preferences () { + var output = FileStream.open (file_name, "w+"); + 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_update"); + } catch (KeyFileError e) { + } + return 0; + } + set { + key_file.set_integer ("update", "last_update", value); + } + } + } +} +\ No newline at end of file diff --git a/src/spek-window.vala b/src/spek-window.vala @@ -268,23 +268,9 @@ namespace Spek { }; private void * check_version () { - var config = Path.build_filename (Environment.get_user_config_dir (), "spek"); - DirUtils.create_with_parents (config, 0755); - config = Path.build_filename (config, "config.ini"); - var key_file = new KeyFile (); - try { - key_file.load_from_file (config, KeyFileFlags.NONE); - } catch (KeyFileError e) { - } catch (FileError e) { - } - // Does the user want to check for updates? - bool check = true; - try { - check = key_file.get_boolean ("update", "check"); - } catch (KeyFileError e) { - check = true; - } + var prefs = new Preferences (); + var check = prefs.check_update; if (!check) { return null; } @@ -294,12 +280,7 @@ namespace Spek { time_val.get_current_time (); Date today = Date (); today.set_time_val (time_val); - int day = 0; - try { - day = key_file.get_integer ("update", "last_update"); - } catch (KeyFileError e) { - day = 0; - } + int day = prefs.last_update; int diff = (int) today.get_julian () - day; if (diff < 7) { return null; @@ -311,15 +292,13 @@ namespace Spek { return null; } - // Write to the config file. - key_file.set_boolean ("update", "check", check); - key_file.set_integer ("update", "last_update", (int) today.get_julian ()); - var output = FileStream.open (config, "w+"); - output.puts (key_file.to_data ()); - if (version != null && version > Config.PACKAGE_VERSION) { Idle.add (() => { message_bar.show_all (); return false; }); } + + // Update the preferences. + prefs.check_update = check; + prefs.last_update = (int) today.get_julian (); return null; } }