changeset 1468:159186076d9a

lots of fixes
author Cristi Magherusan <majeru@atheme-project.org>
date Sat, 11 Aug 2007 15:55:14 +0300
parents 3127da6ae5da
children 08364e03c918
files src/mtp_up/mtp.c
diffstat 1 files changed, 104 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/mtp_up/mtp.c	Sat Aug 11 14:29:58 2007 +0900
+++ b/src/mtp_up/mtp.c	Sat Aug 11 15:55:14 2007 +0300
@@ -39,76 +39,105 @@
 
 GeneralPlugin mtp_gp =
 {
-    NULL,					/* handle */
-    NULL,					/* filename */
-    "MTP Upload " ,			/* description */
-    mtp_init,				/* init */
-    mtp_about,				/* about */
-    mtp_prefs,				/* configure */
-    mtp_cleanup				/* cleanup */
+    NULL,                                   /* handle */
+    NULL,                                   /* filename */
+    "MTP Upload " ,                         /* description */
+    mtp_init,                               /* init */
+    mtp_about,                              /* about */
+    mtp_prefs,                              /* configure */
+    mtp_cleanup                             /* cleanup */
 };
 GtkWidget *menuitem;
 
 GeneralPlugin *mtp_gplist[] = { &mtp_gp, NULL };
 DECLARE_PLUGIN(mtp_gp, NULL, NULL, NULL, NULL, NULL, mtp_gplist, NULL, NULL)
 
-
-gpointer upload(gpointer arg)
+GList * get_upload_list()
 {
-    Playlist *current_play;
     Tuple *tuple;
     gchar *from_path;
-    gchar *comp;
-    char *filename;
-    uint64_t filesize;
-    struct stat sb;
-    LIBMTP_file_t *genfile;
-    int ret;
-    uint32_t parent_id = 0;
-    GList *node;
+    GList *node=NULL,*up_list=NULL;
     PlaylistEntry *entry;
-    current_play = g_new0(Playlist,1);
-    current_play = playlist_get_active();
+    Playlist *current_play = playlist_get_active();
+
     node = current_play->entries;
-    PLAYLIST_LOCK(current_play->mutex); /*needed so that the user doesn't modify the selection*/
-    while (node) {
+    PLAYLIST_LOCK(current_play->mutex);     /*needed so that the user doesn't modify the selection*/ 
+    while (node)                            /*while creating the list of files to be uploaded*/
+    {
         entry = PLAYLIST_ENTRY(node->data);
         if (entry->selected)  
         {
             tuple = entry->tuple;
             from_path = g_strdup_printf("%s/%s", tuple_get_string(tuple, "file-path"), tuple_get_string(tuple, "file-name"));
-            comp = g_strescape(from_path,NULL);
-            if ( stat(from_path, &sb) == -1 )
-            {
-#if DEBUG
-                g_print("ERROR!");
-#endif
-                return NULL;
-            }
-            filesize = (uint64_t) sb.st_size;
-            filename = basename(from_path);
-            parent_id = 0;
-            genfile = LIBMTP_new_file_t();
-            genfile->filesize = filesize;
-            genfile->filename = strdup(filename);
-#if DEBUG
-            g_print("Uploading track '%s'\n",comp);
-#endif
-            ret = LIBMTP_Send_File_From_File(mtp_device,comp , genfile, NULL , NULL, parent_id);
-#if DEBUG
-            if (ret == 0) 
-                g_print("Upload finished!\n");
-            else
-                g_print("An error has occured while uploading '%s'...\nUpload failed!!!",comp);
-#endif
-            LIBMTP_destroy_file_t(genfile);
-            g_free(from_path);
-            g_free(comp);
+            VFSFile* f = vfs_fopen(from_path,"r");
+            if(!vfs_is_streaming(f))
+                up_list=g_list_prepend(up_list,from_path);
+            vfs_fclose(f);
             entry->selected = FALSE;
         }
         node = g_list_next(node);
     }
     PLAYLIST_UNLOCK(current_play->mutex);
+    return g_list_reverse(up_list);
+}
+
+void upload_file(gchar *from_path)
+{
+    int ret;
+    gchar *comp, *filename;
+    uint64_t filesize;
+    uint32_t parent_id = 0;
+    struct stat sb;
+    LIBMTP_file_t *genfile;
+
+    comp = g_strescape(from_path,NULL);
+    if ( stat(from_path, &sb) == -1 )
+    {
+#if DEBUG
+        g_print("ERROR! encountered while stat()'ing \"%s\"\n",from_path);
+#endif
+        return;
+    }
+    filesize = (uint64_t) sb.st_size;
+    filename = g_path_get_basename(from_path);
+    parent_id = 0;
+    genfile = LIBMTP_new_file_t();
+    genfile->filesize = filesize;
+    genfile->filename = strdup(filename);
+#if DEBUG
+    g_print("Uploading track '%s'\n",comp);
+#endif
+    ret = LIBMTP_Send_File_From_File(mtp_device, comp , genfile, NULL , NULL, parent_id);
+#if DEBUG
+    if (ret == 0) 
+        g_print("Upload finished!\n");
+    else
+        g_print("An error has occured while uploading '%s'...\nUpload failed!!!",comp);
+#endif
+    LIBMTP_destroy_file_t(genfile);
+    g_free(filename);
+    g_free(comp);
+}
+
+
+gpointer upload(gpointer arg)
+{
+    if(!mutex)
+        return NULL;
+    g_mutex_lock(mutex);
+    if(!mtp_device)
+        return NULL;
+    gchar* from_path;
+    GList *up_list=NULL,*node;
+    node=up_list=get_upload_list();
+    while(node)
+    {
+        from_path=(gchar*)(node->data);
+        upload_file(from_path);
+        node = g_list_next(node);
+    }
+    g_list_free(up_list);
+    g_mutex_unlock(mutex);
     return NULL;
 }
 
@@ -135,6 +164,7 @@
 #if DEBUG
         g_print("Initializing the MTP device...\n");
 #endif
+        LIBMTP_Init();
         mtp_device = LIBMTP_Get_First_Device();
         mtp_initialised = TRUE;
     }
@@ -147,7 +177,7 @@
         return;
 
     }
-    g_thread_create(upload,NULL,FALSE,NULL);	
+    g_thread_create(upload,NULL,FALSE,NULL);    
 }
 
 void mtp_init(void)
@@ -156,7 +186,6 @@
     gtk_widget_show (menuitem);
     audacious_menu_plugin_item_add(AUDACIOUS_MENU_PLAYLIST_RCLICK, menuitem);
     g_signal_connect (G_OBJECT (menuitem), "button_press_event",G_CALLBACK (mtp_press), NULL);  
-    LIBMTP_Init();
     mutex = g_mutex_new();
     plugin_active = TRUE;
 }
@@ -165,14 +194,37 @@
 {
     if (plugin_active)
     {
+
 #if DEBUG
-        g_print("Cleaning up MTP_upload\n");
+        if(mtp_initialised)
+            g_print("\n\n                 !!!CAUTION!!! \n\n"
+                    "Cleaning up MTP upload plugin, please wait!!!...\n"
+                    "This will block until the pending tracks are uploaded,\n"
+                    "then it will gracefully close your device\n\n"
+                    "!!! FORCING SHUTDOWN NOW MAY CAUSE DAMAGE TO YOUR DEVICE !!!\n\n\n"
+                    "Waiting for the MTP mutex to unlock...\n");
+#endif
+        if(mutex)
+            g_mutex_lock(mutex);
+        if(mtp_device!= NULL)
+        {
+            LIBMTP_Release_Device(mtp_device);
+            mtp_device = NULL;
+        }
+        g_mutex_unlock(mutex);
+#if DEBUG
+        if(mtp_initialised)
+            g_print("The MTP mutex has been unlocked\n");
 #endif
         audacious_menu_plugin_item_remove(AUDACIOUS_MENU_PLAYLIST_RCLICK, menuitem);
         gtk_widget_destroy(menuitem);
         g_mutex_free (mutex);
         mutex = NULL;
         plugin_active = FALSE;
+#if DEBUG
+        if(mtp_initialised)
+            g_print("MTP upload plugin has been cleaned up successfully\n");
+#endif
     }
 }