interfaces.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // This file is part of the AppIndicator/KStatusNotifierItem GNOME Shell extension
  2. //
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. export let StatusNotifierItem = null;
  17. export let StatusNotifierWatcher = null;
  18. export let DBusMenu = null;
  19. // loads a xml file into an in-memory string
  20. function loadInterfaceXml(extension, filename) {
  21. const interfacesDir = extension.dir.get_child('interfaces-xml');
  22. const file = interfacesDir.get_child(filename);
  23. const [result, contents] = imports.gi.GLib.file_get_contents(file.get_path());
  24. if (result) {
  25. // HACK: The "" + trick is important as hell because file_get_contents returns
  26. // an object (WTF?) but Gio.makeProxyWrapper requires `typeof() === "string"`
  27. // Otherwise, it will try to check `instanceof XML` and fail miserably because there
  28. // is no `XML` on very recent SpiderMonkey releases (or, if SpiderMonkey is old enough,
  29. // will spit out a TypeError soon).
  30. let nodeContents = contents;
  31. if (contents instanceof Uint8Array)
  32. nodeContents = imports.byteArray.toString(contents);
  33. return `<node>${nodeContents}</node>`;
  34. } else {
  35. throw new Error(`AppIndicatorSupport: Could not load file: ${filename}`);
  36. }
  37. }
  38. export function initialize(extension) {
  39. StatusNotifierItem = loadInterfaceXml(extension, 'StatusNotifierItem.xml');
  40. StatusNotifierWatcher = loadInterfaceXml(extension, 'StatusNotifierWatcher.xml');
  41. DBusMenu = loadInterfaceXml(extension, 'DBusMenu.xml');
  42. }
  43. export function destroy() {
  44. StatusNotifierItem = null;
  45. StatusNotifierWatcher = null;
  46. DBusMenu = null;
  47. }