Skip to content

Commit b8de377

Browse files
authored
Merge pull request #6 from maquinas07/add-show-in-file-manager
Show in file manager when clicking on the success notification
2 parents 6f71dc7 + 4ca8d7b commit b8de377

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ CFLAGS_XEXTENSIONS=-DHAVE_XEXTENSIONS `pkg-config --cflags xfixes`
55
LDFLAGS_XEXTENSIONS=`pkg-config --libs xfixes`
66

77
# Comment if you want to disable the dependency to libnotify. Errors will be printed to stderr.
8-
CFLAGS_NOTIFY:=-DHAVE_NOTIFY `pkg-config --cflags libnotify`
9-
LDFLAGS_NOTIFY:=`pkg-config --libs libnotify`
8+
CFLAGS_NOTIFY:=-DHAVE_NOTIFY `pkg-config --cflags libnotify gio-2.0`
9+
LDFLAGS_NOTIFY:=`pkg-config --libs libnotify gio-2.0`
1010

1111
# Uncomment if you want to be able to select the output folder via zenity
1212
# CFLAGS_ZENITY=-DHAVE_ZENITY

src/evid.c

+48-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "xrectsel.h"
2727

2828
#ifdef HAVE_NOTIFY
29+
#include <libnotify/notification.h>
2930
#include <libnotify/notify.h>
3031
#endif
3132

@@ -366,11 +367,42 @@ static int notify_cancel() {
366367
"%s: %s", PROGRAM_NAME, "recording was cancelled");
367368
NotifyNotification *success_notification =
368369
notify_notification_new(success_notification_summary, NULL, NULL);
369-
return notify_notification_show(success_notification, NULL);
370+
int result = notify_notification_show(success_notification, NULL);
371+
g_object_unref(success_notification);
372+
return result;
370373
#endif
371374
return 0;
372375
}
373376

377+
#ifdef HAVE_NOTIFY
378+
379+
typedef struct ActionPayload ActionPayload;
380+
struct ActionPayload {
381+
GMainLoop *loop;
382+
const char *target_file;
383+
};
384+
385+
static void show_in_file_manager_callback(NotifyNotification *notification,
386+
char *action, gpointer payload) {
387+
ActionPayload p = (*(ActionPayload *)payload);
388+
if (p.target_file != NULL) {
389+
show_file_in_default_file_manager(p.target_file);
390+
}
391+
g_main_loop_quit(p.loop);
392+
}
393+
394+
static int on_notification_closed(NotifyNotification *notification,
395+
gpointer loop) {
396+
g_main_loop_quit(*((GMainLoop **)loop));
397+
return 0;
398+
}
399+
400+
static int on_notification_timeout(gpointer loop) {
401+
g_main_loop_quit(*((GMainLoop **)loop));
402+
return 0;
403+
}
404+
#endif
405+
374406
int main(int argc, char *argv[]) {
375407
#ifdef HAVE_NOTIFY
376408
notify_init(PROGRAM_NAME);
@@ -475,13 +507,27 @@ int main(int argc, char *argv[]) {
475507

476508
remove_file(tmp_file);
477509
#ifdef HAVE_NOTIFY
510+
GMainLoop *main_loop = g_main_loop_new(0, 1);
511+
ActionPayload action_payload = {.loop = main_loop,
512+
.target_file = new_file};
478513
char success_notification_summary[50];
479514
snprintf(success_notification_summary,
480515
ARR_SIZE(success_notification_summary), "%s: %s", PROGRAM_NAME,
481516
"recording saved successfuly");
482517
NotifyNotification *success_notification = notify_notification_new(
483518
success_notification_summary, new_file, NULL);
519+
notify_notification_add_action(
520+
success_notification, "default", "Show in file manager",
521+
show_in_file_manager_callback, &action_payload, NULL);
484522
notify_notification_show(success_notification, NULL);
523+
int timeout = 5000;
524+
notify_notification_set_timeout(success_notification, timeout);
525+
g_signal_connect(success_notification, "closed",
526+
G_CALLBACK(on_notification_closed), &main_loop);
527+
g_timeout_add(timeout, on_notification_timeout, &main_loop);
528+
g_main_loop_run(main_loop);
529+
g_main_loop_unref(main_loop);
530+
g_object_unref(success_notification);
485531
#endif
486532
break;
487533
}
@@ -495,6 +541,7 @@ int main(int argc, char *argv[]) {
495541
NotifyNotification *success_notification =
496542
notify_notification_new(success_notification_summary, NULL, NULL);
497543
notify_notification_show(success_notification, NULL);
544+
g_object_unref(success_notification);
498545
#endif
499546
break;
500547
}

src/file.c

+49
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
#include <linux/limits.h>
3636

37+
#ifdef HAVE_NOTIFY
38+
#include <gio/gio.h>
39+
#endif
40+
3741
void get_default_file_name(char *default_file_name, Args *args) {
3842
time_t rawtime = time(NULL);
3943
struct tm *tm = localtime(&rawtime);
@@ -185,3 +189,48 @@ int mkdirp(const char *dir) {
185189
}
186190
return 0;
187191
}
192+
193+
#ifdef HAVE_NOTIFY
194+
int show_file_in_default_file_manager(const char *file) {
195+
GError *err = NULL;
196+
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(
197+
G_BUS_TYPE_SESSION,
198+
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
199+
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
200+
NULL, "org.freedesktop.FileManager1", "/org/freedesktop/FileManager1",
201+
"org.freedesktop.FileManager1", NULL, &err);
202+
if (!proxy) {
203+
error("failed to create DBUS proxy for FileManager1: %s\n", err->message);
204+
g_error_free(err);
205+
return -1;
206+
}
207+
208+
gchar *uri = g_filename_to_uri(file, NULL, &err);
209+
if (!uri) {
210+
error("failed to convert to uri: %s\n", err->message);
211+
g_error_free(err);
212+
return -1;
213+
}
214+
215+
GVariantBuilder builder;
216+
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
217+
g_variant_builder_add(&builder, "s", uri);
218+
219+
g_free(uri);
220+
221+
const char *startupId = "";
222+
GVariant *result = g_dbus_proxy_call_sync(
223+
proxy, "ShowItems", g_variant_new("(ass)", &builder, startupId),
224+
G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
225+
226+
g_object_unref(proxy);
227+
if (!result) {
228+
error("failed to query file manager: %s\n", err->message);
229+
g_error_free(err);
230+
return -1;
231+
}
232+
g_variant_unref(result);
233+
234+
return 0;
235+
}
236+
#endif

src/file.h

+4
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ void move_file(const char *source_file, const char *dest_file);
3030
int remove_file(const char *file);
3131
int mkdirp(const char *dir);
3232

33+
#ifdef HAVE_NOTIFY
34+
int show_file_in_default_file_manager(const char *file);
35+
#endif
36+
3337
#endif

0 commit comments

Comments
 (0)