2006-10-27 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / metadata / test-metadata.c
blobac2c85ff82dc04e4f54cabf519da7bc4a206e40a
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Copyright (C) 2006 Jonathan Matthew <jonathan@kaolin.hn.org>
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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Test client for out-of-process metadata reader.
25 #include <config.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "rb-metadata.h"
32 #include "rb-metadata-dbus.h"
33 #include "rb-debug.h"
35 static RBMetaData *md = NULL;
37 static gboolean debug = FALSE;
38 static gboolean can_save = FALSE;
40 static GOptionEntry entries [] = {
41 { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, NULL, NULL },
42 { "can-save", 0, 0, G_OPTION_ARG_NONE, &can_save, NULL, NULL },
43 { NULL }
46 static void
47 print_metadata_string (RBMetaData *md, RBMetaDataField field, const char *name)
49 GValue v = {0,};
50 if (rb_metadata_get (md, field, &v)) {
51 char *s;
53 s = g_strdup_value_contents (&v);
54 printf ("%s: %s\n", name, s);
55 g_free (s);
56 g_value_unset (&v);
60 static gboolean
61 check_can_save_cb (gpointer mt)
63 char *mimetype = (char *)mt;
65 if (rb_metadata_can_save (md, mimetype)) {
66 printf ("Can save %s\n", mimetype);
67 } else {
68 printf ("Unable to save %s\n", mimetype);
71 return FALSE;
74 static gboolean
75 load_metadata_cb (gpointer file)
77 char *uri = (char *)file;
78 GError *error = NULL;
80 if (strncmp (uri, "file://", 7)) {
81 if (uri[0] == '/') {
82 uri = g_strdup_printf ("file://%s", uri);
83 } else {
84 char buf[600];
85 if (getcwd (buf, sizeof (buf)) != NULL)
86 uri = g_strdup_printf ("file://%s/%s", buf, uri);
89 printf ("%s\n", (const char *)uri);
91 rb_metadata_load (md, (const char *)uri, &error);
93 if (error) {
94 switch (error->code) {
95 case RB_METADATA_ERROR_NOT_AUDIO_IGNORE:
96 printf ("file ignored: %s\n", error->message);
97 break;
98 default:
99 printf ("error: %s\n", error->message);
100 break;
102 g_clear_error (&error);
103 } else {
104 RBMetaDataField f;
106 printf ("type: %s\n", rb_metadata_get_mime (md));
107 for (f =(RBMetaDataField)0; f < RB_METADATA_FIELD_LAST; f++)
108 print_metadata_string (md, f, rb_metadata_get_field_name (f));
110 printf ("---\n");
111 return FALSE;
114 static gboolean
115 bye (gpointer nah)
117 g_main_loop_quit ((GMainLoop *)nah);
118 return FALSE;
121 int main(int argc, char **argv)
123 GMainLoop *loop;
124 int filecount = 0;
125 GOptionContext *context;
126 gboolean retval;
127 GError *error = NULL;
129 g_type_init ();
131 context = g_option_context_new (NULL);
132 g_option_context_add_main_entries (context, entries, NULL);
133 retval = g_option_context_parse (context, &argc, &argv, &error);
135 g_option_context_free (context);
137 if (! retval) {
138 g_warning ("%s", error->message);
139 g_error_free (error);
140 exit (1);
143 if (debug) {
144 rb_debug_init (TRUE);
147 if (can_save) {
148 g_idle_add (check_can_save_cb, argv[2]);
151 loop = g_main_loop_new (NULL, FALSE);
152 md = rb_metadata_new ();
153 while (argv[1] != NULL) {
154 g_idle_add (load_metadata_cb, argv[1]);
155 argv++;
156 filecount++;
158 g_idle_add (bye, loop);
160 g_main_loop_run (loop);
162 printf ("%d file(s) read\n", filecount);
163 g_object_unref (G_OBJECT (md));
164 return 0;