forked from hotnuma/applist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappwindow.c
More file actions
356 lines (263 loc) · 10.7 KB
/
appwindow.c
File metadata and controls
356 lines (263 loc) · 10.7 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "config.h"
#include "appwindow.h"
#include "preferences.h"
#include <gtk/gtk.h>
#include <gio/gdesktopappinfo.h>
#include <strings.h>
static void _window_finalize(GObject *object);
static void _window_create_view(AppWindow *window);
static void _window_store_load(AppWindow *window);
static void _on_toggle(GtkListStore *store, gchar *path_str, gpointer data);
static void _treeview_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
GtkTreeViewColumn *column, AppWindow *window);
static bool _window_append_line(AppWindow *window, GAppInfo *info);
static gint _utf8_cmp(gconstpointer a, gconstpointer b);
gboolean _appinfo_show(GAppInfo *info);
static GdkPixbuf* _pixbuf_from_gicon(GtkWidget *widget, GIcon *gicon,
const gchar *id);
static GdkPixbuf* _pixbuf_get_default(GtkWidget *widget, const gchar *id);
static gboolean _window_on_delete(GtkWidget *widget, GdkEvent *event, gpointer data);
enum
{
COL_INFO = 0,
COL_ICON,
COL_TITLE,
COL_VISIBLE,
COL_FILE,
NUM_COLS
};
struct _AppWindow
{
GtkWindow __parent__;
GtkListStore *store;
};
G_DEFINE_TYPE(AppWindow, window, GTK_TYPE_WINDOW)
static void window_class_init(AppWindowClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = _window_finalize;
}
static void _window_finalize(GObject *object)
{
AppWindow *window = APPWINDOW(object);
g_object_unref(window->store);
G_OBJECT_CLASS(window_parent_class)->finalize(object);
}
static gboolean _window_on_delete(GtkWidget *widget, GdkEvent *event, gpointer data)
{
(void) widget;
(void) event;
(void) data;
GtkWindow *window = GTK_WINDOW(widget);
if (gtk_widget_get_visible(GTK_WIDGET(widget)))
{
GdkWindowState state = gdk_window_get_state(
gtk_widget_get_window(widget));
Preferences *prefs = get_preferences();
prefs->window_maximized = ((state & (GDK_WINDOW_STATE_MAXIMIZED
| GDK_WINDOW_STATE_FULLSCREEN))
!= 0);
if (!prefs->window_maximized)
{
gtk_window_get_size(window,
&prefs->window_width,
&prefs->window_height);
}
prefs_write();
}
gtk_main_quit();
return false;
}
static void window_init(AppWindow *window)
{
Preferences *prefs = prefs_file_read();
gtk_window_set_default_size(GTK_WINDOW(window),
prefs->window_width,
prefs->window_height);
if (G_UNLIKELY(prefs->window_maximized))
gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_title(GTK_WINDOW(window), "AppInfo List");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(window, "delete-event",
G_CALLBACK(_window_on_delete), NULL);
_window_create_view(window);
_window_store_load(window);
}
static void _window_create_view(AppWindow *window)
{
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(window), scroll);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
GTK_SHADOW_IN);
GtkListStore *store = gtk_list_store_new(NUM_COLS,
G_TYPE_APP_INFO,
GDK_TYPE_PIXBUF,
G_TYPE_STRING,
G_TYPE_BOOLEAN,
G_TYPE_STRING);
GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
window->store = store; // leak
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
// Title
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Title");
renderer = gtk_cell_renderer_pixbuf_new();
gtk_tree_view_column_pack_start(col, renderer, FALSE);
gtk_tree_view_column_set_attributes(col, renderer,
"pixbuf", COL_ICON,
NULL);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_attributes(col, renderer,
"text", COL_TITLE,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col);
// Visible
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Visible");
renderer = gtk_cell_renderer_toggle_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_attributes(col, renderer,
"active", COL_VISIBLE,
NULL);
//gtk_cell_renderer_toggle_set_activatable(
// GTK_CELL_RENDERER_TOGGLE(renderer), true);
g_signal_connect_swapped(G_OBJECT(renderer), "toggled",
G_CALLBACK(_on_toggle), store);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col);
// File
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "File");
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_attributes(col, renderer,
"text", COL_FILE,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col);
gtk_container_add(GTK_CONTAINER(scroll), treeview);
g_signal_connect(G_OBJECT(treeview), "row-activated",
G_CALLBACK(_treeview_row_activated), window);
}
static void _on_toggle(GtkListStore *store, gchar *path_str, gpointer data)
{
(void) data;
GtkTreeModel *model = GTK_TREE_MODEL(store);
GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
GtkTreeIter iter;
gtk_tree_model_get_iter(model, &iter, path);
gboolean enabled;
gtk_tree_model_get(model, &iter, COL_VISIBLE, &enabled, -1);
enabled = !enabled;
gtk_list_store_set(store, &iter, COL_VISIBLE, enabled, -1);
}
static void _window_store_load(AppWindow *window)
{
//Preferences *prefs = get_preferences();
GList *all = g_app_info_get_all();
all = g_list_sort(all, _utf8_cmp);
for (GList *lp = all; lp; lp = lp->next)
{
GAppInfo *info = G_APP_INFO(lp->data);
//if (prefs->show_all == false && !_appinfo_show(info))
// continue;
_window_append_line(window, info);
}
g_list_free_full(all, g_object_unref);
}
static gint _utf8_cmp(gconstpointer a, gconstpointer b)
{
return g_utf8_collate(g_app_info_get_name(G_APP_INFO(a)),
g_app_info_get_name(G_APP_INFO(b)));
}
gboolean _appinfo_show(GAppInfo *info)
{
g_return_val_if_fail(G_IS_DESKTOP_APP_INFO(info), FALSE);
GDesktopAppInfo *deskinfo = G_DESKTOP_APP_INFO(info);
if (g_desktop_app_info_get_nodisplay(deskinfo))
return false;
return g_desktop_app_info_get_show_in(deskinfo, NULL);
}
static bool _window_append_line(AppWindow *window, GAppInfo *info)
{
GIcon *gicon = g_app_info_get_icon(info);
c_autounref GdkPixbuf *pix = _pixbuf_from_gicon(GTK_WIDGET(window),
gicon,
g_app_info_get_id(info));
GtkTreeIter iter;
gtk_list_store_append(window->store, &iter);
gtk_list_store_set(window->store, &iter,
COL_INFO, info,
COL_ICON, pix,
COL_TITLE, g_app_info_get_name(info),
COL_VISIBLE, _appinfo_show(info),
COL_FILE, g_app_info_get_id(info),
-1);
return true;
}
static GdkPixbuf* _pixbuf_from_gicon(GtkWidget *widget, GIcon *gicon, const gchar *id)
{
if (!gicon)
{
//g_print("_pixbuf_from_gicon : %s : gicon = null\n", id);
return _pixbuf_get_default(widget, id);
}
GtkIconTheme *icon_theme =
gtk_icon_theme_get_for_screen(gtk_widget_get_screen(widget));
gint scale_factor = gtk_widget_get_scale_factor(widget);
gint requested_icon_size = 24 * scale_factor;
c_autounref GtkIconInfo *icon_info =
gtk_icon_theme_lookup_by_gicon(icon_theme,
gicon,
requested_icon_size,
GTK_ICON_LOOKUP_USE_BUILTIN
| GTK_ICON_LOOKUP_FORCE_SIZE);
if (G_UNLIKELY(icon_info == NULL))
{
//g_print("_pixbuf_from_gicon : %s : icon_info = null\n", id);
return _pixbuf_get_default(widget, id);
}
return gtk_icon_info_load_icon(icon_info, NULL);
}
static GdkPixbuf* _pixbuf_get_default(GtkWidget *widget, const gchar *id)
{
GtkIconTheme *icon_theme =
gtk_icon_theme_get_for_screen(gtk_widget_get_screen(widget));
gint scale_factor = gtk_widget_get_scale_factor(widget);
gint requested_icon_size = 24 * scale_factor;
c_autounref GtkIconInfo *icon_info =
gtk_icon_theme_lookup_icon(icon_theme,
"application-x-executable",
requested_icon_size,
GTK_ICON_LOOKUP_USE_BUILTIN
| GTK_ICON_LOOKUP_FORCE_SIZE);
if (G_UNLIKELY(icon_info == NULL))
{
g_print("_pixbuf_get_default : %s : icon_info = null\n", id);
return NULL;
}
return gtk_icon_info_load_icon(icon_info, NULL);
}
static void _treeview_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
GtkTreeViewColumn *column, AppWindow *window)
{
(void) column;
(void) window;
GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
GtkTreeIter iter;
if (!gtk_tree_model_get_iter(model, &iter, path))
return;
c_autounref GAppInfo *info = NULL;
c_autofree char *file = NULL;
gtk_tree_model_get(model, &iter,
COL_INFO, &info,
COL_FILE, &file,
-1);
const gchar *filepath = g_desktop_app_info_get_filename(G_DESKTOP_APP_INFO(info));
gchar *cmd = g_strdup_printf("exo-open %s", filepath);
g_spawn_command_line_async(cmd, NULL);
g_free(cmd);
}