Skip to content

Commit

Permalink
clapper-app: Add command line option to enqueue files
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafostar committed May 11, 2024
1 parent 4a1fa2c commit 2f9958c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/bin/clapper-app/clapper-app-application.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "clapper-app-utils.h"

#define PERCENTAGE_ROUND(a) (round ((gdouble) a / 0.01) * 0.01)
#define ENQUEUE_STATUS 100

#define GST_CAT_DEFAULT clapper_app_application_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
Expand Down Expand Up @@ -374,6 +375,7 @@ clapper_app_application_local_command_line (GApplication *app,
{
gchar **argv = *arguments;
guint i;
gboolean res;

/* NOTE: argv is never NULL, so no need to check */

Expand All @@ -385,7 +387,45 @@ clapper_app_application_local_command_line (GApplication *app,
}
}

return G_APPLICATION_CLASS (parent_class)->local_command_line (app, arguments, exit_status);
res = G_APPLICATION_CLASS (parent_class)->local_command_line (app, arguments, exit_status);

/* If enqueue is set here, that means we stopped app from default processing,
* thus we need to "register" app remote instance and call "open" ourselves */
if (*exit_status == ENQUEUE_STATUS) {
GFile **files = NULL;
gint n_files = 0;

if (clapper_app_utils_files_from_command_line (*arguments, &files, &n_files)) {
gboolean can_open;

if (!(can_open = g_application_get_is_registered (app)))
can_open = g_application_register (app, NULL, NULL);

if (G_LIKELY (can_open)) {
g_application_open (app, files, n_files, "add-only");
*exit_status = EXIT_SUCCESS; // Enqueue was successful
}

clapper_app_utils_files_free (files);
}
}

return res;
}

static gint
clapper_app_application_handle_local_options (GApplication *app, GVariantDict *options)
{
gboolean enqueue = FALSE;

g_variant_dict_lookup (options, "enqueue", "b", &enqueue);

/* Prevent application from calling open without hints,
* we will call it ourselves in local_command_line() */
if (enqueue)
return ENQUEUE_STATUS;

return -1; // Default processing
}

static gboolean
Expand Down Expand Up @@ -560,6 +600,7 @@ clapper_app_application_constructed (GObject *object)
guint i;

const GOptionEntry app_options[] = {
{ "enqueue", 0, 0, G_OPTION_ARG_NONE, NULL, _("Append media to queue in primary application instance"), NULL },
{ "volume", 0, 0, G_OPTION_ARG_DOUBLE, &app_opts.volume, _("Audio volume to set at startup"), NULL },
{ "speed", 0, 0, G_OPTION_ARG_DOUBLE, &app_opts.speed, _("Playback speed to set at startup"), NULL },
{ "video-filter", 0, 0, G_OPTION_ARG_STRING, &app_opts.video_filter, _("Video filter to use (\"none\" to disable)"), NULL },
Expand Down Expand Up @@ -639,5 +680,6 @@ clapper_app_application_class_init (ClapperAppApplicationClass *klass)

application_class->activate = clapper_app_application_activate;
application_class->local_command_line = clapper_app_application_local_command_line;
application_class->handle_local_options = clapper_app_application_handle_local_options;
application_class->open = clapper_app_application_open;
}
19 changes: 19 additions & 0 deletions src/bin/clapper-app/clapper-app-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ clapper_app_utils_files_from_string (const gchar *string, GFile ***files, gint *
return success;
}

gboolean
clapper_app_utils_files_from_command_line (gchar **arguments, GFile ***files, gint *n_files)
{
GSList *slist = NULL;
guint i;
gboolean success;

for (i = 1; arguments[i]; ++i)
slist = g_slist_append (slist, g_file_new_for_commandline_arg (arguments[i]));

if (!slist)
return FALSE;

success = clapper_app_utils_files_from_slist (slist, files, n_files);
g_slist_free_full (slist, g_object_unref);

return success;
}

static inline gboolean
_files_from_file (GFile *file, GFile ***files, gint *n_files)
{
Expand Down
3 changes: 3 additions & 0 deletions src/bin/clapper-app/clapper-app-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ gboolean clapper_app_utils_files_from_slist (GSList *file_list, GFile ***files,
G_GNUC_INTERNAL
gboolean clapper_app_utils_files_from_string (const gchar *string, GFile ***files, gint *n_files);

G_GNUC_INTERNAL
gboolean clapper_app_utils_files_from_command_line (gchar **arguments, GFile ***files, gint *n_files);

G_GNUC_INTERNAL
gboolean clapper_app_utils_files_from_value (const GValue *value, GFile ***files, gint *n_files);

Expand Down

0 comments on commit 2f9958c

Please sign in to comment.