1 /*
2  * gimp-thumbnail-list.c
3  */
4 
5 #include <string.h>
6 #include <gdk-pixbuf/gdk-pixbuf.h>
7 #include <libgimpthumb/gimpthumb.h>
8 
9 
10 #define STATE_NONE  -1
11 #define STATE_ERROR -2
12 
13 
14 static gboolean  parse_option_state (const gchar  *option_name,
15                                      const gchar  *value,
16                                      gpointer      data,
17                                      GError      **error);
18 static gboolean  parse_option_path  (const gchar  *option_name,
19                                      const gchar  *value,
20                                      gpointer      data,
21                                      GError      **error);
22 static void      process_folder     (const gchar  *folder);
23 static void      process_thumbnail  (const gchar  *filename);
24 
25 
26 static GimpThumbState  option_state   = STATE_NONE;
27 static gboolean        option_verbose = FALSE;
28 static gchar          *option_path    = NULL;
29 
30 
31 static const GOptionEntry main_entries[] =
32 {
33   {
34     "state", 's', 0,
35     G_OPTION_ARG_CALLBACK, parse_option_state,
36     "Filter by thumbnail state "
37     "(unknown|remote|folder|special|not-found|exists|old|failed|ok|error)",
38     "<state>"
39   },
40   {
41     "path", 'p', 0,
42     G_OPTION_ARG_CALLBACK, parse_option_path,
43     "Filter by original file's path",
44     "<path>"
45   },
46   {
47     "verbose", 'v', 0,
48     G_OPTION_ARG_NONE, &option_verbose,
49     "Print additional info per matched file", NULL
50   },
51   { NULL }
52 };
53 
54 
55 gint
main(gint argc,gchar * argv[])56 main (gint   argc,
57       gchar *argv[])
58 {
59   GOptionContext *context;
60   GDir           *dir;
61   const gchar    *thumb_folder;
62   const gchar    *folder;
63   GError         *error = NULL;
64 
65   gimp_thumb_init ("gimp-thumbnail-list", NULL);
66 
67   thumb_folder = gimp_thumb_get_thumb_base_dir ();
68 
69   context = g_option_context_new (NULL);
70   g_option_context_add_main_entries (context, main_entries, NULL);
71 
72   if (! g_option_context_parse (context, &argc, &argv, &error))
73     {
74       g_printerr ("%s\n", error->message);
75       return -1;
76     }
77 
78   dir = g_dir_open (thumb_folder, 0, &error);
79 
80   if (! dir)
81     g_error ("Error opening %s: %s", thumb_folder, error->message);
82 
83   while ((folder = g_dir_read_name (dir)))
84     {
85       gchar *filename;
86 
87       filename = g_build_filename (thumb_folder, folder, NULL);
88 
89       if (g_file_test (filename, G_FILE_TEST_IS_DIR))
90         process_folder (filename);
91 
92       g_free (filename);
93     }
94 
95   g_dir_close (dir);
96 
97   return 0;
98 }
99 
100 static gboolean
parse_option_state(const gchar * option_name,const gchar * value,gpointer data,GError ** error)101 parse_option_state (const gchar  *option_name,
102                     const gchar  *value,
103                     gpointer      data,
104                     GError      **error)
105 {
106   if (strcmp (value, "unknown") == 0)
107     option_state = GIMP_THUMB_STATE_UNKNOWN;
108   else if (strcmp (value, "remote") == 0)
109     option_state = GIMP_THUMB_STATE_REMOTE;
110   else if (strcmp (value, "folder") == 0)
111     option_state = GIMP_THUMB_STATE_FOLDER;
112   else if (strcmp (value, "special") == 0)
113     option_state = GIMP_THUMB_STATE_SPECIAL;
114   else if (strcmp (value, "not-found") == 0)
115     option_state = GIMP_THUMB_STATE_NOT_FOUND;
116   else if (strcmp (value, "exists") == 0)
117     option_state = GIMP_THUMB_STATE_EXISTS;
118   else if (strcmp (value, "old") == 0)
119     option_state = GIMP_THUMB_STATE_OLD;
120   else if (strcmp (value, "failed") == 0)
121     option_state = GIMP_THUMB_STATE_FAILED;
122   else if (strcmp (value, "ok") == 0)
123     option_state = GIMP_THUMB_STATE_OK;
124   else if (strcmp (value, "error") == 0)
125     option_state = STATE_ERROR;
126   else
127     return FALSE;
128 
129   return TRUE;
130 }
131 
132 static gboolean
parse_option_path(const gchar * option_name,const gchar * value,gpointer data,GError ** error)133 parse_option_path (const gchar  *option_name,
134                    const gchar  *value,
135                    gpointer      data,
136                    GError      **error)
137 {
138   option_path = g_strdup (value);
139 
140   return TRUE;
141 }
142 
143 static void
process_folder(const gchar * folder)144 process_folder (const gchar *folder)
145 {
146   GDir        *dir;
147   const gchar *name;
148   GError      *error = NULL;
149 
150 #if 0
151   g_print ("processing folder: %s\n", folder);
152 #endif
153 
154   dir = g_dir_open (folder, 0, &error);
155 
156   if (! dir)
157     {
158       g_printerr ("Error opening '%s': %s", folder, error->message);
159       return;
160     }
161 
162   while ((name = g_dir_read_name (dir)))
163     {
164       gchar *filename;
165 
166       filename = g_build_filename (folder, name, NULL);
167 
168       if (g_file_test (filename, G_FILE_TEST_IS_DIR))
169         process_folder (filename);
170       else
171         process_thumbnail (filename);
172 
173       g_free (filename);
174     }
175 
176   g_dir_close (dir);
177 }
178 
179 static void
process_thumbnail(const gchar * filename)180 process_thumbnail (const gchar *filename)
181 {
182   GimpThumbnail *thumbnail;
183   GError        *error = NULL;
184 
185   thumbnail = gimp_thumbnail_new ();
186 
187   if (! gimp_thumbnail_set_from_thumb (thumbnail, filename, &error))
188     {
189       if (option_state == STATE_ERROR)
190         {
191           if (option_verbose)
192             g_print ("%s '%s'\n", filename, error->message);
193           else
194             g_print ("%s\n", filename);
195         }
196 
197       g_clear_error (&error);
198     }
199   else
200     {
201       GimpThumbState state = gimp_thumbnail_peek_image (thumbnail);
202 
203       if ((option_state == STATE_NONE || state == option_state)
204 
205           &&
206 
207           (option_path == NULL ||
208            strstr (thumbnail->image_uri, option_path)))
209         {
210           if (option_verbose)
211             g_print ("%s '%s'\n", filename, thumbnail->image_uri);
212           else
213             g_print ("%s\n", filename);
214         }
215 
216 #if 0
217       switch (foo)
218         {
219         case GIMP_THUMB_STATE_REMOTE:
220           g_print ("%s Remote image '%s'\n", filename, thumbnail->image_uri);
221           break;
222 
223         case GIMP_THUMB_STATE_FOLDER:
224           g_print ("%s Folder '%s'\n", filename, thumbnail->image_uri);
225           break;
226 
227         case GIMP_THUMB_STATE_SPECIAL:
228           g_print ("%s Special file '%s'\n", filename, thumbnail->image_uri);
229           break;
230 
231         case GIMP_THUMB_STATE_NOT_FOUND:
232           g_print ("%s Image not found '%s'\n", filename, thumbnail->image_uri);
233           break;
234 
235         case GIMP_THUMB_STATE_OLD:
236           g_print ("%s Thumbnail old '%s'\n", filename, thumbnail->image_uri);
237           break;
238 
239         case GIMP_THUMB_STATE_FAILED:
240           g_print ("%s EEEEEEEEK '%s'\n", filename, thumbnail->image_uri);
241           break;
242 
243         default:
244           g_print ("%s '%s'\n", filename, thumbnail->image_uri);
245           break;
246         }
247 #endif
248     }
249 
250   g_object_unref (thumbnail);
251 }
252