Examples

Examples — Examples of libitunesdb use.

Reading the content of an iPod

#include <libitunesdb/ipoddb.h>
#include <libitunesdb/ipod-detect.h>

static void
dump_songs (iPodDB *db) 
{
	GList *songs;
	GList *it;
	
	songs = ipod_db_get_songs (db);
	for (it = songs; it != NULL; it = it->next) {
		ipod_dump_song (it->data);
	}
	g_list_foreach (songs, (GFunc)ipod_song_unref, NULL);
	g_list_free (songs);
}

static void
dump_playlists (iPodDB *db)
{
	GList *playlists;
	GList *it;
	
	playlists = ipod_db_get_playlists (db);
	for (it = playlists; it != NULL; it = it->next) {
		ipod_playlist_dump (it->data);
	}
	g_list_foreach (playlists, (GFunc)ipod_playlist_unref, NULL);
	g_list_free (playlists);
}


int 
main (int argc, char **argv)
{
	iPodDB *db;
	GList *ipods;
	GList *ipod;

	ipods = ipod_enumerate ();

	for (ipod = ipods; ipod != NULL; ipod = ipod->next) {
		g_print ("Content of iPod mounted at %s:\n\n\n", 
			 (char *)ipod->data);
		db = ipod_db_load (ipod->data);
		if (db == NULL) {
			g_print ("Error reading iPod database\n");
			continue;
		}
		dump_songs (db);
	        dump_playlists (db);
		ipod_db_destroy (db);
	}

	g_list_foreach (ipods, (GFunc)g_free, NULL);
	g_list_free (ipods);

	return 0;
}

Adding songs to an iPod

#include <libitunesdb/ipoddb.h>
#include <libitunesdb/ipod-detect.h>
#include <libitunesdb/ipod-transfer.h>

static iPodSong *
get_song_info (iPodDB *db, const char *uri)
{
	iPodSong *song;
	char *mount_point;

	/* This function must fill the metadata for the song that will then be
	 * displayed in the iPod UI
	 */
	song = ipod_song_new ();
	song->artist = "Me";
	song->album = "You";
	song->title = "Them";

	mount_point = ipod_db_get_mount_point (db);
	song->ipod_path = ipod_path_from_unix_path (mount_point, uri);
	g_free (mount_point);

	return song;
}

static int 
copy_uri (iPodDB *db, const char *uri)
{
	int res;
	char *mount_point;
	char *dest;
	iPodSong *song;

	mount_point = ipod_db_get_mount_point (db);
	res = ipod_transfer_uri (mount_point, uri, &dest);
	g_free (mount_point);
	if (res != 0) {
		return -1;
	}

	song = get_song_info (db, dest);

	ipod_db_add_song (db, song);
	ipod_song_unref (song);

	return 0;
}

int 
main (int argc, char **argv)
{
	iPodDB *db;
	GList *ipods;
	
	if (argc != 2) {
		g_print ("Usage: %s URI\n", argv[0]);
		return -1;
	}

	ipods = ipod_enumerate ();
	if (ipods == NULL) {
		return -1;
	}
	  
	db = ipod_db_load (ipods->data);
	g_list_foreach (ipods, (GFunc)g_free, NULL);
	if (db == NULL) {
		g_print ("Error loading iPod database\n");
		return -1;
	}

	if (copy_uri (db, argv[1]) != 0) {
		g_print ("Error copying %s to the iPod\n", argv[1]);
	}

	ipod_db_save (db);
	ipod_db_destroy (db);

	return 0;
}