~wasta-linux/wasta-core-wily/master

« back to all changes in this revision

Viewing changes to install-files/extensions/dash-to-dock@micxgx.gmail.com/convenience.js

  • Committer: Rik Shaw
  • Date: 2015-11-01 13:28:40 UTC
  • Revision ID: git-v1:59c62c9b2e4f4f1cf62db1f5dc1cf630feb99933
initial 15.10 commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */
 
2
 
 
3
/*
 
4
 * Part of this file comes from gnome-shell-extensions:
 
5
 * http://git.gnome.org/browse/gnome-shell-extensions/
 
6
 * 
 
7
 */
 
8
 
 
9
 
 
10
const Gettext = imports.gettext;
 
11
const Gio = imports.gi.Gio;
 
12
const Lang = imports.lang;
 
13
 
 
14
const Config = imports.misc.config;
 
15
const ExtensionUtils = imports.misc.extensionUtils;
 
16
 
 
17
 
 
18
/**
 
19
 * initTranslations:
 
20
 * @domain: (optional): the gettext domain to use
 
21
 *
 
22
 * Initialize Gettext to load translations from extensionsdir/locale.
 
23
 * If @domain is not provided, it will be taken from metadata['gettext-domain']
 
24
 */
 
25
function initTranslations(domain) {
 
26
    let extension = ExtensionUtils.getCurrentExtension();
 
27
 
 
28
    domain = domain || extension.metadata['gettext-domain'];
 
29
 
 
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());
 
37
    else
 
38
        Gettext.bindtextdomain(domain, Config.LOCALEDIR);
 
39
}
 
40
 
 
41
/**
 
42
 * getSettings:
 
43
 * @schema: (optional): the GSettings schema id
 
44
 *
 
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'].
 
48
 */
 
49
function getSettings(schema) {
 
50
    let extension = ExtensionUtils.getCurrentExtension();
 
51
 
 
52
    schema = schema || extension.metadata['settings-schema'];
 
53
 
 
54
    const GioSSS = Gio.SettingsSchemaSource;
 
55
 
 
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');
 
62
    let schemaSource;
 
63
    if (schemaDir.query_exists(null))
 
64
        schemaSource = GioSSS.new_from_directory(schemaDir.get_path(),
 
65
                                                 GioSSS.get_default(),
 
66
                                                 false);
 
67
    else
 
68
        schemaSource = GioSSS.get_default();
 
69
 
 
70
    let schemaObj = schemaSource.lookup(schema, true);
 
71
    if (!schemaObj)
 
72
        throw new Error('Schema ' + schema + ' could not be found for extension '
 
73
                        + extension.metadata.uuid + '. Please check your installation.');
 
74
 
 
75
    return new Gio.Settings({ settings_schema: schemaObj });
 
76
}
 
77
 
 
78
// simplify global signals and function injections handling
 
79
// abstract class
 
80
const BasicHandler = new Lang.Class({
 
81
    Name: 'dashToDock.BasicHandler',
 
82
 
 
83
    _init: function(){
 
84
        this._storage = new Object();
 
85
    },
 
86
 
 
87
    add: function(/*unlimited 3-long array arguments*/){
 
88
 
 
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);
 
93
    },
 
94
 
 
95
    destroy: function() {
 
96
        for( let label in this._storage )
 
97
            this.removeWithLabel(label);
 
98
    },
 
99
 
 
100
    addWithLabel: function( label /* plus unlimited 3-long array arguments*/) {
 
101
 
 
102
        if(this._storage[label] == undefined)
 
103
            this._storage[label] = new Array();
 
104
 
 
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]) );
 
108
        }
 
109
 
 
110
    },
 
111
 
 
112
    removeWithLabel: function(label){
 
113
 
 
114
        if(this._storage[label]) {
 
115
            for( let i = 0; i < this._storage[label].length; i++ ) {
 
116
                this._remove(this._storage[label][i]);
 
117
            }
 
118
 
 
119
            delete this._storage[label];
 
120
        }
 
121
    },
 
122
 
 
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);
 
127
    },
 
128
 
 
129
    // correctly delete single element
 
130
    _remove: function(item){
 
131
      throw new Error('no implementation of _remove in ' + this);
 
132
    }
 
133
});
 
134
 
 
135
// Manage global signals
 
136
const GlobalSignalsHandler = new Lang.Class({
 
137
    Name: 'DashToDock.GlobalSignalHandler',
 
138
    Extends: BasicHandler,
 
139
 
 
140
    _create: function(item) {
 
141
 
 
142
      let object = item[0];
 
143
      let event = item[1];
 
144
      let callback = item[2]
 
145
      let id = object.connect(event, callback);
 
146
 
 
147
      return [object, id];
 
148
    },
 
149
 
 
150
    _remove: function(item){
 
151
       item[0].disconnect(item[1]);
 
152
    }
 
153
});
 
154
 
 
155
// Manage function injection: both instances and prototype can be overridden
 
156
// and restored
 
157
const InjectionsHandler = new Lang.Class({
 
158
    Name: 'DashToDock.InjectionsHandler',
 
159
    Extends: BasicHandler,
 
160
 
 
161
    _create: function(item) {
 
162
 
 
163
      let object = item[0];
 
164
      let name = item[1];
 
165
      let injectedFunction = item[2];
 
166
      let original = object[name];
 
167
 
 
168
      object[name] = injectedFunction;
 
169
      return [object, name, injectedFunction, original];
 
170
    },
 
171
 
 
172
    _remove: function(item) {
 
173
        let object = item[0];
 
174
        let name = item[1];
 
175
        let original = item[3];
 
176
        object[name] = original;
 
177
    }
 
178
});