Index: plugins/mtpdevice/rb-mtp-source.c
===================================================================
--- plugins/mtpdevice/rb-mtp-source.c	(revision 5487)
+++ plugins/mtpdevice/rb-mtp-source.c	(working copy)
@@ -71,6 +71,9 @@
 				  const char *mimetype,
 				  const char *extension);
 
+static void rb_mtp_source_load_playlists (RBMtpSource *source);
+static void rb_mtp_source_add_playlist (RBMtpSource *source, LIBMTP_playlist_t *playlist);
+
 static RhythmDB * get_db_for_source (RBMtpSource *source);
 
 typedef struct
@@ -78,6 +81,8 @@
 	LIBMTP_mtpdevice_t *device;
 	GHashTable *entry_map;
 	char *udi;
+	GList *playlists;
+	GList *temp_files;
 } RBMtpSourcePrivate;
 
 RB_PLUGIN_DEFINE_TYPE(RBMtpSource,
@@ -299,6 +304,17 @@
 		g_value_unset (&value);
 	}
 
+	/* Set play count*/
+	GValue value = {0, };
+	g_value_init (&value, G_TYPE_ULONG);
+	g_value_set_ulong (&value, track->usecount);
+	rb_debug ("Play count: %d", track->usecount);
+	rhythmdb_entry_set (RHYTHMDB (db), entry,
+				RHYTHMDB_PROP_PLAY_COUNT,
+				&value);
+
+	g_value_unset (&value);
+
 	/* Set title */
 	entry_set_string_prop (RHYTHMDB (db), entry, RHYTHMDB_PROP_TITLE, track->title);
 
@@ -324,17 +340,19 @@
 
 	g_assert (db != NULL);
 
-	tracks = LIBMTP_Get_Tracklisting (priv->device);
+	tracks = LIBMTP_Get_Tracklisting_With_Callback (priv->device, NULL, NULL);
 	if (tracks != NULL) {
 		LIBMTP_track_t *track, *tmp = NULL;
 		for (track = tracks; track != NULL; track = track->next) {
 			add_mtp_track_to_db (source, track);
 			tmp = track;
 		}
+		rb_mtp_source_load_playlists (source);
 	} else {
 		rb_debug ("No tracks");
 	}
 
+	
 	g_object_unref (G_OBJECT (db));
 	return FALSE;
 }
@@ -355,6 +373,83 @@
 	g_free (name);
 }
 
+static void
+rb_mtp_source_load_playlists (RBMtpSource *source)
+{
+	RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (source);
+	LIBMTP_playlist_t *pl, *tmp;
+
+	pl = LIBMTP_Get_Playlist_List (priv->device);
+	while (pl != NULL) {
+		rb_debug("mtp-playlist: %s", pl->name);
+		rb_mtp_source_add_playlist (source, pl);
+		tmp = pl;
+		pl = pl->next;
+		LIBMTP_destroy_playlist_t (tmp);
+	}
+
+}
+
+static void
+rb_mtp_source_add_playlist (RBMtpSource *source, LIBMTP_playlist_t *playlist)
+{
+	RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (source);
+	RBShell *shell;
+	RhythmDB *db = get_db_for_source (source);
+	RBSource *playlist_source;
+	RhythmDBEntry *entry;
+	RhythmDBEntryType entry_type;
+	uint32_t i;
+
+	g_object_get (source,
+			  "shell", &shell,
+			  "entry-type", &entry_type,
+			  NULL);
+
+	playlist_source = rb_static_playlist_source_new (shell,
+							 playlist->name,
+							 FALSE,
+							 entry_type);
+	g_boxed_free (RHYTHMDB_TYPE_ENTRY_TYPE, entry_type);
+
+
+
+
+	for (i = 0; i < playlist->no_tracks; i++) {
+		LIBMTP_track_t *track;
+		char *loc;
+
+		track = LIBMTP_Get_Trackmetadata(priv->device,
+						playlist->tracks[i]);
+		loc = g_strdup_printf ("rb-mtp-%i", track->item_id);
+		rb_debug ("track location: %s", loc);
+		if (track != NULL) {
+			entry = rhythmdb_entry_lookup_by_location (db, loc);
+			if (entry)
+				rb_static_playlist_source_add_entry (
+						RB_STATIC_PLAYLIST_SOURCE (playlist_source),
+						entry,
+						-1);
+
+			 LIBMTP_destroy_track_t(track);
+		} else {
+			rb_debug ("Invalid track");
+		}
+		
+	}
+	g_object_unref (db);
+
+	g_object_ref (G_OBJECT(playlist_source));
+	priv->playlists = g_list_prepend (priv->playlists, playlist_source);
+
+	if (playlist_source != NULL)
+		rb_shell_append_source (shell, playlist_source, RB_SOURCE (source));
+	g_object_unref (shell);
+}
+
+
+
+
 static gboolean
 destroy_entry_map_pair (RhythmDBEntry *entry, LIBMTP_track_t *track, RhythmDB *db)
 {
@@ -368,7 +463,10 @@
 {
 	RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (asource);
 	RhythmDB *db = get_db_for_source (RB_MTP_SOURCE (asource));
+	GList *p, *q;
+	gchar *data = NULL;
 
+
 	if (priv->entry_map) {
 		g_hash_table_foreach_remove (priv->entry_map, (GHRFunc)destroy_entry_map_pair, db);
 		g_hash_table_destroy (priv->entry_map);
@@ -380,7 +478,27 @@
 
 	LIBMTP_Release_Device (priv->device);
 	priv->device = NULL;
+	rb_debug ("Disconnected device");
 
+	for (q = priv->temp_files; q != NULL; q = q->next) {
+		data = q->data;
+		rb_debug("temp_files->data: %s", data);
+		gnome_vfs_unlink (data);
+	}
+	g_free (data);
+	g_list_free (priv->temp_files);
+	priv->temp_files = NULL;
+	rb_debug("clear transferred files");
+	
+	for (p = priv->playlists; p != NULL; p = p->next) {
+		RBSource *playlist = RB_SOURCE (p->data);
+		rb_source_delete_thyself (playlist);
+	}
+	g_list_free (priv->playlists);
+	priv->playlists = NULL;
+	rb_debug("Deleted playlists");
+
+
 	if (priv->udi != NULL) {
 		g_free (priv->udi);
 		priv->udi = NULL;
@@ -583,6 +701,7 @@
 
 	if (rb_mtp_source_transfer_track_to_disk (priv->device, track, uri) == TRUE) {
 		s = g_strdup_printf ("file://%s", uri);
+		priv->temp_files = g_list_prepend(priv->temp_files, strdup(s));
 		g_free (uri);
 		return s;
 	} else {
