extension.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. import * as Extension from 'resource:///org/gnome/shell/extensions/extension.js';
  17. import * as StatusNotifierWatcher from './statusNotifierWatcher.js';
  18. import * as Interfaces from './interfaces.js';
  19. import * as TrayIconsManager from './trayIconsManager.js';
  20. import * as Util from './util.js';
  21. import {SettingsManager} from './settingsManager.js';
  22. export default class DashToDockExtension extends Extension.Extension {
  23. constructor(...args) {
  24. super(...args);
  25. Util.Logger.init(this);
  26. Interfaces.initialize(this);
  27. this._isEnabled = false;
  28. this._statusNotifierWatcher = null;
  29. this._watchDog = new Util.NameWatcher(StatusNotifierWatcher.WATCHER_BUS_NAME);
  30. this._watchDog.connect('vanished', () => this._maybeEnableAfterNameAvailable());
  31. // HACK: we want to leave the watchdog alive when disabling the extension,
  32. // but if we are being reloaded, we destroy it since it could be considered
  33. // a leak and spams our log, too.
  34. /* eslint-disable no-undef */
  35. if (typeof global['--appindicator-extension-on-reload'] === 'function')
  36. global['--appindicator-extension-on-reload']();
  37. global['--appindicator-extension-on-reload'] = () => {
  38. Util.Logger.debug('Reload detected, destroying old watchdog');
  39. this._watchDog.destroy();
  40. this._watchDog = null;
  41. };
  42. /* eslint-enable no-undef */
  43. }
  44. enable() {
  45. this._isEnabled = true;
  46. SettingsManager.initialize(this);
  47. Util.tryCleanupOldIndicators();
  48. this._maybeEnableAfterNameAvailable();
  49. TrayIconsManager.TrayIconsManager.initialize();
  50. }
  51. disable() {
  52. this._isEnabled = false;
  53. TrayIconsManager.TrayIconsManager.destroy();
  54. if (this._statusNotifierWatcher !== null) {
  55. this._statusNotifierWatcher.destroy();
  56. this._statusNotifierWatcher = null;
  57. }
  58. SettingsManager.destroy();
  59. }
  60. // FIXME: when entering/leaving the lock screen, the extension might be
  61. // enabled/disabled rapidly.
  62. // This will create very bad side effects in case we were not done unowning
  63. // the name while trying to own it again. Since g_bus_unown_name doesn't
  64. // fire any callback when it's done, we need to monitor the bus manually
  65. // to find out when the name vanished so we can reclaim it again.
  66. _maybeEnableAfterNameAvailable() {
  67. // by the time we get called whe might not be enabled
  68. if (!this._isEnabled || this._statusNotifierWatcher)
  69. return;
  70. if (this._watchDog.nameAcquired && this._watchDog.nameOnBus)
  71. return;
  72. this._statusNotifierWatcher = new StatusNotifierWatcher.StatusNotifierWatcher(
  73. this._watchDog);
  74. }
  75. }