]> Pileus Git - ~andy/ct/blob - gallery/gallery.c
Add old version of gallery using php
[~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                 g_message("got image %s", name);
38                 images = g_list_prepend(images, g_strdup(name));
39         }
40         return images;
41 }
42
43 int main()
44 {
45         header();
46         g_print("\n");
47
48         const gchar *query_string = g_getenv("QUERY_STRING");
49         if (query_string == NULL)
50                 frame_index();
51         else if (g_str_equal(query_string, "noframe"))
52                 frame_nav(TRUE, gen_thumbs(read_dir("images")));
53         else if (g_str_equal(query_string, "nav"))
54                 frame_nav(FALSE, gen_thumbs(read_dir("images")));
55         else if (g_str_equal(query_string, "head"))
56                 frame_head();
57         else if (g_str_equal(query_string, "content"))
58                 frame_content();
59         else
60                 frame_index();
61 }