summaryrefslogtreecommitdiff
path: root/src/bin/ethumb_client/ethumbd_client.c
blob: e81fc2e9a7bcadb73a95dcf9e68477c070005b2c (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
 * @file
 *
 * Copyright (C) 2009 by ProFUSION embedded systems
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rafael Antognolli <antognolli@profusion.mobi>
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <Eina.h>
#include <Ecore_Getopt.h>
#include <Ecore.h>
#include <Ethumb_Client.h>

const char *aspect_opt[] = { "keep", "ignore", "crop", NULL };
const char *format_opt[] = { "png", "jpg", "eet", NULL };
struct frame
{
   const char *file;
   const char *group;
   const char *swallow;
};

struct options
{
   Eina_Rectangle geometry;
   unsigned int format, aspect;
   char *format_str;
   char *aspect_str;
   char *directory;
   char *category;
   struct frame frame;
   char *src_path;
   char *src_key;
   const char *thumb_path;
   const char *thumb_key;
   double video_time;
   int page;
};

static unsigned char
_ethumb_getopt_callback_frame_parse(const Ecore_Getopt *parser EINA_UNUSED, const Ecore_Getopt_Desc *desc EINA_UNUSED, const char *str, void *data EINA_UNUSED, Ecore_Getopt_Value *storage)
{
   struct frame *f = (struct frame *)storage->ptrp;
   const char *tfile, *tgroup, *tswallow, *base, *sep;

   base = str;
   sep = strchr(base, ':');
   if (!sep)
	goto error;
   tfile = eina_stringshare_add_length(base, sep - base);
   base = sep + 1;

   sep = strchr(base, ':');
   if (!sep)
     {
	eina_stringshare_del(tfile);
	goto error;
     }
   tgroup = eina_stringshare_add_length(base, sep - base);
   base = sep + 1;
   if (base[0] == '\0')
     {
	eina_stringshare_del(tfile);
	eina_stringshare_del(tgroup);
	goto error;
     }
   tswallow = eina_stringshare_add(base);

   f->file = tfile;
   f->group = tgroup;
   f->swallow = tswallow;
   return 1;

 error:
   fprintf(stderr,
	   "ERROR: invalid theme, not in format "
	   "'file:group:swallow_part': '%s'\n",
	   str);
   return 0;
}

const Ecore_Getopt optdesc = {
  "ethumbd_client",
  NULL,
  PACKAGE_VERSION,
  "(C) 2009 - ProFUSION embedded systems",
  "LGPL v2.1 - GNU Lesser General Public License",
  "Thumbnails generator client using DBus and ethumbd.\n"
  "\n"
  "This program uses ethumbd server to create thumbnails from pictures. "
  "It's an example of use and a test for ethumbd.\n",
  0,
  {
     ECORE_GETOPT_CALLBACK_ARGS
     ('s', "size", "thumbnail size expected.",
      "WxH", ecore_getopt_callback_size_parse, NULL),
     ECORE_GETOPT_CHOICE
     ('f', "format", "file format to save.", format_opt),
     ECORE_GETOPT_CHOICE
     ('a', "aspect", "original image aspect ratio.", aspect_opt),
     ECORE_GETOPT_STORE_STR
     ('d', "directory", "directory to save thumbnails."),
     ECORE_GETOPT_STORE_STR
     ('c', "category", "thumbnails category."),
     ECORE_GETOPT_CALLBACK_ARGS
     ('t', "theme", "path to theme file, group and swallow part.",
      "file:group:swallow_part", _ethumb_getopt_callback_frame_parse, NULL),
     ECORE_GETOPT_STORE_STR
     ('k', "key", "key inside eet file to read image from."),
     ECORE_GETOPT_STORE_DOUBLE
     ('v', "video_time", "time of video frame to use as thumbnail."),
     ECORE_GETOPT_STORE_INT
     ('p', "document_page", "document page to use as thumbnail."),
     ECORE_GETOPT_LICENSE('L', "license"),
     ECORE_GETOPT_COPYRIGHT('C', "copyright"),
     ECORE_GETOPT_VERSION('V', "version"),
     ECORE_GETOPT_HELP('h', "help"),
     ECORE_GETOPT_SENTINEL
  }
};

static void
_thumb_report(const char *mode, const char *src_path, const char *src_key, const char *thumb_path, const char *thumb_key)
{
   printf("%s '%s' '%s' => '%s' '%s'\n",
	  mode,
	  src_path, src_key ? src_key : "",
	  thumb_path, thumb_key ? thumb_key : "");
}

static void
_finished_thumb(void *data EINA_UNUSED, Ethumb_Client *client EINA_UNUSED, int id EINA_UNUSED, const char *src_path, const char *src_key, const char *thumb_path, const char *thumb_key, Eina_Bool success)
{
   const char *mode = success ? "GENERATED" : "FAILED";
   _thumb_report(mode, src_path, src_key, thumb_path, thumb_key);
   ecore_main_loop_quit();
}

static void
_exists(void *data, Ethumb_Client *c, EINA_UNUSED Ethumb_Exists *thread, Eina_Bool exists)
{
   struct options *opts = data;
   const char *thumb_path, *thumb_key;
   long id;

   if (exists)
     {
        ethumb_client_thumb_path_get(c, &thumb_path, &thumb_key);
        _thumb_report
          ("EXISTS", opts->src_path, opts->src_key, thumb_path, thumb_key);
        ecore_main_loop_quit();
        return;
     }

   id = ethumb_client_generate(c, _finished_thumb, NULL, NULL);
   if (id < 0)
     {
	fputs("ERROR: could not request thumbnail to be generated.\n", stderr);
	ecore_main_loop_quit();
	return;
     }
   printf("request id=%ld, file='%s', key='%s'\n",
	  id, opts->src_path, opts->src_key ? opts->src_key : "");

}

static void
_connected(void *data, Ethumb_Client *c, Eina_Bool success)
{
   struct options *opts = data;

   if (!success)
     {
	fputs("ERROR: could not connect to DBus server.\n", stderr);
	ecore_main_loop_quit();
	return;
     }

   fputs("connected to DBus server, setup parameters...\n", stdout);

   ethumb_client_format_set(c, opts->format);
   ethumb_client_aspect_set(c, opts->aspect);

   if (opts->directory) ethumb_client_dir_path_set(c, opts->directory);
   if (opts->category) ethumb_client_category_set(c, opts->category);
   if (opts->geometry.w > 0 && opts->geometry.h > 0)
     ethumb_client_size_set(c, opts->geometry.w, opts->geometry.h);
   if (opts->frame.file)
     ethumb_client_frame_set
       (c, opts->frame.file, opts->frame.group, opts->frame.swallow);
   if (opts->video_time > 0)
     ethumb_client_video_time_set(c, opts->video_time);
   if (opts->page > 0)
     ethumb_client_document_page_set(c, opts->page);

   if (!ethumb_client_file_set(c, opts->src_path, opts->src_key))
     {
	fprintf(stderr, "ERROR: could not set file '%s', key '%s'\n",
		opts->src_path, opts->src_key ? opts->src_key : "");
	ecore_main_loop_quit();
	return;
     }

   ethumb_client_thumb_path_set(c, opts->thumb_path, opts->thumb_key);
   ethumb_client_thumb_exists(c, _exists, opts);
}

int
main(int argc, char *argv[])
{
   Ethumb_Client *c;
   Eina_Bool quit_option = 0;
   const char *format_str = NULL, *aspect_str = NULL;
   struct options opts = {
     {-1, -1, -1, -1},
     0, 0,
     NULL, NULL, NULL, NULL,
     {NULL, NULL, NULL},
     NULL, NULL, NULL, NULL,
     0.0,
     0
   };
   int arg_index;
   int i, ret = 0;

   ethumb_client_init();
   ecore_init();

   Ecore_Getopt_Value values[] = {
     ECORE_GETOPT_VALUE_PTR_CAST(opts.geometry),
     ECORE_GETOPT_VALUE_PTR_CAST(format_str),
     ECORE_GETOPT_VALUE_PTR_CAST(aspect_str),
     ECORE_GETOPT_VALUE_STR(opts.directory),
     ECORE_GETOPT_VALUE_STR(opts.category),
     ECORE_GETOPT_VALUE_PTR_CAST(opts.frame),
     ECORE_GETOPT_VALUE_STR(opts.src_key),
     ECORE_GETOPT_VALUE_DOUBLE(opts.video_time),
     ECORE_GETOPT_VALUE_INT(opts.page),
     ECORE_GETOPT_VALUE_BOOL(quit_option),
     ECORE_GETOPT_VALUE_BOOL(quit_option),
     ECORE_GETOPT_VALUE_BOOL(quit_option),
     ECORE_GETOPT_VALUE_BOOL(quit_option),
     ECORE_GETOPT_VALUE_NONE
   };

   arg_index = ecore_getopt_parse(&optdesc, values, argc, argv);
   if ((arg_index < 0) || (arg_index == argc))
     {
	if (arg_index < 0)
	  fprintf(stderr, "Could not parse arguments.\n");
	else
	  fprintf(stderr, "Missing source file to thumbnail.\n");

	ret = 1;
	goto end;
     }

   if (quit_option)
     {
	ret = 0;
	goto end;
     }

   for (i = 0; i < 3; i++)
     if (format_opt[i] == format_str)
       {
	  opts.format = i;
	  break;
       }

   for (i = 0; i < 3; i++)
     if (aspect_opt[i] == aspect_str)
       {
	  opts.aspect = i;
	  break;
       }

   opts.src_path = argv[arg_index++];
   if (arg_index < argc)
     {
	opts.thumb_path = argv[arg_index++];
	if (arg_index < argc)
	  opts.thumb_key = argv[arg_index];
     }

   c = ethumb_client_connect(_connected, &opts, NULL);
   if (!c)
     {
	fputs("ERROR: could not connect to server.\n", stderr);
	ret = 2;
	goto end;
     }

   ecore_main_loop_begin();
   ethumb_client_disconnect(c);

 end:
   if (opts.frame.file)
     {
	eina_stringshare_del(opts.frame.file);
	eina_stringshare_del(opts.frame.group);
	eina_stringshare_del(opts.frame.swallow);
     }
   ecore_shutdown();
   ethumb_client_shutdown();

   return ret;
}