Add ChangeLog file.
[gmpc-exiftool.git] / exiftool.c
blob9e2dd9ad1e4e225cbea9df71f161b8910babd9cf
1 /* gmpc-exiftool (GMPC plugin)
2 * Copyright (C) 2006-2010 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdio.h>
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include <config.h>
24 #include <glib.h>
25 #include <gmpc/plugin.h>
26 #include <gmpc/metadata.h>
27 #include <libmpd/libmpd.h>
28 #include <libmpd/debug_printf.h>
29 #include <config.h>
30 #include <EXTERN.h>
31 #include <perl.h>
32 static PerlInterpreter *my_perl;
33 int fetch_get_image(mpd_Song *song,MetaDataType type,void (*callback)(GList *uris, gpointer data), gpointer data) ;
34 void music_dir_cover_art_pref_construct(GtkWidget *container);
35 void music_dir_cover_art_pref_destroy(GtkWidget *container);
36 GList * fetch_cover_art_path_list(mpd_Song *song);
37 GList * fetch_cover_art_path(mpd_Song *song);
38 static GtkWidget *wp_pref_vbox = NULL;
40 gmpcPlugin plugin;
42 /**
43 * Required functions
46 * Enable/Disable state of the plugins
48 static int exiftool_get_enabled()
50 return cfg_get_single_value_as_int_with_default(config, "exiftool-cover", "enable", TRUE);
52 static void exiftool_set_enabled(int enabled)
54 cfg_set_single_value_as_int(config, "exiftool-cover", "enable", enabled);
56 /* priority */
57 static int fetch_cover_priority()
59 return cfg_get_single_value_as_int_with_default(config, "exiftool-cover", "priority", 10);
62 static void fetch_cover_priority_set(int priority)
64 cfg_set_single_value_as_int(config, "exiftool-cover", "priority", priority);
69 int fetch_get_image(mpd_Song *song,MetaDataType type,void (*callback)(GList *uris, gpointer data), gpointer data)
71 gchar *path = NULL;
72 MetaData *md = NULL;
73 GList *list = NULL;
74 int num_image = 0;
75 int retval = META_DATA_UNAVAILABLE;
76 if(song == NULL || song->file == NULL)
78 debug_printf(DEBUG_INFO, "ExifTool: No file path to look at.");
79 callback(NULL, data);
80 return META_DATA_UNAVAILABLE;
82 if(type == META_ALBUM_ART)
84 const gchar *musicroot= connection_get_music_directory();
85 if(musicroot)
87 gchar *file = g_build_filename(musicroot, song->file, NULL);
88 if(g_file_test( file, G_FILE_TEST_EXISTS))
90 gchar *perl_program;
91 STRLEN length = 0;
92 char *embedding[] = {"", "-e", "0"};
93 // run Perl program
94 my_perl = perl_alloc();
95 perl_construct( my_perl );
96 perl_parse(my_perl, NULL, 3,embedding, (char **)NULL);
97 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
99 perl_run(my_perl);
100 eval_pv("use Image::ExifTool ':Public'", TRUE);
101 SV *sv2 = get_sv ("path", TRUE);
102 sv_setpv(sv2,file);
103 eval_pv("$info = ImageInfo($path)", TRUE);
104 eval_pv("$return = $$info{'Picture'};", TRUE);
105 SV *sv = get_sv("return", FALSE);
106 while(sv && SvROK(sv) != 0)
108 /* Reference.. */
109 sv = SvRV(sv);
110 gchar *image = SvPV(sv, length);
111 if(length> 0)
113 printf("Exif data tool got image: %s\n", file);
114 md = meta_data_new();
115 md->type = type;
116 md->plugin_name = plugin.name;
117 md->content_type = META_DATA_CONTENT_RAW;
118 md->content = g_memdup(image, length);
119 md->size = length;
120 list = g_list_append(list, md);
121 retval = META_DATA_AVAILABLE;
122 num_image++;
124 path = g_strdup_printf("$return = $$info{'Picture (%i)'};",num_image);
125 eval_pv(path, TRUE);
126 g_free(path);
127 sv = get_sv("return", FALSE);
129 perl_destruct(my_perl);
130 perl_free(my_perl);
131 g_free(perl_program);
133 g_free(file);
137 callback(list, data);
138 return retval;
143 gmpcMetaDataPlugin exiftool_cover = {
144 .get_priority = fetch_cover_priority,
145 .set_priority = fetch_cover_priority_set,
146 .get_metadata = fetch_get_image
148 /* a workaround so gmpc has some sort of api checking */
149 int plugin_api_version = PLUGIN_API_VERSION;
151 void exiftool_init(void)
154 static const gchar *exiftool_get_translation_domain(void)
156 return NULL;
158 /* the plugin */
159 gmpcPlugin plugin = {
160 .name = ("Exiftool Dir Fetcher"),
161 .version = {0,0,1},
162 .plugin_type = GMPC_PLUGIN_META_DATA,
163 .init = exiftool_init,
164 .metadata = &exiftool_cover, /* meta data */
165 .get_enabled = exiftool_get_enabled,
166 .set_enabled = exiftool_set_enabled,
167 .get_translation_domain = exiftool_get_translation_domain