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