Added support for alternative desktop actions

This commit is contained in:
Scoopta
2019-11-13 01:17:45 -08:00
parent 9bc345beae
commit b710a3aee5
5 changed files with 223 additions and 88 deletions

View File

@@ -23,7 +23,7 @@ void wofi_dmenu_init() {
struct cache_line* node, *tmp;
wl_list_for_each_safe(node, tmp, cache, link) {
wofi_insert_widget("dmenu", node->line, node->line, node->line);
wofi_insert_widget("dmenu", &node->line, node->line, &node->line, 1);
map_put(cached, node->line, "true");
free(node->line);
wl_list_remove(&node->link);
@@ -42,7 +42,7 @@ void wofi_dmenu_init() {
if(map_contains(cached, line)) {
continue;
}
wofi_insert_widget("dmenu", line, line, line);
wofi_insert_widget("dmenu", &line, line, &line, 1);
}
free(line);
map_free(cached);

View File

@@ -17,12 +17,17 @@
#include <wofi.h>
static char* get_text(char* file) {
static char* get_text(char* file, char* action) {
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file);
if(info == NULL || g_desktop_app_info_get_is_hidden(info) || g_desktop_app_info_get_nodisplay(info)) {
return NULL;
}
const char* name = g_app_info_get_display_name(G_APP_INFO(info));
const char* name;
if(action == NULL) {
name = g_app_info_get_display_name(G_APP_INFO(info));
} else {
name = g_desktop_app_info_get_action_name(info, action);
}
if(name == NULL) {
return NULL;
}
@@ -60,6 +65,62 @@ static char* get_search_text(char* file) {
return utils_concat(6, name, file, exec == NULL ? "" : exec, description == NULL ? "" : description, categories == NULL ? "" : categories, keywords == NULL ? (const char* const*) "" : keywords);
}
static const gchar* const* get_actions(char* file, size_t* action_count) {
*action_count = 0;
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file);
if(info == NULL) {
return NULL;
}
const gchar* const* actions = g_desktop_app_info_list_actions(info);
if(actions[0] == NULL) {
return NULL;
}
for(; actions[*action_count] != NULL; ++*action_count);
return actions;
}
static char** get_action_text(char* file, size_t* text_count) {
*text_count = 0;
char* tmp = get_text(file, NULL);
if(tmp == NULL) {
return NULL;
}
const gchar* const* action_names = get_actions(file, text_count);
++*text_count;
char** text = malloc(*text_count * sizeof(char*));
text[0] = tmp;
for(size_t count = 1; count < *text_count; ++count) {
text[count] = get_text(file, (gchar*) action_names[count - 1]);
}
return text;
}
static char** get_action_actions(char* file, size_t* action_count) {
*action_count = 0;
char* tmp = strdup(file);
if(tmp == NULL) {
return NULL;
}
const gchar* const* action_names = get_actions(file, action_count);
++*action_count;
char** actions = malloc(*action_count * sizeof(char*));
actions[0] = tmp;
for(size_t count = 1; count < *action_count; ++count) {
actions[count] = utils_concat(3, file, " ", (gchar*) action_names[count - 1]);
}
return actions;
}
static void insert_dir(char* app_dir, struct map* cached, struct map* entries) {
DIR* dir = opendir(app_dir);
if(dir == NULL) {
@@ -82,18 +143,30 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) {
free(full_path);
continue;
}
char* text = get_text(full_path);
if(map_contains(entries, entry->d_name)) {
free(full_path);
continue;
}
size_t action_count;
char** text = get_action_text(full_path, &action_count);
if(text == NULL) {
free(full_path);
continue;
}
if(map_contains(entries, entry->d_name)) {
continue;
}
map_put(entries, entry->d_name, "true");
char** actions = get_action_actions(full_path, &action_count);
char* search_text = get_search_text(full_path);
wofi_insert_widget("drun", text, search_text, full_path);
wofi_insert_widget("drun", text, search_text, actions, action_count);
for(size_t count = 0; count < action_count; ++count) {
free(actions[count]);
free(text[count]);
}
free(text);
free(actions);
free(search_text);
free(full_path);
}
@@ -107,15 +180,26 @@ void wofi_drun_init() {
struct cache_line* node, *tmp;
wl_list_for_each_safe(node, tmp, cache, link) {
char* text = get_text(node->line);
size_t action_count;
char** text = get_action_text(node->line, &action_count);
if(text == NULL) {
goto cache_cont;
}
char** actions = get_action_actions(node->line, &action_count);
char* search_text = get_search_text(node->line);
wofi_insert_widget("drun", text, search_text, node->line);
wofi_insert_widget("drun", text, search_text, actions, action_count);
map_put(cached, node->line, "true");
free(search_text);
for(size_t count = 0; count < action_count; ++count) {
free(text[count]);
free(actions[count]);
}
free(text);
free(actions);
cache_cont:
free(node->line);
@@ -164,6 +248,14 @@ void wofi_drun_exec(const gchar* cmd) {
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(cmd);
if(G_IS_DESKTOP_APP_INFO(info)) {
g_app_info_launch_uris_async(G_APP_INFO(info), NULL, NULL, NULL, launch_done, (gchar*) cmd);
} else if(strrchr(cmd, ' ') != NULL) {
char* space = strrchr(cmd, ' ');
*space = 0;
info = g_desktop_app_info_new_from_filename(cmd);
char* action = space + 1;
g_desktop_app_info_launch_action(info, action, NULL);
utils_sleep_millis(500);
exit(0);
} else {
fprintf(stderr, "%s cannot be executed\n", cmd);
exit(1);

View File

@@ -34,7 +34,7 @@ void wofi_run_init() {
search_prefix = text;
}
char* search_text = utils_concat(2, search_prefix, node->line);
wofi_insert_widget("run", text, search_text, node->line);
wofi_insert_widget("run", &text, search_text, &node->line, 1);
map_put(cached, node->line, "true");
free(search_text);
free(node->line);
@@ -63,8 +63,10 @@ void wofi_run_init() {
stat(full_path, &info);
if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) {
char* search_text = utils_concat(2, entry->d_name, full_path);
wofi_insert_widget("run", entry->d_name, search_text, full_path);
char* text = strdup(entry->d_name);
wofi_insert_widget("run", &text, search_text, &full_path, 1);
free(search_text);
free(text);
}
free(full_path);
}