demux: mp4: move ID3 genres to meta
[vlc.git] / src / misc / addons.c
blob641ebd3b4abfa75cc705f5cbf1944d7f90ca2f1b
1 /*****************************************************************************
2 * addons.c: VLC addons manager
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <vlc_common.h>
26 #include <vlc_atomic.h>
27 #include <vlc_modules.h>
28 #include <vlc_arrays.h>
29 #include "libvlc.h"
31 #include <vlc_addons.h>
33 /*****************************************************************************
34 * Structures/definitions
35 *****************************************************************************/
37 typedef struct addon_entry_owner
39 addon_entry_t entry;
40 atomic_uint refs;
41 } addon_entry_owner_t;
43 struct addons_manager_private_t
45 vlc_object_t *p_parent;
47 struct
49 vlc_thread_t thread;
50 vlc_cond_t waitcond;
51 bool b_live;
52 vlc_mutex_t lock;
53 DECL_ARRAY(char*) uris;
54 DECL_ARRAY(addon_entry_t*) entries;
55 } finder;
57 struct
59 vlc_thread_t thread;
60 vlc_cond_t waitcond;
61 bool b_live;
62 vlc_mutex_t lock;
63 DECL_ARRAY(addon_entry_t*) entries;
64 } installer;
67 static void *FinderThread( void * );
68 static void LoadLocalStorage( addons_manager_t *p_manager );
70 /*****************************************************************************
71 * Public functions
72 *****************************************************************************/
74 addon_entry_t * addon_entry_New(void)
76 addon_entry_owner_t *owner = calloc( 1, sizeof(addon_entry_owner_t) );
77 if( unlikely(owner == NULL) )
78 return NULL;
80 atomic_init( &owner->refs, 1 );
82 addon_entry_t *p_entry = &owner->entry;
83 vlc_mutex_init( &p_entry->lock );
84 ARRAY_INIT( p_entry->files );
85 return p_entry;
88 addon_entry_t * addon_entry_Hold( addon_entry_t * p_entry )
90 addon_entry_owner_t *owner = (addon_entry_owner_t *) p_entry;
92 atomic_fetch_add( &owner->refs, 1 );
93 return p_entry;
96 void addon_entry_Release( addon_entry_t * p_entry )
98 addon_entry_owner_t *owner = (addon_entry_owner_t *) p_entry;
100 if( atomic_fetch_sub(&owner->refs, 1) != 1 )
101 return;
103 free( p_entry->psz_name );
104 free( p_entry->psz_summary );
105 free( p_entry->psz_description );
106 free( p_entry->psz_archive_uri );
107 free( p_entry->psz_author );
108 free( p_entry->psz_source_uri );
109 free( p_entry->psz_image_uri );
110 free( p_entry->psz_image_data );
111 free( p_entry->psz_source_module );
112 free( p_entry->psz_version );
113 free( p_entry->p_custom );
115 addon_file_t *p_file;
116 FOREACH_ARRAY( p_file, p_entry->files )
117 free( p_file->psz_filename );
118 free( p_file->psz_download_uri );
119 FOREACH_END()
120 ARRAY_RESET( p_entry->files );
122 vlc_mutex_destroy( &p_entry->lock );
123 free( owner );
126 addons_manager_t *addons_manager_New( vlc_object_t *p_this,
127 const struct addons_manager_owner *restrict owner )
129 addons_manager_t *p_manager = malloc( sizeof(addons_manager_t) );
130 if ( !p_manager ) return NULL;
132 p_manager->p_priv = malloc( sizeof(addons_manager_private_t) );
133 if ( !p_manager->p_priv )
135 free( p_manager );
136 return NULL;
139 p_manager->owner = *owner;
140 p_manager->p_priv->p_parent = p_this;
142 #define INIT_QUEUE( name ) \
143 p_manager->p_priv->name.b_live = false;\
144 vlc_mutex_init( &p_manager->p_priv->name.lock );\
145 vlc_cond_init( &p_manager->p_priv->name.waitcond );\
146 ARRAY_INIT( p_manager->p_priv->name.entries );
148 INIT_QUEUE( finder )
149 INIT_QUEUE( installer )
150 ARRAY_INIT( p_manager->p_priv->finder.uris );
152 return p_manager;
155 void addons_manager_Delete( addons_manager_t *p_manager )
157 bool b_live;
159 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
160 b_live = p_manager->p_priv->finder.b_live;
161 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
162 if ( b_live )
164 vlc_cancel( p_manager->p_priv->finder.thread );
165 vlc_join( p_manager->p_priv->finder.thread, NULL );
168 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
169 b_live = p_manager->p_priv->installer.b_live;
170 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
171 if ( b_live )
173 vlc_cancel( p_manager->p_priv->installer.thread );
174 vlc_join( p_manager->p_priv->installer.thread, NULL );
177 #define FREE_QUEUE( name ) \
178 FOREACH_ARRAY( addon_entry_t *p_entry, p_manager->p_priv->name.entries )\
179 addon_entry_Release( p_entry );\
180 FOREACH_END();\
181 ARRAY_RESET( p_manager->p_priv->name.entries );\
182 vlc_mutex_destroy( &p_manager->p_priv->name.lock );\
183 vlc_cond_destroy( &p_manager->p_priv->name.waitcond );
185 FREE_QUEUE( finder )
186 FREE_QUEUE( installer )
187 FOREACH_ARRAY( char *psz_uri, p_manager->p_priv->finder.uris )
188 free( psz_uri );
189 FOREACH_END();
190 ARRAY_RESET( p_manager->p_priv->finder.uris );
192 free( p_manager->p_priv );
193 free( p_manager );
196 void addons_manager_Gather( addons_manager_t *p_manager, const char *psz_uri )
198 if ( !psz_uri )
199 return;
201 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
203 ARRAY_APPEND( p_manager->p_priv->finder.uris, strdup( psz_uri ) );
205 if( !p_manager->p_priv->finder.b_live )
207 if( vlc_clone( &p_manager->p_priv->finder.thread, FinderThread, p_manager,
208 VLC_THREAD_PRIORITY_LOW ) )
210 msg_Err( p_manager->p_priv->p_parent,
211 "cannot spawn entries provider thread" );
212 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
213 return;
215 p_manager->p_priv->finder.b_live = true;
218 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
219 vlc_cond_signal( &p_manager->p_priv->finder.waitcond );
222 /*****************************************************************************
223 * Private functions
224 *****************************************************************************/
226 static addon_entry_t * getHeldEntryByUUID( addons_manager_t *p_manager,
227 const addon_uuid_t uuid )
229 addon_entry_t *p_return = NULL;
230 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
231 FOREACH_ARRAY( addon_entry_t *p_entry, p_manager->p_priv->finder.entries )
232 if ( !memcmp( p_entry->uuid, uuid, sizeof( addon_uuid_t ) ) )
234 p_return = p_entry;
235 addon_entry_Hold( p_return );
236 break;
238 FOREACH_END()
239 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
240 return p_return;
243 static void MergeSources( addons_manager_t *p_manager,
244 addon_entry_t **pp_addons, int i_count )
246 addon_entry_t *p_entry, *p_manager_entry;
247 addon_uuid_t zerouuid;
248 memset( zerouuid, 0, sizeof( addon_uuid_t ) );
249 for ( int i=0; i < i_count; i++ )
251 p_entry = pp_addons[i];
252 vlc_mutex_lock( &p_entry->lock );
253 if ( memcmp( p_entry->uuid, zerouuid, sizeof( addon_uuid_t ) ) )
254 p_manager_entry = getHeldEntryByUUID( p_manager, p_entry->uuid );
255 else
256 p_manager_entry = NULL;
257 if ( !p_manager_entry )
259 ARRAY_APPEND( p_manager->p_priv->finder.entries, p_entry );
260 p_manager->owner.addon_found( p_manager, p_entry );
262 else
264 vlc_mutex_lock( &p_manager_entry->lock );
265 if ( ( p_manager_entry->psz_version && p_entry->psz_version )
266 && /* FIXME: better version comparison */
267 strcmp( p_manager_entry->psz_version, p_entry->psz_version )
270 p_manager_entry->e_flags |= ADDON_UPDATABLE;
272 vlc_mutex_unlock( &p_manager_entry->lock );
273 addon_entry_Release( p_manager_entry );
275 vlc_mutex_unlock( &p_entry->lock );
279 static void LoadLocalStorage( addons_manager_t *p_manager )
281 addons_finder_t *p_finder =
282 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_finder ), "entries finder" );
283 p_finder->obj.flags |= OBJECT_FLAGS_NOINTERACT;
285 module_t *p_module = module_need( p_finder, "addons finder",
286 "addons.store.list", true );
287 if( p_module )
289 ARRAY_INIT( p_finder->entries );
290 p_finder->psz_uri = NULL;
291 p_finder->pf_find( p_finder );
292 module_unneed( p_finder, p_module );
294 MergeSources( p_manager, p_finder->entries.p_elems, p_finder->entries.i_size );
296 ARRAY_RESET( p_finder->entries );
298 vlc_object_release( p_finder );
301 static void *FinderThread( void *p_data )
303 addons_manager_t *p_manager = p_data;
305 for( ;; )
307 char *psz_uri;
309 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
310 mutex_cleanup_push( &p_manager->p_priv->finder.lock );
311 while( p_manager->p_priv->finder.uris.i_size == 0 )
313 vlc_cond_wait( &p_manager->p_priv->finder.waitcond,
314 &p_manager->p_priv->finder.lock );
316 psz_uri = p_manager->p_priv->finder.uris.p_elems[0];
317 ARRAY_REMOVE( p_manager->p_priv->finder.uris, 0 );
318 vlc_cleanup_pop();
319 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
321 int i_cancel = vlc_savecancel();
323 addons_finder_t *p_finder =
324 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_finder ), "entries finder" );
326 if( p_finder != NULL )
328 p_finder->obj.flags |= OBJECT_FLAGS_NOINTERACT;
329 module_t *p_module;
330 ARRAY_INIT( p_finder->entries );
331 p_finder->psz_uri = psz_uri;
333 p_module = module_need( p_finder, "addons finder", NULL, false );
334 if( p_module )
336 p_finder->pf_find( p_finder );
337 module_unneed( p_finder, p_module );
338 MergeSources( p_manager, p_finder->entries.p_elems, p_finder->entries.i_size );
340 ARRAY_RESET( p_finder->entries );
341 free( psz_uri );
342 vlc_object_release( p_finder );
345 p_manager->owner.discovery_ended( p_manager );
346 vlc_restorecancel( i_cancel );
347 vlc_testcancel();
350 return NULL;
353 static int addons_manager_WriteCatalog( addons_manager_t *p_manager )
355 int i_return = VLC_EGENERIC;
357 addons_storage_t *p_storage =
358 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_storage ), "entries storage" );
359 p_storage->obj.flags |= OBJECT_FLAGS_NOINTERACT;
361 module_t *p_module = module_need( p_storage, "addons storage",
362 "addons.store.install", true );
363 if( p_module )
365 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
366 i_return = p_storage->pf_catalog( p_storage, p_manager->p_priv->finder.entries.p_elems,
367 p_manager->p_priv->finder.entries.i_size );
368 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
369 module_unneed( p_storage, p_module );
371 vlc_object_release( p_storage );
373 return i_return;
376 int addons_manager_LoadCatalog( addons_manager_t *p_manager )
378 LoadLocalStorage( p_manager );
379 return VLC_SUCCESS;
382 static int installOrRemoveAddon( addons_manager_t *p_manager, addon_entry_t *p_entry, bool b_install )
384 int i_return = VLC_EGENERIC;
386 addons_storage_t *p_storage =
387 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_storage ), "entries storage" );
388 p_storage->obj.flags |= OBJECT_FLAGS_NOINTERACT;
390 module_t *p_module = module_need( p_storage, "addons storage",
391 "addons.store.install", true );
392 if( p_module )
394 if ( b_install )
395 i_return = p_storage->pf_install( p_storage, p_entry );
396 else
397 i_return = p_storage->pf_remove( p_storage, p_entry );
398 module_unneed( p_storage, p_module );
399 msg_Dbg( p_manager->p_priv->p_parent, "InstallAddon returns %d", i_return );
400 if ( i_return == VLC_SUCCESS )
402 /* Reset flags */
403 vlc_mutex_lock( &p_entry->lock );
404 p_entry->e_flags = ADDON_MANAGEABLE;
405 vlc_mutex_unlock( &p_entry->lock );
408 vlc_object_release( p_storage );
410 return i_return;
413 static void *InstallerThread( void *p_data )
415 addons_manager_t *p_manager = p_data;
416 int i_ret;
418 for( ;; )
420 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
421 mutex_cleanup_push( &p_manager->p_priv->installer.lock );
422 while ( !p_manager->p_priv->installer.entries.i_size )
424 /* No queued addons */
425 vlc_cond_wait( &p_manager->p_priv->installer.waitcond,
426 &p_manager->p_priv->installer.lock );
428 vlc_cleanup_pop();
430 addon_entry_t *p_entry = p_manager->p_priv->installer.entries.p_elems[0];
431 ARRAY_REMOVE( p_manager->p_priv->installer.entries, 0 );
432 addon_entry_Hold( p_entry );
433 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
435 int i_cancel = vlc_savecancel();
436 vlc_mutex_lock( &p_entry->lock );
437 /* DO WORK */
438 if ( p_entry->e_state == ADDON_INSTALLED )
440 p_entry->e_state = ADDON_UNINSTALLING;
441 vlc_mutex_unlock( &p_entry->lock );
443 /* notify */
444 p_manager->owner.addon_changed( p_manager, p_entry );
446 i_ret = installOrRemoveAddon( p_manager, p_entry, false );
448 vlc_mutex_lock( &p_entry->lock );
449 p_entry->e_state = ( i_ret == VLC_SUCCESS ) ? ADDON_NOTINSTALLED
450 : ADDON_INSTALLED;
452 else if ( p_entry->e_state == ADDON_NOTINSTALLED )
454 p_entry->e_state = ADDON_INSTALLING;
455 vlc_mutex_unlock( &p_entry->lock );
457 /* notify */
458 p_manager->owner.addon_changed( p_manager, p_entry );
460 i_ret = installOrRemoveAddon( p_manager, p_entry, true );
462 vlc_mutex_lock( &p_entry->lock );
463 p_entry->e_state = ( i_ret == VLC_SUCCESS ) ? ADDON_INSTALLED
464 : ADDON_NOTINSTALLED;
466 vlc_mutex_unlock( &p_entry->lock );
467 /* !DO WORK */
469 p_manager->owner.addon_changed( p_manager, p_entry );
471 addon_entry_Release( p_entry );
473 addons_manager_WriteCatalog( p_manager );
474 vlc_restorecancel( i_cancel );
477 return NULL;
480 static int InstallEntry( addons_manager_t *p_manager, addon_entry_t *p_entry )
482 if ( p_entry->e_type == ADDON_UNKNOWN ||
483 p_entry->e_type == ADDON_PLUGIN ||
484 p_entry->e_type == ADDON_OTHER )
485 return VLC_EBADVAR;
487 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
488 ARRAY_APPEND( p_manager->p_priv->installer.entries, p_entry );
489 if( !p_manager->p_priv->installer.b_live )
491 if( vlc_clone( &p_manager->p_priv->installer.thread, InstallerThread, p_manager,
492 VLC_THREAD_PRIORITY_LOW ) )
494 msg_Err( p_manager->p_priv->p_parent,
495 "cannot spawn addons installer thread" );
496 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
497 return VLC_EGENERIC;
499 else
500 p_manager->p_priv->installer.b_live = true;
502 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
503 vlc_cond_signal( &p_manager->p_priv->installer.waitcond );
504 return VLC_SUCCESS;
507 int addons_manager_Install( addons_manager_t *p_manager, const addon_uuid_t uuid )
509 addon_entry_t *p_install_entry = getHeldEntryByUUID( p_manager, uuid );
510 if ( ! p_install_entry ) return VLC_EGENERIC;
511 int i_ret = InstallEntry( p_manager, p_install_entry );
512 addon_entry_Release( p_install_entry );
513 return i_ret;
516 int addons_manager_Remove( addons_manager_t *p_manager, const addon_uuid_t uuid )
518 return addons_manager_Install( p_manager, uuid );