convenience.js 3.8 KB

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