summaryrefslogtreecommitdiff
path: root/src/setup.c
blob: 17d8fdac046ed1ba3eab480a16dbe4d0eab4654f (plain)
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
/*   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 <unistd.h>

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include <glib.h>

#include <config.h>

#include "gallery-utils.h"
#include "setup.h"
#include "xml-parser.h"



/*
 *   find_setup_xml: try to find setup.xml in standard paths
 */
gboolean
find_setup_xml (TGallerySetup *setup)
{
  #define BUFFER_SIZE 65536

  char *pth;
  char *cwd;
  gboolean b;

  cwd = malloc (BUFFER_SIZE);
  cwd = getcwd (cwd, BUFFER_SIZE);
  pth = g_strconcat (cwd, "/", SETUP_XML, NULL);
  free (cwd);

  b = parse_setup_xml (pth, setup);
  g_free (pth);
  if (b)  return TRUE;

  pth = g_strconcat (getenv ("HOME"), "/.cgg/", SETUP_XML, NULL);
  b = parse_setup_xml (pth, setup);
  g_free (pth);
  if (b)  return TRUE;

  pth = g_strconcat (DATADIR, "/cgg/", SETUP_XML, NULL);
  b = parse_setup_xml (pth, setup);
  g_free (pth);
  return b;
}


/*
 *   parse_setup_xml: XML parser for setup.xml file
 */
gboolean
parse_setup_xml (const char *filename, TGallerySetup *setup)
{
  TXMLFile *xml;
  char *s;

  xml = xml_parser_load (filename);
  if (xml == NULL)
	  return FALSE;

  /*  initialize data struct  */
  if (setup == NULL)
	  return FALSE;
  memset (setup, 0, sizeof (TGallerySetup));
  setup->setup_xml_path = strdup (filename);

  setup->templates_path = xml_file_get_node_value (xml, "/gallery_setup/templates/path/text()");
  setup->template_index = xml_file_get_node_value (xml, "/gallery_setup/templates/index/text()");
  setup->template_album = xml_file_get_node_value (xml, "/gallery_setup/templates/album/text()");
  setup->template_photo = xml_file_get_node_value (xml, "/gallery_setup/templates/photo/text()");
  s = xml_file_get_node_value (xml, "/gallery_setup/templates/template_files/text()");
  if (s) {
    setup->template_files = g_strsplit (s, "\n", -1);
    free (s);
  }
  s = xml_file_get_node_attribute (xml, "/gallery_setup/templates/support_files_use_common_root", "value");
  setup->support_files_use_common_root = s ? strcasecmp (s, "yes") == 0 : FALSE;  /*  default to FALSE  */
  if (s)  g_free (s);

  setup->thumbnail_landscape_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "landscape_w", 0);
  setup->thumbnail_landscape_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "landscape_h", 0);
  setup->thumbnail_portrait_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "portrait_w", 0);
  setup->thumbnail_portrait_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "portrait_h", 0);
  setup->thumbnail_quality = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "quality", -1);
  setup->preview_landscape_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "landscape_w", 0);
  setup->preview_landscape_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "landscape_h", 0);
  setup->preview_portrait_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "portrait_w", 0);
  setup->preview_portrait_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "portrait_h", 0);
  setup->preview_quality = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "quality", -1);
  setup->footer = xml_file_get_node_value (xml, "/gallery_setup/footer/text()");
  setup->border_style = xml_file_get_node_attribute (xml, "/gallery_setup/images/border", "style");
  setup->meta_author = xml_file_get_node_value (xml, "/gallery_setup/meta/author/text()");
  setup->meta_description = xml_file_get_node_value (xml, "/gallery_setup/meta/description/text()");
  setup->meta_keywords = xml_file_get_node_value (xml, "/gallery_setup/meta/keywords/text()");

  s = xml_file_get_node_attribute (xml, "/gallery_setup/images/preload", "value");
  setup->preload = s ? strcasecmp (s, "yes") == 0 : TRUE;  /*  default to TRUE  */
  if (s)  g_free (s);
  s = xml_file_get_node_attribute (xml, "/gallery_setup/images/use_iptc_exif", "value");
  setup->use_iptc_exif = s ? strcasecmp (s, "yes") == 0 : FALSE;  /*  default to FALSE  */
  if (s)  g_free (s);
  s = xml_file_get_node_attribute (xml, "/gallery_setup/images/erase_embed_thumbnail", "value");
  setup->erase_exif_thumbnail = s ? strcasecmp (s, "yes") == 0 : FALSE;  /*  default to FALSE  */
  if (s)  g_free (s);
  s = xml_file_get_node_attribute (xml, "/gallery_setup/meta/use_title_as_meta", "value");
  setup->use_title_as_meta = s ? strcasecmp (s, "yes") == 0 : TRUE;  /*  default to TRUE  */
  if (s)  g_free (s);

  setup->site_title = xml_file_get_node_attribute (xml, "/gallery_setup/meta/site", "title");
  setup->add_copyright = xml_file_get_node_value (xml, "/gallery_setup/meta/add_copyright/text()");
  s = xml_file_get_node_attribute (xml, "/gallery_setup/navigation/use_inpage_links", "value");
  setup->use_inpage_links = s ? strcasecmp (s, "yes") == 0 : TRUE;  /*  default to TRUE  */
  if (s)  g_free (s);

  setup->nofullsize = xml_file_get_node_present (xml, "/gallery_setup/images/nofullsize");

  setup->favicon_file = xml_file_get_node_value (xml, "/gallery_setup/meta/favicon/text()");
  setup->favicon_type = xml_file_get_node_attribute (xml, "/gallery_setup/meta/favicon", "type");

  xml_parser_close (xml);

  #ifdef __DEBUG_ALL__
    printf("setup: templates_path = '%s'\n", setup->templates_path);
    printf("setup: template_index = '%s'\n", setup->template_index);
    printf("setup: template_album = '%s'\n", setup->template_album);
    printf("setup: template_photo = '%s'\n", setup->template_photo);
    printf("setup: thumbnail_quality = %d\n", setup->thumbnail_quality);
    printf("setup: thumbnail_landscape_width = %ld\n", setup->thumbnail_landscape_width);
    printf("setup: thumbnail_landscape_height = %ld\n", setup->thumbnail_landscape_height);
    printf("setup: thumbnail_portrait_width = %ld\n", setup->thumbnail_portrait_width);
    printf("setup: thumbnail_portrait_height = %ld\n", setup->thumbnail_portrait_height);
    printf("setup: preview_quality = %d\n", setup->preview_quality);
    printf("setup: preview_landscape_width = %ld\n", setup->preview_landscape_width);
    printf("setup: preview_landscape_height = %ld\n", setup->preview_landscape_height);
    printf("setup: preview_portrait_width = %ld\n", setup->preview_portrait_width);
    printf("setup: preview_portrait_height = %ld\n", setup->preview_portrait_height);
    printf("setup: footer = '%s'\n", setup->footer);
    printf("setup: border_style = '%s'\n", setup->border_style);
    printf("setup: meta_author = '%s'\n", setup->meta_author);
    printf("setup: meta_description = '%s'\n", setup->meta_description);
    printf("setup: meta_keywords = '%s'\n", setup->meta_keywords);
    printf("setup: preload = %d\n", setup->preload);
    printf("setup: use_iptc_exif = %d\n", setup->use_iptc_exif);
    printf("setup: erase_exif_thumbnail = %d\n", setup->erase_exif_thumbnail);
  #endif

  return TRUE;
}



int
test_tmpl_access (const char *dir, const char *path)
{
  char *s;
  int b;

  s = g_strconcat (dir, "/", path, NULL);
  b = access (s, R_OK);
  g_free (s);
  return b;
}

int
test_tmpl_files (const char *dir, TGallerySetup *setup)
{
  return test_tmpl_access (dir, setup->template_album) | test_tmpl_access (dir, setup->template_photo) |
         test_tmpl_access (dir, setup->template_index);
}

/*
 *   find_templates_directory: absolute/relative path checks, trying to find templates directory
 *                             - returned string should be freed
 */
char *
find_templates_directory (TGallerySetup *setup)
{
  char *base_dir;
  char *pth;

  if (IS_DIR_SEP (*setup->templates_path))
    {
      #ifdef __DEBUG_ALL__
        printf("Warning: using absolute paths to templates\n");
      #endif

      if (! test_tmpl_files (setup->templates_path, setup))
    	  return strdup (setup->templates_path);
    }
  else
    {
      base_dir = g_path_get_dirname (setup->setup_xml_path);
	  pth = g_strconcat (base_dir, "/", setup->templates_path, NULL);
	  g_free (base_dir);

	  #ifdef __DEBUG_ALL__
	    printf("Warning: using relative paths to templates\n");
	  #endif

	  if (! test_tmpl_files (pth, setup))
		  return pth;
    }

  fprintf (stderr, "Couldn't find proper templates directory (tried '%s')\n", setup->templates_path);
  return NULL;
}


/*
 *   free_setup_data: free allocated setup data
 */
void
free_setup_data (TGallerySetup *setup)
{
  if (setup) {
    if (setup->real_templates_dir)
    	free (setup->real_templates_dir);
    if (setup->setup_xml_path)
    	free (setup->setup_xml_path);
    if (setup->templates_path)
    	free (setup->templates_path);
    if (setup->template_index)
    	free (setup->template_index);
    if (setup->template_album)
    	free (setup->template_album);
    if (setup->template_photo)
    	free (setup->template_photo);
    if (setup->template_files)
        g_strfreev (setup->template_files);
    if (setup->footer)
    	free (setup->footer);
    if (setup->meta_author)
        free (setup->meta_author);
    if (setup->border_style)
        free (setup->border_style);
    if (setup->site_title)
        free (setup->site_title);
    if (setup->add_copyright)
        free (setup->add_copyright);
    if (setup->favicon_file)
        free (setup->favicon_file);
    if (setup->favicon_type)
        free (setup->favicon_type);
    free (setup);
    setup = NULL;
  }
}