comparison src/mtp_up/mtp.c @ 1456:475eac76a8ba

MTP upload plugin
author Cristi Magherusan <majeru@atheme-project.org>
date Sat, 11 Aug 2007 01:52:28 +0300
parents
children b2debf8c9ec8
comparison
equal deleted inserted replaced
1455:68c9906e0ac4 1456:475eac76a8ba
1 /*
2 * Audacious MTP upload plugin
3 *
4 * Copyright (c) 2007 Cristian Magherusan <majeru@atheme.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 3 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses>.
17 */
18
19 #include <glib.h>
20 #include <libmtp.h>
21 #include <audacious/plugin.h>
22 #include <audacious/playlist.h>
23 #include <audacious/ui_plugin_menu.h>
24 #define DEBUG 1
25
26 GMutex * mutex = NULL;
27 gboolean mtp_initialised = FALSE;
28 LIBMTP_mtpdevice_t *mtp_device = NULL;
29 LIBMTP_progressfunc_t *callback;
30 LIBMTP_file_t *filelist;
31 Playlist *active_playlist;
32
33 void mtp_init ( void );
34 void mtp_cleanup ( void );
35 void mtp_prefs ( void );
36 void mtp_about ( void );
37
38 GeneralPlugin mtp_gp =
39 {
40 NULL, /* handle */
41 NULL, /* filename */
42 "MTP Upload " , /* description */
43 mtp_init, /* init */
44 mtp_about, /* about */
45 mtp_prefs, /* configure */
46 mtp_cleanup /* cleanup */
47 };
48 GtkWidget *menuitem;
49
50 GeneralPlugin *mtp_gplist[] = { &mtp_gp, NULL };
51 DECLARE_PLUGIN(mtp_gp, NULL, NULL, NULL, NULL, NULL, mtp_gplist, NULL, NULL)
52
53
54 gpointer upload(gpointer arg)
55 {
56 Playlist *current_play;
57 Tuple *tuple;
58 gchar *from_path;
59 gchar *comp;
60 char *filename;
61 uint64_t filesize;
62 struct stat sb;
63 LIBMTP_file_t *genfile;
64 int ret;
65 uint32_t parent_id = 0;
66 GList *node;
67 PlaylistEntry *entry;
68 current_play = g_new0(Playlist,1);
69 current_play = playlist_get_active();
70 node = current_play->entries;
71 PLAYLIST_LOCK(current_play->mutex); /*needed so that the user doesn't modify the selection*/
72 while (node) {
73 entry = PLAYLIST_ENTRY(node->data);
74 if (entry->selected)
75 {
76 tuple = entry->tuple;
77 from_path = g_strdup_printf("%s/%s", tuple_get_string(tuple, "file-path"), tuple_get_string(tuple, "file-name"));
78 comp = g_strescape(from_path,NULL);
79 if ( stat(from_path, &sb) == -1 )
80 {
81 #if DEBUG
82 g_print("ERROR!");
83 #endif
84 return NULL;
85 }
86 filesize = (uint64_t) sb.st_size;
87 filename = basename(from_path);
88 parent_id = 0;
89 genfile = LIBMTP_new_file_t();
90 genfile->filesize = filesize;
91 genfile->filename = strdup(filename);
92 #if DEBUG
93 g_print("Uploading track '%s'\n",comp);
94 #endif
95 ret = LIBMTP_Send_File_From_File(mtp_device,comp , genfile, NULL , NULL, parent_id);
96 #if DEBUG
97 if (ret == 0)
98 g_print("Upload finished!\n");
99 else
100 g_print("An error has occured while uploading '%s'...\nUpload failed!!!",comp);
101 #endif
102 LIBMTP_destroy_file_t(genfile);
103 g_free(from_path);
104 g_free(comp);
105 entry->selected = FALSE;
106 }
107 node = g_list_next(node);
108 }
109 PLAYLIST_UNLOCK(current_play->mutex);
110 return NULL;
111 }
112
113 void
114 mtp_prefs ( void )
115 {
116 /*pref stub*/
117 }
118
119
120 void
121 mtp_about ( void )
122 {
123 /*about stub*/
124 }
125
126 void mtp_press()
127 {
128 if(!mutex)
129 return;
130 g_mutex_lock(mutex);
131 if(!mtp_initialised)
132 {
133 #if DEBUG
134 g_print("Initializing the MTP device...\n");
135 #endif
136 mtp_device = LIBMTP_Get_First_Device();
137 mtp_initialised = TRUE;
138 }
139 g_mutex_unlock(mutex);
140 if(mtp_device == NULL)
141 {
142 #if DEBUG
143 g_print("No MTP devices have been found !!!");
144 #endif
145 return;
146
147 }
148 g_thread_create(upload,NULL,FALSE,NULL);
149 }
150
151 void mtp_init(void)
152 {
153 menuitem=gtk_menu_item_new_with_label("Upload to MTP");
154 gtk_widget_show (menuitem);
155 audacious_menu_plugin_item_add(AUDACIOUS_MENU_PLAYLIST_RCLICK, menuitem);
156 g_signal_connect (G_OBJECT (menuitem), "button_press_event",G_CALLBACK (mtp_press), NULL);
157 LIBMTP_Init();
158 mutex = g_mutex_new();
159 }
160
161 void mtp_cleanup(void)
162 {
163 #if DEBUG
164 g_print("Cleaning up MTP_upload\n");
165 #endif
166 // audacious_menu_plugin_item_remove(AUDACIOUS_MENU_PLAYLIST, menuitem );
167 g_mutex_free (mutex);
168 mutex = NULL;
169 }
170