Cache write is now controlled by the mode, this allows for greater flexibility with caching

This commit is contained in:
Scoopta 2019-11-16 14:52:20 -08:00
parent 0f78b557b5
commit 7573eecb02
5 changed files with 70 additions and 57 deletions

View File

@ -43,6 +43,8 @@ struct cache_line {
void wofi_init(struct map* config);
void wofi_write_cache(const gchar* mode, const gchar* cmd);
struct wl_list* wofi_read_cache(char* mode);
void wofi_insert_widget(char* mode, char** text, char* search_text, char** actions, size_t action_count);

View File

@ -17,13 +17,15 @@
#include <wofi.h>
#define MODE "dmenu"
void wofi_dmenu_init() {
struct map* cached = map_init();
struct wl_list* cache = wofi_read_cache("dmenu");
struct wl_list* cache = wofi_read_cache(MODE);
struct cache_line* node, *tmp;
wl_list_for_each_safe(node, tmp, cache, link) {
wofi_insert_widget("dmenu", &node->line, node->line, &node->line, 1);
wofi_insert_widget(MODE, &node->line, node->line, &node->line, 1);
map_put(cached, node->line, "true");
free(node->line);
wl_list_remove(&node->link);
@ -42,13 +44,14 @@ void wofi_dmenu_init() {
if(map_contains(cached, line)) {
continue;
}
wofi_insert_widget("dmenu", &line, line, &line, 1);
wofi_insert_widget(MODE, &line, line, &line, 1);
}
free(line);
map_free(cached);
}
void wofi_dmenu_exec(const gchar* cmd) {
wofi_write_cache(MODE, cmd);
printf("%s\n", cmd);
exit(0);
}

View File

@ -17,6 +17,8 @@
#include <wofi.h>
#define MODE "drun"
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)) {
@ -158,7 +160,7 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) {
char** actions = get_action_actions(full_path, &action_count);
char* search_text = get_search_text(full_path);
wofi_insert_widget("drun", text, search_text, actions, action_count);
wofi_insert_widget(MODE, text, search_text, actions, action_count);
for(size_t count = 0; count < action_count; ++count) {
free(actions[count]);
@ -176,7 +178,7 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) {
void wofi_drun_init() {
struct map* cached = map_init();
struct map* entries = map_init();
struct wl_list* cache = wofi_read_cache("drun");
struct wl_list* cache = wofi_read_cache(MODE);
struct cache_line* node, *tmp;
wl_list_for_each_safe(node, tmp, cache, link) {
@ -189,7 +191,7 @@ void wofi_drun_init() {
char** actions = get_action_actions(node->line, &action_count);
char* search_text = get_search_text(node->line);
wofi_insert_widget("drun", text, search_text, actions, action_count);
wofi_insert_widget(MODE, text, search_text, actions, action_count);
map_put(cached, node->line, "true");
free(search_text);
@ -247,10 +249,12 @@ static void launch_done(GObject* obj, GAsyncResult* result, gpointer data) {
void wofi_drun_exec(const gchar* cmd) {
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(cmd);
if(G_IS_DESKTOP_APP_INFO(info)) {
wofi_write_cache(MODE, cmd);
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;
wofi_write_cache(MODE, cmd);
info = g_desktop_app_info_new_from_filename(cmd);
char* action = space + 1;
g_desktop_app_info_launch_action(info, action, NULL);

View File

@ -17,9 +17,11 @@
#include <wofi.h>
#define MODE "run"
void wofi_run_init() {
struct map* cached = map_init();
struct wl_list* cache = wofi_read_cache("run");
struct wl_list* cache = wofi_read_cache(MODE);
struct cache_line* node, *tmp;
wl_list_for_each_safe(node, tmp, cache, link) {
@ -34,7 +36,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, 1);
wofi_insert_widget(MODE, &text, search_text, &node->line, 1);
map_put(cached, node->line, "true");
free(search_text);
free(node->line);
@ -64,7 +66,7 @@ void wofi_run_init() {
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);
char* text = strdup(entry->d_name);
wofi_insert_widget("run", &text, search_text, &full_path, 1);
wofi_insert_widget(MODE, &text, search_text, &full_path, 1);
free(search_text);
free(text);
}
@ -77,6 +79,7 @@ void wofi_run_init() {
}
void wofi_run_exec(const gchar* cmd) {
wofi_write_cache(MODE, cmd);
if(wofi_run_in_term()) {
wofi_term_run(cmd);
}

View File

@ -156,9 +156,58 @@ static char* get_cache_path(const gchar* mode) {
return cache_path;
}
static void execute_action(const gchar* mode, const gchar* cmd, bool primary_action) {
static void execute_action(const gchar* mode, const gchar* cmd) {
struct mode* mode_ptr = map_get(modes, mode);
mode_ptr->mode_exec(cmd);
}
static void activate_item(GtkFlowBox* flow_box, GtkFlowBoxChild* row, gpointer data) {
(void) flow_box;
(void) data;
GtkWidget* box = gtk_bin_get_child(GTK_BIN(row));
bool primary_action = GTK_IS_EXPANDER(box);
if(primary_action) {
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
}
execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"));
}
static gboolean _insert_widget(gpointer data) {
struct node* node = data;
GtkWidget* parent;
if(node->action_count > 1) {
parent = gtk_expander_new("");
GtkWidget* box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
gtk_expander_set_label_widget(GTK_EXPANDER(parent), box);
GtkWidget* exp_box = gtk_list_box_new();
gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(exp_box), FALSE);
g_signal_connect(exp_box, "row-activated", G_CALLBACK(activate_item), NULL);
gtk_container_add(GTK_CONTAINER(parent), exp_box);
for(size_t count = 1; count < node->action_count; ++count) {
box = create_label(node->mode, node->text[count], node->search_text, node->actions[count]);
gtk_container_add(GTK_CONTAINER(exp_box), box);
}
} else {
parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
}
gtk_container_add(GTK_CONTAINER(inner_box), parent);
gtk_widget_show_all(parent);
free(node->mode);
for(size_t count = 0; count < node->action_count; ++count) {
free(node->text[count]);
}
free(node->text);
free(node->search_text);
for(size_t count = 0; count < node->action_count; ++count) {
free(node->actions[count]);
}
free(node->actions);
free(node);
return FALSE;
}
void wofi_write_cache(const gchar* mode, const gchar* cmd) {
char* cache_path = get_cache_path(mode);
struct wl_list lines;
wl_list_init(&lines);
@ -201,54 +250,6 @@ static void execute_action(const gchar* mode, const gchar* cmd, bool primary_act
fclose(file);
free(cache_path);
}
mode_ptr->mode_exec(cmd);
}
static void activate_item(GtkFlowBox* flow_box, GtkFlowBoxChild* row, gpointer data) {
(void) flow_box;
(void) data;
GtkWidget* box = gtk_bin_get_child(GTK_BIN(row));
bool primary_action = GTK_IS_EXPANDER(box);
if(primary_action) {
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
}
execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"), primary_action);
}
static gboolean _insert_widget(gpointer data) {
struct node* node = data;
GtkWidget* parent;
if(node->action_count > 1) {
parent = gtk_expander_new("");
GtkWidget* box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
gtk_expander_set_label_widget(GTK_EXPANDER(parent), box);
GtkWidget* exp_box = gtk_list_box_new();
gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(exp_box), FALSE);
g_signal_connect(exp_box, "row-activated", G_CALLBACK(activate_item), NULL);
gtk_container_add(GTK_CONTAINER(parent), exp_box);
for(size_t count = 1; count < node->action_count; ++count) {
box = create_label(node->mode, node->text[count], node->search_text, node->actions[count]);
gtk_container_add(GTK_CONTAINER(exp_box), box);
}
} else {
parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
}
gtk_container_add(GTK_CONTAINER(inner_box), parent);
gtk_widget_show_all(parent);
free(node->mode);
for(size_t count = 0; count < node->action_count; ++count) {
free(node->text[count]);
}
free(node->text);
free(node->search_text);
for(size_t count = 0; count < node->action_count; ++count) {
free(node->actions[count]);
}
free(node->actions);
free(node);
return FALSE;
}
struct wl_list* wofi_read_cache(char* mode) {
@ -370,14 +371,14 @@ static void activate_search(GtkEntry* entry, gpointer data) {
(void) data;
GtkWidget* child = get_first_child(GTK_CONTAINER(inner_box));
if(exec_search || child == NULL) {
execute_action(mode, gtk_entry_get_text(entry), true);
execute_action(mode, gtk_entry_get_text(entry));
} else {
GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
bool primary_action = GTK_IS_EXPANDER(box);
if(primary_action) {
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
}
execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"), primary_action);
execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"));
}
}