-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoose.c
294 lines (254 loc) · 10.7 KB
/
choose.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <gtk/gtk.h>
#define WINDOW_HEIGHT 400
#define WINDOW_WIDTH 350
#define RENDER_TYPE_FAIL 0
#define RENDER_TYPE_IMAGE 1
#define RENDER_TYPE_TEXT 2
#define RENDER_TYPE_UNKNOWN 3
const gchar *historydir = NULL;
GtkWidget *render_listing(const gchar *listing);
void selectaction(GtkListBoxRow *row) {
const GtkListBox *listbox = gtk_widget_get_parent(row);
const char *listing = g_object_get_data(row, "listing");
printf("%s\n", listing);
gtk_main_quit();
}
void deleteaction(GtkListBoxRow *row) {
const GtkListBox *listbox = gtk_widget_get_parent(row);
const char *listing = g_object_get_data(row, "listing");
gchar *listingdir = g_build_filename(historydir, listing, NULL);
if (getenv("COFFEE_PESTO_TRASH")) {
g_file_trash(g_file_new_for_path(listingdir), NULL, NULL);
fprintf(stderr, "INFO: Trashing %s\n", listingdir);
} else {
gchar *command = g_strconcat("/bin/rm -r ", listingdir, NULL);
system(command);
fprintf(stderr, "INFO: Deleting %s with command: %s\n", listingdir, command);
}
gtk_widget_destroy(row);
}
void pinaction(GtkListBoxRow *row) {
GtkListBox *listbox = gtk_widget_get_parent(row);
const char *listing = g_object_get_data(row, "listing");
gchar *listingdir = g_build_filename(historydir, listing, NULL);
gchar *negative = g_strdup_printf("%ld", -strtol(listing, NULL, 10));
gchar *negative_listingdir = g_build_filename(historydir, negative, NULL);
if (rename(listingdir, negative_listingdir) != 0) {
fprintf(stderr, "ERROR: Failed to pin/unpin item\n");
return;
}
gtk_widget_destroy(row);
GtkListBoxRow *updated_row = render_listing(negative);
gtk_list_box_insert(listbox, updated_row, -1);
gtk_list_box_invalidate_sort(listbox);
gtk_widget_show_all(listbox);
}
gboolean onclick(GtkEventBox *eventbox, GdkEventButton *event, gpointer unused) {
GtkListBoxRow *row = gtk_widget_get_parent(eventbox);
switch (event->button) {
case 1:
selectaction(row);
return TRUE;
case 2:
deleteaction(row);
return TRUE;
case 3:
pinaction(row);
return TRUE;
}
return FALSE;
}
gboolean onkeyboard(GtkEventBox *row, GdkEventKey *event, gpointer unused) {
if (event->keyval == GDK_KEY_KP_Enter || event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter) {
selectaction(row);
return TRUE;
}
if (event->keyval == GDK_KEY_BackSpace || event->keyval == GDK_KEY_KP_Delete || event->keyval == GDK_KEY_Delete) {
deleteaction(row);
return TRUE;
}
if (event->keyval == GDK_KEY_p) {
pinaction(row);
return TRUE;
}
return FALSE;
}
int what_to_render(const gchar *listing, gchar **chosen_dir) {
gchar *listing_dir = g_build_filename(historydir, listing, NULL);
GDir *dir = g_dir_open(listing_dir, 0, NULL);
if (!dir) {
fprintf(stderr, "ERROR: Failed to open listing %s\n", listing);
return RENDER_TYPE_FAIL;
}
gchar *first_text_entry = NULL;
const gchar *entry = NULL;
while ((entry = g_dir_read_name(dir))) {
if (!entry) {
fprintf(stderr, "Error reading a variant of listing %s\n", listing);
continue;
}
if (g_str_equal(entry, "text\\plain")) {
*chosen_dir = g_build_filename(listing_dir, entry, NULL);
return RENDER_TYPE_TEXT;
}
if (g_str_equal(entry, "image\\jpeg") || g_str_equal(entry, "image\\png") || g_str_equal(entry, "image\\svg+xml")) {
*chosen_dir = g_build_filename(listing_dir, entry, NULL);
return RENDER_TYPE_IMAGE;
}
if (!first_text_entry && g_str_has_prefix(entry, "text\\")) {
first_text_entry = g_build_filename(listing_dir, entry, NULL);
}
}
if (first_text_entry) {
*chosen_dir = first_text_entry;
return RENDER_TYPE_TEXT;
}
*chosen_dir = NULL;
return RENDER_TYPE_UNKNOWN;
}
GtkWidget *render_listing(const gchar *listing) {
GtkWidget *widget = NULL;
gchar *chosen_dir = NULL;
switch (what_to_render(listing, &chosen_dir)) {
case RENDER_TYPE_TEXT: {
gchar *data_file = g_build_filename(chosen_dir, "data", NULL);
gchar *text = NULL;
if (!g_file_get_contents(data_file, &text, NULL, NULL)) {
fprintf(stderr, "ERROR: Failed to read text data from %s\n", data_file);
return NULL;
}
widget = gtk_label_new(g_utf8_substring(text, 0, MIN(strlen(text), 100))); // dont ask
g_free(text);
gtk_label_set_ellipsize(widget, PANGO_ELLIPSIZE_END);
break;
}
case RENDER_TYPE_IMAGE: {
gchar *data_file = g_build_filename(chosen_dir, "data", NULL);
GdkPixbuf *buffer = gdk_pixbuf_new_from_file_at_size(data_file, 400, 200, NULL);
if (!buffer) {
fprintf(stderr, "ERROR: Failed to read image data from %s\n", data_file);
return NULL;
}
widget = gtk_image_new_from_pixbuf(buffer);
break;
}
case RENDER_TYPE_UNKNOWN:
widget = gtk_label_new("(blob)");
break;
case RENDER_TYPE_FAIL:
default:
return NULL;
}
gchar *index_file = g_build_filename(chosen_dir, "index", NULL);
gchar *index_data = NULL;
if (!g_file_get_contents(index_file, &index_data, NULL, NULL)) {
fprintf(stderr, "ERROR: Failed to read index data from %s\n", index_file);
return NULL;
}
GtkOverlay *overlay = gtk_overlay_new();
gtk_container_add(overlay, widget);
if (listing[0] == '-') {
GtkImage *pinned_icon = gtk_image_new_from_icon_name("pin", GTK_ICON_SIZE_DND);
gtk_widget_set_halign(pinned_icon, GTK_ALIGN_END);
gtk_widget_set_valign(pinned_icon, GTK_ALIGN_START);
gtk_overlay_add_overlay(overlay, pinned_icon);
}
GtkEventBox *eventbox = gtk_event_box_new();
g_signal_connect(eventbox, "button-release-event", onclick, NULL);
gtk_container_add(eventbox, overlay);
GtkListBoxRow *row = gtk_list_box_row_new();
g_signal_connect(row, "key-release-event", onkeyboard, NULL);
gtk_container_add(row, eventbox);
g_object_set_data(row, "listing", g_strdup(listing));
g_object_set_data(row, "index", index_data);
gtk_widget_set_tooltip_text(row, "Left click to copy, middle click to delete, right click to pin");
return row;
}
gboolean onescape(GtkWidget *widget, GdkEventKey *event, gpointer unused) {
if (event->keyval == GDK_KEY_Escape) {
gtk_main_quit();
return TRUE;
}
return FALSE;
}
int sorter(GtkListBoxRow *one, GtkListBoxRow *two, gpointer unused) {
return strtoul(g_object_get_data(one, "listing"), NULL, 10) < strtoul(g_object_get_data(two, "listing"), NULL, 10);
}
int clamp(gint min, gint val, gint max) {
if (val > max) return max;
if (val < min) return min;
return val;
}
gboolean triggerlistupdate(GtkSearchEntry *searchbox, GtkListBox *listbox) {
gtk_list_box_invalidate_filter(listbox);
return TRUE;
}
gboolean filterpredicate(GtkListBoxRow *row, GtkSearchEntry *searchbox) {
const gchar *searchtext = gtk_entry_get_text(searchbox);
const gchar *indexdata = g_object_get_data(row, "index");
return g_str_match_string(searchtext, indexdata, TRUE);
}
int main() {
gtk_init(NULL, NULL);
GtkListBox *listbox = gtk_list_box_new();
gtk_list_box_set_activate_on_single_click(listbox, FALSE);
gtk_list_box_set_sort_func(listbox, sorter, NULL, NULL);
const gchar *default_base_dir = g_build_filename(getenv("HOME"), ".coffeepesto", NULL);
const gchar *provided_dir = getenv("COFFEE_PESTO_DIR");
const gchar *dir_to_use = provided_dir ? provided_dir : default_base_dir;
historydir = g_build_filename(dir_to_use, "h", NULL);
GDir *fd = g_dir_open(historydir, 0, NULL);
if (!fd) {
fprintf(stderr, "ERROR: App directory %s doesn't exist\n", historydir);
return 1;
}
const gchar *listing;
GtkListBoxRow *row;
while ((listing = g_dir_read_name(fd))) {
if (!(row = render_listing(listing))) {
fprintf(stderr, "WARN: Failed to render listing %s. Skipping\n", listing);
continue;
}
gtk_list_box_insert(listbox, row, -1);
}
GtkScrolledWindow *scrollingcontainer = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(scrollingcontainer, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add(scrollingcontainer, listbox);
GtkSearchEntry *searchbox = gtk_search_entry_new();
g_signal_connect(searchbox, "changed", triggerlistupdate, listbox);
gtk_list_box_set_filter_func(listbox, filterpredicate, searchbox, NULL);
GtkHeaderBar *header = gtk_header_bar_new();
gtk_header_bar_set_title(header, "CoffeePesto");
GtkBox *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
gtk_box_pack_start(vbox, header, FALSE, FALSE, 0);
gtk_box_pack_start(vbox, searchbox, FALSE, FALSE, 0);
gtk_box_pack_start(vbox, scrollingcontainer, TRUE, TRUE, 0);
GtkWindow *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(window, "CoffeePesto");
gtk_window_set_decorated(window, FALSE);
gtk_window_set_default_size(window, WINDOW_WIDTH, WINDOW_HEIGHT);
g_signal_connect(window, "focus-out-event", gtk_main_quit, NULL);
g_signal_connect(window, "key-release-event", onescape, NULL);
gtk_container_add(window, vbox);
gtk_widget_show_all(window);
GdkScreen *screen = gtk_window_get_screen(window);
GdkDisplay *display = gtk_widget_get_display(window);
GdkMonitor *monitor = gdk_display_get_monitor_at_window(display, gtk_widget_get_window(window));
GdkRectangle geometry;
gdk_monitor_get_geometry(monitor, &geometry);
gint screenwidth = geometry.width;
gint screenheight = geometry.height;
gint mouseX = 0, mouseY = 0;
GdkDevice *device = gdk_seat_get_pointer(gdk_display_get_default_seat(display));
gdk_device_get_position(device, &screen, &mouseX, &mouseY);
gint desiredX = clamp(100, mouseX, screenwidth - WINDOW_WIDTH - 100); // 100 is leeway for taskbars n stuff
gint desiredY = clamp(100, mouseY, screenheight - WINDOW_HEIGHT - 100);
gtk_window_move(window, desiredX, desiredY);
GtkCssProvider *cssprovider = gtk_css_provider_new();
const gchar *css = "row { border-bottom: 0.01em solid @secondary_color; } list { padding: .4em; }";
gtk_css_provider_load_from_data(cssprovider, css, -1, NULL);
gtk_style_context_add_provider_for_screen(gtk_window_get_screen(window), cssprovider,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_main();
return 0;
}