deal with unavailable dbus server (Issue: #365)

If the dbus server cannot be started or is not available, calling a notification
would result in an exception. During initialization check that we can get the
server information, and disable notifications if this is not possible.

Note: we change dnotify.d from the originally distributed version to include
a check for availability function.
This commit is contained in:
Norbert Preining 2019-01-30 17:34:07 +09:00 committed by GitHub
parent 8b8cf9c777
commit 1f955c82be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -41,6 +41,17 @@ void init(string logDir)
void setNotifications(bool value)
{
version(Notifications) {
// if we try to enable notifications, check for server availability
// and disable in case dbus server is not reachable
if (value) {
auto serverAvailable = dnotify.check_availability();
if (!serverAvailable) {
log("Notification (dbus) server not available, disabling");
value = false;
}
}
}
doNotifications = value;
}

View file

@ -57,6 +57,20 @@ class NotificationError : Exception {
}
}
bool check_availability() {
// notify_init might return without dbus server actually started
// try to check for running dbus server
char **ret_name;
char **ret_vendor;
char **ret_version;
char **ret_spec_version;
bool ret;
try {
return notify_get_server_info(ret_name, ret_vendor, ret_version, ret_spec_version);
} catch (NotificationError e) {
throw new NotificationError("Cannot find dbus server!");
}
}
void init(in char[] name) {
notify_init(name.toStringz());
@ -306,4 +320,4 @@ version(TestMain) {
n.timeout = 3;
n.show();
}
}
}