summaryrefslogtreecommitdiff
path: root/src/generators.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2009-02-26 22:50:39 +0100
committerTomas Bzatek <tbzatek@users.sourceforge.net>2009-02-26 22:50:39 +0100
commit89c58dc04c264c5778ae34d1428e12483f3ac5ac (patch)
treed5aef506841b3b0e2e91016d0c0bbb608535873c /src/generators.c
parent10a77c7a1c4648693ded958d6ac8641afcdf1d34 (diff)
downloadcataract-89c58dc04c264c5778ae34d1428e12483f3ac5ac.tar.xz
Autotoolize
Diffstat (limited to 'src/generators.c')
-rw-r--r--src/generators.c1037
1 files changed, 1037 insertions, 0 deletions
diff --git a/src/generators.c b/src/generators.c
new file mode 100644
index 0000000..8a2053a
--- /dev/null
+++ b/src/generators.c
@@ -0,0 +1,1037 @@
+/* Cataract - Static web photo gallery generator
+ * Copyright (C) 2008 Tomas Bzatek <tbzatek@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <glib.h>
+
+#include <config.h>
+
+#include "cgg-config.h"
+#include "setup.h"
+#include "items.h"
+#include "jpeg-utils.h"
+#include "gallery-utils.h"
+#include "generators-replace-table.h"
+
+
+/*
+ * generate_image: auxiliary function for write_html_album
+ * - img_src and thumb should be freed afterwards
+ */
+void
+generate_image (TGallerySetup *setup,
+ TAlbum *items,
+ TIndexItem *item,
+ unsigned int item_index,
+ const char *dst,
+ unsigned long *img_w, unsigned long *img_h,
+ char **img_src, char **thumb)
+{
+ unsigned long new_w, new_h;
+ unsigned long thumb_w, thumb_h;
+ char *thumb_dst;
+ char *big_dst;
+ char *big_src;
+ char *orig_dst;
+ char *img_src_full;
+ char *thumb_src_full;
+ char *s1;
+ int bigq;
+
+
+ *img_src = NULL;
+ *thumb = NULL;
+ *img_w = 0;
+ *img_h = 0;
+
+ if (items->type == GALLERY_TYPE_INDEX) {
+ if (item->thumbnail == NULL || strlen (item->thumbnail) == 0)
+ return;
+ img_src_full = g_strconcat (items->base_dir, "/", item->thumbnail, NULL);
+ thumb_src_full = g_strconcat (items->base_dir, "/", item->thumbnail, NULL);
+ *img_src = g_path_get_basename (item->thumbnail);
+ s1 = g_path_get_basename (item->thumbnail);
+ *thumb = g_strdup_printf ("thn_%.3d_%s", item_index, s1);
+ g_free (s1);
+ }
+ else
+ if (items->type == GALLERY_TYPE_ALBUM) {
+ *img_src = (item->path == NULL && item->preview) ? item->preview : item->path;
+ *thumb = (item->thumbnail) ? item->thumbnail : *img_src;
+ img_src_full = g_strconcat (items->base_dir, "/", *img_src, NULL);
+ thumb_src_full = g_strconcat (items->base_dir, "/", *thumb, NULL);
+ *img_src = g_path_get_basename (*img_src);
+ s1 = g_path_get_basename (*thumb);
+ *thumb = g_strdup_printf ("thn_%.3d_%s", item_index, s1);
+ g_free (s1);
+ }
+
+ get_image_sizes (img_src_full, img_w, img_h);
+
+ if ((img_w > 0) && (img_h > 0)) {
+ new_w = *img_w;
+ new_h = *img_h;
+
+ /* Generate thumbnail */
+ get_image_sizes (thumb_src_full, &thumb_w, &thumb_h);
+ s1 = g_path_get_dirname (dst);
+ thumb_dst = g_strconcat (s1, "/", THUMBNAIL_DIR, "/", *thumb, NULL);
+ g_free (s1);
+ if (setup->verbose) printf (" Generating thumbnail of '%s' ...", *thumb);
+
+ if ((thumb_w > 0) && (thumb_h > 0)) {
+ if ((thumb_w / thumb_h) >= 1)
+ calculate_sizes (setup->thumbnail_landscape_width, setup->thumbnail_landscape_height, &thumb_w, &thumb_h);
+ else
+ calculate_sizes (setup->thumbnail_portrait_width, setup->thumbnail_portrait_height, &thumb_w, &thumb_h);
+ if (! resize_image (thumb_src_full, thumb_dst, thumb_w, thumb_h, setup->thumbnail_quality, TRUE))
+ fprintf (stderr, "write_html_index: error resizing thumbnail %s\n", thumb_src_full);
+ else
+ if (setup->verbose) printf (" done.\n");
+ g_free (thumb_dst);
+ } else
+ printf (" failed!\n");
+
+
+ /* Generate/copy preview and original image */
+ if (items->type == GALLERY_TYPE_ALBUM)
+ {
+ s1 = g_path_get_dirname (dst);
+ big_dst = g_strconcat (s1, "/", IMG_BIG_DIR, "/", *img_src, NULL);
+ g_free (s1);
+
+ if (item->preview == NULL) {
+ /* No preview image supplied, generate it from original */
+ bigq = setup->preview_quality;
+ if ((items->quality > 0) && (items->quality <= 100))
+ bigq = items->quality;
+ if ((item->quality > 0) && (item->quality <= 100))
+ bigq = item->quality;
+ new_w = *img_w;
+ new_h = *img_h;
+
+ if (setup->verbose) printf (" Generating preview of '%s' ...", *img_src);
+ if ((item->width > 0) && (item->height > 0)) {
+ calculate_sizes (item->width, item->height, &new_w, &new_h);
+ } else {
+ if ((*img_w / *img_h) >= 1)
+ {
+ if ((items->landscape_width > 0) && (items->landscape_height > 0))
+ calculate_sizes (items->landscape_width, items->landscape_height, &new_w, &new_h);
+ else
+ calculate_sizes (setup->preview_landscape_width, setup->preview_landscape_height, &new_w, &new_h);
+ }
+ else
+ {
+ if ((items->portrait_width > 0) && (items->portrait_height > 0))
+ calculate_sizes (items->portrait_width, items->portrait_height, &new_w, &new_h);
+ else
+ calculate_sizes (setup->preview_portrait_width, setup->preview_portrait_height, &new_w, &new_h);
+ }
+ }
+
+ if (! resize_image (img_src_full, big_dst, new_w, new_h, bigq, FALSE))
+ fprintf (stderr, "write_html_index: error resizing big image %s\n", img_src_full);
+ else
+ if (setup->verbose) printf (" done.\n");
+ }
+ else
+ {
+ /* Copy the preview (big) image provided */
+ big_src = g_strconcat (items->base_dir, "/", item->preview, NULL);
+ if (setup->verbose) printf (" Copying preview image '%s' ...", *img_src);
+ if (! copy_file (big_src, big_dst))
+ fprintf (stderr, "write_html_index: error copying preview image %s\n", big_src);
+ else
+ if (setup->verbose) printf (" done.\n");
+ g_free (big_src);
+ }
+ modify_exif (big_dst, setup->erase_exif_thumbnail, setup->add_copyright);
+ g_free (big_dst);
+
+ if (item->force_fullsize || (items->fullsize && ! item->force_nofullsize) ||
+ (! item->force_nofullsize && ! items->nofullsize && ! setup->nofullsize))
+ {
+ s1 = g_path_get_dirname(dst);
+ orig_dst = g_strconcat (s1, "/", IMG_ORIG_DIR, "/", *img_src, NULL);
+ g_free (s1);
+ if (setup->verbose) printf (" Copying original image '%s' ...", *img_src);
+ if (! copy_file(img_src_full, orig_dst))
+ fprintf (stderr, "write_html_index: error copying original image %s\n", img_src_full);
+ else
+ if (setup->verbose) printf(" done.\n");
+ modify_exif (orig_dst, setup->erase_exif_thumbnail, setup->add_copyright);
+ g_free (orig_dst);
+ }
+ }
+ }
+ g_free (img_src_full);
+ g_free (thumb_src_full);
+}
+
+
+
+/*
+ * write_html_album: process album and index template files
+ *
+ * template_src = template file of the album/index
+ * dst = save generated file as
+ * items = array of items in the album/index
+ *
+ */
+gboolean
+write_html_album (TGallerySetup *setup,
+ const char *template_src,
+ const char *dst,
+ TAlbum *items)
+{
+ #define BUFFER_SIZE 65536
+ FILE *fin;
+ FILE *fout;
+ char *buffer;
+ char *buf_img_list_landscape;
+ char *buf_img_list_portrait;
+ char *buf_img_separator;
+ char *buf_go_up_string;
+ gboolean in_img_list;
+ gboolean in_img_list_landscape;
+ gboolean in_img_list_portrait;
+ gboolean in_img_separator;
+ gboolean in_go_up_string;
+ char *b;
+ char *s1, *s2, *s3, *s4, *s5;
+ TAlbum *parent;
+ TIndexItem *item;
+ TIndexItem *tmp_item;
+ int level, old_parent_item_index;
+ gboolean res, bb;
+ int i;
+ unsigned long img_w, img_h;
+ char *img_src;
+ char *thumb;
+ unsigned int real_total_items;
+ ReplaceTable *global_replace_table;
+ ReplaceTable *local_replace_table;
+
+
+ fin = fopen (template_src, "r");
+ if (fin == NULL) {
+ fprintf (stderr, "write_html_index: error reading file \"%s\": %s\n", template_src, strerror (errno));
+ return FALSE;
+ }
+ fout = fopen (dst, "w");
+ if (fout == NULL) {
+ fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno));
+ fclose (fin);
+ return FALSE;
+ }
+
+ buffer = malloc (BUFFER_SIZE);
+ buf_img_list_landscape = malloc (BUFFER_SIZE);
+ buf_img_list_portrait = malloc (BUFFER_SIZE);
+ buf_img_separator = malloc (BUFFER_SIZE);
+ buf_go_up_string = malloc (BUFFER_SIZE);
+ in_img_list = FALSE;
+ in_img_list_landscape = FALSE;
+ in_img_list_portrait = FALSE;
+ in_img_separator = FALSE;
+ in_go_up_string = FALSE;
+ res = TRUE;
+
+ global_replace_table = replace_table_new ();
+
+ /* Get number of real pictures in the list */
+ real_total_items = 0;
+ for (i = 0; i < items->items->len; i++) {
+ tmp_item = g_ptr_array_index (items->items, i);
+ if (tmp_item->type == INDEX_ITEM_TYPE_PICTURE)
+ real_total_items++;
+ }
+ replace_table_add_key_int (global_replace_table, "TOTAL_ITEMS", real_total_items);
+
+ /* Page title */
+ if (items->parent_index == NULL || setup->site_title == NULL)
+ s1 = g_strdup (setup->site_title ? setup->site_title : items->ID);
+ else
+ s1 = g_strdup_printf ("%s | %s", items->title, setup->site_title);
+ replace_table_add_key (global_replace_table, "PAGE_TITLE", s1);
+ g_free (s1);
+
+ /* Simple placeholders */
+ replace_table_add_key (global_replace_table, "ID", items->ID);
+ replace_table_add_key (global_replace_table, "TITLE", items->title);
+ replace_table_add_key (global_replace_table, "DESCRIPTION", items->desc);
+ replace_table_add_key (global_replace_table, "FOOTNOTE", items->footnote);
+ replace_table_add_key (global_replace_table, "FOOTER", setup->footer);
+ s1 = setup->use_inpage_links ? g_strdup_printf ("../index.html#i%d", items->parent_item_index + 1) : g_strdup ("../index.html");
+ replace_table_add_key (global_replace_table, "GO_UP_LINK", s1);
+ g_free (s1);
+
+ /* Navigation bar */
+ s1 = g_strdup (items->ID);
+ old_parent_item_index = items->parent_item_index + 1;
+ parent = items->parent_index;
+ level = 1;
+ while (parent) {
+ s3 = make_string ("../", level);
+ s4 = g_strdup (parent->ID);
+ s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup ("");
+ s2 = g_strdup_printf ("<a href=\"%sindex.html%s\">%s</a> &gt; %s", s3, s5, s4, s1);
+ free (s3);
+ g_free (s1);
+ g_free (s4);
+ g_free (s5);
+ s1 = s2;
+ old_parent_item_index = parent->parent_item_index + 1;
+ parent = parent->parent_index;
+ level++;
+ }
+ replace_table_add_key (global_replace_table, "NAV_BAR", s1);
+ g_free (s1);
+
+ /* META tags */
+ s1 = g_strdup_printf ("\t<meta name=\"generator\" content=\"Cataract Gallery Generator v%s\" />\n", VERSION);
+ if (setup->meta_author || items->meta_author) {
+ s3 = g_strdup (items->meta_author ? items->meta_author : setup->meta_author);
+ adjust_tags_parameter (&s3);
+ s2 = g_strdup_printf ("%s\t<meta name=\"author\" content=\"%s\" />\n", s1, s3);
+ g_free (s3);
+ g_free (s1);
+ s1 = s2;
+ }
+ if (setup->meta_description || items->meta_description) {
+ s3 = g_strdup (items->meta_description ? items->meta_description : setup->meta_description);
+ adjust_tags_parameter (&s3);
+ s2 = g_strdup_printf ("%s\t<meta name=\"description\" content=\"%s\" />\n", s1, s3);
+ g_free (s3);
+ g_free (s1);
+ s1 = s2;
+ }
+ if (setup->meta_keywords || items->meta_keywords) {
+ s3 = g_strdup (items->meta_keywords ? items->meta_keywords : setup->meta_keywords);
+ adjust_tags_parameter (&s3);
+ s2 = g_strdup_printf ("%s\t<meta name=\"keywords\" content=\"%s\" />\n", s1, s3);
+ g_free (s3);
+ g_free (s1);
+ s1 = s2;
+ }
+ replace_table_add_key (global_replace_table, "CGG_META_TAGS", s1);
+ g_free (s1);
+
+
+ /* Read through the template and replace placeholders with real data */
+ while (! feof (fin))
+ {
+ memset (buffer, 0, BUFFER_SIZE);
+ if (! fgets (buffer, BUFFER_SIZE, fin))
+ break;
+
+ /* Block placeholders */
+ if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_IMG_LIST_LANDSCAPE) -->"))) {
+ in_img_list_landscape = TRUE;
+ continue;
+ }
+ if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST_LANDSCAPE) -->"))) {
+ in_img_list_landscape = FALSE;
+ continue;
+ }
+ if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_IMG_LIST_PORTRAIT) -->"))) {
+ in_img_list_portrait = TRUE;
+ continue;
+ }
+ if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST_PORTRAIT) -->"))) {
+ in_img_list_portrait = FALSE;
+ continue;
+ }
+ if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_LIST_SEPARATOR) -->"))) {
+ in_img_separator = TRUE;
+ continue;
+ }
+ if (in_img_list && (strstr (buffer, "<!-- $(END_LIST_SEPARATOR) -->"))) {
+ in_img_separator = FALSE;
+ continue;
+ }
+ if (in_img_list && in_img_list_landscape) {
+ buf_img_list_landscape = strncat (buf_img_list_landscape, buffer, BUFFER_SIZE - strlen (buf_img_list_landscape) - 2);
+ continue;
+ }
+ if (in_img_list && in_img_list_portrait) {
+ buf_img_list_portrait = strncat (buf_img_list_portrait, buffer, BUFFER_SIZE - strlen (buf_img_list_portrait) - 2);
+ continue;
+ }
+ if (in_img_list && in_img_separator) {
+ buf_img_separator = strncat (buf_img_separator, buffer, BUFFER_SIZE - strlen (buf_img_separator) - 2);
+ continue;
+ }
+
+ if (strstr (buffer, "<!-- $(BEGIN_GO_UP) -->")) {
+ memset (buf_go_up_string, 0, BUFFER_SIZE);
+ in_go_up_string = TRUE;
+ continue;
+ }
+ if (in_go_up_string && strstr (buffer, "<!-- $(END_GO_UP) -->")) {
+ in_go_up_string = FALSE;
+ if (! items->parent_index)
+ continue;
+ }
+ if (in_go_up_string) {
+ buf_go_up_string = strncat (buf_go_up_string, buffer, BUFFER_SIZE - strlen (buf_go_up_string) - 2);
+ continue;
+ }
+
+ /* Image list, nested placeholders */
+ if (strstr (buffer, "<!-- $(BEGIN_IMG_LIST) -->")) {
+ memset (buf_img_list_landscape, 0, BUFFER_SIZE);
+ memset (buf_img_list_portrait, 0, BUFFER_SIZE);
+ memset (buf_img_separator, 0, BUFFER_SIZE);
+ in_img_list = TRUE;
+ in_img_list_landscape = FALSE;
+ in_img_list_portrait = FALSE;
+ in_img_separator = FALSE;
+ continue;
+ }
+ if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST) -->"))) {
+ in_img_list = FALSE;
+ in_img_list_landscape = FALSE;
+ in_img_list_portrait = FALSE;
+
+ /* Now we have all block placeholders read, generate the items: */
+ for (i = 0; i < items->items->len; i++)
+ {
+ item = g_ptr_array_index (items->items, i);
+ if (item == NULL) {
+ fprintf (stderr, "write_html_index: error getting item %d\n", i);
+ continue;
+ }
+
+ /* Generate the images (preview, original, thumbnail) */
+ local_replace_table = replace_table_new ();
+ img_w = 0;
+ img_h = 0;
+ s1 = NULL;
+
+ switch (item->type) {
+ case INDEX_ITEM_TYPE_PICTURE:
+ img_src = NULL;
+ thumb = NULL;
+ generate_image (setup, items, item, i, dst, &img_w, &img_h, &img_src, &thumb);
+ /* Skip HTML code generation if it's a hidden item */
+ if (! item->hidden) {
+ if (img_w == 0 || img_h == 0 || (img_w / img_h) >= 1)
+ s1 = strdup (buf_img_list_landscape);
+ else
+ s1 = strdup (buf_img_list_portrait);
+
+ replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/index.html", item->path);
+ replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", "%s.html", img_src);
+ replace_table_add_key (local_replace_table, "IMG_TITLE", item->title);
+ replace_table_add_key (local_replace_table, "IMG_DESCRIPTION", item->title_description);
+ replace_table_add_key_printf (local_replace_table, "IMG_LIST_ID", "i%d", i + 1);
+ replace_table_add_key_printf (local_replace_table, "IMG_THUMBNAIL", "%s/%s", THUMBNAIL_DIR, thumb);
+ replace_table_add_key (local_replace_table, "IMG_FILENAME", img_src);
+ if (items->type == GALLERY_TYPE_INDEX) {
+ s3 = g_strconcat (items->base_dir, "/", item->path, "/index.xml", NULL);
+ replace_table_add_key_int (local_replace_table, "ALBUM_NUM_ITEMS", get_album_objects_count (s3));
+ g_free (s3);
+ }
+ }
+ if (img_src)
+ g_free (img_src);
+ if (thumb)
+ g_free (thumb);
+ break;
+
+ case INDEX_ITEM_TYPE_SEPARATOR:
+ s1 = strdup (buf_img_separator);
+ replace_table_add_key (local_replace_table, "LIST_SEPARATOR_TITLE", item->title);
+ break;
+ }
+
+ bb = TRUE;
+ if (s1) {
+ replace_table_process (&s1, local_replace_table);
+ bb = fputs (s1, fout);
+ free (s1);
+ }
+ replace_table_free (local_replace_table);
+ if (! bb) {
+ fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno));
+ res = FALSE;
+ break;
+ }
+ }
+ continue; /* no need to write anything */
+ }
+
+ /* Select apropriate line buffer */
+ if (strstr (buffer, "<!-- $(END_GO_UP) -->") && items->parent_index) {
+ b = strdup (buf_go_up_string);
+ } else
+ b = strdup (buffer);
+
+ /* Replace all known tags */
+ replace_table_process (&b, global_replace_table);
+
+ /* Write to file */
+ bb = fputs (b, fout);
+ free (b);
+ if (! bb) {
+ fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno));
+ res = FALSE;
+ break;
+ }
+ }
+
+ fclose (fout);
+ fclose (fin);
+ replace_table_free (global_replace_table);
+ free (buffer);
+ free (buf_img_list_landscape);
+ free (buf_img_list_portrait);
+ free (buf_img_separator);
+ free (buf_go_up_string);
+ return res;
+}
+
+
+/*
+ * write_html_image: process single image template file
+ *
+ * template_src = template file of the album/index
+ * original_img = source image file (original full-size) to get EXIF data from
+ * dst = save generated file as
+ * item = data for the current item
+ * parent_items = array of items in the album, to determine our position and make links to previous/next image
+ *
+ */
+gboolean
+write_html_image (TGallerySetup *setup,
+ const char *template_src,
+ const char *original_img,
+ const char *dst,
+ TIndexItem *item,
+ TAlbum *parent_items)
+{
+ #define BUFFER_SIZE 65536
+ FILE *fin;
+ FILE *fout;
+ char *buffer;
+ char *big_dst;
+ char *orig_dst;
+ char *buf_img_fullsize_link;
+ gboolean in_img_fullsize_link;
+ TExifData *exif;
+ unsigned long img_big_w, img_big_h, img_orig_w, img_orig_h;
+ unsigned int item_index, real_item_index, real_total_items;
+ TIndexItem *previous_item;
+ TIndexItem *next_item;
+ TIndexItem *tmp_item;
+ TAlbum *parent;
+ int i;
+ char *s1, *s2, *s3, *s4, *s5;
+ char *imgname, *preload_imgname;
+ char *title, *title_desc;
+ char *b;
+ gboolean res, bb;
+ int level, old_parent_item_index;
+ gboolean override_title_meta;
+ gboolean image_fullsize;
+ ReplaceTable *replace_table;
+
+
+ fin = fopen (template_src, "r");
+ if (fin == NULL) {
+ fprintf (stderr, "write_html_image: error reading file \"%s\": %s\n", template_src, strerror (errno));
+ return FALSE;
+ }
+ fout = fopen (dst, "w");
+ if (fout == NULL) {
+ fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno));
+ fclose (fin);
+ return FALSE;
+ }
+
+ buffer = malloc (BUFFER_SIZE);
+ preload_imgname = NULL;
+
+ replace_table = replace_table_new ();
+
+ /* Get our index in the album */
+ item_index = 0;
+ real_item_index = 0;
+ real_total_items = 0;
+ for (i = 0; i < parent_items->items->len; i++) {
+ tmp_item = g_ptr_array_index (parent_items->items, i);
+ if (tmp_item->type == INDEX_ITEM_TYPE_PICTURE) {
+ if (! item_index) real_item_index++;
+ real_total_items++;
+ }
+ if (tmp_item == item)
+ item_index = i + 1;
+ }
+
+ /* Get previous and next items */
+ previous_item = NULL;
+ next_item = NULL;
+ for (i = item_index - 2; i >= 0 && (previous_item == NULL || previous_item->type != INDEX_ITEM_TYPE_PICTURE); i--)
+ previous_item = g_ptr_array_index (parent_items->items, i);
+ if (previous_item && previous_item->type != INDEX_ITEM_TYPE_PICTURE)
+ previous_item = NULL;
+ for (i = item_index; item_index < parent_items->items->len && (next_item == NULL || next_item->type != INDEX_ITEM_TYPE_PICTURE); i++)
+ next_item = g_ptr_array_index (parent_items->items, i);
+ if (next_item && next_item->type != INDEX_ITEM_TYPE_PICTURE)
+ next_item = NULL;
+
+ /* Paths setup */
+ imgname = g_path_get_basename ((item->path == NULL && item->preview) ? item->preview : item->path);
+ if (next_item && setup->preload) {
+ s1 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path);
+ preload_imgname = g_strconcat (IMG_BIG_DIR, "/", s1, NULL);
+ g_free (s1);
+ }
+ s1 = g_path_get_dirname (dst);
+ big_dst = g_strconcat (s1, "/", IMG_BIG_DIR, "/", imgname, NULL);
+ orig_dst = g_strconcat (s1, "/", IMG_ORIG_DIR, "/", imgname, NULL);
+ g_free (s1);
+ buf_img_fullsize_link = malloc (BUFFER_SIZE);
+ memset (buf_img_fullsize_link, 0, BUFFER_SIZE);
+ in_img_fullsize_link = FALSE;
+ res = TRUE;
+
+ /* Get EXIF data from the original image */
+ if (get_exif (original_img, &exif))
+ fprintf (stderr, "write_html_image: error getting exif data from file \"%s\"\n", orig_dst);
+
+ /* Retrieve image sizes of preview and original image */
+ get_image_sizes (big_dst, &img_big_w, &img_big_h);
+ image_fullsize = item->force_fullsize || (parent_items->fullsize && ! item->force_nofullsize) ||
+ (! item->force_nofullsize && ! parent_items->nofullsize && ! setup->nofullsize);
+ if (image_fullsize)
+ get_image_sizes (orig_dst, &img_orig_w, &img_orig_h);
+
+ /* Get title and description from IPTC/EXIF/JPEG if not defined */
+ title = g_strdup (item->title);
+ title_desc = g_strdup (item->title_description);
+ if (setup->use_iptc_exif && title == NULL && title_desc == NULL) {
+ if (exif->iptc_caption)
+ title = g_strdup (exif->iptc_caption);
+ if (exif->jpeg_comment) {
+ if (! title)
+ title = g_strdup (exif->jpeg_comment);
+ else
+ title_desc = g_strdup (exif->jpeg_comment);
+ }
+ if (exif->exif_imgdesc) {
+ if (! title)
+ title = g_strdup (exif->exif_imgdesc);
+/* if (! title_desc) -- disabled
+ title_desc = g_strdup (exif->exif_imgdesc); */
+ }
+ if (exif->exif_usercomment) {
+ if (! title)
+ title = g_strdup (exif->exif_usercomment);
+ if (! title_desc)
+ title_desc = g_strdup (exif->exif_usercomment);
+ }
+ /* Convert line breaks to be visible in the HTML code */
+ if (title) {
+ str_replace (&title, "\r\n", "<br />");
+ str_replace (&title, "\n", "<br />");
+ }
+ if (title_desc) {
+ str_replace (&title_desc, "\r\n", "<br />");
+ str_replace (&title_desc, "\n", "<br />");
+ }
+ }
+ if (title) title = g_strstrip (title);
+ if (title_desc) title_desc = g_strstrip (title_desc);
+
+ /* Page title */
+ s1 = (title && strlen (title) > 0) ? g_strdup_printf("%s | ", title) : NULL;
+ s2 = g_strdup_printf ("%s [%d/%d]", parent_items->title ? parent_items->title : parent_items->ID, real_item_index, real_total_items);
+ s3 = setup->site_title ? g_strdup_printf(" | %s", setup->site_title) : NULL;
+ s4 = g_strconcat (s1 ? s1 : "", s2, s3 ? s3 : "", NULL);
+ replace_table_add_key (replace_table, "PAGE_TITLE", s4);
+ if (s1) g_free (s1);
+ if (s2) g_free (s2);
+ if (s3) g_free (s3);
+ g_free (s4);
+
+ /* Simple placeholders */
+ replace_table_add_key (replace_table, "FILE_NAME", imgname);
+ replace_table_add_key (replace_table, "TITLE", title);
+ replace_table_add_key (replace_table, "DESCRIPTION", title_desc);
+ replace_table_add_key (replace_table, "FOOTER", setup->footer);
+ replace_table_add_key_int (replace_table, "TOTAL_ITEMS", real_total_items);
+ replace_table_add_key_int (replace_table, "FILE_NO", real_item_index);
+ replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_W", img_big_w);
+ replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_H", img_big_h);
+ replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_W", img_orig_w);
+ replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_H", img_orig_h);
+ replace_table_add_key_printf (replace_table, "IMG_SRC_BIG", "%s/%s", IMG_BIG_DIR, imgname);
+ replace_table_add_key_printf (replace_table, "IMG_SRC_FULL", "%s/%s", IMG_ORIG_DIR, imgname);
+ replace_table_add_key (replace_table, "IMG_SRC_PRELOAD", preload_imgname ? preload_imgname : "");
+
+ /* Navigation bar */
+ s1 = g_strdup (imgname);
+ parent = parent_items;
+ level = 0;
+ while (parent) {
+ s3 = make_string ("../", level);
+ s4 = g_strdup (parent->ID);
+ s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", parent == parent_items ? item_index : old_parent_item_index) : g_strdup ("");
+ s2 = g_strdup_printf ("<a href=\"%sindex.html%s\">%s</a> &gt; %s", s3, s5, s4, s1);
+ free (s3);
+ g_free (s1);
+ g_free (s4);
+ g_free (s5);
+ s1 = s2;
+ old_parent_item_index = parent->parent_item_index + 1;
+ parent = parent->parent_index;
+ level++;
+ }
+ replace_table_add_key (replace_table, "NAV_BAR", s1);
+ g_free (s1);
+
+ /* EXIF values */
+ replace_table_add_key (replace_table, "EXIF_ISO", exif->iso ? exif->iso : "??");
+ replace_table_add_key (replace_table, "EXIF_TIME", exif->exposure ? exif->exposure : "??");
+ replace_table_add_key (replace_table, "EXIF_APERTURE", exif->aperture ? exif->aperture : "??");
+ replace_table_add_key (replace_table, "EXIF_FOCAL_LENGTH", exif->focal_length ? exif->focal_length : "??");
+ replace_table_add_key (replace_table, "EXIF_FLASH", exif->flash ? exif->flash : "??");
+ replace_table_add_key (replace_table, "EXIF_DATE", exif->datetime ? exif->datetime : "??");
+ replace_table_add_key (replace_table, "EXIF_CAMERA_MODEL", exif->camera_model ? exif->camera_model : "??");
+ s1 = g_strdup_printf ("(%s)", exif->focal_length_35mm);
+ replace_table_add_key (replace_table, "EXIF_FOCAL_35", exif->focal_length_35mm ? s1 : "");
+ g_free (s1);
+
+ /* Border style */
+ s1 = item->border_style;
+ if (s1 == NULL)
+ s1 = parent_items->border_style;
+ if (s1 == NULL)
+ s1 = setup->border_style;
+ if (s1 == NULL)
+ s1 = "border_single";
+ replace_table_add_key (replace_table, "IMG_BORDER_STYLE", s1);
+
+ /* Next/Previous links */
+ if (next_item) {
+ s2 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path);
+ replace_table_add_key_printf (replace_table, "LINK_NEXT", "%s.html", s2);
+ g_free (s2);
+ }
+ else
+ replace_table_add_key (replace_table, "LINK_NEXT", "index.html");
+ if (previous_item) {
+ s2 = g_path_get_basename ((previous_item->path == NULL && previous_item->preview) ? previous_item->preview : previous_item->path);
+ replace_table_add_key_printf (replace_table, "LINK_PREV", "%s.html", s2);
+ g_free (s2);
+ }
+ else
+ replace_table_add_key (replace_table, "LINK_PREV", "index.html");
+
+ /* META tags */
+ override_title_meta = setup->use_title_as_meta && title && (strlen (title) > 0);
+ s1 = g_strdup_printf ("\t<meta name=\"generator\" content=\"Cataract Gallery Generator v%s\" />\n", VERSION);
+ if (setup->meta_author || parent_items->meta_author) {
+ s3 = g_strdup (parent_items->meta_author ? parent_items->meta_author : setup->meta_author);
+ adjust_tags_parameter (&s3);
+ s2 = g_strdup_printf ("%s\t<meta name=\"author\" content=\"%s\" />\n", s1, s3);
+ g_free (s3);
+ g_free (s1);
+ s1 = s2;
+ }
+ if (setup->meta_description || parent_items->meta_description || override_title_meta) {
+ s3 = g_strdup (override_title_meta ? title : (parent_items->meta_description ? parent_items->meta_description : setup->meta_description));
+ adjust_tags_parameter (&s3);
+ s2 = g_strdup_printf ("%s\t<meta name=\"description\" content=\"%s\" />\n", s1, s3);
+ g_free (s3);
+ g_free (s1);
+ s1 = s2;
+ }
+ if ((setup->meta_keywords || parent_items->meta_keywords) && (! override_title_meta)) {
+ s3 = g_strdup (parent_items->meta_keywords ? parent_items->meta_keywords : setup->meta_keywords);
+ adjust_tags_parameter (&s3);
+ s2 = g_strdup_printf ("%s\t<meta name=\"keywords\" content=\"%s\" />\n", s1, s3);
+ g_free (s3);
+ g_free (s1);
+ s1 = s2;
+ }
+ replace_table_add_key (replace_table, "CGG_META_TAGS", s1);
+ g_free (s1);
+
+
+ /* Read through the template and replace placeholders with real data */
+ while (! feof (fin)) {
+ memset (buffer, 0, BUFFER_SIZE);
+ if (! fgets (buffer, BUFFER_SIZE, fin))
+ break;
+
+ /* Block placeholders */
+ if (strstr (buffer, "<!-- $(BEGIN_IMG_FULLSIZE_LINK) -->")) {
+ in_img_fullsize_link = TRUE;
+ continue;
+ }
+ if (strstr (buffer, "<!-- $(END_IMG_FULLSIZE_LINK) -->")) {
+ in_img_fullsize_link = FALSE;
+ if (! image_fullsize) /* write down the block later in this cycle */
+ continue;
+ }
+ if (in_img_fullsize_link) {
+ buf_img_fullsize_link = strncat (buf_img_fullsize_link, buffer, BUFFER_SIZE - strlen (buf_img_fullsize_link) - 2);
+ continue;
+ }
+
+ /* Select apropriate line buffer */
+ if (strstr (buffer, "<!-- $(END_IMG_FULLSIZE_LINK) -->") && image_fullsize) {
+ b = strdup (buf_img_fullsize_link);
+ } else
+ b = strdup (buffer);
+
+ /* Replace all known tags */
+ replace_table_process (&b, replace_table);
+
+ /* Write to file */
+ bb = fputs (b, fout);
+ free (b);
+ if (! bb) {
+ fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno));
+ res = FALSE;
+ break;
+ }
+ }
+
+ fclose (fout);
+ fclose (fin);
+ if (title) g_free (title);
+ if (title_desc) g_free (title_desc);
+ free (buffer);
+ free (big_dst);
+ free (orig_dst);
+ free (buf_img_fullsize_link);
+ g_free (imgname);
+ if (preload_imgname)
+ g_free (preload_imgname);
+ free_exif_data (exif);
+ replace_table_free (replace_table);
+ return res;
+}
+
+
+/*
+ * build_tree: generate complete gallery tree based on source xml files
+ *
+ * src_tree = source directory of the items
+ * dst_dir = destination of the generated items
+ * parent_index = parent album to determine our descent in the tree
+ *
+ */
+gboolean
+build_tree (TGallerySetup *setup,
+ const char *src_tree,
+ const char *dst_dir,
+ TAlbum *parent_index,
+ int parent_item_index)
+{
+ char *idx_file;
+ TAlbum *items;
+ TIndexItem *item;
+ char *s1, *s2, *s3;
+ char *thumb_dir;
+ char *img_big_dir;
+ char *img_orig_dir;
+ char *template;
+ char *imgname;
+ gboolean res;
+ int i;
+
+ printf ("*** Entering directory '%s'\n", src_tree);
+ #ifdef __DEBUG_ALL__
+ printf ("setup->real_templates_dir = %s\n", setup->real_templates_dir);
+ #endif
+
+ /* Check access permissions */
+ if (access (src_tree, R_OK)) {
+ fprintf (stderr, "error accessing source directory: %s\n", strerror (errno));
+ return FALSE;
+ }
+ if (access (dst_dir, R_OK | W_OK | X_OK)) {
+ if (mkdir (dst_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+ fprintf (stderr, "error creating destination directory: %s\n", strerror (errno));
+ return FALSE;
+ }
+ }
+
+ /* Check the index file */
+ idx_file = g_strconcat (src_tree, "/index.xml", NULL);
+ if (access (idx_file, R_OK)) {
+ fprintf (stderr, "error accessing index file '%s': %s\n", idx_file, strerror (errno));
+ g_free (idx_file);
+ return FALSE;
+ }
+
+ /* Read the index file and fill items array */
+ items = malloc (sizeof (TAlbum));
+ memset (items, 0, sizeof (TAlbum));
+ if (! parse_album_xml (idx_file, items)) {
+ fprintf (stderr, "error reading index file '%s'\n", idx_file);
+ g_free (idx_file);
+ free_album_data (items);
+ return FALSE;
+ }
+ g_free (idx_file);
+ items->parent_index = parent_index;
+ items->parent_item_index = parent_item_index;
+
+
+ /* Copy support files */
+ if (setup->verbose) printf ("Writing '%s' ...", setup->styles);
+ s1 = g_strconcat (setup->real_templates_dir, "/", setup->styles, NULL);
+ s2 = g_strconcat (dst_dir, "/", setup->styles, NULL);
+ copy_file (s1, s2);
+ g_free (s1);
+ g_free (s2);
+ if (setup->verbose) printf (" done.\n");
+
+ if (setup->verbose) printf ("Writing '%s' ...", setup->scripts);
+ s1 = g_strconcat (setup->real_templates_dir, "/", setup->scripts, NULL);
+ s2 = g_strconcat (dst_dir, "/", setup->scripts, NULL);
+ copy_file (s1, s2);
+ g_free (s1);
+ g_free (s2);
+ if (setup->verbose) printf (" done.\n");
+
+
+ /* Prepare target thumbnail directory */
+ thumb_dir = g_strconcat (dst_dir, "/", THUMBNAIL_DIR, NULL);
+ if (access (thumb_dir, R_OK | W_OK | X_OK))
+ if (mkdir (thumb_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+ fprintf (stderr, "error making target thumbnail directory: %s\n", strerror (errno));
+ g_free (thumb_dir);
+ free_album_data (items);
+ return FALSE;
+ }
+ g_free (thumb_dir);
+
+ /* Prepare target preview and orig directories */
+ if (items->type == GALLERY_TYPE_ALBUM)
+ {
+ res = TRUE;
+ img_big_dir = g_strconcat (dst_dir, "/", IMG_BIG_DIR, NULL);
+ img_orig_dir = g_strconcat (dst_dir, "/", IMG_ORIG_DIR, NULL);
+ if (access (img_big_dir, R_OK | W_OK | X_OK))
+ if (mkdir (img_big_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+ fprintf (stderr, "error making target preview directory: %s\n", strerror (errno));
+ res = FALSE;
+ }
+ if (access (img_orig_dir, R_OK | W_OK | X_OK))
+ if (mkdir (img_orig_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+ fprintf (stderr, "error making target full size directory: %s\n", strerror (errno));
+ res = FALSE;
+ }
+ g_free (img_big_dir);
+ g_free (img_orig_dir);
+ if (! res) {
+ free_album_data (items);
+ return FALSE;
+ }
+ }
+
+
+ /* Start generating items */
+ if (items->type == GALLERY_TYPE_INDEX)
+ template = g_strconcat ("/", setup->template_index, NULL);
+ else
+ if (items->type == GALLERY_TYPE_ALBUM)
+ template = g_strconcat ("/", setup->template_album, NULL);
+
+ if (setup->verbose) printf ("Writing 'index.html' ...\n");
+ s1 = g_strconcat (setup->real_templates_dir, template, NULL);
+ s2 = g_strconcat (dst_dir, "/index.html", NULL);
+ res = write_html_album (setup, s1, s2, items);
+ g_free (s1);
+ g_free (s2);
+ g_free (template);
+ if (! res) {
+ fprintf (stderr, "error generating target index file\n");
+ free_album_data (items);
+ return FALSE;
+ }
+ if (setup->verbose) printf (" done.\n");
+
+
+ /* Recurse to sub-albums (in case of album index) */
+ if (items->type == GALLERY_TYPE_INDEX)
+ {
+ if (items->items->len > 0) {
+ for (i = 0; i < items->items->len; i++) {
+ item = g_ptr_array_index (items->items, i);
+ if (item == NULL) {
+ fprintf (stderr, "build_tree: error getting item %d\n", i);
+ continue;
+ }
+ if (item->type == INDEX_ITEM_TYPE_PICTURE) {
+ s1 = g_strconcat (src_tree, "/", item->path, "/", NULL);
+ s2 = g_strconcat (dst_dir, "/", item->path, "/", NULL);
+ build_tree (setup, s1, s2, items, i);
+ g_free (s1);
+ g_free (s2);
+ }
+ }
+ }
+ }
+
+ /* Generate separate image pages (in case of album) */
+ if (items->type == GALLERY_TYPE_ALBUM)
+ {
+ if (items->items->len > 0) {
+ for (i = 0; i < items->items->len; i++) {
+ item = g_ptr_array_index (items->items, i);
+ if (item == NULL) {
+ fprintf (stderr, "build_tree: error getting item %d\n", i);
+ continue;
+ }
+ if (item->type == INDEX_ITEM_TYPE_PICTURE) {
+ imgname = g_path_get_basename ((item->path == NULL && item->preview) ? item->preview : item->path);
+ if (setup->verbose) printf ("Writing '%s.html' ...", imgname);
+ s1 = g_strconcat (setup->real_templates_dir, "/", setup->template_photo, NULL);
+ s2 = g_strconcat (items->base_dir, "/", (item->path == NULL && item->preview) ? item->preview : item->path, NULL);
+ s3 = g_strconcat (dst_dir, "/", imgname, ".html", NULL);
+ res = write_html_image (setup, s1, s2, s3, item, items);
+ g_free (s1);
+ g_free (s2);
+ g_free (s3);
+ g_free (imgname);
+ if (! res ) continue;
+ if (setup->verbose) printf (" done.\n");
+ }
+ }
+ }
+ }
+
+ printf ("*** Leaving directory '%s'\n", src_tree);
+ free_album_data (items);
+
+ return TRUE;
+}