From 75bb13e531654a561dc7baa2f2bc594aa4a8fc52 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sun, 29 May 2011 19:00:44 +0200 Subject: Introduce new theming system This extends current templating system to another dimension, bringing the possibility to have multiple themes generated at once. The purpose is to be able to switch between multiple designs, from PC to mobile, classic vs. flat view, slideshow, etc. For the moment, only the classic theme is available. Rules: * for switching between themes, place a link inside your template manually - it's not a cgg concern, only your theming infrastructure * don't forget to define different file names for index and album pages across different themes * it's recommended to keep default theme named as "index.*" to avoid showing directory listing on webserver Notes: * some TODOs will be fixed when we have new theme using these advanced features (e.g. pictures in album pages) * TODO: introduce tag/block conditional system, allow custom user defines for each theme/page * TODO: deprecate , and in favor of conditionals (these tags belong to theming) --- src/job-manager.c | 178 +++++++++++++++++++++++++++++------------------------- 1 file changed, 95 insertions(+), 83 deletions(-) (limited to 'src/job-manager.c') diff --git a/src/job-manager.c b/src/job-manager.c index c2188a9..bf3b0df 100644 --- a/src/job-manager.c +++ b/src/job-manager.c @@ -114,9 +114,10 @@ thread_func (gpointer data) gchar *imgname; gchar *s1, *s2, *s3; TJob *job = data; - gboolean updated; + gboolean needs_update; GList *l; TJobItem *job_item; + TGalleryDesignTheme *theme; do { item = NULL; @@ -137,24 +138,34 @@ thread_func (gpointer data) /* actually do some work */ if (item != NULL && job_item != NULL) { - imgname = g_path_get_basename ((item->path == NULL && item->preview) ? item->preview : item->path); - updated = generate_image (job->setup, job->items, item, job_item->real_index, job->path_info, ! job->force_update); + imgname = GET_ITEM_TARGET_FILENAME (item); + /* Two-pass check whether images need to be updated. First check does no I/O except of stat() calls. */ + needs_update = job->force_update; + if (! needs_update) + needs_update = generate_image (job->setup, job->items, item, job_item->real_index, job->path_info, TRUE); + if (needs_update) + generate_image (job->setup, job->items, item, job_item->real_index, job->path_info, FALSE); - if (updated && job->setup->verbose) { + if (needs_update && job->setup->verbose) { G_LOCK (items_print); g_print (" [%d/%d] Processing item \"%s\"\n", job_item->index, job->total_items, imgname); G_UNLOCK (items_print); } - if (updated && job->items->type == GALLERY_TYPE_ALBUM) { - s1 = g_build_filename (job->setup->real_templates_dir, job->setup->template_photo, NULL); - s2 = g_strconcat (imgname, GET_EXT (job->setup->index_file_name), NULL); - s3 = g_build_filename (job->path_info->dest_dir, s2, NULL); - write_html_image (job->setup, job->path_info, s1, s3, item, job->items); - g_free (s1); - g_free (s2); - g_free (s3); - } + if (needs_update && job->items->type == GALLERY_TYPE_ALBUM) + for (l = g_list_first (job->setup->design->themes); l; l = g_list_next (l)) { + theme = l->data; + /* Do not generate particular image pages when theme doesn't define it */ + if (theme->enabled && theme->picture_template && theme->picture_filename) { + s1 = g_build_filename (job->path_info->templates_root, theme->picture_template, NULL); + s2 = g_strdup_printf (theme->picture_filename, imgname); + s3 = g_build_filename (job->path_info->dest_dir, s2, NULL); + write_html_image (job->setup, job->path_info, theme, s1, s3, item, job->items); + g_free (s1); + g_free (s2); + g_free (s3); + } + } g_free (imgname); } @@ -182,13 +193,7 @@ build_tree (TGallerySetup *setup, gchar *idx_file; TAlbum *items; TIndexItem *item; - gchar *s1, *s2, *s3; - gchar *thumb_dir; - gchar *img_big_dir; - gchar *img_orig_dir; - const gchar *template; - gchar *dst_album_file; - gboolean res; + gchar *s1, *s2, *s3, *s4; int i; TJob *job; GError *error; @@ -199,6 +204,9 @@ build_tree (TGallerySetup *setup, GList *job_items; GList *l; TPathInfo *child_path_info; + TGalleryDesignTheme *theme; + TImageSize *image_size; + printf ("Processing directory \"%s\"\n", path_info->src_dir); stats_dirs_inc (); @@ -233,15 +241,17 @@ build_tree (TGallerySetup *setup, items->parent_index = parent_index; items->parent_item_index = parent_item_index; - /* Check if update is necessary */ - dst_album_file = g_build_filename (path_info->dest_dir, setup->index_file_name, NULL); - force_update = (! setup->update_mode || needs_update (idx_file, dst_album_file)); - g_free (idx_file); - /* Copy support files */ - if (! setup->support_files_use_common_root || parent_index == NULL) { + if (! setup->supplemental_files_use_common_root || parent_index == NULL) { /* copy only if we're in root level or old-style is active */ - mirror_files (setup, setup->template_files, setup->real_templates_dir, path_info->dest_dir, " Copying template files: "); + mirror_files (setup, setup->design->supplemental_files, path_info->templates_root, path_info->dest_dir, " Copying global theme supplemental files: "); + + for (l = g_list_first (setup->design->themes); l; l = g_list_next (l)) { + theme = l->data; + g_assert (theme != NULL); + if (theme->enabled) + mirror_files (setup, theme->supplemental_files, path_info->templates_root, path_info->dest_dir, " Copying theme supplemental files: "); + } /* favicon */ if (setup->favicon_file && strlen (setup->favicon_file) > 0) { @@ -258,41 +268,45 @@ build_tree (TGallerySetup *setup, } } - /* Prepare target thumbnail directory */ - thumb_dir = g_build_path (G_DIR_SEPARATOR_S, path_info->dest_dir, setup->thumbnail_dir, NULL); - if (access (thumb_dir, R_OK | W_OK | X_OK)) - if (g_mkdir_with_parents (thumb_dir, DEFAULT_DATA_DIR_MODE)) { - log_error ("error making target thumbnail directory: %s\n", strerror (errno)); - g_free (thumb_dir); - g_free (dst_album_file); - free_album_data (items); - return FALSE; + /* Copy extra files */ + mirror_files (setup, items->extra_files, path_info->src_dir, path_info->dest_dir, " Copying extra files: "); + + /* Prepare target image directories */ + for (l = g_list_first (setup->design->image_sizes); l; l = g_list_next (l)) { + image_size = l->data; + g_assert (image_size != NULL); + /* We don't allow having single pictures in index pages at the moment, might change in the future */ + if (items->type != GALLERY_TYPE_INDEX || g_ascii_strcasecmp (image_size->name, "thumbnail") == 0) { + s1 = g_strdup_printf ("%s%s", TARGET_IMAGE_DIR_PREFIX, image_size->name); + s2 = g_build_path (G_DIR_SEPARATOR_S, path_info->dest_dir, s1, NULL); + g_free (s1); + if (access (s2, R_OK | W_OK | X_OK)) + if (g_mkdir_with_parents (s2, DEFAULT_DATA_DIR_MODE)) { + log_error ("error making target image directory '%s': %s\n", s2, strerror (errno)); + g_free (s2); + free_album_data (items); + return FALSE; + } + g_free (s2); } - g_free (thumb_dir); - - /* Prepare target preview and orig directories */ - if (items->type == GALLERY_TYPE_ALBUM) { - res = TRUE; - img_big_dir = g_build_path (G_DIR_SEPARATOR_S, path_info->dest_dir, setup->img_big_dir, NULL); - img_orig_dir = g_build_path (G_DIR_SEPARATOR_S, path_info->dest_dir, setup->img_orig_dir, NULL); - if (access (img_big_dir, R_OK | W_OK | X_OK)) - if (g_mkdir_with_parents (img_big_dir, DEFAULT_DATA_DIR_MODE)) { - log_error ("error making target preview directory: %s\n", strerror (errno)); - res = FALSE; - } - if (access (img_orig_dir, R_OK | W_OK | X_OK)) - if (g_mkdir_with_parents (img_orig_dir, DEFAULT_DATA_DIR_MODE)) { - log_error ("error making target full size directory: %s\n", strerror (errno)); - res = FALSE; + } + + /* Check if update of whole album/index is necessary */ + force_update = ! setup->update_mode; + if (! force_update) + for (l = g_list_first (setup->design->themes); l; l = g_list_next (l)) { + theme = l->data; + g_assert (theme != NULL); + if (theme->enabled) { + s1 = (items->type == GALLERY_TYPE_ALBUM || ! theme->index_filename) ? theme->album_filename : theme->index_filename; + s2 = g_build_filename (path_info->dest_dir, s1, NULL); + force_update = needs_update (idx_file, s2); + g_free (s2); } - g_free (img_big_dir); - g_free (img_orig_dir); - if (! res) { - g_free (dst_album_file); - free_album_data (items); - return FALSE; + if (force_update) + break; } - } + g_free (idx_file); /* Prepare job manager structures */ job = g_malloc0 (sizeof (TJob)); @@ -356,28 +370,28 @@ build_tree (TGallerySetup *setup, g_list_free (job_items); /* Generate album page */ - if (force_update) { - if (items->type == GALLERY_TYPE_INDEX) - template = setup->template_index; - else - if (items->type == GALLERY_TYPE_ALBUM) - template = setup->template_album; - else - /* default to album */ - template = setup->template_album; - - if (setup->verbose) printf (" Writing %s\n", setup->index_file_name); - s1 = g_build_filename (setup->real_templates_dir, template, NULL); - res = write_html_album (setup, path_info, s1, dst_album_file, items); - g_free (s1); - if (! res) { - log_error ("error generating target index file\n"); - free_album_data (items); - return FALSE; + if (force_update) + for (l = g_list_first (setup->design->themes); l; l = g_list_next (l)) { + theme = l->data; + g_assert (theme != NULL); + if (theme->enabled) { + if (items->type == GALLERY_TYPE_ALBUM || ! theme->index_template) { + s1 = theme->album_template; + s2 = theme->album_filename; + } else { + s1 = theme->index_template; + s2 = theme->index_filename; + } + if (setup->verbose) + printf (" Writing %s\n", s2); + s3 = g_build_filename (path_info->templates_root, s1, NULL); + s4 = g_build_filename (path_info->dest_dir, s2, NULL); + if (! write_html_album (setup, path_info, theme, s3, s4, items)) + log_error ("error generating target index file\n"); + g_free (s3); + g_free (s4); + } } - } - g_free (dst_album_file); - /* Recurse to sub-albums (in case of album index) */ if (items->type == GALLERY_TYPE_INDEX) { @@ -390,6 +404,7 @@ build_tree (TGallerySetup *setup, } if (item->type == INDEX_ITEM_TYPE_PICTURE) { child_path_info = g_malloc0 (sizeof (TPathInfo)); + child_path_info->templates_root = g_strdup (path_info->templates_root); child_path_info->source_root = g_strdup (path_info->source_root); child_path_info->dest_root = g_strdup (path_info->dest_root); child_path_info->src_dir = g_build_path (G_DIR_SEPARATOR_S, path_info->src_dir, item->path, NULL); @@ -402,9 +417,6 @@ build_tree (TGallerySetup *setup, } } - /* Copy extra files */ - mirror_files (setup, items->extra_files, path_info->src_dir, path_info->dest_dir, " Copying extra files: "); - free_album_data (items); return TRUE; } -- cgit v1.2.3