diff --git a/extension.js b/extension.js index 98a1c29..649bbdc 100644 --- a/extension.js +++ b/extension.js @@ -24,13 +24,39 @@ const N_ = function(e) { return e; }; const ExtensionUtils = imports.misc.extensionUtils; const Me = ExtensionUtils.getCurrentExtension(); -// TODO: make this configurable via gsettings -const defaultTeaList = [ - {name : "Green tea", time : 180 }, - {name : "Black tea", time : 210 }, - {name : "Fruit tea", time : 7 * 60}, - {name : "White tea", time : 120} - ]; +const TEALIST_KEY = "steep-times" + + +function getSettings(schema) { + let extension = ExtensionUtils.getCurrentExtension(); + + 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({ Name : 'TeaTime', @@ -39,6 +65,8 @@ const TeaTime = new Lang.Class({ _init : function() { this.parent(0.0, "TeaTime"); + this._settings = getSettings(); + this._logo = new St.Icon({ icon_name : 'utilities-teatime', style_class : 'system-status-icon' @@ -60,10 +88,10 @@ const TeaTime = new Lang.Class({ this._createMenu(); }, - _createMenu : function() { this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); - this._addTeaList(); + this._settings.connect("changed::" + TEALIST_KEY, Lang.bind(this, this._updateTeaList)); + this._updateTeaList(); }, _formatTime : function(seconds) { 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 return a.toLocaleFormat("%M:%S"); }, - _addTeaList : function(config, output) { - let item = new PopupMenu.PopupMenuItem(_("brewing times")); - item.label.add_style_class_name('display-subtitle'); - item.actor.reactive = false; - item.actor.can_focus = false; - this.menu.addMenuItem(item); - this._callbacks = []; - - defaultTeaList.sort(function(a, b) { - return -1 * (a.time < b.time) + (a.time > b.time); - }); + _updateTeaList : function(config, output) { + // make sure the menu is empty + this.menu.removeAll(); - for ( var i = 0; i < defaultTeaList.length; i++) { - let tea = defaultTeaList[i]; - let item = new PopupMenu.PopupMenuItem(this._formatTime(tea.time) + " - " + tea.name); - - this._callbacks.push( function() {this._initCountdown(tea.time); }); - item.connect('activate', Lang.bind(this, this._callbacks[i])); - this.menu.addMenuItem(item); + // fill with new teas + 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(); + + let menuItem = new PopupMenu.PopupMenuItem(teaname + ": " + this._formatTime(time)); + menuItem.connect('activate', Lang.bind(this, function() { + this._initCountdown(time); + })); + this.menu.addMenuItem(menuItem); } }, _showNotification : function(subject, text) { diff --git a/metadata.json b/metadata.json index fae136d..d55c672 100644 --- a/metadata.json +++ b/metadata.json @@ -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"} diff --git a/schemas/org.gnome.shell.extensions.teatime.gschema.xml b/schemas/org.gnome.shell.extensions.teatime.gschema.xml new file mode 100644 index 0000000..e4d1df2 --- /dev/null +++ b/schemas/org.gnome.shell.extensions.teatime.gschema.xml @@ -0,0 +1,12 @@ + + + + + { "Green tea": 180, "Black tea": 210, "Fruit tea": 420, "White tea": 120 } + Tea drawing times list + A mapping of a tea times to their corresponding drawing time in seconds. + + + + +