Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbus/dunstctl: add history command (#307) #970

Merged
merged 13 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions dunstctl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,42 @@ case "${1:-}" in
dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_DUNST}.Ping" >/dev/null 2>/dev/null \
|| die "Dunst controlling interface not available. Is the version too old?"
;;
"history")
counter=0
json_string="{ notifications: ["
for i in $(dbus-send --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.dunstproject.cmd0.NotificationListAll | grep "string" | cut -d'"' -f2)
do
case $(($counter%8)) in
"0")
json_string=$json_string"{body:\"$i\", "
;;
"1")
json_string=$json_string"message:\"$i\", "
;;
"2")
json_string=$json_string"summary:\"$i\", "
;;
"3")
json_string=$json_string"actions:\"$i\", "
;;
"4")
json_string=$json_string"app_name:\"$i\", "
;;
"5")
json_string=$json_string"category:\"$i\", "
;;
"6")
json_string=$json_string"default_action_name:\"$i\","
;;
"7")
json_string=$json_string"icon_path:\"$i\" }, "
;;
esac
((counter=counter+1))
done
json_string=$json_string"]}"
echo $json_string
;;
"")
die "dunstctl: No command specified. Please consult the usage."
;;
Expand Down
49 changes: 49 additions & 0 deletions src/dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ static const char *introspection_xml =
" <method name=\"NotificationCloseLast\" />"
" <method name=\"NotificationCloseAll\" />"
" <method name=\"NotificationShow\" />"
" <method name=\"NotificationListAll\">"
" <arg direction=\"out\" name=\"notifications\" type=\"aas\"/>"
" </method>"
" <method name=\"Ping\" />"

" <property name=\"paused\" type=\"b\" access=\"readwrite\">"
Expand Down Expand Up @@ -163,13 +166,15 @@ DBUS_METHOD(dunst_NotificationAction);
DBUS_METHOD(dunst_NotificationCloseAll);
DBUS_METHOD(dunst_NotificationCloseLast);
DBUS_METHOD(dunst_NotificationShow);
DBUS_METHOD(dunst_NotificationListAll);
DBUS_METHOD(dunst_Ping);
static struct dbus_method methods_dunst[] = {
{"ContextMenuCall", dbus_cb_dunst_ContextMenuCall},
{"NotificationAction", dbus_cb_dunst_NotificationAction},
{"NotificationCloseAll", dbus_cb_dunst_NotificationCloseAll},
{"NotificationCloseLast", dbus_cb_dunst_NotificationCloseLast},
{"NotificationShow", dbus_cb_dunst_NotificationShow},
{"NotificationListAll", dbus_cb_dunst_NotificationListAll},
{"Ping", dbus_cb_dunst_Ping},
};

Expand Down Expand Up @@ -283,6 +288,50 @@ static void dbus_cb_dunst_NotificationShow(GDBusConnection *connection,
g_dbus_connection_flush(connection, NULL, NULL, NULL);
}

static void dbus_cb_dunst_NotificationListAll(GDBusConnection *connection,
const gchar *sender,
GVariant *parameters,
GDBusMethodInvocation *invocation)
{
LOG_D("CMD: Listing all notifications from history");

GVariant *answer = NULL;
GVariantBuilder *builder;

builder = g_variant_builder_new(G_VARIANT_TYPE("aas"));

GList *notification_list = queues_get_history();

// reverse chronological list
for(int i = queues_length_history(); i > 0; i--) {
GVariantBuilder *notif_builder;
struct notification *n;
n = g_list_nth_data(notification_list, i-1);

// create sub-list per notification
notif_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));

g_variant_builder_add(notif_builder, "s", n->body);
g_variant_builder_add(notif_builder, "s", n->msg);
g_variant_builder_add(notif_builder, "s", n->summary);
g_variant_builder_add(notif_builder, "s", n->actions);
g_variant_builder_add(notif_builder, "s", n->appname);
g_variant_builder_add(notif_builder, "s", n->category);
g_variant_builder_add(notif_builder, "s", n->default_action_name);
g_variant_builder_add(notif_builder, "s", n->icon_path);

// add to whole list
g_variant_builder_add(builder, "as", notif_builder);
g_clear_pointer(&notif_builder, g_variant_builder_unref);
}

answer = g_variant_new("(aas)", builder);

g_clear_pointer(&builder, g_variant_builder_unref);
g_dbus_method_invocation_return_value(invocation, answer);
g_dbus_connection_flush(connection, NULL, NULL, NULL);
}

/* Just a simple Ping command to give the ability to dunstctl to test for the existence of this interface
* Any other way requires parsing the XML of the Introspection or other foo. Just calling the Ping on an old dunst version will fail. */
static void dbus_cb_dunst_Ping(GDBusConnection *connection,
Expand Down
6 changes: 6 additions & 0 deletions src/queues.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ unsigned int queues_length_history(void)
return history->length;
}

/* see queues.h */
GList *queues_get_history(void)
{
return g_queue_peek_head_link(history);
}

/**
* Swap two given queue elements. The element's data has to be a notification.
*
Expand Down
7 changes: 7 additions & 0 deletions src/queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ void queues_init(void);
*/
GList *queues_get_displayed(void);

/**
* Recieve the list of all notifications encountered
*
* @return read only list of notifications
*/
GList *queues_get_history(void);

/**
* Get the highest notification in line
*
Expand Down