Skip to content

Commit

Permalink
Merge pull request #358 from matthijskooijman/handle-hamster-restarts
Browse files Browse the repository at this point in the history
Gracefully handle hamster restarts for development
  • Loading branch information
hedayat committed May 1, 2023
2 parents a668e9b + 6d9807c commit 75541ac
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
36 changes: 27 additions & 9 deletions extension/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ class Controller {
enable() {
this.shouldEnable = true;
new ApiProxy(Gio.DBus.session, 'org.gnome.Hamster', '/org/gnome/Hamster',
function(proxy) {
function(proxy, err) {
this.reportIfError(_("Connection to DBUS service failed"), err);
this.apiProxy = proxy;
this.deferred_enable();
}.bind(this));
new WindowsProxy(Gio.DBus.session, "org.gnome.Hamster.WindowServer",
"/org/gnome/Hamster/WindowServer",
function(proxy) {
function(proxy, err) {
this.reportIfError(_("Connection to DBUS window service failed"), err);
this.windowsProxy = proxy;
this.deferred_enable();
}.bind(this));
Expand All @@ -146,23 +148,21 @@ class Controller {

// Callbacks that handle appearing/vanishing dbus services.
function apiProxy_appeared_callback() {
if (this.shouldEnable)
this.panelWidget.show();
}

function apiProxy_vanished_callback() {
/* jshint validthis: true */
global.log(_("hamster-shell-extension: 'hamster-service' not running. Shutting down."));
Main.notify(_("hamster-shell-extension: 'hamster-service' not running. Shutting down."));
this.disable();
this.reportIfError(_("DBUS proxy disappeared"), _("Disabling extension until it comes back"));
if (this.shouldEnable)
this.panelWidget.hide();
}

function windowsProxy_appeared_callback() {
}

function windowsProxy_vanished_callback() {
/* jshint validthis: true */
global.log(_("hamster-shell-extension: 'hamster-windows-service' not running. Shutting down."));
Main.notify(_("hamster-shell-extension: 'hamster-windows-service' not running. Shutting down."));
this.disable();
}

// Set-up watchers that watch for required dbus services.
Expand Down Expand Up @@ -210,12 +210,30 @@ class Controller {

this.runningActivitiesQuery = true;
this.apiProxy.GetActivitiesRemote("", function([response], err) {
this.reportIfError(_("Failed to get activities"), err);
this.runningActivitiesQuery = false;
this.activities = response;
// global.log('ACTIVITIES HAMSTER: ', this.activities);
}.bind(this));
}

/**
* Report an error if one is passed. If error is falsey (e.g.
* null), nothing is reported.
*/
reportIfError(msg, error) {
if (error) {
// Use toString, error can be a string, exception, etc.
global.log("error: Hamster: " + msg + ": " + error.toString());
// Prefix msg to details (second argument), since the
// details are word-wrapped and the title is not.
Main.notify("Hamster: " + msg, msg + "\n" + error.toString());
// Close menu so notification can be seen
if (this.panelWidget)
this.panelWidget.close_menu();
}
}

/**
* Place the actual extension wi
* get in the right place according to settings.
Expand Down
1 change: 1 addition & 0 deletions extension/widgets/ongoingFactEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class OngoingFactEntry extends St.Entry {
_onEntryActivated() {
let text = this.get_text();
this._controller.apiProxy.AddFactRemote(text, 0, 0, false, function(response, error) {
this._controller.reportIfError(_("Failed to add activity"), error);
// not interested in the new id - this shuts up the warning
}.bind(this));
this.set_text('');
Expand Down
32 changes: 25 additions & 7 deletions extension/widgets/panelWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,8 @@ class PanelWidget extends PanelMenu.Button {

let facts = [];

// [FIXME]
// This seems a rather naive way to handle potential errors.
if (err) {
log(err);
this._controller.reportIfError(_("Failed to get activities"), err);
} else if (response.length > 0) {
facts = Stuff.fromDbusFacts(response);
}
Expand All @@ -207,6 +205,12 @@ class PanelWidget extends PanelMenu.Button {
this.menu.toggle();
}

/**
* Close the 'popup menu'
*/
close_menu() {
this.menu.close();
}

/**
* Update the rendering of the PanelWidget in the panel itself.
Expand Down Expand Up @@ -286,7 +290,9 @@ class PanelWidget extends PanelMenu.Button {
now.getMinutes(),
now.getSeconds());
epochSeconds = Math.floor(epochSeconds / 1000);
this._controller.apiProxy.StopTrackingRemote(GLib.Variant.new('i', [epochSeconds]));
this._controller.apiProxy.StopTrackingRemote(GLib.Variant.new('i', [epochSeconds]), function(response, err) {
this._controller.reportIfError(_("Failed to stop tracking"), err);
}.bind(this));
}

/**
Expand All @@ -295,7 +301,11 @@ class PanelWidget extends PanelMenu.Button {
* @callback panelWidget~_onOpenOverview
*/
_onOpenOverview() {
this._controller.windowsProxy.overviewSync();
try {
this._controller.windowsProxy.overviewSync();
} catch (error) {
this._controller.reportIfError(_("Failed to open overview window"), error);
}
}

/**
Expand All @@ -304,7 +314,11 @@ class PanelWidget extends PanelMenu.Button {
* @callback panelWidget~_onOpenAddFact
*/
_onOpenAddFact() {
this._controller.windowsProxy.editSync(GLib.Variant.new('i', [0]));
try {
this._controller.windowsProxy.editSync(GLib.Variant.new('i', [0]));
} catch (error) {
this._controller.reportIfError(_("Failed to open add window"), error);
}
}

/**
Expand All @@ -315,6 +329,10 @@ class PanelWidget extends PanelMenu.Button {
* Note: This will open the GUI settings, not the extension settings!
*/
_onOpenSettings() {
this._controller.windowsProxy.preferencesSync();
try {
this._controller.windowsProxy.preferencesSync();
} catch (error) {
this._controller.reportIfError(_("Failed to open settings window"), error);
}
}
});
4 changes: 4 additions & 0 deletions extension/widgets/todaysFactsWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;

const Gettext = imports.gettext.domain('hamster-shell-extension');
const _ = Gettext.gettext;

const Me = imports.misc.extensionUtils.getCurrentExtension();
const Stuff = Me.imports.stuff;

Expand Down Expand Up @@ -114,6 +117,7 @@ class TodaysFactsWidget extends St.ScrollView {

/* jshint validthis: true */
controller.apiProxy.AddFactRemote(factStr, 0, 0, false, function(response, err) {
controller.reportIfError(_("Failed to continue activity"), err);
// not interested in the new id - this shuts up the warning
}.bind(this));
menu.close();
Expand Down

0 comments on commit 75541ac

Please sign in to comment.