Mercurial > audlegacy-plugins
comparison src/mtp_up/mtp.c @ 1500:611d8db6b421
made it enabled by default if having libmtp, fixes Chainsaw's bug and adds some improvements
author | Cristi Magherusan <majeru@atheme-project.org> |
---|---|
date | Fri, 17 Aug 2007 00:12:43 +0300 |
parents | 318e3bcdf51d |
children | d72a44fdb6a3 |
comparison
equal
deleted
inserted
replaced
1499:799f92d815b2 | 1500:611d8db6b421 |
---|---|
19 #include <glib.h> | 19 #include <glib.h> |
20 #include <libmtp.h> | 20 #include <libmtp.h> |
21 #include <audacious/plugin.h> | 21 #include <audacious/plugin.h> |
22 #include <audacious/playlist.h> | 22 #include <audacious/playlist.h> |
23 #include <audacious/ui_plugin_menu.h> | 23 #include <audacious/ui_plugin_menu.h> |
24 | |
25 #include <gtk/gtk.h> | |
26 #include <audacious/util.h> | |
27 | |
24 #define DEBUG 1 | 28 #define DEBUG 1 |
25 | 29 |
26 #define DEFAULT_LABEL "Upload to MTP device" | 30 #define DEFAULT_LABEL "Upload to MTP device" |
27 #define DISABLED_LABEL "MTP upload in progress..." | 31 #define DISABLED_LABEL "MTP upload in progress..." |
28 | 32 |
32 LIBMTP_mtpdevice_t *mtp_device = NULL; | 36 LIBMTP_mtpdevice_t *mtp_device = NULL; |
33 LIBMTP_progressfunc_t *callback; | 37 LIBMTP_progressfunc_t *callback; |
34 LIBMTP_file_t *filelist; | 38 LIBMTP_file_t *filelist; |
35 Playlist *active_playlist; | 39 Playlist *active_playlist; |
36 | 40 |
37 static gboolean plugin_active = FALSE; | 41 static gboolean plugin_active = FALSE,exiting=FALSE; |
38 | 42 |
39 void mtp_init ( void ); | 43 void mtp_init ( void ); |
40 void mtp_cleanup ( void ); | 44 void mtp_cleanup ( void ); |
41 void mtp_prefs ( void ); | 45 void mtp_prefs ( void ); |
42 void mtp_about ( void ); | 46 void mtp_about ( void ); |
55 | 59 |
56 GeneralPlugin *mtp_gplist[] = { &mtp_gp, NULL }; | 60 GeneralPlugin *mtp_gplist[] = { &mtp_gp, NULL }; |
57 DECLARE_PLUGIN(mtp_gp, NULL, NULL, NULL, NULL, NULL, mtp_gplist, NULL, NULL) | 61 DECLARE_PLUGIN(mtp_gp, NULL, NULL, NULL, NULL, NULL, mtp_gplist, NULL, NULL) |
58 | 62 |
59 | 63 |
60 void show_dialog(gchar *message) | 64 void show_dialog(const gchar* message) |
61 { | 65 { |
62 GtkWidget *dialog; | 66 GDK_THREADS_ENTER(); |
63 dialog = gtk_message_dialog_new (NULL, | 67 GtkWidget *dialog = gtk_message_dialog_new (NULL, |
64 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | 68 GTK_DIALOG_MODAL, |
65 GTK_MESSAGE_ERROR, | 69 GTK_MESSAGE_ERROR, |
66 GTK_BUTTONS_OK, | 70 GTK_BUTTONS_OK, |
67 message); | 71 message); |
68 gtk_dialog_run (GTK_DIALOG (dialog)); | 72 gtk_dialog_run (GTK_DIALOG (dialog)); |
69 gtk_widget_destroy (dialog); | 73 gtk_widget_show(dialog); |
74 gtk_widget_destroy(dialog); | |
75 GDK_THREADS_LEAVE(); | |
76 | |
70 } | 77 } |
71 | 78 |
72 | 79 |
73 GList * get_upload_list() | 80 GList * get_upload_list() |
74 { | 81 { |
117 } | 124 } |
118 PLAYLIST_UNLOCK(current_play->mutex); | 125 PLAYLIST_UNLOCK(current_play->mutex); |
119 return g_list_reverse(up_list); | 126 return g_list_reverse(up_list); |
120 } | 127 } |
121 | 128 |
122 void upload_file(gchar *from_path) | 129 gint upload_file(gchar *from_path) |
123 { | 130 { |
124 int ret; | 131 int ret; |
125 gchar *comp, *filename; | 132 gchar *comp, *filename; |
126 uint64_t filesize; | 133 uint64_t filesize; |
127 uint32_t parent_id = 0; | 134 uint32_t parent_id = 0; |
132 if ( stat(from_path, &sb) == -1 ) | 139 if ( stat(from_path, &sb) == -1 ) |
133 { | 140 { |
134 #if DEBUG | 141 #if DEBUG |
135 g_print("ERROR! encountered while stat()'ing \"%s\"\n",from_path); | 142 g_print("ERROR! encountered while stat()'ing \"%s\"\n",from_path); |
136 #endif | 143 #endif |
137 return; | 144 return 1; |
138 } | 145 } |
139 filesize = (uint64_t) sb.st_size; | 146 filesize = (uint64_t) sb.st_size; |
140 filename = g_path_get_basename(from_path); | 147 filename = g_path_get_basename(from_path); |
141 parent_id = 0; | 148 parent_id = mtp_device->default_music_folder; |
149 #if DEBUG | |
150 g_print("Parent id : %d\n",parent_id); | |
151 #endif | |
142 genfile = LIBMTP_new_file_t(); | 152 genfile = LIBMTP_new_file_t(); |
143 genfile->filesize = filesize; | 153 genfile->filesize = filesize; |
144 genfile->filename = strdup(filename); | 154 genfile->filename = strdup(filename); |
145 #if DEBUG | 155 #if DEBUG |
146 g_print("Uploading track '%s'\n",comp); | 156 g_print("Uploading track '%s'\n",comp); |
149 if (ret == 0) | 159 if (ret == 0) |
150 g_print("Upload finished!\n"); | 160 g_print("Upload finished!\n"); |
151 else | 161 else |
152 { | 162 { |
153 g_print("An error has occured while uploading '%s'...\nUpload failed!!!",comp); | 163 g_print("An error has occured while uploading '%s'...\nUpload failed!!!",comp); |
154 show_dialog("An error has occured while uploading...Upload failed! "); | |
155 mtp_initialised = FALSE; | 164 mtp_initialised = FALSE; |
156 return; | 165 return 1; |
157 } | 166 } |
158 LIBMTP_destroy_file_t(genfile); | 167 LIBMTP_destroy_file_t(genfile); |
159 #if DEBUG | 168 #if DEBUG |
160 g_print("genfile destroyed \n"); | 169 g_print("genfile destroyed \n"); |
161 #endif | 170 #endif |
162 g_free(filename); | 171 g_free(filename); |
163 g_free(comp); | 172 g_free(comp); |
164 #if DEBUG | 173 #if DEBUG |
165 g_print("Free ok..exiting upload_file \n "); | 174 g_print("Free ok..exiting upload_file \n "); |
166 #endif | 175 #endif |
167 return; | 176 return 0; |
168 } | 177 } |
169 | 178 |
170 | 179 |
171 gpointer upload(gpointer arg) | 180 gpointer upload(gpointer arg) |
172 { | 181 { |
173 /* if(!mutex) | 182 if(!mutex) |
174 { | 183 { |
175 gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menuitem))),DEFAULT_LABEL); | 184 gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menuitem))),DEFAULT_LABEL); |
176 gtk_widget_set_sensitive(menuitem, TRUE); | 185 gtk_widget_set_sensitive(menuitem, TRUE); |
177 return NULL; | 186 return NULL; |
178 } | 187 } |
179 g_mutex_lock(mutex); */ | 188 g_mutex_lock(mutex); |
180 if(!mtp_device) | 189 if(!mtp_device) |
181 { | 190 { |
182 gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menuitem))),DEFAULT_LABEL); | 191 gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menuitem))),DEFAULT_LABEL); |
183 gtk_widget_set_sensitive(menuitem, TRUE); | 192 gtk_widget_set_sensitive(menuitem, TRUE); |
184 /* g_mutex_unlock(mutex); */ | 193 g_mutex_unlock(mutex); |
185 return NULL; | 194 return NULL; |
186 } | 195 } |
187 | 196 |
188 gchar* from_path; | 197 gchar* from_path; |
198 | |
189 GList *up_list=NULL,*node; | 199 GList *up_list=NULL,*node; |
190 node=up_list=get_upload_list(); | 200 node=up_list=get_upload_list(); |
201 gint up_err=0; | |
191 while(node) | 202 while(node) |
192 { | 203 { |
193 from_path=(gchar*)(node->data); | 204 from_path=(gchar*)(node->data); |
194 upload_file(from_path); | 205 up_err = upload_file(from_path); |
206 if(up_err ) | |
207 { | |
208 show_dialog("An error has occured while uploading...\nUpload failed!"); | |
209 break; | |
210 } | |
211 if(exiting) | |
212 { | |
213 /*show_dialog("Shutting down MTP while uploading.\nPending uploads were cancelled");*/ | |
214 break; | |
215 } | |
216 | |
195 node = g_list_next(node); | 217 node = g_list_next(node); |
196 } | 218 } |
197 g_list_free(up_list); | 219 g_list_free(up_list); |
198 #if DEBUG | 220 #if DEBUG |
199 g_print("up_list free ok, seting menuitem ...\n"); | 221 g_print("up_list free ok, seting menuitem ...\n"); |
200 #endif | 222 #endif |
201 | 223 |
202 gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menuitem))),DEFAULT_LABEL); | 224 gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menuitem))),DEFAULT_LABEL); |
203 gtk_widget_set_sensitive(menuitem, TRUE); | 225 gtk_widget_set_sensitive(menuitem, TRUE); |
204 /* g_mutex_unlock(mutex); */ | 226 g_mutex_unlock(mutex); |
205 #if DEBUG | 227 #if DEBUG |
206 g_print("upload thread killed exiting upload function\n"); | 228 g_print("upload thread killed exiting upload function\n"); |
207 #endif | 229 #endif |
208 g_thread_exit(NULL); | 230 g_thread_exit(NULL); |
209 return NULL; | 231 return NULL; |
258 gtk_widget_show (menuitem); | 280 gtk_widget_show (menuitem); |
259 audacious_menu_plugin_item_add(AUDACIOUS_MENU_PLAYLIST_RCLICK, menuitem); | 281 audacious_menu_plugin_item_add(AUDACIOUS_MENU_PLAYLIST_RCLICK, menuitem); |
260 g_signal_connect (G_OBJECT (menuitem), "button_press_event",G_CALLBACK (mtp_press), NULL); | 282 g_signal_connect (G_OBJECT (menuitem), "button_press_event",G_CALLBACK (mtp_press), NULL); |
261 mutex = g_mutex_new(); | 283 mutex = g_mutex_new(); |
262 plugin_active = TRUE; | 284 plugin_active = TRUE; |
285 exiting=FALSE; | |
263 } | 286 } |
264 | 287 |
265 void mtp_cleanup(void) | 288 void mtp_cleanup(void) |
266 { | 289 { |
267 if (plugin_active) | 290 if (plugin_active) |
268 { | 291 { |
269 | 292 |
270 #if DEBUG | 293 #if DEBUG |
271 if(mtp_initialised) | 294 if(mtp_initialised) |
272 g_print("\n\n !!!CAUTION!!! \n\n" | 295 { |
296 g_print("\n\n !!!CAUTION!!! \n\n" | |
273 "Cleaning up MTP upload plugin, please wait!!!...\n" | 297 "Cleaning up MTP upload plugin, please wait!!!...\n" |
274 "This will block until the pending tracks are uploaded,\n" | 298 "This will block until the pending tracks are uploaded,\n" |
275 "then it will gracefully close your device\n\n" | 299 "then it will gracefully close your device\n\n" |
276 "!!! FORCING SHUTDOWN NOW MAY CAUSE DAMAGE TO YOUR DEVICE !!!\n\n\n" | 300 "!!! FORCING SHUTDOWN NOW MAY CAUSE DAMAGE TO YOUR DEVICE !!!\n\n\n" |
277 "Waiting for the MTP mutex to unlock...\n"); | 301 "Waiting for the MTP mutex to unlock...\n"); |
302 exiting=TRUE; | |
303 } | |
278 #endif | 304 #endif |
279 if(mutex) | 305 if(mutex) |
280 g_mutex_lock(mutex); | 306 g_mutex_lock(mutex); |
281 if(mtp_device!= NULL) | 307 if(mtp_device!= NULL) |
282 { | 308 { |