Add a class for app preferences

This commit is contained in:
Alexander Kojevnikov 2011-03-02 20:17:20 +08:00
parent 0180b76f3c
commit 6b9ce12116
3 changed files with 74 additions and 28 deletions

View File

@ -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

66
src/spek-preferences.vala Normal file
View File

@ -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);
}
}
}
}

View File

@ -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;
}
}