1
/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */
4
* Part of this file comes from gnome-shell-extensions:
5
* http://git.gnome.org/browse/gnome-shell-extensions/
10
const Gettext = imports.gettext;
11
const Gio = imports.gi.Gio;
12
const Lang = imports.lang;
14
const Config = imports.misc.config;
15
const ExtensionUtils = imports.misc.extensionUtils;
20
* @domain: (optional): the gettext domain to use
22
* Initialize Gettext to load translations from extensionsdir/locale.
23
* If @domain is not provided, it will be taken from metadata['gettext-domain']
25
function initTranslations(domain) {
26
let extension = ExtensionUtils.getCurrentExtension();
28
domain = domain || extension.metadata['gettext-domain'];
30
// check if this extension was built with "make zip-file", and thus
31
// has the locale files in a subfolder
32
// otherwise assume that extension has been installed in the
33
// same prefix as gnome-shell
34
let localeDir = extension.dir.get_child('locale');
35
if (localeDir.query_exists(null))
36
Gettext.bindtextdomain(domain, localeDir.get_path());
38
Gettext.bindtextdomain(domain, Config.LOCALEDIR);
43
* @schema: (optional): the GSettings schema id
45
* Builds and return a GSettings schema for @schema, using schema files
46
* in extensionsdir/schemas. If @schema is not provided, it is taken from
47
* metadata['settings-schema'].
49
function getSettings(schema) {
50
let extension = ExtensionUtils.getCurrentExtension();
52
schema = schema || extension.metadata['settings-schema'];
54
const GioSSS = Gio.SettingsSchemaSource;
56
// check if this extension was built with "make zip-file", and thus
57
// has the schema files in a subfolder
58
// otherwise assume that extension has been installed in the
59
// same prefix as gnome-shell (and therefore schemas are available
60
// in the standard folders)
61
let schemaDir = extension.dir.get_child('schemas');
63
if (schemaDir.query_exists(null))
64
schemaSource = GioSSS.new_from_directory(schemaDir.get_path(),
68
schemaSource = GioSSS.get_default();
70
let schemaObj = schemaSource.lookup(schema, true);
72
throw new Error('Schema ' + schema + ' could not be found for extension '
73
+ extension.metadata.uuid + '. Please check your installation.');
75
return new Gio.Settings({ settings_schema: schemaObj });
78
// simplify global signals and function injections handling
80
const BasicHandler = new Lang.Class({
81
Name: 'dashToDock.BasicHandler',
84
this._storage = new Object();
87
add: function(/*unlimited 3-long array arguments*/){
89
// convert arguments object to array, concatenate with generic
90
let args = Array.concat('generic', Array.slice(arguments));
91
// call addWithLabel with ags as if they were passed arguments
92
this.addWithLabel.apply(this, args);
96
for( let label in this._storage )
97
this.removeWithLabel(label);
100
addWithLabel: function( label /* plus unlimited 3-long array arguments*/) {
102
if(this._storage[label] == undefined)
103
this._storage[label] = new Array();
105
// skip first element of the arguments
106
for( let i = 1; i < arguments.length; i++ ) {
107
this._storage[label].push( this._create(arguments[i]) );
112
removeWithLabel: function(label){
114
if(this._storage[label]) {
115
for( let i = 0; i < this._storage[label].length; i++ ) {
116
this._remove(this._storage[label][i]);
119
delete this._storage[label];
123
/* Virtual methods to be implemented by subclass */
124
// create single element to be stored in the storage structure
125
_create: function(item){
126
throw new Error('no implementation of _create in ' + this);
129
// correctly delete single element
130
_remove: function(item){
131
throw new Error('no implementation of _remove in ' + this);
135
// Manage global signals
136
const GlobalSignalsHandler = new Lang.Class({
137
Name: 'DashToDock.GlobalSignalHandler',
138
Extends: BasicHandler,
140
_create: function(item) {
142
let object = item[0];
144
let callback = item[2]
145
let id = object.connect(event, callback);
150
_remove: function(item){
151
item[0].disconnect(item[1]);
155
// Manage function injection: both instances and prototype can be overridden
157
const InjectionsHandler = new Lang.Class({
158
Name: 'DashToDock.InjectionsHandler',
159
Extends: BasicHandler,
161
_create: function(item) {
163
let object = item[0];
165
let injectedFunction = item[2];
166
let original = object[name];
168
object[name] = injectedFunction;
169
return [object, name, injectedFunction, original];
172
_remove: function(item) {
173
let object = item[0];
175
let original = item[3];
176
object[name] = original;