Tidying everything up.

This commit is contained in:
Thomas Liebetraut 2013-05-22 02:33:09 +02:00
parent 17d7c024d2
commit 2b2c3f40be
3 changed files with 95 additions and 105 deletions

View File

@ -25,39 +25,7 @@ const N_ = function(e) { return e; };
const ExtensionUtils = imports.misc.extensionUtils; const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension(); const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
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({ const TeaTime = new Lang.Class({
@ -67,7 +35,7 @@ const TeaTime = new Lang.Class({
_init : function() { _init : function() {
this.parent(0.0, "TeaTime"); this.parent(0.0, "TeaTime");
this._settings = getSettings(); this._settings = Utils.getSettings();
this._logo = new St.Icon({ this._logo = new St.Icon({
icon_name : 'utilities-teatime', icon_name : 'utilities-teatime',
@ -75,7 +43,6 @@ const TeaTime = new Lang.Class({
}); });
// set timer widget // set timer widget
this._timer = new St.DrawingArea({ this._timer = new St.DrawingArea({
reactive : true reactive : true
}); });
@ -92,31 +59,20 @@ const TeaTime = new Lang.Class({
}, },
_createMenu : function() { _createMenu : function() {
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._settings.connect("changed::" + TEALIST_KEY, Lang.bind(this, this._updateTeaList)); this._settings.connect("changed::" + Utils.TEATIME_STEEP_TIMES_KEY,
Lang.bind(this, this._updateTeaList));
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
a.setTime(a.getTime()+ seconds * 1000); // set time in msec, adding the time we want
if (seconds > 3600)
return a.toLocaleFormat("%H:%M:%S");
else
return a.toLocaleFormat("%M:%S");
},
_updateTeaList : function(config, output) { _updateTeaList : function(config, output) {
// make sure the menu is empty // make sure the menu is empty
this.menu.removeAll(); this.menu.removeAll();
// fill with new teas // fill with new teas
let list = this._settings.get_value(TEALIST_KEY); let list = this._settings.get_value(Utils.TEATIME_STEEP_TIMES_KEY).unpack();
for (let i = 0; i < list.n_children(); ++i) { for (let teaname in list) {
let item = list.get_child_value(i); let time = list[teaname].get_uint32();
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)); let menuItem = new PopupMenu.PopupMenuItem(teaname + ": " + Utils.formatTime(time));
menuItem.connect('activate', Lang.bind(this, function() { menuItem.connect('activate', Lang.bind(this, function() {
this._initCountdown(time); this._initCountdown(time);
})); }));

View File

@ -21,40 +21,13 @@ const N_ = function(e) { return e; };
const ExtensionUtils = imports.misc.extensionUtils; const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension(); const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
let settings;
const SETTINGS_TEALIST_KEY = 'steep-times';
function getSettings(schema) { const Columns = {
let extension = ExtensionUtils.getCurrentExtension(); TEA_NAME: 0,
STEEP_TIME: 1,
schema = schema || extension.metadata['settings-schema']; ADJUSTMENT: 2
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 TeaTimePrefsWidget = new Lang.Class({ const TeaTimePrefsWidget = new Lang.Class({
@ -65,11 +38,16 @@ const TeaTimePrefsWidget = new Lang.Class({
this.parent({ orientation: Gtk.Orientation.VERTICAL }); this.parent({ orientation: Gtk.Orientation.VERTICAL });
this._tealist = new Gtk.ListStore(); this._tealist = new Gtk.ListStore();
this._tealist.set_column_types([GObject.TYPE_STRING, GObject.TYPE_INT, Gtk.Adjustment]); this._tealist.set_column_types([
GObject.TYPE_STRING,
GObject.TYPE_INT,
Gtk.Adjustment
]);
this._settings = getSettings(); this._settings = Utils.getSettings();
this._inhibitUpdate = true; this._inhibitUpdate = true;
this._settings.connect("changed::" + SETTINGS_TEALIST_KEY, Lang.bind(this, this._refresh)); this._settings.connect("changed::" + Utils.TEATIME_STEEP_TIMES_KEY,
Lang.bind(this, this._refresh));
this._initWindow(); this._initWindow();
this.vexpand = true; this.vexpand = true;
@ -92,10 +70,10 @@ const TeaTimePrefsWidget = new Lang.Class({
// when the UI does not know about the config storage backend. // when the UI does not know about the config storage backend.
renderer.connect("edited", Lang.bind(this, function(renderer, pathString, newValue) { renderer.connect("edited", Lang.bind(this, function(renderer, pathString, newValue) {
let [store, iter] = this._tealist.get_iter(Gtk.TreePath.new_from_string(pathString)); let [store, iter] = this._tealist.get_iter(Gtk.TreePath.new_from_string(pathString));
this._tealist.set(iter, [0], [newValue]); this._tealist.set(iter, [Columns.TEA_NAME], [newValue]);
})); }));
teaname.pack_start(renderer, true); teaname.pack_start(renderer, true);
teaname.add_attribute(renderer, "text", 0); teaname.add_attribute(renderer, "text", Columns.TEA_NAME);
this.treeview.append_column(teaname); this.treeview.append_column(teaname);
let steeptime = new Gtk.TreeViewColumn({ title: _("Steep time"), min_width: 150 }); let steeptime = new Gtk.TreeViewColumn({ title: _("Steep time"), min_width: 150 });
@ -103,12 +81,12 @@ const TeaTimePrefsWidget = new Lang.Class({
// See comment above. // See comment above.
spinrenderer.connect("edited", Lang.bind(this, function(renderer, pathString, newValue) { spinrenderer.connect("edited", Lang.bind(this, function(renderer, pathString, newValue) {
let [store, iter] = this._tealist.get_iter(Gtk.TreePath.new_from_string(pathString)); let [store, iter] = this._tealist.get_iter(Gtk.TreePath.new_from_string(pathString));
this._tealist.set(iter, [1], [parseInt(newValue)]); this._tealist.set(iter, [Columns.STEEP_TIME], [parseInt(newValue)]);
})); }));
steeptime.pack_start(spinrenderer, true); steeptime.pack_start(spinrenderer, true);
steeptime.add_attribute(spinrenderer, "adjustment", 2); steeptime.add_attribute(spinrenderer, "adjustment", Columns.ADJUSTMENT);
steeptime.add_attribute(spinrenderer, "text", 1); steeptime.add_attribute(spinrenderer, "text", Columns.STEEP_TIME);
this.treeview.append_column(steeptime); this.treeview.append_column(steeptime);
@ -127,20 +105,20 @@ const TeaTimePrefsWidget = new Lang.Class({
if (this._inhibitUpdate) if (this._inhibitUpdate)
return; return;
let list = this._settings.get_value(SETTINGS_TEALIST_KEY); let list = this._settings.get_value(Utils.TEATIME_STEEP_TIMES_KEY).unpack();
// stop everyone from reacting to the changes we are about to produce // stop everyone from reacting to the changes we are about to produce
// in the model // in the model
this._inhibitUpdate = true; this._inhibitUpdate = true;
this._tealist.clear(); this._tealist.clear();
for (let i = 0; i < list.n_children(); ++i) { for (let teaname in list) {
let item = list.get_child_value(i); let time = list[teaname].get_uint32();
let teaname = item.get_child_value(0).get_string()[0];
let time = item.get_child_value(1).get_uint32();
let adj = new Gtk.Adjustment({ lower: 1, step_increment: 1, upper: 65535, value: time }); let adj = new Gtk.Adjustment({ lower: 1, step_increment: 1, upper: 65535, value: time });
this._tealist.set(this._tealist.append(), [0, 1, 2], [teaname, time, adj]); this._tealist.set(this._tealist.append(),
[Columns.TEA_NAME, Columns.STEEP_TIME, Columns.ADJUSTMENT],
[teaname, time, adj]);
} }
this._inhibitUpdate = false; this._inhibitUpdate = false;
@ -148,8 +126,12 @@ const TeaTimePrefsWidget = new Lang.Class({
_addTea: function() { _addTea: function() {
let adj = new Gtk.Adjustment({ lower: 1, step_increment: 1, upper: 65535, value: 1 }); let adj = new Gtk.Adjustment({ lower: 1, step_increment: 1, upper: 65535, value: 1 });
let item = this._tealist.append(); let item = this._tealist.append();
this._tealist.set(item, [0, 1, 2], ["", 1, adj]); this._tealist.set(item,
this.treeview.set_cursor(this._tealist.get_path(item), this.treeview.get_column(0), true); [Columns.TEA_NAME, Columns.STEEP_TIME, Columns.ADJUSTMENT],
["", 1, adj]);
this.treeview.set_cursor(this._tealist.get_path(item),
this.treeview.get_column(Columns.TEA_NAME),
true);
}, },
_removeSelectedTea: function() { _removeSelectedTea: function() {
let [selection, store] = this.treeview.get_selection().get_selected_rows(); let [selection, store] = this.treeview.get_selection().get_selected_rows();
@ -175,8 +157,8 @@ const TeaTimePrefsWidget = new Lang.Class({
let values = []; let values = [];
this._tealist.foreach(function(store, path, iter) { this._tealist.foreach(function(store, path, iter) {
values.push(GLib.Variant.new_dict_entry( values.push(GLib.Variant.new_dict_entry(
GLib.Variant.new_string(store.get_value(iter, 0)), GLib.Variant.new_string(store.get_value(iter, Columns.TEA_NAME)),
GLib.Variant.new_uint32(store.get_value(iter, 1)))) GLib.Variant.new_uint32(store.get_value(iter, Columns.STEEP_TIME))))
}); });
let settingsValue = GLib.Variant.new_array(GLib.VariantType.new("{su}"), values); let settingsValue = GLib.Variant.new_array(GLib.VariantType.new("{su}"), values);
@ -184,11 +166,10 @@ const TeaTimePrefsWidget = new Lang.Class({
// disable updating it here to avoid an infinite loop // disable updating it here to avoid an infinite loop
this._inhibitUpdate = true; this._inhibitUpdate = true;
this._settings.set_value(SETTINGS_TEALIST_KEY, settingsValue); this._settings.set_value(Utils.TEATIME_STEEP_TIMES_KEY, settingsValue);
this._inhibitUpdate = false; this._inhibitUpdate = false;
} }
}); });

53
utils.js Normal file
View File

@ -0,0 +1,53 @@
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
/* Olaf Leidinger <oleid@mescharet.de>
Thomas Liebetraut <thomas@tommie-lie.de>
*/
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const TEATIME_STEEP_TIMES_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 });
}
function formatTime(seconds) {
let a = new Date(0,0,0); // important: hour needs to be set to zero in _locale_ time
a.setTime(a.getTime()+ seconds * 1000); // set time in msec, adding the time we want
if (seconds > 3600)
return a.toLocaleFormat("%H:%M:%S");
else
return a.toLocaleFormat("%M:%S");
}