Skip to content

Commit

Permalink
Added file preview feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Kakueeen committed Aug 21, 2023
1 parent 34b826f commit 1e02c80
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "fsearch_window.h"
#include "icon_resources.h"
#include "ui_resources.h"
#include "fsearch_preview.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <limits.h>
Expand Down Expand Up @@ -556,6 +557,9 @@ fsearch_application_shutdown(GApplication *app) {
g_debug("[app] database thread finished.");
}

// close the preview
fsearch_preview_call_close();

g_clear_pointer(&fsearch->db, db_unref);
g_clear_object(&fsearch->db_thread_cancellable);

Expand Down
88 changes: 88 additions & 0 deletions src/fsearch_preview.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
FSearch - A fast file search utility
Copyright © 2020 Christian Boxdörfer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "fsearch_preview.h"
#include "fsearch.h"

#include <gio/gio.h>

#define PREVIEWER_DBUS_NAME "org.gnome.NautilusPreviewer"
#define PREVIEWER_DBUS_IFACE "org.gnome.NautilusPreviewer"
#define PREVIEWER_DBUS_PATH "/org/gnome/NautilusPreviewer"

static void
preview_show_file_ready_cb(GObject *source, GAsyncResult *res, gpointer user_data) {
GError *error = NULL;

g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);

if (error != NULL) {
g_debug("Unable to call ShowFile on NautilusPreviewer: %s", error->message);
g_error_free(error);
}
}

static void
preview_close_ready_cb(GObject *source, GAsyncResult *res, gpointer user_data) {
GError *error = NULL;

g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);

if (error != NULL) {
g_debug("Unable to call Close on NautilusPreviewer: %s", error->message);
g_error_free(error);
}
}

void
fsearch_preview_call_show_file(GList *file_list, guint xid, gboolean close_if_already_visible) {
GDBusConnection *connection = g_application_get_dbus_connection(G_APPLICATION(FSEARCH_APPLICATION_DEFAULT));
g_autofree gchar *uri = g_filename_to_uri((char *)file_list->data, NULL, NULL);
GVariant *variant = g_variant_new("(sib)", uri, xid, close_if_already_visible);

g_dbus_connection_call(connection,
PREVIEWER_DBUS_NAME,
PREVIEWER_DBUS_PATH,
PREVIEWER_DBUS_IFACE,
"ShowFile",
variant,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
preview_show_file_ready_cb,
NULL);
}

void
fsearch_preview_call_close(void) {
GDBusConnection *connection = g_application_get_dbus_connection(G_APPLICATION(FSEARCH_APPLICATION_DEFAULT));

g_dbus_connection_call(connection,
PREVIEWER_DBUS_NAME,
PREVIEWER_DBUS_PATH,
PREVIEWER_DBUS_IFACE,
"Close",
NULL,
NULL,
G_DBUS_CALL_FLAGS_NO_AUTO_START,
-1,
NULL,
preview_close_ready_cb,
NULL);
}
27 changes: 27 additions & 0 deletions src/fsearch_preview.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
FSearch - A fast file search utility
Copyright © 2020 Christian Boxdörfer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <glib.h>

void
fsearch_preview_call_show_file(GList *file_list, guint xid, gboolean close_if_already_visible);

void
fsearch_preview_call_close(void);
3 changes: 3 additions & 0 deletions src/fsearch_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ on_listview_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_da
case GDK_KEY_KP_Enter:
g_action_group_activate_action(group, "open", NULL);
return GDK_EVENT_STOP;
case GDK_KEY_space:
g_action_group_activate_action(group, "preview", NULL);
return GDK_EVENT_STOP;
default:
return GDK_EVENT_PROPAGATE;
}
Expand Down
27 changes: 27 additions & 0 deletions src/fsearch_window_actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <glib/gi18n.h>
#include <stdint.h>
#include <gdk/gdkx.h>

#include "fsearch_clipboard.h"
#include "fsearch_config.h"
Expand All @@ -35,6 +36,7 @@
#include "fsearch_statusbar.h"
#include "fsearch_ui_utils.h"
#include "fsearch_window_actions.h"
#include "fsearch_preview.h"

static void
action_set_active_int(GActionGroup *group, const gchar *action_name, int32_t value) {
Expand Down Expand Up @@ -584,6 +586,30 @@ fsearch_window_action_open_folder(GSimpleAction *action, GVariant *variant, gpoi
fsearch_window_action_open_generic(self, true, false);
}

static void
fsearch_window_action_preview(GSimpleAction *action, GVariant *variant, gpointer user_data) {
FsearchApplicationWindow *self = user_data;
guint xid = 0;
GList *file_list = NULL;

const guint selected_rows = fsearch_application_window_get_num_selected(self);
if (selected_rows == 0) {
return;
}

fsearch_application_window_selection_for_each(self, prepend_full_path_to_list, &file_list);
file_list = g_list_reverse(file_list);

GtkWidget *toplevel = gtk_widget_get_toplevel(GTK_WIDGET(self));
#ifdef GDK_WINDOWING_X11
GdkWindow *window = gtk_widget_get_window (toplevel);
if (GDK_IS_X11_WINDOW(window)) {
xid = gdk_x11_window_get_xid(gtk_widget_get_window(toplevel));
}
#endif
fsearch_preview_call_show_file(file_list, xid, TRUE);
}

static void
on_fsearch_window_action_open_with_response(GtkDialog *dialog, gint response_id, gpointer user_data) {
if (response_id != GTK_RESPONSE_OK) {
Expand Down Expand Up @@ -825,6 +851,7 @@ static GActionEntry FsearchWindowActions[] = {
{"open_with", fsearch_window_action_open_with, "s"},
{"open_with_other", fsearch_window_action_open_with_other, "s"},
{"open_folder", fsearch_window_action_open_folder},
{"preview", fsearch_window_action_preview},
{"close_window", fsearch_window_action_close_window},
{"copy_clipboard", fsearch_window_action_copy},
{"copy_as_text_path_and_name_clipboard", fsearch_window_action_copy_full_path},
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ libfsearch_sources = [
'fsearch_utf.c',
'fsearch_window.c',
'fsearch_window_actions.c',
'fsearch_preview.c',
]

if build_machine.system()=='darwin'
Expand Down

0 comments on commit 1e02c80

Please sign in to comment.