2017-04-15 06:32:05 -04:00
|
|
|
/*
|
2015-11-03 02:31:38 -05:00
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2021-06-09 08:50:39 -04:00
|
|
|
* Copyright © 2013-2021 Qball Cow <qball@gmpclient.org>
|
2015-11-03 02:31:38 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
2017-04-15 05:58:49 -04:00
|
|
|
|
2018-06-12 05:45:42 -04:00
|
|
|
/** The log domain of this dialog. */
|
2017-04-27 16:59:14 -04:00
|
|
|
#define G_LOG_DOMAIN "Dialogs.DRun"
|
2017-04-15 05:58:49 -04:00
|
|
|
|
2015-11-03 02:31:38 -05:00
|
|
|
#include <config.h>
|
2016-08-29 15:38:29 -04:00
|
|
|
#ifdef ENABLE_DRUN
|
2015-11-03 02:31:38 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2017-10-03 11:35:48 -04:00
|
|
|
#include <limits.h>
|
2015-11-03 02:31:38 -05:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
2016-06-05 13:01:38 -04:00
|
|
|
#include <sys/stat.h>
|
2015-11-03 02:31:38 -05:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "rofi.h"
|
2016-01-07 10:01:56 -05:00
|
|
|
#include "settings.h"
|
2015-11-03 02:31:38 -05:00
|
|
|
#include "helper.h"
|
2016-10-31 03:07:02 -04:00
|
|
|
#include "timings.h"
|
2016-10-08 12:57:59 -04:00
|
|
|
#include "widgets/textbox.h"
|
2016-02-16 04:40:21 -05:00
|
|
|
#include "history.h"
|
2015-11-03 02:31:38 -05:00
|
|
|
#include "dialogs/drun.h"
|
2021-06-08 13:28:13 -04:00
|
|
|
#include "dialogs/filebrowser.h"
|
|
|
|
#include "mode-private.h"
|
2017-05-31 18:12:03 -04:00
|
|
|
#include "xcb.h"
|
2015-11-03 02:31:38 -05:00
|
|
|
|
2018-06-09 13:13:57 -04:00
|
|
|
#include "rofi-icon-fetcher.h"
|
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
#define DRUN_CACHE_FILE "rofi3.druncache"
|
2020-01-26 06:47:04 -05:00
|
|
|
#define DRUN_DESKTOP_CACHE_FILE "rofi-drun-desktop.cache"
|
2015-11-03 02:31:38 -05:00
|
|
|
|
2018-07-28 05:12:11 -04:00
|
|
|
char *DRUN_GROUP_NAME = "Desktop Entry";
|
2017-12-09 13:55:39 -05:00
|
|
|
|
2018-06-09 13:13:57 -04:00
|
|
|
typedef struct _DRunModePrivateData DRunModePrivateData;
|
2020-08-26 15:10:04 -04:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
DRUN_DESKTOP_ENTRY_TYPE_UNDETERMINED = 0,
|
|
|
|
DRUN_DESKTOP_ENTRY_TYPE_APPLICATION,
|
|
|
|
DRUN_DESKTOP_ENTRY_TYPE_LINK,
|
2021-05-30 17:21:42 -04:00
|
|
|
DRUN_DESKTOP_ENTRY_TYPE_SERVICE,
|
2020-08-26 15:10:04 -04:00
|
|
|
DRUN_DESKTOP_ENTRY_TYPE_DIRECTORY,
|
|
|
|
} DRunDesktopEntryType;
|
|
|
|
|
2015-11-07 05:20:25 -05:00
|
|
|
/**
|
|
|
|
* Store extra information about the entry.
|
|
|
|
* Currently the executable and if it should run in terminal.
|
|
|
|
*/
|
2016-03-24 17:15:10 -04:00
|
|
|
typedef struct
|
2015-11-03 11:34:02 -05:00
|
|
|
{
|
2018-06-09 13:13:57 -04:00
|
|
|
DRunModePrivateData *pd;
|
2018-07-28 05:12:11 -04:00
|
|
|
/* category */
|
2018-08-08 09:55:13 -04:00
|
|
|
char *action;
|
2016-08-28 16:43:42 -04:00
|
|
|
/* Root */
|
2018-08-08 09:55:13 -04:00
|
|
|
char *root;
|
2016-02-16 04:40:21 -05:00
|
|
|
/* Path to desktop file */
|
2018-08-08 09:55:13 -04:00
|
|
|
char *path;
|
2017-06-02 08:55:36 -04:00
|
|
|
/* Application id (.desktop filename) */
|
2018-08-08 09:55:13 -04:00
|
|
|
char *app_id;
|
2017-10-04 02:59:09 -04:00
|
|
|
/* Desktop id */
|
2018-08-08 09:55:13 -04:00
|
|
|
char *desktop_id;
|
2017-04-12 12:08:57 -04:00
|
|
|
/* Icon stuff */
|
2018-08-08 09:55:13 -04:00
|
|
|
char *icon_name;
|
2017-06-02 15:34:07 -04:00
|
|
|
/* Icon size is used to indicate what size is requested by the gui.
|
|
|
|
* secondary it indicates if the request for a lookup has been issued (0 not issued )
|
|
|
|
*/
|
2020-08-28 10:41:59 -04:00
|
|
|
int icon_size;
|
2017-06-02 15:34:07 -04:00
|
|
|
/* Surface holding the icon. */
|
2020-08-28 10:41:59 -04:00
|
|
|
cairo_surface_t *icon;
|
2020-08-26 15:10:04 -04:00
|
|
|
/* Executable - for Application entries only */
|
2020-08-28 10:41:59 -04:00
|
|
|
char *exec;
|
2015-11-21 17:59:59 -05:00
|
|
|
/* Name of the Entry */
|
2020-08-28 10:41:59 -04:00
|
|
|
char *name;
|
2015-11-21 17:59:59 -05:00
|
|
|
/* Generic Name */
|
2020-08-28 10:41:59 -04:00
|
|
|
char *generic_name;
|
2017-12-09 13:55:39 -05:00
|
|
|
/* Categories */
|
2020-08-28 10:41:59 -04:00
|
|
|
char **categories;
|
2019-08-20 16:51:07 -04:00
|
|
|
/* Keywords */
|
2020-08-28 10:41:59 -04:00
|
|
|
char **keywords;
|
2017-12-09 13:55:39 -05:00
|
|
|
/* Comments */
|
2020-08-28 10:41:59 -04:00
|
|
|
char *comment;
|
2016-12-26 09:18:52 -05:00
|
|
|
|
2020-08-28 10:41:59 -04:00
|
|
|
GKeyFile *key_file;
|
2017-10-03 11:35:48 -04:00
|
|
|
|
2020-08-28 10:41:59 -04:00
|
|
|
gint sort_index;
|
2018-06-09 13:13:57 -04:00
|
|
|
|
2020-08-28 10:41:59 -04:00
|
|
|
uint32_t icon_fetch_uid;
|
2020-08-26 15:10:04 -04:00
|
|
|
|
|
|
|
DRunDesktopEntryType type;
|
2015-11-03 11:34:02 -05:00
|
|
|
} DRunModeEntry;
|
|
|
|
|
2017-10-21 05:18:52 -04:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *entry_field_name;
|
2021-06-15 08:32:46 -04:00
|
|
|
gboolean enabled_match;
|
|
|
|
gboolean enabled_display;
|
2017-10-21 05:18:52 -04:00
|
|
|
} DRunEntryField;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
DRUN_MATCH_FIELD_NAME,
|
|
|
|
DRUN_MATCH_FIELD_GENERIC,
|
|
|
|
DRUN_MATCH_FIELD_EXEC,
|
|
|
|
DRUN_MATCH_FIELD_CATEGORIES,
|
2019-08-20 16:51:07 -04:00
|
|
|
DRUN_MATCH_FIELD_KEYWORDS,
|
2017-12-09 13:55:39 -05:00
|
|
|
DRUN_MATCH_FIELD_COMMENT,
|
2017-10-21 05:18:52 -04:00
|
|
|
DRUN_MATCH_NUM_FIELDS,
|
|
|
|
} DRunMatchingFields;
|
|
|
|
|
|
|
|
static DRunEntryField matching_entry_fields[DRUN_MATCH_NUM_FIELDS] = {
|
2021-06-15 08:32:46 -04:00
|
|
|
{ .entry_field_name = "name", .enabled_match = TRUE, .enabled_display = TRUE, },
|
|
|
|
{ .entry_field_name = "generic", .enabled_match = TRUE, .enabled_display = TRUE, },
|
|
|
|
{ .entry_field_name = "exec", .enabled_match = TRUE, .enabled_display = TRUE, },
|
|
|
|
{ .entry_field_name = "categories", .enabled_match = TRUE, .enabled_display = TRUE, },
|
|
|
|
{ .entry_field_name = "keywords", .enabled_match = TRUE, .enabled_display = TRUE, },
|
|
|
|
{ .entry_field_name = "comment", .enabled_match = FALSE,.enabled_display = FALSE,}
|
2017-10-21 05:18:52 -04:00
|
|
|
};
|
|
|
|
|
2018-06-09 13:13:57 -04:00
|
|
|
struct _DRunModePrivateData
|
2015-11-03 11:34:02 -05:00
|
|
|
{
|
2018-08-08 09:55:13 -04:00
|
|
|
DRunModeEntry *entry_list;
|
|
|
|
unsigned int cmd_list_length;
|
|
|
|
unsigned int cmd_list_length_actual;
|
2016-08-30 15:49:46 -04:00
|
|
|
// List of disabled entries.
|
2018-08-08 09:55:13 -04:00
|
|
|
GHashTable *disabled_entries;
|
|
|
|
unsigned int disabled_entries_length;
|
|
|
|
unsigned int expected_line_height;
|
2017-10-21 05:18:52 -04:00
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
char **show_categories;
|
2019-08-09 13:33:23 -04:00
|
|
|
|
2017-06-02 12:37:03 -04:00
|
|
|
// Theme
|
2018-08-08 09:55:13 -04:00
|
|
|
const gchar *icon_theme;
|
2017-11-02 16:05:07 -04:00
|
|
|
// DE
|
2018-08-08 09:55:13 -04:00
|
|
|
gchar **current_desktop_list;
|
2021-06-08 13:28:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
gboolean file_complete;
|
|
|
|
Mode *completer;
|
|
|
|
char *old_completer_input;
|
|
|
|
uint32_t selected_line;
|
|
|
|
char *old_input;
|
2021-06-08 14:21:28 -04:00
|
|
|
|
|
|
|
/** fallback icon */
|
|
|
|
uint32_t fallback_icon_fetch_uid;
|
|
|
|
cairo_surface_t *fallback_icon;
|
2018-06-09 13:13:57 -04:00
|
|
|
};
|
2015-11-03 11:34:02 -05:00
|
|
|
|
2016-08-29 15:14:03 -04:00
|
|
|
struct RegexEvalArg
|
|
|
|
{
|
|
|
|
DRunModeEntry *e;
|
2021-06-08 13:28:13 -04:00
|
|
|
const char *path;
|
2016-08-29 15:14:03 -04:00
|
|
|
gboolean success;
|
|
|
|
};
|
2017-10-21 05:18:52 -04:00
|
|
|
|
2016-08-29 15:14:03 -04:00
|
|
|
static gboolean drun_helper_eval_cb ( const GMatchInfo *info, GString *res, gpointer data )
|
2015-11-03 11:34:02 -05:00
|
|
|
{
|
2016-08-29 15:14:03 -04:00
|
|
|
// TODO quoting is not right? Find description not very clear, need to check.
|
|
|
|
struct RegexEvalArg *e = (struct RegexEvalArg *) data;
|
|
|
|
|
|
|
|
gchar *match;
|
|
|
|
// Get the match
|
|
|
|
match = g_match_info_fetch ( info, 0 );
|
|
|
|
if ( match != NULL ) {
|
|
|
|
switch ( match[1] )
|
|
|
|
{
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
case 'u':
|
|
|
|
case 'U':
|
2021-06-08 13:28:13 -04:00
|
|
|
g_string_append(res, e->path);
|
|
|
|
break;
|
|
|
|
// Unsupported
|
2016-08-29 15:14:03 -04:00
|
|
|
case 'i':
|
2021-06-08 13:28:13 -04:00
|
|
|
// TODO
|
|
|
|
if ( e->e && e->e->icon ) {
|
|
|
|
g_string_append_printf(res, "--icon %s", e->e->icon_name );
|
|
|
|
}
|
|
|
|
break;
|
2016-08-29 15:14:03 -04:00
|
|
|
// Deprecated
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
case 'v':
|
|
|
|
case 'm':
|
|
|
|
break;
|
2020-04-15 13:06:39 -04:00
|
|
|
case '%':
|
2020-08-28 10:41:59 -04:00
|
|
|
g_string_append ( res, "%" );
|
2020-04-15 13:06:39 -04:00
|
|
|
break;
|
2016-08-29 15:14:03 -04:00
|
|
|
case 'k':
|
|
|
|
if ( e->e->path ) {
|
|
|
|
char *esc = g_shell_quote ( e->e->path );
|
|
|
|
g_string_append ( res, esc );
|
|
|
|
g_free ( esc );
|
2015-11-03 11:34:02 -05:00
|
|
|
}
|
2016-08-29 15:14:03 -04:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if ( e->e->name ) {
|
|
|
|
char *esc = g_shell_quote ( e->e->name );
|
|
|
|
g_string_append ( res, esc );
|
|
|
|
g_free ( esc );
|
2015-11-23 11:11:12 -05:00
|
|
|
}
|
2016-08-29 15:14:03 -04:00
|
|
|
break;
|
|
|
|
// Invalid, this entry should not be processed -> throw error.
|
|
|
|
default:
|
|
|
|
e->success = FALSE;
|
|
|
|
g_free ( match );
|
|
|
|
return TRUE;
|
2015-11-03 11:34:02 -05:00
|
|
|
}
|
2016-08-29 15:14:03 -04:00
|
|
|
g_free ( match );
|
|
|
|
}
|
|
|
|
// Continue replacement.
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-08-26 15:10:04 -04:00
|
|
|
static void launch_link_entry ( DRunModeEntry *e )
|
|
|
|
{
|
|
|
|
if ( e->key_file == NULL ) {
|
|
|
|
GKeyFile *kf = g_key_file_new ();
|
|
|
|
GError *error = NULL;
|
|
|
|
gboolean res = g_key_file_load_from_file ( kf, e->path, 0, &error );
|
|
|
|
if ( res ) {
|
|
|
|
e->key_file = kf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g_warning ( "[%s] [%s] Failed to parse desktop file because: %s.", e->app_id, e->path, error->message );
|
|
|
|
g_error_free ( error );
|
|
|
|
g_key_file_free ( kf );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *url = g_key_file_get_string ( e->key_file, e->action, "URL", NULL );
|
|
|
|
if ( url == NULL || strlen ( url ) == 0 ) {
|
|
|
|
g_warning ( "[%s] [%s] No URL found.", e->app_id, e->path );
|
|
|
|
g_free ( url );
|
2020-08-28 10:41:59 -04:00
|
|
|
return;
|
2020-08-26 15:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:41:59 -04:00
|
|
|
gsize command_len = strlen ( config.drun_url_launcher ) + strlen ( url ) + 2; // space + terminator = 2
|
|
|
|
gchar *command = g_newa ( gchar, command_len );
|
|
|
|
g_snprintf ( command, command_len, "%s %s", config.drun_url_launcher, url );
|
2020-08-26 15:10:04 -04:00
|
|
|
g_free ( url );
|
|
|
|
|
|
|
|
g_debug ( "Link launch command: |%s|", command );
|
|
|
|
if ( helper_execute_command ( NULL, command, FALSE, NULL ) ) {
|
|
|
|
char *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL );
|
|
|
|
// Store it based on the unique identifiers (desktop_id).
|
|
|
|
history_set ( path, e->desktop_id );
|
|
|
|
g_free ( path );
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 13:28:13 -04:00
|
|
|
static void exec_cmd_entry ( DRunModeEntry *e, const char *path )
|
2016-08-29 15:14:03 -04:00
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2020-04-15 13:06:39 -04:00
|
|
|
GRegex *reg = g_regex_new ( "%[a-zA-Z%]", 0, 0, &error );
|
2016-08-29 15:14:03 -04:00
|
|
|
if ( error != NULL ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Internal error, failed to create regex: %s.", error->message );
|
2016-08-29 15:14:03 -04:00
|
|
|
g_error_free ( error );
|
|
|
|
return;
|
|
|
|
}
|
2021-06-08 13:28:13 -04:00
|
|
|
struct RegexEvalArg earg = { .e = e, .path = path, .success = TRUE };
|
2016-08-29 15:14:03 -04:00
|
|
|
char *str = g_regex_replace_eval ( reg, e->exec, -1, 0, 0, drun_helper_eval_cb, &earg, &error );
|
|
|
|
if ( error != NULL ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Internal error, failed replace field codes: %s.", error->message );
|
2016-08-29 15:14:03 -04:00
|
|
|
g_error_free ( error );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_regex_unref ( reg );
|
|
|
|
if ( earg.success == FALSE ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Invalid field code in Exec line: %s.", e->exec );;
|
2016-08-29 15:14:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( str == NULL ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Nothing to execute after processing: %s.", e->exec );;
|
2016-08-29 15:14:03 -04:00
|
|
|
return;
|
2015-11-03 11:34:02 -05:00
|
|
|
}
|
2020-04-15 13:08:01 -04:00
|
|
|
g_debug ( "Parsed command: |%s| into |%s|.", e->exec, str );
|
2017-07-02 05:33:02 -04:00
|
|
|
|
2020-01-26 06:47:04 -05:00
|
|
|
if ( e->key_file == NULL ) {
|
2020-02-02 07:56:37 -05:00
|
|
|
GKeyFile *kf = g_key_file_new ();
|
2020-01-26 06:47:04 -05:00
|
|
|
GError *error = NULL;
|
|
|
|
gboolean res = g_key_file_load_from_file ( kf, e->path, 0, &error );
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( res ) {
|
2020-01-26 06:47:04 -05:00
|
|
|
e->key_file = kf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g_warning ( "[%s] [%s] Failed to parse desktop file because: %s.", e->app_id, e->path, error->message );
|
|
|
|
g_error_free ( error );
|
|
|
|
g_key_file_free ( kf );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 05:33:02 -04:00
|
|
|
const gchar *fp = g_strstrip ( str );
|
2018-07-28 05:12:11 -04:00
|
|
|
gchar *exec_path = g_key_file_get_string ( e->key_file, e->action, "Path", NULL );
|
2017-01-21 06:58:52 -05:00
|
|
|
if ( exec_path != NULL && strlen ( exec_path ) == 0 ) {
|
2017-01-17 14:00:00 -05:00
|
|
|
// If it is empty, ignore this property. (#529)
|
2017-01-21 06:58:52 -05:00
|
|
|
g_free ( exec_path );
|
2017-01-17 14:00:00 -05:00
|
|
|
exec_path = NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-02 08:56:59 -04:00
|
|
|
RofiHelperExecuteContext context = {
|
2017-06-03 14:35:50 -04:00
|
|
|
.name = e->name,
|
|
|
|
.icon = e->icon_name,
|
2017-06-02 08:56:59 -04:00
|
|
|
.app_id = e->app_id,
|
|
|
|
};
|
2018-07-28 05:12:11 -04:00
|
|
|
gboolean sn = g_key_file_get_boolean ( e->key_file, e->action, "StartupNotify", NULL );
|
2017-06-03 14:35:50 -04:00
|
|
|
gchar *wmclass = NULL;
|
2018-07-28 05:12:11 -04:00
|
|
|
if ( sn && g_key_file_has_key ( e->key_file, e->action, "StartupWMClass", NULL ) ) {
|
|
|
|
context.wmclass = wmclass = g_key_file_get_string ( e->key_file, e->action, "StartupWMClass", NULL );
|
2017-06-02 08:56:59 -04:00
|
|
|
}
|
|
|
|
|
2017-02-02 02:35:33 -05:00
|
|
|
// Returns false if not found, if key not found, we don't want run in terminal.
|
2018-07-28 05:12:11 -04:00
|
|
|
gboolean terminal = g_key_file_get_boolean ( e->key_file, e->action, "Terminal", NULL );
|
2017-06-02 08:56:59 -04:00
|
|
|
if ( helper_execute_command ( exec_path, fp, terminal, sn ? &context : NULL ) ) {
|
2016-02-16 04:40:21 -05:00
|
|
|
char *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL );
|
2017-10-04 02:59:09 -04:00
|
|
|
// Store it based on the unique identifiers (desktop_id).
|
2017-10-05 11:45:50 -04:00
|
|
|
history_set ( path, e->desktop_id );
|
2016-02-16 04:40:21 -05:00
|
|
|
g_free ( path );
|
|
|
|
}
|
2017-06-02 08:56:59 -04:00
|
|
|
g_free ( wmclass );
|
2016-12-26 09:18:52 -05:00
|
|
|
g_free ( exec_path );
|
2015-11-03 11:34:02 -05:00
|
|
|
g_free ( str );
|
|
|
|
}
|
2019-08-09 13:33:23 -04:00
|
|
|
|
|
|
|
static gboolean rofi_strv_contains ( const char * const *categories, const char *const *field )
|
|
|
|
{
|
2020-02-02 07:56:37 -05:00
|
|
|
for ( int i = 0; categories && categories[i]; i++ ) {
|
|
|
|
for ( int j = 0; field[j]; j++ ) {
|
2019-08-09 13:33:23 -04:00
|
|
|
if ( g_str_equal ( categories[i], field[j] ) ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
/**
|
|
|
|
* This function absorbs/freeś path, so this is no longer available afterwards.
|
|
|
|
*/
|
2019-01-29 03:29:47 -05:00
|
|
|
static void read_desktop_file ( DRunModePrivateData *pd, const char *root, const char *path, const gchar *basename, const char *action )
|
2016-02-16 04:40:21 -05:00
|
|
|
{
|
2020-08-26 15:10:04 -04:00
|
|
|
DRunDesktopEntryType desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_UNDETERMINED;
|
2020-08-28 10:41:59 -04:00
|
|
|
int parse_action = ( config.drun_show_actions && action != DRUN_GROUP_NAME );
|
2016-08-30 02:52:02 -04:00
|
|
|
// Create ID on stack.
|
|
|
|
// We know strlen (path ) > strlen(root)+1
|
2016-08-31 04:06:42 -04:00
|
|
|
const ssize_t id_len = strlen ( path ) - strlen ( root );
|
2016-08-30 11:41:30 -04:00
|
|
|
char id[id_len];
|
|
|
|
g_strlcpy ( id, &( path[strlen ( root ) + 1] ), id_len );
|
2016-08-28 16:43:42 -04:00
|
|
|
for ( int index = 0; index < id_len; index++ ) {
|
|
|
|
if ( id[index] == '/' ) {
|
|
|
|
id[index] = '-';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 15:49:46 -04:00
|
|
|
// Check if item is on disabled list.
|
2018-07-28 05:12:11 -04:00
|
|
|
if ( g_hash_table_contains ( pd->disabled_entries, id ) && !parse_action ) {
|
2017-11-17 08:20:03 -05:00
|
|
|
g_debug ( "[%s] [%s] Skipping, was previously seen.", id, path );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
|
|
|
GKeyFile *kf = g_key_file_new ();
|
|
|
|
GError *error = NULL;
|
2017-11-07 14:04:07 -05:00
|
|
|
gboolean res = g_key_file_load_from_file ( kf, path, 0, &error );
|
2016-02-16 04:40:21 -05:00
|
|
|
// If error, skip to next entry
|
2017-11-17 08:20:03 -05:00
|
|
|
if ( !res ) {
|
|
|
|
g_debug ( "[%s] [%s] Failed to parse desktop file because: %s.", id, path, error->message );
|
2016-02-16 04:40:21 -05:00
|
|
|
g_error_free ( error );
|
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
2018-07-28 05:12:11 -04:00
|
|
|
|
|
|
|
if ( g_key_file_has_group ( kf, action ) == FALSE ) {
|
|
|
|
// No type? ignore.
|
|
|
|
g_debug ( "[%s] [%s] Invalid desktop file: No %s group", id, path, action );
|
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2018-07-28 05:12:11 -04:00
|
|
|
}
|
2016-08-29 02:55:28 -04:00
|
|
|
// Skip non Application entries.
|
2017-12-09 13:55:39 -05:00
|
|
|
gchar *key = g_key_file_get_string ( kf, DRUN_GROUP_NAME, "Type", NULL );
|
2016-08-30 11:41:30 -04:00
|
|
|
if ( key == NULL ) {
|
2016-08-29 15:14:03 -04:00
|
|
|
// No type? ignore.
|
2017-11-17 08:20:03 -05:00
|
|
|
g_debug ( "[%s] [%s] Invalid desktop file: No type indicated", id, path );
|
2016-08-29 15:14:03 -04:00
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-08-29 15:14:03 -04:00
|
|
|
}
|
2020-08-26 15:10:04 -04:00
|
|
|
if ( !g_strcmp0 ( key, "Application" ) ) {
|
|
|
|
desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_APPLICATION;
|
2020-08-28 10:41:59 -04:00
|
|
|
}
|
|
|
|
else if ( !g_strcmp0 ( key, "Link" ) ) {
|
2020-08-26 15:10:04 -04:00
|
|
|
desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_LINK;
|
2020-08-28 10:41:59 -04:00
|
|
|
}
|
2021-05-30 17:21:42 -04:00
|
|
|
else if ( !g_strcmp0 ( key, "Service" ) ) {
|
|
|
|
desktop_entry_type = DRUN_DESKTOP_ENTRY_TYPE_SERVICE;
|
2021-06-01 04:44:23 -04:00
|
|
|
g_debug ( "Service file detected." );
|
2021-05-30 17:21:42 -04:00
|
|
|
}
|
2020-08-28 10:41:59 -04:00
|
|
|
else {
|
2020-08-26 15:10:04 -04:00
|
|
|
g_debug ( "[%s] [%s] Skipping desktop file: Not of type Application or Link (%s)", id, path, key );
|
2016-08-30 02:52:02 -04:00
|
|
|
g_free ( key );
|
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-08-30 11:41:30 -04:00
|
|
|
}
|
|
|
|
g_free ( key );
|
2016-08-31 03:39:00 -04:00
|
|
|
|
|
|
|
// Name key is required.
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( !g_key_file_has_key ( kf, DRUN_GROUP_NAME, "Name", NULL ) ) {
|
2017-11-17 08:20:03 -05:00
|
|
|
g_debug ( "[%s] [%s] Invalid desktop file: no 'Name' key present.", id, path );
|
2016-08-31 03:39:00 -04:00
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-08-31 03:39:00 -04:00
|
|
|
}
|
|
|
|
|
2016-02-16 04:40:21 -05:00
|
|
|
// Skip hidden entries.
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( g_key_file_get_boolean ( kf, DRUN_GROUP_NAME, "Hidden", NULL ) ) {
|
2017-11-17 08:20:03 -05:00
|
|
|
g_debug ( "[%s] [%s] Adding desktop file to disabled list: 'Hidden' key is true", id, path );
|
2016-08-30 02:52:02 -04:00
|
|
|
g_key_file_free ( kf );
|
2016-10-31 03:44:04 -04:00
|
|
|
g_hash_table_add ( pd->disabled_entries, g_strdup ( id ) );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
2017-11-02 16:05:07 -04:00
|
|
|
if ( pd->current_desktop_list ) {
|
|
|
|
gboolean show = TRUE;
|
|
|
|
// If the DE is set, check the keys.
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( g_key_file_has_key ( kf, DRUN_GROUP_NAME, "OnlyShowIn", NULL ) ) {
|
2017-11-02 16:05:07 -04:00
|
|
|
gsize llength = 0;
|
|
|
|
show = FALSE;
|
2017-12-09 13:55:39 -05:00
|
|
|
gchar **list = g_key_file_get_string_list ( kf, DRUN_GROUP_NAME, "OnlyShowIn", &llength, NULL );
|
2017-11-07 14:04:07 -05:00
|
|
|
if ( list ) {
|
2017-11-02 16:05:07 -04:00
|
|
|
for ( gsize lcd = 0; !show && pd->current_desktop_list[lcd]; lcd++ ) {
|
|
|
|
for ( gsize lle = 0; !show && lle < llength; lle++ ) {
|
|
|
|
show = ( g_strcmp0 ( pd->current_desktop_list[lcd], list[lle] ) == 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev ( list );
|
|
|
|
}
|
|
|
|
}
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( show && g_key_file_has_key ( kf, DRUN_GROUP_NAME, "NotShowIn", NULL ) ) {
|
2017-11-02 16:05:07 -04:00
|
|
|
gsize llength = 0;
|
2017-12-09 13:55:39 -05:00
|
|
|
gchar **list = g_key_file_get_string_list ( kf, DRUN_GROUP_NAME, "NotShowIn", &llength, NULL );
|
2017-11-07 14:04:07 -05:00
|
|
|
if ( list ) {
|
2017-11-02 16:05:07 -04:00
|
|
|
for ( gsize lcd = 0; show && pd->current_desktop_list[lcd]; lcd++ ) {
|
|
|
|
for ( gsize lle = 0; show && lle < llength; lle++ ) {
|
2017-11-07 14:04:07 -05:00
|
|
|
show = !( g_strcmp0 ( pd->current_desktop_list[lcd], list[lle] ) == 0 );
|
2017-11-02 16:05:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev ( list );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 14:04:07 -05:00
|
|
|
if ( !show ) {
|
2017-11-17 08:20:03 -05:00
|
|
|
g_debug ( "[%s] [%s] Adding desktop file to disabled list: 'OnlyShowIn'/'NotShowIn' keys don't match current desktop", id, path );
|
2017-11-02 16:05:07 -04:00
|
|
|
g_key_file_free ( kf );
|
|
|
|
g_hash_table_add ( pd->disabled_entries, g_strdup ( id ) );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2017-11-02 16:05:07 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
// Skip entries that have NoDisplay set.
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( g_key_file_get_boolean ( kf, DRUN_GROUP_NAME, "NoDisplay", NULL ) ) {
|
2017-11-17 08:20:03 -05:00
|
|
|
g_debug ( "[%s] [%s] Adding desktop file to disabled list: 'NoDisplay' key is true", id, path );
|
2016-08-30 02:52:02 -04:00
|
|
|
g_key_file_free ( kf );
|
2016-10-31 03:44:04 -04:00
|
|
|
g_hash_table_add ( pd->disabled_entries, g_strdup ( id ) );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
2020-08-26 15:10:04 -04:00
|
|
|
|
2016-08-29 02:55:28 -04:00
|
|
|
// We need Exec, don't support DBusActivatable
|
2020-08-26 15:10:04 -04:00
|
|
|
if ( desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION
|
|
|
|
&& !g_key_file_has_key ( kf, DRUN_GROUP_NAME, "Exec", NULL ) ) {
|
|
|
|
g_debug ( "[%s] [%s] Unsupported desktop file: no 'Exec' key present for type Application.", id, path );
|
|
|
|
g_key_file_free ( kf );
|
|
|
|
return;
|
|
|
|
}
|
2021-05-30 17:21:42 -04:00
|
|
|
if ( desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_SERVICE
|
|
|
|
&& !g_key_file_has_key ( kf, DRUN_GROUP_NAME, "Exec", NULL ) ) {
|
|
|
|
g_debug ( "[%s] [%s] Unsupported desktop file: no 'Exec' key present for type Service.", id, path );
|
|
|
|
g_key_file_free ( kf );
|
|
|
|
return;
|
|
|
|
}
|
2020-08-26 15:10:04 -04:00
|
|
|
if ( desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_LINK
|
|
|
|
&& !g_key_file_has_key ( kf, DRUN_GROUP_NAME, "URL", NULL ) ) {
|
|
|
|
g_debug ( "[%s] [%s] Unsupported desktop file: no 'URL' key present for type Link.", id, path );
|
2016-08-29 02:55:28 -04:00
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-08-29 02:55:28 -04:00
|
|
|
}
|
2017-07-04 07:38:26 -04:00
|
|
|
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( g_key_file_has_key ( kf, DRUN_GROUP_NAME, "TryExec", NULL ) ) {
|
|
|
|
char *te = g_key_file_get_string ( kf, DRUN_GROUP_NAME, "TryExec", NULL );
|
2017-07-04 07:38:26 -04:00
|
|
|
if ( !g_path_is_absolute ( te ) ) {
|
|
|
|
char *fp = g_find_program_in_path ( te );
|
|
|
|
if ( fp == NULL ) {
|
|
|
|
g_free ( te );
|
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2017-07-04 07:38:26 -04:00
|
|
|
}
|
|
|
|
g_free ( fp );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( g_file_test ( te, G_FILE_TEST_IS_EXECUTABLE ) == FALSE ) {
|
|
|
|
g_free ( te );
|
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2017-07-04 07:38:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free ( te );
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:33:23 -04:00
|
|
|
char **categories = NULL;
|
|
|
|
if ( pd->show_categories ) {
|
|
|
|
categories = g_key_file_get_locale_string_list ( kf, DRUN_GROUP_NAME, "Categories", NULL, NULL, NULL );
|
2020-08-28 10:41:59 -04:00
|
|
|
if ( !rofi_strv_contains ( (const char * const *) categories, (const char * const *) pd->show_categories ) ) {
|
2020-02-02 07:56:37 -05:00
|
|
|
g_strfreev ( categories );
|
2019-08-09 13:33:23 -04:00
|
|
|
g_key_file_free ( kf );
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2019-08-09 13:33:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 10:51:47 -04:00
|
|
|
size_t nl = ( ( pd->cmd_list_length ) + 1 );
|
2017-02-02 02:35:33 -05:00
|
|
|
if ( nl >= pd->cmd_list_length_actual ) {
|
2017-02-03 14:49:16 -05:00
|
|
|
pd->cmd_list_length_actual += 256;
|
|
|
|
pd->entry_list = g_realloc ( pd->entry_list, pd->cmd_list_length_actual * sizeof ( *( pd->entry_list ) ) );
|
2017-02-02 02:35:33 -05:00
|
|
|
}
|
2017-10-03 11:35:48 -04:00
|
|
|
// Make sure order is preserved, this will break when cmd_list_length is bigger then INT_MAX.
|
|
|
|
// This is not likely to happen.
|
|
|
|
if ( G_UNLIKELY ( pd->cmd_list_length > INT_MAX ) ) {
|
|
|
|
// Default to smallest value.
|
|
|
|
pd->entry_list[pd->cmd_list_length].sort_index = INT_MIN;
|
2017-10-05 11:45:50 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-02-17 14:42:14 -05:00
|
|
|
pd->entry_list[pd->cmd_list_length].sort_index = -nl;
|
2017-10-03 11:35:48 -04:00
|
|
|
}
|
2018-08-08 09:55:13 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].icon_size = 0;
|
2018-06-09 13:13:57 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].icon_fetch_uid = 0;
|
2018-08-08 09:55:13 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].root = g_strdup ( root );
|
|
|
|
pd->entry_list[pd->cmd_list_length].path = g_strdup ( path );
|
|
|
|
pd->entry_list[pd->cmd_list_length].desktop_id = g_strdup ( id );
|
|
|
|
pd->entry_list[pd->cmd_list_length].app_id = g_strndup ( basename, strlen ( basename ) - strlen ( ".desktop" ) );
|
2017-12-09 13:55:39 -05:00
|
|
|
gchar *n = g_key_file_get_locale_string ( kf, DRUN_GROUP_NAME, "Name", NULL, NULL );
|
2018-07-28 05:12:11 -04:00
|
|
|
|
2018-08-08 09:55:13 -04:00
|
|
|
if ( action != DRUN_GROUP_NAME ) {
|
2018-07-28 05:12:11 -04:00
|
|
|
gchar *na = g_key_file_get_locale_string ( kf, action, "Name", NULL, NULL );
|
2018-08-08 09:55:13 -04:00
|
|
|
gchar *l = g_strdup_printf ( "%s - %s", n, na );
|
|
|
|
g_free ( n );
|
2018-07-28 05:12:11 -04:00
|
|
|
n = l;
|
|
|
|
}
|
2018-08-08 09:55:13 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].name = n;
|
2018-07-28 05:12:11 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].action = DRUN_GROUP_NAME;
|
2017-12-09 13:55:39 -05:00
|
|
|
gchar *gn = g_key_file_get_locale_string ( kf, DRUN_GROUP_NAME, "GenericName", NULL, NULL );
|
2016-10-31 10:51:47 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].generic_name = gn;
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match
|
|
|
|
|| matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display) {
|
2020-02-02 07:56:37 -05:00
|
|
|
pd->entry_list[pd->cmd_list_length].keywords = g_key_file_get_locale_string_list ( kf, DRUN_GROUP_NAME, "Keywords", NULL, NULL, NULL );
|
|
|
|
}
|
|
|
|
else {
|
2019-08-20 16:51:07 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].keywords = NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match
|
|
|
|
|| matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_display ) {
|
2019-08-09 13:33:23 -04:00
|
|
|
if ( categories ) {
|
|
|
|
pd->entry_list[pd->cmd_list_length].categories = categories;
|
2020-02-02 07:56:37 -05:00
|
|
|
categories = NULL;
|
|
|
|
}
|
|
|
|
else {
|
2019-08-09 13:33:23 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].categories = g_key_file_get_locale_string_list ( kf, DRUN_GROUP_NAME, "Categories", NULL, NULL, NULL );
|
|
|
|
}
|
2017-12-09 13:55:39 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pd->entry_list[pd->cmd_list_length].categories = NULL;
|
|
|
|
}
|
2020-02-02 07:56:37 -05:00
|
|
|
g_strfreev ( categories );
|
2019-08-09 13:33:23 -04:00
|
|
|
|
2020-08-26 15:10:04 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].type = desktop_entry_type;
|
2021-05-30 17:21:42 -04:00
|
|
|
if ( desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_APPLICATION ||
|
2021-06-01 04:44:23 -04:00
|
|
|
desktop_entry_type == DRUN_DESKTOP_ENTRY_TYPE_SERVICE ) {
|
2020-08-26 15:10:04 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].exec = g_key_file_get_string ( kf, action, "Exec", NULL );
|
2020-08-28 10:41:59 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-08-26 15:10:04 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].exec = NULL;
|
|
|
|
}
|
2016-10-15 14:00:26 -04:00
|
|
|
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match
|
|
|
|
|| matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_display ) {
|
2017-12-09 13:55:39 -05:00
|
|
|
pd->entry_list[pd->cmd_list_length].comment = g_key_file_get_locale_string ( kf,
|
|
|
|
DRUN_GROUP_NAME, "Comment", NULL, NULL );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pd->entry_list[pd->cmd_list_length].comment = NULL;
|
|
|
|
}
|
2021-06-08 19:20:36 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].icon_name = g_key_file_get_locale_string ( kf, DRUN_GROUP_NAME, "Icon", NULL, NULL );
|
2017-05-30 13:07:33 -04:00
|
|
|
pd->entry_list[pd->cmd_list_length].icon = NULL;
|
2017-04-12 12:08:57 -04:00
|
|
|
|
2016-12-26 09:18:52 -05:00
|
|
|
// Keep keyfile around.
|
|
|
|
pd->entry_list[pd->cmd_list_length].key_file = kf;
|
2016-10-31 10:51:47 -04:00
|
|
|
// We don't want to parse items with this id anymore.
|
|
|
|
g_hash_table_add ( pd->disabled_entries, g_strdup ( id ) );
|
2017-11-17 08:20:03 -05:00
|
|
|
g_debug ( "[%s] Using file %s.", id, path );
|
2016-10-31 10:51:47 -04:00
|
|
|
( pd->cmd_list_length )++;
|
2018-07-28 05:12:11 -04:00
|
|
|
|
|
|
|
if ( !parse_action ) {
|
|
|
|
gsize actions_length = 0;
|
2018-08-08 09:55:13 -04:00
|
|
|
char **actions = g_key_file_get_string_list ( kf, DRUN_GROUP_NAME, "Actions", &actions_length, NULL );
|
|
|
|
for ( gsize iter = 0; iter < actions_length; iter++ ) {
|
|
|
|
char *new_action = g_strdup_printf ( "Desktop Action %s", actions[iter] );
|
2019-01-29 03:29:47 -05:00
|
|
|
read_desktop_file ( pd, root, path, basename, new_action );
|
|
|
|
g_free ( new_action );
|
2018-07-28 05:12:11 -04:00
|
|
|
}
|
2018-08-08 09:55:13 -04:00
|
|
|
g_strfreev ( actions );
|
2018-07-28 05:12:11 -04:00
|
|
|
}
|
2020-02-02 07:56:37 -05:00
|
|
|
return;
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
|
|
|
|
2015-11-03 02:31:38 -05:00
|
|
|
/**
|
|
|
|
* Internal spider used to get list of executables.
|
|
|
|
*/
|
2016-08-28 16:43:42 -04:00
|
|
|
static void walk_dir ( DRunModePrivateData *pd, const char *root, const char *dirname )
|
2015-11-03 02:31:38 -05:00
|
|
|
{
|
2016-06-05 13:01:38 -04:00
|
|
|
DIR *dir;
|
2015-11-03 02:31:38 -05:00
|
|
|
|
2017-06-02 12:13:11 -04:00
|
|
|
g_debug ( "Checking directory %s for desktop files.", dirname );
|
2016-06-05 13:01:38 -04:00
|
|
|
dir = opendir ( dirname );
|
|
|
|
if ( dir == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-03 02:31:38 -05:00
|
|
|
|
2016-06-05 13:01:38 -04:00
|
|
|
struct dirent *file;
|
|
|
|
gchar *filename = NULL;
|
|
|
|
struct stat st;
|
|
|
|
while ( ( file = readdir ( dir ) ) != NULL ) {
|
|
|
|
if ( file->d_name[0] == '.' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch ( file->d_type )
|
|
|
|
{
|
|
|
|
case DT_LNK:
|
|
|
|
case DT_REG:
|
|
|
|
case DT_DIR:
|
|
|
|
case DT_UNKNOWN:
|
|
|
|
filename = g_build_filename ( dirname, file->d_name, NULL );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On a link, or if FS does not support providing this information
|
|
|
|
// Fallback to stat method.
|
|
|
|
if ( file->d_type == DT_LNK || file->d_type == DT_UNKNOWN ) {
|
|
|
|
file->d_type = DT_UNKNOWN;
|
|
|
|
if ( stat ( filename, &st ) == 0 ) {
|
|
|
|
if ( S_ISDIR ( st.st_mode ) ) {
|
|
|
|
file->d_type = DT_DIR;
|
|
|
|
}
|
|
|
|
else if ( S_ISREG ( st.st_mode ) ) {
|
|
|
|
file->d_type = DT_REG;
|
|
|
|
}
|
2015-11-03 11:34:02 -05:00
|
|
|
}
|
2015-11-03 02:31:38 -05:00
|
|
|
}
|
|
|
|
|
2016-06-05 13:01:38 -04:00
|
|
|
switch ( file->d_type )
|
|
|
|
{
|
|
|
|
case DT_REG:
|
2016-10-31 03:44:04 -04:00
|
|
|
// Skip files not ending on .desktop.
|
|
|
|
if ( g_str_has_suffix ( file->d_name, ".desktop" ) ) {
|
2018-07-28 05:12:11 -04:00
|
|
|
read_desktop_file ( pd, root, filename, file->d_name, DRUN_GROUP_NAME );
|
2016-10-31 03:44:04 -04:00
|
|
|
}
|
2016-06-05 13:01:38 -04:00
|
|
|
break;
|
|
|
|
case DT_DIR:
|
2016-08-28 16:43:42 -04:00
|
|
|
walk_dir ( pd, root, filename );
|
2016-06-05 13:01:38 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_free ( filename );
|
2015-11-03 02:31:38 -05:00
|
|
|
}
|
2016-06-05 13:01:38 -04:00
|
|
|
closedir ( dir );
|
2015-11-03 02:31:38 -05:00
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
/**
|
2016-06-01 01:34:41 -04:00
|
|
|
* @param entry The command entry to remove from history
|
2016-02-16 04:40:21 -05:00
|
|
|
*
|
|
|
|
* Remove command from history.
|
|
|
|
*/
|
|
|
|
static void delete_entry_history ( const DRunModeEntry *entry )
|
|
|
|
{
|
|
|
|
char *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL );
|
2017-10-04 02:59:09 -04:00
|
|
|
history_remove ( path, entry->desktop_id );
|
2016-02-16 04:40:21 -05:00
|
|
|
g_free ( path );
|
|
|
|
}
|
2016-06-05 13:01:38 -04:00
|
|
|
|
2016-02-16 04:40:21 -05:00
|
|
|
static void get_apps_history ( DRunModePrivateData *pd )
|
|
|
|
{
|
2017-10-03 11:35:48 -04:00
|
|
|
TICK_N ( "Start drun history" );
|
2016-02-16 04:40:21 -05:00
|
|
|
unsigned int length = 0;
|
|
|
|
gchar *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL );
|
|
|
|
gchar **retv = history_get_list ( path, &length );
|
|
|
|
for ( unsigned int index = 0; index < length; index++ ) {
|
2017-10-03 11:35:48 -04:00
|
|
|
for ( size_t i = 0; i < pd->cmd_list_length; i++ ) {
|
2017-10-04 02:59:09 -04:00
|
|
|
if ( g_strcmp0 ( pd->entry_list[i].desktop_id, retv[index] ) == 0 ) {
|
2017-10-05 11:45:50 -04:00
|
|
|
unsigned int sort_index = length - index;
|
2017-10-03 11:35:48 -04:00
|
|
|
if ( G_LIKELY ( sort_index < INT_MAX ) ) {
|
|
|
|
pd->entry_list[i].sort_index = sort_index;
|
2017-10-05 11:45:50 -04:00
|
|
|
}
|
|
|
|
else {
|
2017-10-03 11:35:48 -04:00
|
|
|
// This won't sort right anymore, but never gonna hit it anyway.
|
|
|
|
pd->entry_list[i].sort_index = INT_MAX;
|
|
|
|
}
|
2017-03-21 12:19:05 -04:00
|
|
|
}
|
2016-08-28 16:43:42 -04:00
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
2016-10-31 12:02:10 -04:00
|
|
|
g_strfreev ( retv );
|
2017-03-21 12:19:05 -04:00
|
|
|
g_free ( path );
|
2017-10-03 11:35:48 -04:00
|
|
|
TICK_N ( "Stop drun history" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint drun_int_sort_list ( gconstpointer a, gconstpointer b, G_GNUC_UNUSED gpointer user_data )
|
|
|
|
{
|
2017-10-05 11:45:50 -04:00
|
|
|
DRunModeEntry *da = (DRunModeEntry *) a;
|
|
|
|
DRunModeEntry *db = (DRunModeEntry *) b;
|
2017-10-03 11:35:48 -04:00
|
|
|
|
2019-02-10 08:15:35 -05:00
|
|
|
if ( da->sort_index < 0 && db->sort_index < 0 ) {
|
2021-05-22 18:31:38 -04:00
|
|
|
if ( da->name == NULL && db->name == NULL ) {
|
|
|
|
return 0;
|
2021-06-01 04:44:23 -04:00
|
|
|
}
|
|
|
|
else if ( da->name == NULL ) {
|
2021-05-22 18:31:38 -04:00
|
|
|
return -1;
|
2021-06-01 04:44:23 -04:00
|
|
|
}
|
|
|
|
else if ( db->name == NULL ) {
|
2021-05-22 18:31:38 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2019-02-10 08:15:35 -05:00
|
|
|
return g_utf8_collate ( da->name, db->name );
|
2020-02-02 07:56:37 -05:00
|
|
|
}
|
|
|
|
else {
|
2019-02-10 08:15:35 -05:00
|
|
|
return db->sort_index - da->sort_index;
|
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
2016-06-05 13:01:38 -04:00
|
|
|
|
2020-01-26 06:47:04 -05:00
|
|
|
/*******************************************
|
2020-02-02 07:56:37 -05:00
|
|
|
* Cache voodoo *
|
|
|
|
*******************************************/
|
2020-01-26 06:47:04 -05:00
|
|
|
|
2020-09-09 16:46:18 -04:00
|
|
|
#define CACHE_VERSION 2
|
2020-01-26 06:47:04 -05:00
|
|
|
static void drun_write_str ( FILE *fd, const char *str )
|
2015-11-03 02:31:38 -05:00
|
|
|
{
|
2020-02-02 07:56:37 -05:00
|
|
|
size_t l = ( str == NULL ? 0 : strlen ( str ) );
|
|
|
|
fwrite ( &l, sizeof ( l ), 1, fd );
|
2020-01-26 06:47:04 -05:00
|
|
|
// Only write string if it is not NULL or empty.
|
|
|
|
if ( l > 0 ) {
|
|
|
|
// Also writeout terminating '\0'
|
2020-02-02 07:56:37 -05:00
|
|
|
fwrite ( str, 1, l + 1, fd );
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-09 16:46:18 -04:00
|
|
|
static void drun_write_integer ( FILE *fd, int32_t val )
|
|
|
|
{
|
2020-11-03 17:57:02 -05:00
|
|
|
fwrite ( &val, sizeof ( val ), 1, fd );
|
2020-09-09 16:46:18 -04:00
|
|
|
}
|
|
|
|
static void drun_read_integer ( FILE *fd, int32_t *type )
|
|
|
|
{
|
2020-11-03 17:57:02 -05:00
|
|
|
if ( fread ( type, sizeof ( int32_t ), 1, fd ) != 1 ) {
|
2020-09-09 16:46:18 -04:00
|
|
|
g_warning ( "Failed to read entry, cache corrupt?" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 06:47:04 -05:00
|
|
|
static void drun_read_string ( FILE *fd, char **str )
|
|
|
|
{
|
|
|
|
size_t l = 0;
|
2015-11-03 11:34:02 -05:00
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( fread ( &l, sizeof ( l ), 1, fd ) != 1 ) {
|
|
|
|
g_warning ( "Failed to read entry, cache corrupt?" );
|
2020-01-26 06:47:04 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-02-02 07:56:37 -05:00
|
|
|
( *str ) = NULL;
|
2020-01-26 06:47:04 -05:00
|
|
|
if ( l > 0 ) {
|
|
|
|
// Include \0
|
|
|
|
l++;
|
2020-02-02 07:56:37 -05:00
|
|
|
( *str ) = g_malloc ( l );
|
|
|
|
if ( fread ( ( *str ), 1, l, fd ) != l ) {
|
|
|
|
g_warning ( "Failed to read entry, cache corrupt?" );
|
2017-06-02 12:13:11 -04:00
|
|
|
}
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static void drun_write_strv ( FILE *fd, char **str )
|
|
|
|
{
|
2020-02-02 07:56:37 -05:00
|
|
|
guint vl = ( str == NULL ? 0 : g_strv_length ( str ) );
|
|
|
|
fwrite ( &vl, sizeof ( vl ), 1, fd );
|
|
|
|
for ( guint index = 0; index < vl; index++ ) {
|
2020-01-26 06:47:04 -05:00
|
|
|
drun_write_str ( fd, str[index] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void drun_read_stringv ( FILE *fd, char ***str )
|
|
|
|
{
|
|
|
|
guint vl = 0;
|
2020-02-02 07:56:37 -05:00
|
|
|
( *str ) = NULL;
|
|
|
|
if ( fread ( &vl, sizeof ( vl ), 1, fd ) != 1 ) {
|
|
|
|
g_warning ( "Failed to read entry, cache corrupt?" );
|
2020-01-26 06:47:04 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( vl > 0 ) {
|
2020-01-26 06:47:04 -05:00
|
|
|
// Include terminating NULL entry.
|
2020-02-02 07:56:37 -05:00
|
|
|
( *str ) = g_malloc0 ( ( vl + 1 ) * sizeof ( **str ) );
|
2020-01-26 06:47:04 -05:00
|
|
|
for ( guint index = 0; index < vl; index++ ) {
|
2020-02-02 07:56:37 -05:00
|
|
|
drun_read_string ( fd, &( ( *str )[index] ) );
|
2017-06-02 12:13:11 -04:00
|
|
|
}
|
2015-11-03 11:34:02 -05:00
|
|
|
}
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_cache ( DRunModePrivateData *pd, const char *cache_file )
|
|
|
|
{
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( cache_file == NULL || config.drun_use_desktop_cache == FALSE ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 06:47:04 -05:00
|
|
|
TICK_N ( "DRUN Write CACHE: start" );
|
2017-10-03 11:35:48 -04:00
|
|
|
|
2020-01-26 06:47:04 -05:00
|
|
|
FILE *fd = fopen ( cache_file, "w" );
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( fd == NULL ) {
|
2020-01-26 06:47:04 -05:00
|
|
|
g_warning ( "Failed to write to cache file" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t version = CACHE_VERSION;
|
2020-02-02 07:56:37 -05:00
|
|
|
fwrite ( &version, sizeof ( version ), 1, fd );
|
2017-10-03 11:35:48 -04:00
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
fwrite ( &( pd->cmd_list_length ), sizeof ( pd->cmd_list_length ), 1, fd );
|
|
|
|
for ( unsigned int index = 0; index < pd->cmd_list_length; index++ ) {
|
|
|
|
DRunModeEntry *entry = &( pd->entry_list[index] );
|
2020-01-26 06:47:04 -05:00
|
|
|
|
|
|
|
drun_write_str ( fd, entry->action );
|
|
|
|
drun_write_str ( fd, entry->root );
|
|
|
|
drun_write_str ( fd, entry->path );
|
|
|
|
drun_write_str ( fd, entry->app_id );
|
|
|
|
drun_write_str ( fd, entry->desktop_id );
|
|
|
|
drun_write_str ( fd, entry->icon_name );
|
|
|
|
drun_write_str ( fd, entry->exec );
|
|
|
|
drun_write_str ( fd, entry->name );
|
|
|
|
drun_write_str ( fd, entry->generic_name );
|
|
|
|
|
|
|
|
drun_write_strv ( fd, entry->categories );
|
|
|
|
drun_write_strv ( fd, entry->keywords );
|
|
|
|
|
|
|
|
drun_write_str ( fd, entry->comment );
|
2020-11-03 17:57:02 -05:00
|
|
|
drun_write_integer ( fd, (int32_t) entry->type );
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
fclose ( fd );
|
2020-01-26 06:47:04 -05:00
|
|
|
TICK_N ( "DRUN Write CACHE: end" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read cache file. returns FALSE when success.
|
|
|
|
*/
|
|
|
|
static gboolean drun_read_cache ( DRunModePrivateData *pd, const char *cache_file )
|
|
|
|
{
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( cache_file == NULL || config.drun_use_desktop_cache == FALSE ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2020-01-26 06:47:04 -05:00
|
|
|
|
|
|
|
if ( config.drun_reload_desktop_cache ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
TICK_N ( "DRUN Read CACHE: start" );
|
|
|
|
FILE *fd = fopen ( cache_file, "r" );
|
|
|
|
if ( fd == NULL ) {
|
|
|
|
TICK_N ( "DRUN Read CACHE: stop" );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read version.
|
|
|
|
uint8_t version = 0;
|
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( fread ( &version, sizeof ( version ), 1, fd ) != 1 ) {
|
2020-01-26 06:47:04 -05:00
|
|
|
fclose ( fd );
|
|
|
|
g_warning ( "Cache corrupt, ignoring." );
|
|
|
|
TICK_N ( "DRUN Read CACHE: stop" );
|
2020-09-09 16:46:18 -04:00
|
|
|
return TRUE;
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( version != CACHE_VERSION ) {
|
|
|
|
fclose ( fd );
|
|
|
|
g_warning ( "Cache file wrong version, ignoring." );
|
|
|
|
TICK_N ( "DRUN Read CACHE: stop" );
|
2020-09-09 16:46:18 -04:00
|
|
|
return TRUE;
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( fread ( &( pd->cmd_list_length ), sizeof ( pd->cmd_list_length ), 1, fd ) != 1 ) {
|
2020-01-26 06:47:04 -05:00
|
|
|
fclose ( fd );
|
|
|
|
g_warning ( "Cache corrupt, ignoring." );
|
|
|
|
TICK_N ( "DRUN Read CACHE: stop" );
|
2020-09-09 16:46:18 -04:00
|
|
|
return TRUE;
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
// set actual length to length;
|
|
|
|
pd->cmd_list_length_actual = pd->cmd_list_length;
|
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
pd->entry_list = g_malloc0 ( pd->cmd_list_length_actual * sizeof ( *( pd->entry_list ) ) );
|
2020-01-26 06:47:04 -05:00
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
for ( unsigned int index = 0; index < pd->cmd_list_length; index++ ) {
|
|
|
|
DRunModeEntry *entry = &( pd->entry_list[index] );
|
2020-01-26 06:47:04 -05:00
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
drun_read_string ( fd, &( entry->action ) );
|
|
|
|
drun_read_string ( fd, &( entry->root ) );
|
|
|
|
drun_read_string ( fd, &( entry->path ) );
|
|
|
|
drun_read_string ( fd, &( entry->app_id ) );
|
|
|
|
drun_read_string ( fd, &( entry->desktop_id ) );
|
|
|
|
drun_read_string ( fd, &( entry->icon_name ) );
|
|
|
|
drun_read_string ( fd, &( entry->exec ) );
|
|
|
|
drun_read_string ( fd, &( entry->name ) );
|
|
|
|
drun_read_string ( fd, &( entry->generic_name ) );
|
2020-01-26 06:47:04 -05:00
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
drun_read_stringv ( fd, &( entry->categories ) );
|
|
|
|
drun_read_stringv ( fd, &( entry->keywords ) );
|
2020-01-26 06:47:04 -05:00
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
drun_read_string ( fd, &( entry->comment ) );
|
2020-09-09 16:46:18 -04:00
|
|
|
int32_t type = 0;
|
2020-11-03 17:57:02 -05:00
|
|
|
drun_read_integer ( fd, &( type ) );
|
2020-09-09 16:46:18 -04:00
|
|
|
entry->type = type;
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose ( fd );
|
|
|
|
TICK_N ( "DRUN Read CACHE: stop" );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_apps ( DRunModePrivateData *pd )
|
|
|
|
{
|
|
|
|
char *cache_file = g_build_filename ( cache_dir, DRUN_DESKTOP_CACHE_FILE, NULL );
|
|
|
|
TICK_N ( "Get Desktop apps (start)" );
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( drun_read_cache ( pd, cache_file ) ) {
|
2021-06-15 10:19:57 -04:00
|
|
|
ThemeWidget *wid = rofi_config_find_widget ( drun_mode.name, NULL, TRUE );
|
|
|
|
|
|
|
|
/** Load user entires */
|
|
|
|
Property *p = rofi_theme_find_property ( wid, P_BOOLEAN, "parse-user", TRUE );
|
|
|
|
if ( p == NULL || ( p->type == P_BOOLEAN && p->value.b )) {
|
|
|
|
gchar *dir;
|
|
|
|
// First read the user directory.
|
|
|
|
dir = g_build_filename ( g_get_user_data_dir (), "applications", NULL );
|
|
|
|
walk_dir ( pd, dir, dir );
|
|
|
|
g_free ( dir );
|
|
|
|
TICK_N ( "Get Desktop apps (user dir)" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Load application entires */
|
|
|
|
p = rofi_theme_find_property ( wid, P_BOOLEAN, "parse-system", TRUE );
|
|
|
|
if ( p == NULL || ( p->type == P_BOOLEAN && p->value.b )) {
|
|
|
|
// Then read thee system data dirs.
|
|
|
|
const gchar * const * sys = g_get_system_data_dirs ();
|
|
|
|
for ( const gchar * const *iter = sys; *iter != NULL; ++iter ) {
|
2020-01-26 06:47:04 -05:00
|
|
|
gboolean unique = TRUE;
|
|
|
|
// Stupid duplicate detection, better then walking dir.
|
|
|
|
for ( const gchar *const *iterd = sys; iterd != iter; ++iterd ) {
|
2021-06-15 10:19:57 -04:00
|
|
|
if ( g_strcmp0 ( *iter, *iterd ) == 0 ) {
|
|
|
|
unique = FALSE;
|
|
|
|
}
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
// Check, we seem to be getting empty string...
|
|
|
|
if ( unique && ( **iter ) != '\0' ) {
|
2021-06-15 10:19:57 -04:00
|
|
|
char *dir = g_build_filename ( *iter, "applications", NULL );
|
|
|
|
walk_dir ( pd, dir, dir );
|
|
|
|
g_free ( dir );
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
2021-06-15 10:19:57 -04:00
|
|
|
}
|
|
|
|
TICK_N ( "Get Desktop apps (system dirs)" );
|
2020-01-26 06:47:04 -05:00
|
|
|
}
|
|
|
|
get_apps_history ( pd );
|
|
|
|
|
|
|
|
g_qsort_with_data ( pd->entry_list, pd->cmd_list_length, sizeof ( DRunModeEntry ), drun_int_sort_list, NULL );
|
|
|
|
|
|
|
|
TICK_N ( "Sorting done." );
|
|
|
|
|
|
|
|
write_cache ( pd, cache_file );
|
|
|
|
}
|
|
|
|
g_free ( cache_file );
|
2015-11-03 11:34:02 -05:00
|
|
|
}
|
2015-11-03 02:31:38 -05:00
|
|
|
|
2017-10-21 05:18:52 -04:00
|
|
|
static void drun_mode_parse_entry_fields ()
|
|
|
|
{
|
|
|
|
char *savept = NULL;
|
|
|
|
// Make a copy, as strtok will modify it.
|
|
|
|
char *switcher_str = g_strdup ( config.drun_match_fields );
|
|
|
|
const char * const sep = ",#";
|
|
|
|
// Split token on ','. This modifies switcher_str.
|
|
|
|
for ( unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++ ) {
|
2021-06-15 08:32:46 -04:00
|
|
|
matching_entry_fields[i].enabled_match = FALSE;
|
|
|
|
matching_entry_fields[i].enabled_display = FALSE;
|
2017-10-21 05:18:52 -04:00
|
|
|
}
|
|
|
|
for ( char *token = strtok_r ( switcher_str, sep, &savept ); token != NULL;
|
|
|
|
token = strtok_r ( NULL, sep, &savept ) ) {
|
|
|
|
if ( strcmp ( token, "all" ) == 0 ) {
|
|
|
|
for ( unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++ ) {
|
2021-06-15 08:32:46 -04:00
|
|
|
matching_entry_fields[i].enabled_match = TRUE;
|
|
|
|
matching_entry_fields[i].enabled_display = TRUE;
|
2017-10-21 05:18:52 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gboolean matched = FALSE;
|
|
|
|
for ( unsigned int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++ ) {
|
|
|
|
const char * entry_name = matching_entry_fields[i].entry_field_name;
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( g_ascii_strcasecmp ( token, entry_name ) == 0 ) {
|
2021-06-15 08:32:46 -04:00
|
|
|
matching_entry_fields[i].enabled_match = TRUE;
|
|
|
|
matching_entry_fields[i].enabled_display = TRUE;
|
2017-10-21 05:18:52 -04:00
|
|
|
matched = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !matched ) {
|
|
|
|
g_warning ( "Invalid entry name :%s", token );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Free string that was modified by strtok_r
|
|
|
|
g_free ( switcher_str );
|
|
|
|
}
|
|
|
|
|
2021-06-15 08:32:46 -04:00
|
|
|
static void drun_mode_parse_display_format() {
|
|
|
|
for (int i = 0; i < DRUN_MATCH_NUM_FIELDS; i++) {
|
|
|
|
if ( matching_entry_fields[i].enabled_display ) continue;
|
|
|
|
|
|
|
|
gchar* search_term = g_strdup_printf("{%s}",matching_entry_fields[i].entry_field_name);
|
|
|
|
if ( strstr( config.drun_display_format, search_term) ) {
|
|
|
|
matching_entry_fields[i].enabled_match = TRUE;
|
|
|
|
}
|
|
|
|
g_free( search_term );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 03:16:59 -05:00
|
|
|
static int drun_mode_init ( Mode *sw )
|
2015-11-03 02:31:38 -05:00
|
|
|
{
|
2017-12-09 13:55:39 -05:00
|
|
|
if ( mode_get_private_data ( sw ) != NULL ) {
|
2017-11-25 05:51:19 -05:00
|
|
|
return TRUE;
|
2017-12-09 13:55:39 -05:00
|
|
|
}
|
2018-08-08 09:55:13 -04:00
|
|
|
DRunModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
2017-11-25 05:51:19 -05:00
|
|
|
pd->disabled_entries = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, NULL );
|
|
|
|
mode_set_private_data ( sw, (void *) pd );
|
|
|
|
// current destkop
|
|
|
|
const char *current_desktop = g_getenv ( "XDG_CURRENT_DESKTOP" );
|
|
|
|
pd->current_desktop_list = current_desktop ? g_strsplit ( current_desktop, ":", 0 ) : NULL;
|
|
|
|
|
2020-02-02 07:56:37 -05:00
|
|
|
if ( config.drun_categories && *( config.drun_categories ) ) {
|
|
|
|
pd->show_categories = g_strsplit ( config.drun_categories, ",", 0 );
|
2019-08-09 13:33:23 -04:00
|
|
|
}
|
|
|
|
|
2017-11-25 05:51:19 -05:00
|
|
|
drun_mode_parse_entry_fields ();
|
2021-06-15 08:32:46 -04:00
|
|
|
drun_mode_parse_display_format();
|
2017-12-09 13:55:39 -05:00
|
|
|
get_apps ( pd );
|
2021-06-08 13:28:13 -04:00
|
|
|
|
|
|
|
pd->completer = create_new_file_browser ();
|
|
|
|
mode_init ( pd->completer );
|
2016-01-08 03:16:59 -05:00
|
|
|
return TRUE;
|
2015-11-03 02:31:38 -05:00
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
static void drun_entry_clear ( DRunModeEntry *e )
|
|
|
|
{
|
2016-08-28 16:43:42 -04:00
|
|
|
g_free ( e->root );
|
2016-02-16 04:40:21 -05:00
|
|
|
g_free ( e->path );
|
2017-06-02 08:55:36 -04:00
|
|
|
g_free ( e->app_id );
|
2017-10-04 02:59:09 -04:00
|
|
|
g_free ( e->desktop_id );
|
2017-04-12 12:08:57 -04:00
|
|
|
if ( e->icon != NULL ) {
|
|
|
|
cairo_surface_destroy ( e->icon );
|
|
|
|
}
|
|
|
|
g_free ( e->icon_name );
|
2016-02-16 04:40:21 -05:00
|
|
|
g_free ( e->exec );
|
|
|
|
g_free ( e->name );
|
|
|
|
g_free ( e->generic_name );
|
2017-12-09 13:55:39 -05:00
|
|
|
g_free ( e->comment );
|
2018-07-28 05:12:11 -04:00
|
|
|
if ( e->action != DRUN_GROUP_NAME ) {
|
|
|
|
g_free ( e->action );
|
|
|
|
}
|
2017-03-14 12:34:41 -04:00
|
|
|
g_strfreev ( e->categories );
|
2019-08-20 16:51:07 -04:00
|
|
|
g_strfreev ( e->keywords );
|
2020-01-26 06:47:04 -05:00
|
|
|
if ( e->key_file ) {
|
|
|
|
g_key_file_free ( e->key_file );
|
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
2015-11-03 02:31:38 -05:00
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static ModeMode drun_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
2015-11-03 02:31:38 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw );
|
2015-11-25 03:26:38 -05:00
|
|
|
ModeMode retv = MODE_EXIT;
|
2015-11-03 02:31:38 -05:00
|
|
|
|
2021-06-08 13:28:13 -04:00
|
|
|
if ( rmpd->file_complete == TRUE ) {
|
|
|
|
|
|
|
|
retv = RELOAD_DIALOG;
|
|
|
|
|
|
|
|
if ( ( mretv& (MENU_COMPLETE)) ) {
|
|
|
|
g_free ( rmpd->old_completer_input );
|
|
|
|
rmpd->old_completer_input = *input;
|
|
|
|
*input = NULL;
|
|
|
|
if ( rmpd->selected_line < rmpd->cmd_list_length ) {
|
|
|
|
(*input) = g_strdup ( rmpd->old_input );
|
|
|
|
}
|
|
|
|
rmpd->file_complete = FALSE;
|
|
|
|
} else if ( (mretv&MENU_CANCEL) ) {
|
|
|
|
retv = MODE_EXIT;
|
|
|
|
} else {
|
|
|
|
char *path = NULL;
|
|
|
|
retv = file_browser_mode_completer ( rmpd->completer, mretv, input, selected_line, &path );
|
|
|
|
if ( retv == MODE_EXIT ) {
|
|
|
|
exec_cmd_entry ( &( rmpd->entry_list[rmpd->selected_line] ), path );
|
|
|
|
|
|
|
|
}
|
|
|
|
g_free (path);
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
2020-09-08 11:30:03 -04:00
|
|
|
if ( ( mretv & MENU_OK ) ) {
|
2020-08-28 10:41:59 -04:00
|
|
|
switch ( rmpd->entry_list[selected_line].type )
|
|
|
|
{
|
2021-05-30 17:21:42 -04:00
|
|
|
case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
|
2020-08-28 10:41:59 -04:00
|
|
|
case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION:
|
2021-06-08 13:28:13 -04:00
|
|
|
exec_cmd_entry ( &( rmpd->entry_list[selected_line] ), NULL );
|
2020-08-28 10:41:59 -04:00
|
|
|
break;
|
|
|
|
case DRUN_DESKTOP_ENTRY_TYPE_LINK:
|
|
|
|
launch_link_entry ( &( rmpd->entry_list[selected_line] ) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2020-08-26 15:10:04 -04:00
|
|
|
}
|
2015-11-03 02:31:38 -05:00
|
|
|
}
|
|
|
|
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
2020-11-03 17:57:02 -05:00
|
|
|
RofiHelperExecuteContext context = { .name = NULL };
|
|
|
|
gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
|
2020-09-13 15:56:25 -04:00
|
|
|
// FIXME: We assume startup notification in terminals, not in others
|
2020-11-03 17:57:02 -05:00
|
|
|
if ( !helper_execute_command ( NULL, *input, run_in_term, run_in_term ? &context : NULL ) ) {
|
2020-09-13 15:56:25 -04:00
|
|
|
retv = RELOAD_DIALOG;
|
|
|
|
}
|
2015-11-03 02:31:38 -05:00
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
else if ( ( mretv & MENU_ENTRY_DELETE ) && selected_line < rmpd->cmd_list_length ) {
|
2017-10-03 11:35:48 -04:00
|
|
|
// Possitive sort index means it is in history.
|
|
|
|
if ( rmpd->entry_list[selected_line].sort_index >= 0 ) {
|
2016-02-16 04:40:21 -05:00
|
|
|
delete_entry_history ( &( rmpd->entry_list[selected_line] ) );
|
|
|
|
drun_entry_clear ( &( rmpd->entry_list[selected_line] ) );
|
|
|
|
memmove ( &( rmpd->entry_list[selected_line] ), &rmpd->entry_list[selected_line + 1],
|
|
|
|
sizeof ( DRunModeEntry ) * ( rmpd->cmd_list_length - selected_line - 1 ) );
|
|
|
|
rmpd->cmd_list_length--;
|
|
|
|
}
|
|
|
|
retv = RELOAD_DIALOG;
|
2021-06-01 04:44:23 -04:00
|
|
|
}
|
|
|
|
else if ( mretv & MENU_CUSTOM_COMMAND ) {
|
2021-04-10 17:04:05 -04:00
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2021-06-08 13:28:13 -04:00
|
|
|
} else if ( ( mretv& MENU_COMPLETE) ) {
|
|
|
|
retv = RELOAD_DIALOG;
|
|
|
|
if ( selected_line < rmpd->cmd_list_length ) {
|
|
|
|
switch ( rmpd->entry_list[selected_line].type )
|
|
|
|
{
|
|
|
|
case DRUN_DESKTOP_ENTRY_TYPE_SERVICE:
|
|
|
|
case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION:
|
|
|
|
{
|
2021-06-08 13:35:51 -04:00
|
|
|
GRegex *regex = g_regex_new ("%[fFuU]", 0, 0, NULL);
|
2021-06-08 13:28:13 -04:00
|
|
|
|
2021-06-08 13:35:51 -04:00
|
|
|
if (g_regex_match (regex, rmpd->entry_list[selected_line].exec, 0, NULL) ) {
|
|
|
|
rmpd->selected_line = selected_line;
|
|
|
|
// TODO add check if it supports passing file.
|
2021-06-08 13:28:13 -04:00
|
|
|
|
2021-06-08 13:35:51 -04:00
|
|
|
g_free ( rmpd->old_input );
|
|
|
|
rmpd->old_input = g_strdup ( *input );
|
2021-06-08 13:28:13 -04:00
|
|
|
|
2021-06-08 13:35:51 -04:00
|
|
|
if ( *input ) g_free (*input);
|
|
|
|
*input = g_strdup ( rmpd->old_completer_input );
|
|
|
|
|
|
|
|
rmpd->file_complete = TRUE;
|
|
|
|
}
|
|
|
|
g_regex_unref ( regex );
|
2021-06-08 13:28:13 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 04:40:21 -05:00
|
|
|
}
|
2015-11-03 02:31:38 -05:00
|
|
|
return retv;
|
|
|
|
}
|
2015-11-25 03:26:38 -05:00
|
|
|
static void drun_mode_destroy ( Mode *sw )
|
2015-11-03 02:31:38 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw );
|
2015-11-03 02:31:38 -05:00
|
|
|
if ( rmpd != NULL ) {
|
2015-11-03 11:34:02 -05:00
|
|
|
for ( size_t i = 0; i < rmpd->cmd_list_length; i++ ) {
|
2016-02-16 04:40:21 -05:00
|
|
|
drun_entry_clear ( &( rmpd->entry_list[i] ) );
|
2015-11-03 11:34:02 -05:00
|
|
|
}
|
2016-10-31 03:44:04 -04:00
|
|
|
g_hash_table_destroy ( rmpd->disabled_entries );
|
2015-11-03 11:34:02 -05:00
|
|
|
g_free ( rmpd->entry_list );
|
2017-11-02 16:05:07 -04:00
|
|
|
|
2021-06-08 13:28:13 -04:00
|
|
|
g_free ( rmpd->old_completer_input );
|
|
|
|
g_free ( rmpd->old_input );
|
|
|
|
mode_destroy ( rmpd->completer );
|
|
|
|
|
2017-11-02 16:05:07 -04:00
|
|
|
g_strfreev ( rmpd->current_desktop_list );
|
2019-08-09 13:33:23 -04:00
|
|
|
g_strfreev ( rmpd->show_categories );
|
2015-11-03 02:31:38 -05:00
|
|
|
g_free ( rmpd );
|
2016-01-07 15:27:20 -05:00
|
|
|
mode_set_private_data ( sw, NULL );
|
2015-11-03 02:31:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:39:29 -05:00
|
|
|
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, int *state, G_GNUC_UNUSED GList **list, int get_entry )
|
2015-11-03 02:31:38 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
DRunModePrivateData *pd = (DRunModePrivateData *) mode_get_private_data ( sw );
|
2021-06-08 13:28:13 -04:00
|
|
|
|
|
|
|
if ( pd->file_complete ){
|
|
|
|
return pd->completer->_get_display_value (pd->completer, selected_line, state, list, get_entry );
|
|
|
|
}
|
2015-11-03 11:34:02 -05:00
|
|
|
*state |= MARKUP;
|
2015-11-21 17:59:59 -05:00
|
|
|
if ( !get_entry ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ( pd->entry_list == NULL ) {
|
|
|
|
// Should never get here.
|
|
|
|
return g_strdup ( "Failed" );
|
|
|
|
}
|
|
|
|
/* Free temp storage. */
|
2020-02-02 07:56:37 -05:00
|
|
|
DRunModeEntry *dr = &( pd->entry_list[selected_line] );
|
|
|
|
gchar *cats = NULL;
|
|
|
|
if ( dr->categories ) {
|
|
|
|
char *tcats = g_strjoinv ( ",", dr->categories );
|
2019-02-10 08:15:35 -05:00
|
|
|
if ( tcats ) {
|
|
|
|
cats = g_markup_escape_text ( tcats, -1 );
|
2020-02-02 07:56:37 -05:00
|
|
|
g_free ( tcats );
|
2019-02-10 08:15:35 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 07:56:37 -05:00
|
|
|
gchar *keywords = NULL;
|
|
|
|
if ( dr->keywords ) {
|
|
|
|
char *tkeyw = g_strjoinv ( ",", dr->keywords );
|
2019-08-20 16:51:07 -04:00
|
|
|
if ( tkeyw ) {
|
|
|
|
keywords = g_markup_escape_text ( tkeyw, -1 );
|
2020-02-02 07:56:37 -05:00
|
|
|
g_free ( tkeyw );
|
2019-08-20 16:51:07 -04:00
|
|
|
}
|
|
|
|
}
|
2019-02-10 08:15:35 -05:00
|
|
|
// Needed for display.
|
|
|
|
char *egn = NULL;
|
2020-02-02 07:56:37 -05:00
|
|
|
char *en = NULL;
|
|
|
|
char *ec = NULL;
|
2019-02-10 08:15:35 -05:00
|
|
|
if ( dr->generic_name ) {
|
|
|
|
egn = g_markup_escape_text ( dr->generic_name, -1 );
|
|
|
|
}
|
|
|
|
if ( dr->name ) {
|
|
|
|
en = g_markup_escape_text ( dr->name, -1 );
|
2018-10-17 17:02:50 -04:00
|
|
|
}
|
2019-02-10 08:15:35 -05:00
|
|
|
if ( dr->comment ) {
|
2020-02-02 07:56:37 -05:00
|
|
|
ec = g_markup_escape_text ( dr->comment, -1 );
|
2019-02-10 08:15:35 -05:00
|
|
|
}
|
|
|
|
|
2018-10-17 17:02:50 -04:00
|
|
|
char *retv = helper_string_replace_if_exists ( config.drun_display_format,
|
2020-02-02 07:56:37 -05:00
|
|
|
"{generic}", egn,
|
|
|
|
"{name}", en,
|
|
|
|
"{comment}", ec,
|
|
|
|
"{exec}", dr->exec,
|
|
|
|
"{categories}", cats,
|
|
|
|
"{keywords}", keywords,
|
|
|
|
NULL );
|
2019-02-10 08:15:35 -05:00
|
|
|
g_free ( egn );
|
|
|
|
g_free ( en );
|
|
|
|
g_free ( ec );
|
2020-02-02 07:56:37 -05:00
|
|
|
g_free ( cats );
|
2018-10-17 17:02:50 -04:00
|
|
|
return retv;
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
2017-04-12 12:08:57 -04:00
|
|
|
|
2021-06-08 14:21:28 -04:00
|
|
|
static cairo_surface_t *fallback_icon ( DRunModePrivateData *pd, int height )
|
|
|
|
{
|
|
|
|
if ( config.application_fallback_icon ) {
|
|
|
|
// FALLBACK
|
|
|
|
if ( pd->fallback_icon_fetch_uid > 0 ) {
|
|
|
|
return rofi_icon_fetcher_get ( pd->fallback_icon_fetch_uid );
|
|
|
|
}
|
|
|
|
pd->fallback_icon_fetch_uid = rofi_icon_fetcher_query ( config.application_fallback_icon, height );
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-12 12:08:57 -04:00
|
|
|
static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, int height )
|
|
|
|
{
|
|
|
|
DRunModePrivateData *pd = (DRunModePrivateData *) mode_get_private_data ( sw );
|
2021-06-08 13:28:13 -04:00
|
|
|
if ( pd->file_complete ) {
|
|
|
|
return pd->completer->_get_icon ( pd->completer, selected_line, height );
|
|
|
|
}
|
2017-04-12 12:08:57 -04:00
|
|
|
g_return_val_if_fail ( pd->entry_list != NULL, NULL );
|
2017-05-31 14:21:58 -04:00
|
|
|
DRunModeEntry *dr = &( pd->entry_list[selected_line] );
|
2021-06-08 19:20:36 -04:00
|
|
|
if ( dr->icon_name != NULL ) {
|
|
|
|
if ( dr->icon_fetch_uid > 0 ) {
|
|
|
|
cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid );
|
|
|
|
if ( icon ) {
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
return fallback_icon ( pd, height );
|
|
|
|
}
|
|
|
|
dr->icon_fetch_uid = rofi_icon_fetcher_query ( dr->icon_name, height );
|
2021-06-08 14:21:28 -04:00
|
|
|
cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid );
|
|
|
|
if ( icon ) {
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fallback_icon ( pd, height );
|
2017-04-12 12:08:57 -04:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static char *drun_get_completion ( const Mode *sw, unsigned int index )
|
2015-11-24 07:59:35 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
DRunModePrivateData *pd = (DRunModePrivateData *) mode_get_private_data ( sw );
|
2015-11-24 07:59:35 -05:00
|
|
|
/* Free temp storage. */
|
|
|
|
DRunModeEntry *dr = &( pd->entry_list[index] );
|
|
|
|
if ( dr->generic_name == NULL ) {
|
|
|
|
return g_strdup ( dr->name );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return g_strdup_printf ( "%s", dr->name );
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 17:59:59 -05:00
|
|
|
|
2017-09-29 02:32:09 -04:00
|
|
|
static int drun_token_match ( const Mode *data, rofi_int_matcher **tokens, unsigned int index )
|
2015-11-21 17:59:59 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( data );
|
2021-06-08 13:28:13 -04:00
|
|
|
if ( rmpd->file_complete ){
|
|
|
|
return rmpd->completer->_token_match (rmpd->completer, tokens, index );
|
|
|
|
}
|
2015-12-14 14:28:25 -05:00
|
|
|
int match = 1;
|
|
|
|
if ( tokens ) {
|
2015-12-14 14:25:36 -05:00
|
|
|
for ( int j = 0; match && tokens != NULL && tokens[j] != NULL; j++ ) {
|
2017-10-05 11:45:50 -04:00
|
|
|
int test = 0;
|
2017-09-29 02:32:09 -04:00
|
|
|
rofi_int_matcher *ftokens[2] = { tokens[j], NULL };
|
2016-12-26 09:18:52 -05:00
|
|
|
// Match name
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_NAME].enabled_match ) {
|
2017-10-21 05:18:52 -04:00
|
|
|
if ( rmpd->entry_list[index].name ) {
|
|
|
|
test = helper_token_match ( ftokens, rmpd->entry_list[index].name );
|
|
|
|
}
|
2015-12-14 14:25:36 -05:00
|
|
|
}
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_GENERIC].enabled_match ) {
|
2017-10-21 05:18:52 -04:00
|
|
|
// Match generic name
|
|
|
|
if ( test == tokens[j]->invert && rmpd->entry_list[index].generic_name ) {
|
|
|
|
test = helper_token_match ( ftokens, rmpd->entry_list[index].generic_name );
|
|
|
|
}
|
2015-12-14 14:25:36 -05:00
|
|
|
}
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_EXEC].enabled_match ) {
|
2017-10-21 05:18:52 -04:00
|
|
|
// Match executable name.
|
2020-08-26 15:10:04 -04:00
|
|
|
if ( test == tokens[j]->invert && rmpd->entry_list[index].exec ) {
|
2017-10-21 05:18:52 -04:00
|
|
|
test = helper_token_match ( ftokens, rmpd->entry_list[index].exec );
|
|
|
|
}
|
2015-12-14 14:25:36 -05:00
|
|
|
}
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_CATEGORIES].enabled_match ) {
|
2017-10-21 05:18:52 -04:00
|
|
|
// Match against category.
|
|
|
|
if ( test == tokens[j]->invert ) {
|
|
|
|
gchar **list = rmpd->entry_list[index].categories;
|
|
|
|
for ( int iter = 0; test == tokens[j]->invert && list && list[iter]; iter++ ) {
|
|
|
|
test = helper_token_match ( ftokens, list[iter] );
|
|
|
|
}
|
2017-03-14 12:34:41 -04:00
|
|
|
}
|
2016-12-26 09:18:52 -05:00
|
|
|
}
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_KEYWORDS].enabled_match ) {
|
2019-08-20 16:51:07 -04:00
|
|
|
// Match against category.
|
|
|
|
if ( test == tokens[j]->invert ) {
|
|
|
|
gchar **list = rmpd->entry_list[index].keywords;
|
|
|
|
for ( int iter = 0; test == tokens[j]->invert && list && list[iter]; iter++ ) {
|
|
|
|
test = helper_token_match ( ftokens, list[iter] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-15 08:32:46 -04:00
|
|
|
if ( matching_entry_fields[DRUN_MATCH_FIELD_COMMENT].enabled_match ) {
|
|
|
|
|
2017-12-09 13:55:39 -05:00
|
|
|
// Match executable name.
|
|
|
|
if ( test == tokens[j]->invert && rmpd->entry_list[index].comment ) {
|
|
|
|
test = helper_token_match ( ftokens, rmpd->entry_list[index].comment );
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 14:25:36 -05:00
|
|
|
if ( test == 0 ) {
|
|
|
|
match = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 05:18:52 -04:00
|
|
|
|
2015-12-14 14:25:36 -05:00
|
|
|
return match;
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static unsigned int drun_mode_get_num_entries ( const Mode *sw )
|
2015-11-21 17:59:59 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
const DRunModePrivateData *pd = (const DRunModePrivateData *) mode_get_private_data ( sw );
|
2021-06-08 13:28:13 -04:00
|
|
|
if ( pd->file_complete ){
|
|
|
|
return pd->completer->_get_num_entries( pd->completer );
|
|
|
|
}
|
2015-11-21 17:59:59 -05:00
|
|
|
return pd->cmd_list_length;
|
|
|
|
}
|
2021-06-08 13:28:13 -04:00
|
|
|
static char *drun_get_message ( const Mode *sw )
|
|
|
|
{
|
|
|
|
DRunModePrivateData *pd = sw->private_data;
|
|
|
|
if ( pd->file_complete ) {
|
|
|
|
if ( pd->selected_line < pd->cmd_list_length ) {
|
|
|
|
char *msg = mode_get_message ( pd->completer);
|
|
|
|
if (msg ){
|
|
|
|
char *retv = g_strdup_printf("File complete for: %s\n%s", pd->entry_list[pd->selected_line].name, msg);
|
|
|
|
g_free (msg);
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
return g_strdup_printf("File complete for: %s", pd->entry_list[pd->selected_line].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-07 15:27:20 -05:00
|
|
|
#include "mode-private.h"
|
2015-11-25 03:26:38 -05:00
|
|
|
Mode drun_mode =
|
2015-11-03 02:31:38 -05:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
.name = "drun",
|
2016-01-12 16:17:53 -05:00
|
|
|
.cfg_name_key = "display-drun",
|
2016-01-07 15:27:20 -05:00
|
|
|
._init = drun_mode_init,
|
|
|
|
._get_num_entries = drun_mode_get_num_entries,
|
|
|
|
._result = drun_mode_result,
|
|
|
|
._destroy = drun_mode_destroy,
|
|
|
|
._token_match = drun_token_match,
|
2021-06-08 13:28:13 -04:00
|
|
|
._get_message = drun_get_message,
|
2016-01-07 15:27:20 -05:00
|
|
|
._get_completion = drun_get_completion,
|
|
|
|
._get_display_value = _get_display_value,
|
2017-04-12 12:08:57 -04:00
|
|
|
._get_icon = _get_icon,
|
2016-05-26 02:39:33 -04:00
|
|
|
._preprocess_input = NULL,
|
2016-01-07 15:27:20 -05:00
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-11-03 02:31:38 -05:00
|
|
|
};
|
2016-08-29 15:38:29 -04:00
|
|
|
|
|
|
|
#endif // ENABLE_DRUN
|