|
| 1 | +/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + Copyright (c) 2011-2012, Giovanni Campagna <scampa.giovanni@gmail.com> |
| 4 | +
|
| 5 | + Redistribution and use in source and binary forms, with or without |
| 6 | + modification, are permitted provided that the following conditions are met: |
| 7 | + * Redistributions of source code must retain the above copyright |
| 8 | + notice, this list of conditions and the following disclaimer. |
| 9 | + * Redistributions in binary form must reproduce the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer in the |
| 11 | + documentation and/or other materials provided with the distribution. |
| 12 | + * Neither the name of the GNOME nor the |
| 13 | + names of its contributors may be used to endorse or promote products |
| 14 | + derived from this software without specific prior written permission. |
| 15 | +
|
| 16 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
| 20 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*/ |
| 27 | + |
| 28 | +const Gettext = imports.gettext; |
| 29 | +const Gio = imports.gi.Gio; |
| 30 | + |
| 31 | +const Config = imports.misc.config; |
| 32 | +const ExtensionUtils = imports.misc.extensionUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * initTranslations: |
| 36 | + * @domain: (optional): the gettext domain to use |
| 37 | + * |
| 38 | + * Initialize Gettext to load translations from extensionsdir/locale. |
| 39 | + * If @domain is not provided, it will be taken from metadata['gettext-domain'] |
| 40 | + */ |
| 41 | +function initTranslations(domain) { |
| 42 | + let extension = ExtensionUtils.getCurrentExtension(); |
| 43 | + |
| 44 | + domain = domain || extension.metadata['gettext-domain']; |
| 45 | + |
| 46 | + // check if this extension was built with "make zip-file", and thus |
| 47 | + // has the locale files in a subfolder |
| 48 | + // otherwise assume that extension has been installed in the |
| 49 | + // same prefix as gnome-shell |
| 50 | + let localeDir = extension.dir.get_child('locale'); |
| 51 | + if (localeDir.query_exists(null)) |
| 52 | + Gettext.bindtextdomain(domain, localeDir.get_path()); |
| 53 | + else |
| 54 | + Gettext.bindtextdomain(domain, Config.LOCALEDIR); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * getSettings: |
| 59 | + * @schema: (optional): the GSettings schema id |
| 60 | + * |
| 61 | + * Builds and return a GSettings schema for @schema, using schema files |
| 62 | + * in extensionsdir/schemas. If @schema is not provided, it is taken from |
| 63 | + * metadata['settings-schema']. |
| 64 | + */ |
| 65 | +function getSettings(schema) { |
| 66 | + let extension = ExtensionUtils.getCurrentExtension(); |
| 67 | + |
| 68 | + schema = schema || extension.metadata['settings-schema']; |
| 69 | + |
| 70 | + const GioSSS = Gio.SettingsSchemaSource; |
| 71 | + |
| 72 | + // check if this extension was built with "make zip-file", and thus |
| 73 | + // has the schema files in a subfolder |
| 74 | + // otherwise assume that extension has been installed in the |
| 75 | + // same prefix as gnome-shell (and therefore schemas are available |
| 76 | + // in the standard folders) |
| 77 | + let schemaDir = extension.dir.get_child('schemas'); |
| 78 | + let schemaSource; |
| 79 | + if (schemaDir.query_exists(null)) |
| 80 | + schemaSource = GioSSS.new_from_directory(schemaDir.get_path(), |
| 81 | + GioSSS.get_default(), |
| 82 | + false); |
| 83 | + else |
| 84 | + schemaSource = GioSSS.get_default(); |
| 85 | + |
| 86 | + let schemaObj = schemaSource.lookup(schema, true); |
| 87 | + if (!schemaObj) |
| 88 | + throw new Error('Schema ' + schema + ' could not be found for extension ' |
| 89 | + + extension.metadata.uuid + '. Please check your installation.'); |
| 90 | + |
| 91 | + return new Gio.Settings({ settings_schema: schemaObj }); |
| 92 | +} |
| 93 | + |
0 commit comments