]> Pileus Git - ~andy/ct/blob - gallery/gallery.c
08590cc0fd067dddc8d13bf6c67696fc0e5dc4ee
[~andy/ct] / gallery / gallery.c
1 #include <glib.h>
2 #include <glib/gstdio.h>
3 #include "html.h"
4
5 void resize(gchar *orig, gchar *thumb)
6 {
7         gchar *argv[] = {"convert", "-resize", "200x200", orig, thumb, NULL};
8         /* god damn glib */
9         g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
10                 NULL, NULL, NULL, NULL, NULL, NULL);
11 }
12
13 GList *gen_thumbs(GList *images)
14 {
15         if (!g_file_test("thumbs", G_FILE_TEST_EXISTS))
16                 g_mkdir("thumbs", 0644);
17         if (!g_file_test("images", G_FILE_TEST_EXISTS))
18                 g_mkdir("images", 0644);
19         for (GList *cur = images; cur; cur = cur->next) {
20                 gchar *name = cur->data;
21                 gchar *thumb = g_strconcat("thumbs/", name, NULL);
22                 gchar *image = g_strconcat("images/", name, NULL);
23                 if (!g_file_test(thumb, G_FILE_TEST_EXISTS))
24                         resize(image, thumb);
25                 g_free(thumb);
26                 g_free(image);
27         }
28         return images;
29 }
30
31 GList *read_dir(gchar *dirname)
32 {
33         GDir *dir = g_dir_open(dirname, 0, NULL);
34         const gchar *name = NULL;
35         GList *images = NULL;
36         while ((name = g_dir_read_name(dir)))
37                 images = g_list_prepend(images, g_strdup(name));
38         return images;
39 }
40
41 int main()
42 {
43         header();
44         g_print("\n");
45
46         const gchar *query_string = g_getenv("QUERY_STRING");
47         if (query_string == NULL)
48                 frame_index();
49         else if (g_str_equal(query_string, "noframe"))
50                 frame_nav(TRUE, gen_thumbs(read_dir("images")));
51         else if (g_str_equal(query_string, "nav"))
52                 frame_nav(FALSE, gen_thumbs(read_dir("images")));
53         else if (g_str_equal(query_string, "head"))
54                 frame_head();
55         else if (g_str_equal(query_string, "content"))
56                 frame_content();
57         else
58                 frame_index();
59 }