Update of /cvsroot/gtkpod/libgpod/tests
In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv4513/tests
Modified Files:
test-photos.c
Log Message:
Major rework of picture support.
* src/db-artwork-parser.c, src/db-artwork-writer.c, src/itdb.h,
src/db-itunes-parser.h: renamed 'master' to 'album_type' in
MhbaHeader and Itdb_PhotoAlbum.
* src/db-artwork-parser.c, src/db-artwork-writer.c,
src/itdb_photoalbum.c: Itdb_Photoalbum->members are now pointers
to the corresponding Itdb_Artwork instead of image_ids.
* src/itdb_photoalbum.c: album_ids and image_ids are set just
before writing the PhotoDB in itdb_photodb_write().
* src/itdb_photoalbum.c: new interface, basically use as follows:
itdb_photodb_parse():
Read an existing PhotoDB.
itdb_photodb_create():
Create a new Itdb_PhotoDB structure. The Photo Library Album is
(first album) is created automatically.
itdb_photodb_add_photo(), itdb_photodb_add_photo_from_data():
Add a photo to the PhotoDB (from file or from a chunk of
memory). It is automatically added to the Photo Library Album
(first album), which is created if it does not exist already.
itdb_photodb_photoalbum_craete():
Create and add a new photoalbum.
itdb_photodb_photoalbum_add_photo():
Add a photo (Itdb_Artwork) to an existing photoalbum.
itdb_photodb_photoalbum_remove():
Remove an existing photoalbum. Pictures can be kept in the
Photo Library or automatically removed as well.
itdb_photodb_remove_photo():
Remove a photo either from a photoalbum or completely from the
database.
itdb_photodb_write():
Write out your PhotoDB.
itdb_photodb_free():
Free all memory taken by the PhotoDB.
itdb_photodb_photoalbum_by_name():
Find the first photoalbum with a given name.
* src/itdb_playlist.c (itdb_playlist_add, itdb_playlist_add_track):
src/itdb_track.c (itdb_track_add): simplify code by using
g_list_insert().
* tests/test-photos.c: change to new interface, add new commands
'list' to list photo IDs in the database, 'remove' to remove IDs
from an album or the iPod, or remove entire photoalbums from the
iPod.
* src/db-itunes-parser.h: added comments to _MhbaHeader definition.
Index: test-photos.c
===================================================================
RCS file: /cvsroot/gtkpod/libgpod/tests/test-photos.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- test-photos.c 28 Sep 2006 00:32:50 -0000 1.6
+++ test-photos.c 29 Oct 2006 08:43:28 -0000 1.7
@@ -31,6 +31,34 @@
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gi18n-lib.h>
+static void usage (int argc, char **argv)
+{
+/* gchar *name = argv[0];*/
+ gchar *name = "test-photos";
+
+ g_print (_("Usage to add photos:\n %s add <mountpoint> <albumname>
[<filename(s)>]\n <albumname> should be set to 'NULL' to add photos to the
master photo album\n (Photo Library) only. If you don't specify any filenames
an empty album will\n be created.\n"), name);
+ g_print (_("Usage to dump all photos to <output_dir>:\n %s dump
<mountpoint> <output_dir>\n"), name);
+ g_print (_("Usage to list all photos IDs to stdout:\n %s list
<mountpoint>\n"), name);
+ g_print (_("Usage to remove photo IDs from Photo Library:\n %s remove
<mountpoint> <albumname> [<ID(s)>]\n <albumname> should be set to 'NULL' to to
remove photos from the iPod\n altogether. If you don't specify any IDs, the
photoalbum will be removed\n instead.\n WARNING: IDs may change when writing
the PhotoDB file.\n"), name);
+}
+
+/* Retrieve the photo whose ID is @id */
+static Itdb_Artwork *get_photo_by_id (Itdb_PhotoDB *db, guint32 id)
+{
+ GList *gl;
+
+ g_return_val_if_fail (db, NULL);
+
+ for (gl=db->photos; gl; gl=gl->next)
+ {
+ Itdb_Artwork *photo = gl->data;
+ g_return_val_if_fail (photo, NULL);
+
+ if (photo->id == id) return photo;
+ }
+ return NULL;
+}
+
static void
save_itdb_thumb (Itdb_PhotoDB *itdb, Itdb_Thumb *thumb,
const gchar *filename)
@@ -69,24 +97,6 @@
}
static void
-dump_artwork (Itdb_PhotoDB *db, gint photo_id,
- const gchar *album_name, const gchar *dir)
-{
- GList *it;
-
- for (it = db->photos; it != NULL; it = it->next) {
- Itdb_Artwork *artwork;
-
- artwork = (Itdb_Artwork *)it->data;
- g_return_if_fail (artwork);
- if( artwork->id == photo_id ) {
- dump_thumbs (db, artwork, album_name, dir);
- break;
- }
- }
-}
-
-static void
dump_albums (Itdb_PhotoDB *db, const gchar *dir)
{
GList *it;
@@ -99,91 +109,304 @@
g_return_if_fail (album);
for (it2 = album->members; it2 != NULL; it2 = it2->next) {
- gint photo_id = GPOINTER_TO_INT(it2->data);
- dump_artwork (db, photo_id, album->name, dir);
+ Itdb_Artwork *photo = it2->data;
+ dump_thumbs (db, photo, album->name, dir);
}
}
}
-int
-main (int argc, char **argv)
+
+static int do_dump (int argc, char **argv)
+{
+ GError *error = NULL;
+ Itdb_PhotoDB *db;
+
+ if (argc != 4)
+ {
+ g_print (_("Wrong number of command line arguments.\n"));
+ usage (argc, argv);
+ return 1;
+ }
+
+ if (!g_file_test (argv[3], G_FILE_TEST_EXISTS))
+ {
+ if (mkdir (argv[3], 0777) == -1)
+ {
+ g_print (_("Error creating '%s' (mkdir)\n"), argv[3]);
+ return 1;
+ }
+ }
+ if (!g_file_test (argv[3], G_FILE_TEST_IS_DIR))
+ {
+ g_print (_("Error: '%s' is not a directory\n"), argv[3]);
+ return 1;
+ }
+
+ db = itdb_photodb_parse (argv[2], &error);
+ if (db == NULL)
+ {
+ if (error)
+ {
+ g_print (_("Error reading iPod photo database (%s).\n"),
error->message);
+ g_error_free (error);
+ error = NULL;
+ }
+ else
+ {
+ g_print (_("Error reading iPod photo database.\n"));
+ }
+ return 1;
+ }
+ dump_albums (db, argv[3]);
+ itdb_photodb_free (db);
+ return 0;
+}
+
+static int do_list (int argc, char **argv)
+{
+ GError *error = NULL;
+ Itdb_PhotoDB *db;
+ GList *gl_album;
+
+
+ if (argc != 3)
+ {
+ g_print (_("Insufficient number of command line arguments.\n"));
+ usage (argc, argv);
+ return 1;
+ }
+
+ db = itdb_photodb_parse (argv[2], &error);
+ if (db == NULL)
+ {
+ if (error)
+ {
+ g_print (_("Error reading iPod photo database (%s).\n"),
error->message);
+ g_error_free (error);
+ error = NULL;
+ }
+ else
+ {
+ g_print (_("Error reading iPod photo database.\n"));
+ }
+ return 1;
+ }
+
+ for (gl_album=db->photoalbums; gl_album; gl_album=gl_album->next)
+ {
+ GList *gl_photo;
+ Itdb_PhotoAlbum *album = gl_album->data;
+ g_return_val_if_fail (album, 1);
+
+ g_print ("%s: ", album->name?album->name:_("<Unnamed>"));
+
+ for (gl_photo=album->members; gl_photo; gl_photo=gl_photo->next)
+ {
+ Itdb_Artwork *photo = gl_photo->data;
+ g_return_val_if_fail (photo, 1);
+
+ g_print ("%d ", photo->id);
+ }
+ if (g_list_length (album->members) > 0)
+ {
+ g_print ("\n");
+ }
+ else
+ {
+ g_print (_("<No members>\n"));
+ }
+ }
+ itdb_photodb_free (db);
+ return 0;
+}
+
+
+static int do_add (int argc, char **argv)
{
GError *error = NULL;
+ Itdb_PhotoAlbum *album = NULL;
Itdb_PhotoDB *db;
gint i;
- if (argc < 4) {
- g_print (_("Usage to add photos:\n"));
- g_print (_("%s <mountpoint> <albumname> <filename(s)>\n"), argv[0]);
- g_print (_("albumname should be set to 'master' to add photos to the
master photo album\n"));
- g_print (_("\n"));
- g_print (_("Usage to dump all photos to <output_dir>:\n"));
- g_print (_("%s dump <mountpoint> <output_dir>\n"), argv[0]);
- return 1;
+ if (argc < 4)
+ {
+ g_print (_("Insufficient number of command line arguments.\n"));
+ usage (argc, argv);
+ return 1;
}
- setlocale (LC_ALL, "");
- g_type_init ();
- if (strcmp (argv[1], "dump") == 0)
+ db = itdb_photodb_parse (argv[2], &error);
+ if (db == NULL)
{
- if (!g_file_test (argv[3], G_FILE_TEST_EXISTS))
+ if (error)
{
- if (mkdir (argv[3], 0777) == -1)
- {
- g_print (_("Error creating '%s' (mkdir)\n"), argv[3]);
- return 1;
- }
+ g_print (_("Error reading iPod photo database (%s).\nWill attempt
to create a new database.\n"), error->message);
+ g_error_free (error);
+ error = NULL;
}
- if (!g_file_test (argv[3], G_FILE_TEST_IS_DIR))
+ else
{
- g_print (_("Error: '%s' is not a directory\n"), argv[3]);
- return 1;
+ g_print (_("Error reading iPod photo database, will attempt to
create a new database\n"));
}
+ db = itdb_photodb_create (argv[2]);
+ }
- db = itdb_photodb_parse (argv[2], &error);
- if (db == NULL)
+ /* Find or create specified photoalbum */
+ if (strcmp (argv[3], "NULL") != 0)
+ {
+ album = itdb_photodb_photoalbum_by_name (db, argv[3]);
+ if (!album)
+ {
+ album = itdb_photodb_photoalbum_create (db, argv[3], -1);
+ }
+ }
+
+ for (i=4; i<argc; ++i)
+ {
+ Itdb_Artwork *photo;
+
+ photo = itdb_photodb_add_photo (db, argv[i], &error);
+ if (photo == NULL)
{
if (error)
{
- g_print (_("Error reading iPod photo database (%s).\n"),
error->message);
+ g_print (_("Error adding photo (%s) to photo database: %s\n"),
+ argv[i], error->message);
g_error_free (error);
error = NULL;
}
- else
+ }
+ else
+ {
+ if (album)
{
- g_print (_("Error reading iPod photo database.\n"));
+ itdb_photodb_photoalbum_add_photo (db, album, photo);
}
+ }
+ }
+
+ itdb_photodb_write (db, NULL);
+ itdb_photodb_free (db);
+ return 0;
+}
+
+
+static int do_remove (int argc, char **argv)
+{
+ GError *error = NULL;
+ Itdb_PhotoDB *db;
+ Itdb_PhotoAlbum *album = NULL;
+
+ if (argc < 4)
+ {
+ g_print (_("Insufficient number of command line arguments.\n"));
+ usage (argc, argv);
+ return 1;
+ }
+
+ db = itdb_photodb_parse (argv[2], &error);
+ if (db == NULL)
+ {
+ if (error)
+ {
+ g_print (_("Error reading iPod photo database (%s).\n"),
+ error->message);
+ g_error_free (error);
+ error = NULL;
+ }
+ else
+ {
+ g_print (_("Error reading iPod photo database"));
+ }
+ return 1;
+ }
+
+ /* Find specified photoalbum */
+ if (strcmp (argv[3], "NULL") != 0)
+ {
+ album = itdb_photodb_photoalbum_by_name (db, argv[3]);
+ if (!album)
+ {
+ g_print (_("Specified album '%s' not found. Aborting.\n"),
+ argv[3]);
+ itdb_photodb_free (db);
return 1;
}
- dump_albums (db, argv[3]);
- itdb_photodb_free (db);
+ }
+
+ if (argc == 4)
+ {
+ /* Remove photoalbum altogether, but preserve pics */
+ if (album == NULL)
+ {
+ g_print (_("Cannot remove Photo Libarary playlist. Aborting.\n"));
+ itdb_photodb_free (db);
+ return 1;
+ }
+ itdb_photodb_photoalbum_remove (db, album, FALSE);
}
else
{
- db = itdb_photodb_parse (argv[1], &error);
- if (db == NULL)
+ /* Remove specified pictures */
+ int i;
+ for (i=4; i<argc; ++i)
{
- if (error)
+ Itdb_Artwork *photo;
+ guint32 id;
+
+ id = g_strtod (argv[i], NULL);
+
+ photo = get_photo_by_id (db, id);
+
+ if (photo == NULL)
{
- g_print (_("Error reading iPod photo database (%s).\nWill
attempt to create a new database.\n"), error->message);
- g_error_free (error);
- error = NULL;
+ g_print (_("Error: could not find photo with ID <%d>.
Skipping...\n"),
+ id);
}
else
{
- g_print (_("Error reading iPod photo database, will attempt to
create a new database\n"));
+ itdb_photodb_remove_photo (db, album, photo);
}
- db = itdb_photodb_new ();
- itdb_device_set_mountpoint (db->device, argv[1]);
- }
- for (i=3; i<argc; ++i)
- {
- itdb_photodb_add_photo (db, argv[2], argv[i]);
}
+ }
- itdb_photodb_write (db, NULL);
- itdb_photodb_free (db);
+ itdb_photodb_write (db, NULL);
+ itdb_photodb_free (db);
+ return 0;
+}
+
+
+
+int
+main (int argc, char **argv)
+{
+ if (argc < 2)
+ {
+ g_print (_("Insufficient number of command line arguments.\n"));
+ usage (argc, argv);
+ return 1;
}
+ setlocale (LC_ALL, "");
+ g_type_init ();
+
+ if (strcmp (argv[1], "dump") == 0)
+ {
+ return do_dump (argc, argv);
+ }
+ if (strcmp (argv[1], "add") == 0)
+ {
+ return do_add (argc, argv);
+ }
+ if (strcmp (argv[1], "list") == 0)
+ {
+ return do_list (argc, argv);
+ }
+ if (strcmp (argv[1], "remove") == 0)
+ {
+ return do_remove (argc, argv);
+ }
+ g_print (_("Unknown command '%s'\n"), argv[1]);
+ usage (argc, argv);
return 0;
}
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2