Added gsettings support for the tea drawing times.

This commit is contained in:
Thomas Liebetraut 2013-05-21 05:43:31 +02:00
parent a2c2ed1e54
commit 4af181c789
3 changed files with 65 additions and 28 deletions

View File

@ -24,13 +24,39 @@ const N_ = function(e) { return e; };
const ExtensionUtils = imports.misc.extensionUtils; const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension(); const Me = ExtensionUtils.getCurrentExtension();
// TODO: make this configurable via gsettings const TEALIST_KEY = "steep-times"
const defaultTeaList = [
{name : "Green tea", time : 180 },
{name : "Black tea", time : 210 }, function getSettings(schema) {
{name : "Fruit tea", time : 7 * 60}, let extension = ExtensionUtils.getCurrentExtension();
{name : "White tea", time : 120}
]; schema = schema || extension.metadata['settings-schema'];
const GioSSS = Gio.SettingsSchemaSource;
// check if this extension was built with "make zip-file", and thus
// has the schema files in a subfolder
// otherwise assume that extension has been installed in the
// same prefix as gnome-shell (and therefore schemas are available
// in the standard folders)
let schemaDir = extension.dir.get_child('schemas');
let schemaSource;
if (schemaDir.query_exists(null)) {
schemaSource = GioSSS.new_from_directory(schemaDir.get_path(),
GioSSS.get_default(),
false);
}
else
schemaSource = GioSSS.get_default();
let schemaObj = schemaSource.lookup(schema, true);
if (!schemaObj)
throw new Error('Schema ' + schema + ' could not be found for extension '
+ extension.metadata.uuid + '. Please check your installation.');
return new Gio.Settings({ settings_schema: schemaObj });
}
const TeaTime = new Lang.Class({ const TeaTime = new Lang.Class({
Name : 'TeaTime', Name : 'TeaTime',
@ -39,6 +65,8 @@ const TeaTime = new Lang.Class({
_init : function() { _init : function() {
this.parent(0.0, "TeaTime"); this.parent(0.0, "TeaTime");
this._settings = getSettings();
this._logo = new St.Icon({ this._logo = new St.Icon({
icon_name : 'utilities-teatime', icon_name : 'utilities-teatime',
style_class : 'system-status-icon' style_class : 'system-status-icon'
@ -60,10 +88,10 @@ const TeaTime = new Lang.Class({
this._createMenu(); this._createMenu();
}, },
_createMenu : function() { _createMenu : function() {
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._addTeaList(); this._settings.connect("changed::" + TEALIST_KEY, Lang.bind(this, this._updateTeaList));
this._updateTeaList();
}, },
_formatTime : function(seconds) { _formatTime : function(seconds) {
let a = new Date(0,0,0); // important: hour needs to be set to zero in _locale_ time let a = new Date(0,0,0); // important: hour needs to be set to zero in _locale_ time
@ -75,25 +103,22 @@ const TeaTime = new Lang.Class({
else else
return a.toLocaleFormat("%M:%S"); return a.toLocaleFormat("%M:%S");
}, },
_addTeaList : function(config, output) { _updateTeaList : function(config, output) {
let item = new PopupMenu.PopupMenuItem(_("brewing times")); // make sure the menu is empty
item.label.add_style_class_name('display-subtitle'); this.menu.removeAll();
item.actor.reactive = false;
item.actor.can_focus = false;
this.menu.addMenuItem(item);
this._callbacks = [];
defaultTeaList.sort(function(a, b) { // fill with new teas
return -1 * (a.time < b.time) + (a.time > b.time); let list = this._settings.get_value(TEALIST_KEY);
}); for (let i = 0; i < list.n_children(); ++i) {
let item = list.get_child_value(i);
let teaname = item.get_child_value(0).get_string()[0];
let time = item.get_child_value(1).get_uint32();
for ( var i = 0; i < defaultTeaList.length; i++) { let menuItem = new PopupMenu.PopupMenuItem(teaname + ": " + this._formatTime(time));
let tea = defaultTeaList[i]; menuItem.connect('activate', Lang.bind(this, function() {
let item = new PopupMenu.PopupMenuItem(this._formatTime(tea.time) + " - " + tea.name); this._initCountdown(time);
}));
this._callbacks.push( function() {this._initCountdown(tea.time); }); this.menu.addMenuItem(menuItem);
item.connect('activate', Lang.bind(this, this._callbacks[i]));
this.menu.addMenuItem(item);
} }
}, },
_showNotification : function(subject, text) { _showNotification : function(subject, text) {

View File

@ -1 +1 @@
{"shell-version": ["3.6"], "uuid": "TeaTime@oleid.mescharet.de", "name": "TeaTime", "description": "A tea brewing timer"} {"shell-version": ["3.6"], "uuid": "TeaTime@oleid.mescharet.de", "name": "TeaTime", "settings-schema": "org.gnome.shell.extensions.teatime", "description": "A tea brewing timer"}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema id="org.gnome.shell.extensions.teatime" path="/org/gnome/shell/extensions/teatime/">
<key name="steep-times" type="a{su}">
<default>{ "Green tea": 180, "Black tea": 210, "Fruit tea": 420, "White tea": 120 }</default>
<summary>Tea drawing times list</summary>
<description>A mapping of a tea times to their corresponding drawing time in seconds.</description>
</key>
</schema>
</schemalist>