qt: playlist: use item title if available
[vlc.git] / src / misc / addons.c
blob49a0f3246385ddef0218ff4dd73be5095ee98205
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 <vlc_interrupt.h>
30 #include "libvlc.h"
32 #include <vlc_addons.h>
34 /*****************************************************************************
35 * Structures/definitions
36 *****************************************************************************/
38 typedef struct addon_entry_owner
40 addon_entry_t entry;
41 vlc_atomic_rc_t rc;
42 } addon_entry_owner_t;
44 struct addons_manager_private_t
46 vlc_object_t *p_parent;
48 struct
50 vlc_thread_t thread;
51 vlc_cond_t waitcond;
52 bool b_live;
53 vlc_mutex_t lock;
54 vlc_interrupt_t *p_interrupt;
55 DECL_ARRAY(char*) uris;
56 DECL_ARRAY(addon_entry_t*) entries;
57 } finder;
59 struct
61 vlc_thread_t thread;
62 vlc_cond_t waitcond;
63 bool b_live;
64 vlc_mutex_t lock;
65 vlc_interrupt_t *p_interrupt;
66 DECL_ARRAY(addon_entry_t*) entries;
67 } installer;
70 static void *FinderThread( void * );
71 static void LoadLocalStorage( addons_manager_t *p_manager );
73 /*****************************************************************************
74 * Public functions
75 *****************************************************************************/
77 addon_entry_t * addon_entry_New(void)
79 addon_entry_owner_t *owner = calloc( 1, sizeof(addon_entry_owner_t) );
80 if( unlikely(owner == NULL) )
81 return NULL;
83 vlc_atomic_rc_init( &owner->rc );
85 addon_entry_t *p_entry = &owner->entry;
86 vlc_mutex_init( &p_entry->lock );
87 ARRAY_INIT( p_entry->files );
88 return p_entry;
91 addon_entry_t * addon_entry_Hold( addon_entry_t * p_entry )
93 addon_entry_owner_t *owner = (addon_entry_owner_t *) p_entry;
95 vlc_atomic_rc_inc( &owner->rc );
96 return p_entry;
99 void addon_entry_Release( addon_entry_t * p_entry )
101 addon_entry_owner_t *owner = (addon_entry_owner_t *) p_entry;
103 if( !vlc_atomic_rc_dec( &owner->rc ) )
104 return;
106 free( p_entry->psz_name );
107 free( p_entry->psz_summary );
108 free( p_entry->psz_description );
109 free( p_entry->psz_archive_uri );
110 free( p_entry->psz_author );
111 free( p_entry->psz_source_uri );
112 free( p_entry->psz_image_uri );
113 free( p_entry->psz_image_data );
114 free( p_entry->psz_source_module );
115 free( p_entry->psz_version );
116 free( p_entry->p_custom );
118 addon_file_t *p_file;
119 ARRAY_FOREACH( p_file, p_entry->files )
121 free( p_file->psz_filename );
122 free( p_file->psz_download_uri );
123 free( p_file );
125 ARRAY_RESET( p_entry->files );
127 free( owner );
130 addons_manager_t *addons_manager_New( vlc_object_t *p_this,
131 const struct addons_manager_owner *restrict owner )
133 addons_manager_t *p_manager = malloc( sizeof(addons_manager_t) );
134 if ( !p_manager ) return NULL;
136 p_manager->p_priv = malloc( sizeof(addons_manager_private_t) );
137 if ( !p_manager->p_priv )
139 free( p_manager );
140 return NULL;
143 p_manager->owner = *owner;
144 p_manager->p_priv->p_parent = p_this;
146 p_manager->p_priv->finder.p_interrupt = vlc_interrupt_create();
147 p_manager->p_priv->installer.p_interrupt = vlc_interrupt_create();
148 if ( !p_manager->p_priv->finder.p_interrupt ||
149 !p_manager->p_priv->installer.p_interrupt )
151 if( p_manager->p_priv->finder.p_interrupt )
152 vlc_interrupt_destroy( p_manager->p_priv->finder.p_interrupt );
153 if( p_manager->p_priv->installer.p_interrupt )
154 vlc_interrupt_destroy( p_manager->p_priv->installer.p_interrupt );
155 free( p_manager->p_priv );
156 free( p_manager );
157 return NULL;
160 #define INIT_QUEUE( name ) \
161 p_manager->p_priv->name.b_live = false;\
162 vlc_mutex_init( &p_manager->p_priv->name.lock );\
163 vlc_cond_init( &p_manager->p_priv->name.waitcond );\
164 ARRAY_INIT( p_manager->p_priv->name.entries );
166 INIT_QUEUE( finder )
167 INIT_QUEUE( installer )
168 ARRAY_INIT( p_manager->p_priv->finder.uris );
170 return p_manager;
173 void addons_manager_Delete( addons_manager_t *p_manager )
175 bool b_live;
177 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
178 b_live = p_manager->p_priv->finder.b_live;
179 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
180 if ( b_live )
182 vlc_interrupt_kill( p_manager->p_priv->finder.p_interrupt );
183 vlc_join( p_manager->p_priv->finder.thread, NULL );
186 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
187 b_live = p_manager->p_priv->installer.b_live;
188 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
189 if ( b_live )
191 vlc_interrupt_kill( p_manager->p_priv->installer.p_interrupt );
192 vlc_join( p_manager->p_priv->installer.thread, NULL );
195 addon_entry_t *p_entry;
197 #define FREE_QUEUE( name ) \
198 ARRAY_FOREACH( p_entry, p_manager->p_priv->name.entries )\
199 addon_entry_Release( p_entry );\
200 ARRAY_RESET( p_manager->p_priv->name.entries );\
201 vlc_interrupt_destroy( p_manager->p_priv->name.p_interrupt );
203 FREE_QUEUE( finder )
204 FREE_QUEUE( installer )
206 char *psz_uri;
207 ARRAY_FOREACH( psz_uri, p_manager->p_priv->finder.uris )
208 free( psz_uri );
209 ARRAY_RESET( p_manager->p_priv->finder.uris );
211 free( p_manager->p_priv );
212 free( p_manager );
215 void addons_manager_Gather( addons_manager_t *p_manager, const char *psz_uri )
217 if ( !psz_uri )
218 return;
220 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
222 ARRAY_APPEND( p_manager->p_priv->finder.uris, strdup( psz_uri ) );
224 if( !p_manager->p_priv->finder.b_live )
226 if( vlc_clone( &p_manager->p_priv->finder.thread, FinderThread, p_manager,
227 VLC_THREAD_PRIORITY_LOW ) )
229 msg_Err( p_manager->p_priv->p_parent,
230 "cannot spawn entries provider thread" );
231 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
232 return;
234 p_manager->p_priv->finder.b_live = true;
237 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
238 vlc_cond_signal( &p_manager->p_priv->finder.waitcond );
241 /*****************************************************************************
242 * Private functions
243 *****************************************************************************/
245 static addon_entry_t * getHeldEntryByUUID( addons_manager_t *p_manager,
246 const addon_uuid_t uuid )
248 addon_entry_t *p_return = NULL;
249 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
250 addon_entry_t *p_entry;
251 ARRAY_FOREACH( p_entry, p_manager->p_priv->finder.entries )
253 if ( !memcmp( p_entry->uuid, uuid, sizeof( addon_uuid_t ) ) )
255 p_return = p_entry;
256 addon_entry_Hold( p_return );
257 break;
260 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
261 return p_return;
264 static void MergeSources( addons_manager_t *p_manager,
265 addon_entry_t **pp_addons, int i_count )
267 addon_entry_t *p_entry, *p_manager_entry;
268 addon_uuid_t zerouuid;
269 memset( zerouuid, 0, sizeof( addon_uuid_t ) );
270 for ( int i=0; i < i_count; i++ )
272 p_entry = pp_addons[i];
273 vlc_mutex_lock( &p_entry->lock );
274 if ( memcmp( p_entry->uuid, zerouuid, sizeof( addon_uuid_t ) ) )
275 p_manager_entry = getHeldEntryByUUID( p_manager, p_entry->uuid );
276 else
277 p_manager_entry = NULL;
278 if ( !p_manager_entry )
280 ARRAY_APPEND( p_manager->p_priv->finder.entries, p_entry );
281 p_manager->owner.addon_found( p_manager, p_entry );
283 else
285 vlc_mutex_lock( &p_manager_entry->lock );
286 if ( ( p_manager_entry->psz_version && p_entry->psz_version )
287 && /* FIXME: better version comparison */
288 strcmp( p_manager_entry->psz_version, p_entry->psz_version )
291 p_manager_entry->e_flags |= ADDON_UPDATABLE;
293 vlc_mutex_unlock( &p_manager_entry->lock );
294 addon_entry_Release( p_manager_entry );
296 vlc_mutex_unlock( &p_entry->lock );
300 static void LoadLocalStorage( addons_manager_t *p_manager )
302 addons_finder_t *p_finder =
303 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_finder ), "entries finder" );
304 p_finder->obj.no_interact = true;
306 module_t *p_module = module_need( p_finder, "addons finder",
307 "addons.store.list", true );
308 if( p_module )
310 ARRAY_INIT( p_finder->entries );
311 p_finder->psz_uri = NULL;
312 p_finder->pf_find( p_finder );
313 module_unneed( p_finder, p_module );
315 MergeSources( p_manager, p_finder->entries.p_elems, p_finder->entries.i_size );
317 ARRAY_RESET( p_finder->entries );
319 vlc_object_delete(p_finder);
322 static void finder_thread_interrupted( void* p_data )
324 addons_manager_t *p_manager = p_data;
325 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
326 p_manager->p_priv->finder.b_live = false;
327 vlc_cond_signal( &p_manager->p_priv->finder.waitcond );
328 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
331 static void *FinderThread( void *p_data )
333 addons_manager_t *p_manager = p_data;
334 int i_cancel = vlc_savecancel();
335 vlc_interrupt_set( p_manager->p_priv->finder.p_interrupt );
337 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
338 while( p_manager->p_priv->finder.b_live )
340 char *psz_uri;
342 vlc_interrupt_register( finder_thread_interrupted, p_data );
343 while( p_manager->p_priv->finder.uris.i_size == 0 &&
344 p_manager->p_priv->finder.b_live )
346 vlc_cond_wait( &p_manager->p_priv->finder.waitcond,
347 &p_manager->p_priv->finder.lock );
349 vlc_interrupt_unregister();
350 if( !p_manager->p_priv->finder.b_live )
351 break;
352 psz_uri = p_manager->p_priv->finder.uris.p_elems[0];
353 ARRAY_REMOVE( p_manager->p_priv->finder.uris, 0 );
355 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
357 addons_finder_t *p_finder =
358 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_finder ), "entries finder" );
360 if( p_finder != NULL )
362 p_finder->obj.no_interact = true;
363 module_t *p_module;
364 ARRAY_INIT( p_finder->entries );
365 p_finder->psz_uri = psz_uri;
367 p_module = module_need( p_finder, "addons finder", NULL, false );
368 if( p_module )
370 p_finder->pf_find( p_finder );
371 module_unneed( p_finder, p_module );
372 MergeSources( p_manager, p_finder->entries.p_elems, p_finder->entries.i_size );
374 ARRAY_RESET( p_finder->entries );
375 free( psz_uri );
376 vlc_object_delete(p_finder);
379 p_manager->owner.discovery_ended( p_manager );
380 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
383 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
384 vlc_restorecancel( i_cancel );
385 return NULL;
388 static int addons_manager_WriteCatalog( addons_manager_t *p_manager )
390 int i_return = VLC_EGENERIC;
392 addons_storage_t *p_storage =
393 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_storage ), "entries storage" );
394 p_storage->obj.no_interact = true;
396 module_t *p_module = module_need( p_storage, "addons storage",
397 "addons.store.install", true );
398 if( p_module )
400 vlc_mutex_lock( &p_manager->p_priv->finder.lock );
401 i_return = p_storage->pf_catalog( p_storage, p_manager->p_priv->finder.entries.p_elems,
402 p_manager->p_priv->finder.entries.i_size );
403 vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
404 module_unneed( p_storage, p_module );
406 vlc_object_delete(p_storage);
408 return i_return;
411 int addons_manager_LoadCatalog( addons_manager_t *p_manager )
413 LoadLocalStorage( p_manager );
414 return VLC_SUCCESS;
417 static int installOrRemoveAddon( addons_manager_t *p_manager, addon_entry_t *p_entry, bool b_install )
419 int i_return = VLC_EGENERIC;
421 addons_storage_t *p_storage =
422 vlc_custom_create( p_manager->p_priv->p_parent, sizeof( *p_storage ), "entries storage" );
423 p_storage->obj.no_interact = true;
425 module_t *p_module = module_need( p_storage, "addons storage",
426 "addons.store.install", true );
427 if( p_module )
429 if ( b_install )
430 i_return = p_storage->pf_install( p_storage, p_entry );
431 else
432 i_return = p_storage->pf_remove( p_storage, p_entry );
433 module_unneed( p_storage, p_module );
434 msg_Dbg( p_manager->p_priv->p_parent, "InstallAddon returns %d", i_return );
435 if ( i_return == VLC_SUCCESS )
437 /* Reset flags */
438 vlc_mutex_lock( &p_entry->lock );
439 p_entry->e_flags = ADDON_MANAGEABLE;
440 vlc_mutex_unlock( &p_entry->lock );
443 vlc_object_delete(p_storage);
445 return i_return;
448 static void installer_thread_interrupted( void* p_data )
450 addons_manager_t *p_manager = p_data;
451 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
452 p_manager->p_priv->installer.b_live = false;
453 vlc_cond_signal( &p_manager->p_priv->installer.waitcond );
454 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
457 static void *InstallerThread( void *p_data )
459 addons_manager_t *p_manager = p_data;
460 int i_cancel = vlc_savecancel();
461 vlc_interrupt_set( p_manager->p_priv->installer.p_interrupt );
462 int i_ret;
464 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
465 while( p_manager->p_priv->installer.b_live )
467 vlc_interrupt_register( installer_thread_interrupted, p_data );
468 while ( !p_manager->p_priv->installer.entries.i_size &&
469 p_manager->p_priv->installer.b_live )
471 /* No queued addons */
472 vlc_cond_wait( &p_manager->p_priv->installer.waitcond,
473 &p_manager->p_priv->installer.lock );
475 vlc_interrupt_unregister();
476 if( !p_manager->p_priv->installer.b_live )
477 break;
479 addon_entry_t *p_entry = p_manager->p_priv->installer.entries.p_elems[0];
480 ARRAY_REMOVE( p_manager->p_priv->installer.entries, 0 );
481 addon_entry_Hold( p_entry );
482 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
484 vlc_mutex_lock( &p_entry->lock );
485 /* DO WORK */
486 if ( p_entry->e_state == ADDON_INSTALLED )
488 p_entry->e_state = ADDON_UNINSTALLING;
489 vlc_mutex_unlock( &p_entry->lock );
491 /* notify */
492 p_manager->owner.addon_changed( p_manager, p_entry );
494 i_ret = installOrRemoveAddon( p_manager, p_entry, false );
496 vlc_mutex_lock( &p_entry->lock );
497 p_entry->e_state = ( i_ret == VLC_SUCCESS ) ? ADDON_NOTINSTALLED
498 : ADDON_INSTALLED;
500 else if ( p_entry->e_state == ADDON_NOTINSTALLED )
502 p_entry->e_state = ADDON_INSTALLING;
503 vlc_mutex_unlock( &p_entry->lock );
505 /* notify */
506 p_manager->owner.addon_changed( p_manager, p_entry );
508 i_ret = installOrRemoveAddon( p_manager, p_entry, true );
510 vlc_mutex_lock( &p_entry->lock );
511 p_entry->e_state = ( i_ret == VLC_SUCCESS ) ? ADDON_INSTALLED
512 : ADDON_NOTINSTALLED;
514 vlc_mutex_unlock( &p_entry->lock );
515 /* !DO WORK */
517 p_manager->owner.addon_changed( p_manager, p_entry );
519 addon_entry_Release( p_entry );
521 addons_manager_WriteCatalog( p_manager );
522 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
524 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
525 vlc_restorecancel( i_cancel );
526 return NULL;
529 static int InstallEntry( addons_manager_t *p_manager, addon_entry_t *p_entry )
531 if ( p_entry->e_type == ADDON_UNKNOWN ||
532 p_entry->e_type == ADDON_PLUGIN ||
533 p_entry->e_type == ADDON_OTHER )
534 return VLC_EBADVAR;
536 vlc_mutex_lock( &p_manager->p_priv->installer.lock );
537 ARRAY_APPEND( p_manager->p_priv->installer.entries, p_entry );
538 if( !p_manager->p_priv->installer.b_live )
540 if( vlc_clone( &p_manager->p_priv->installer.thread, InstallerThread, p_manager,
541 VLC_THREAD_PRIORITY_LOW ) )
543 msg_Err( p_manager->p_priv->p_parent,
544 "cannot spawn addons installer thread" );
545 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
546 return VLC_EGENERIC;
548 else
549 p_manager->p_priv->installer.b_live = true;
551 vlc_mutex_unlock( &p_manager->p_priv->installer.lock );
552 vlc_cond_signal( &p_manager->p_priv->installer.waitcond );
553 return VLC_SUCCESS;
556 int addons_manager_Install( addons_manager_t *p_manager, const addon_uuid_t uuid )
558 addon_entry_t *p_install_entry = getHeldEntryByUUID( p_manager, uuid );
559 if ( ! p_install_entry ) return VLC_EGENERIC;
560 int i_ret = InstallEntry( p_manager, p_install_entry );
561 addon_entry_Release( p_install_entry );
562 return i_ret;
565 int addons_manager_Remove( addons_manager_t *p_manager, const addon_uuid_t uuid )
567 return addons_manager_Install( p_manager, uuid );