ethumb: reduce number of generate.
[framework/uifw/ethumb.git] / src / tests / ethumb_dbus.c
1 /**
2  * @file
3  *
4  * Copyright (C) 2009 by ProFUSION embedded systems
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library;
18  * if not, see <http://www.gnu.org/licenses/>.
19  *
20  * @author Rafael Antognolli <antognolli@profusion.mobi>
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <Ethumb.h>
31 #include <Ethumb_Client.h>
32 #include <Eina.h>
33 #include <Ecore_Getopt.h>
34 #include <Ecore.h>
35
36 static void
37 _on_server_die_cb(void *data __UNUSED__, Ethumb_Client *client __UNUSED__)
38 {
39    ecore_main_loop_quit();
40 }
41
42 static void
43 _queue_add_cb(void *data __UNUSED__, Ethumb_Client *client __UNUSED__, int id, const char *file, const char *key __UNUSED__, const char *thumb_path, const char *thumb_key __UNUSED__, Eina_Bool success)
44 {
45    fprintf(stderr, ">>> %hhu file ready: %s; thumb ready: %s; id = %d\n", success, file, thumb_path, id);
46 }
47
48 static void
49 _request_thumbnails(Ethumb_Client *client, void *data)
50 {
51    const char *path = data;
52    DIR *dir;
53    struct dirent *de;
54    char buf[PATH_MAX];
55
56    dir = opendir(path);
57    if (!dir)
58      {
59         fprintf(stderr, "ERROR: could not open directory: %s\n", path);
60         return;
61      }
62
63    ethumb_client_format_set(client, ETHUMB_THUMB_JPEG);
64    ethumb_client_aspect_set(client, ETHUMB_THUMB_CROP);
65    ethumb_client_crop_align_set(client, 0.2, 0.2);
66    ethumb_client_size_set(client, 192, 192);
67    ethumb_client_category_set(client, "custom");
68
69    while ((de = readdir(dir)))
70      {
71         if (de->d_type != DT_REG)
72           continue;
73         snprintf(buf, sizeof(buf), "%s/%s", path, de->d_name);
74         ethumb_client_file_set(client, buf, NULL);
75         ethumb_client_generate(client, _queue_add_cb, NULL, NULL);
76      }
77
78    closedir(dir);
79 }
80
81 static void
82 _connect_cb(void *data, Ethumb_Client *client, Eina_Bool success)
83 {
84    fprintf(stderr, "connected: %d\n", success);
85    if (!success)
86      {
87         ecore_main_loop_quit();
88         return;
89      }
90
91    _request_thumbnails(client, data);
92 }
93
94 int
95 main(int argc, char *argv[])
96 {
97    Ethumb_Client *client;
98
99    if (argc < 2)
100      {
101         fprintf(stderr, "ERROR: directory not specified.\n");
102         fprintf(stderr, "usage:\n\tethumb_dbus <images directory>\n");
103         return -2;
104      }
105
106    ethumb_client_init();
107    client = ethumb_client_connect(_connect_cb, argv[1], NULL);
108    if (!client)
109      {
110         fprintf(stderr, "ERROR: couldn't connect to server.\n");
111         ethumb_client_shutdown();
112         return -1;
113      }
114    ethumb_client_on_server_die_callback_set(client, _on_server_die_cb, NULL, NULL);
115
116    fprintf(stderr, "*** debug\n");
117    ecore_main_loop_begin();
118
119    ethumb_client_disconnect(client);
120
121    ethumb_client_shutdown();
122
123    return 0;
124 }