]> git.sesse.net Git - vlc/blob - src/misc/media_library.c
ML: Replace pl_Hold with pl_Get
[vlc] / src / misc / media_library.c
1 /*****************************************************************************
2  * media_library.c: SQL-based media library: ML creators and destructors
3  *****************************************************************************
4  * Copyright (C) 2009-2010 the VideoLAN team and AUTHORS
5  * $Id$
6  *
7  * Authors: Srikanth Raju <srikiraju at gmail dot com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #if !defined( __LIBVLC__ )
25   #error You are not libvlc or one of its plugins. You cannot include this file
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #if defined(MEDIA_LIBRARY)
33
34 #include <assert.h>
35 #include <vlc_media_library.h>
36 #include "../libvlc.h"
37
38 /**
39  * @brief Destroy the medialibrary object
40  * @param Parent object that holds the media library object
41  */
42 void __ml_Destroy( vlc_object_t * p_this )
43 {
44     media_library_t* p_ml = ( media_library_t* )p_this;
45     module_unneed( p_ml, p_ml->p_module );
46 }
47
48
49 /**
50  * Atomically set the reference count to 1.
51  * @param p_gc reference counted object
52  * @param pf_destruct destruction calback
53  * @return p_gc.
54  */
55 static void *ml_gc_init (ml_gc_object_t *p_gc, void (*pf_destruct) (ml_gc_object_t *))
56 {
57     /* There is no point in using the GC if there is no destructor... */
58     assert (pf_destruct);
59     p_gc->pf_destructor = pf_destruct;
60
61     p_gc->pool = false;
62     p_gc->refs = 1;
63     /* Nobody else can possibly lock the spin - it's there as a barrier */
64     vlc_spin_init (&p_gc->spin);
65     vlc_spin_lock (&p_gc->spin);
66     vlc_spin_unlock (&p_gc->spin);
67     return p_gc;
68 }
69
70
71
72 /**
73  * @brief Create an instance of the media library
74  * @param p_this Parent object
75  * @param psz_name Name which is passed to module_need (not needed)
76  * @return p_ml created and attached, module loaded. NULL if
77  * not able to load
78  */
79 media_library_t *__ml_Create( vlc_object_t *p_this, char *psz_name )
80 {
81     media_library_t *p_ml = NULL;
82
83     p_ml = ( media_library_t * ) vlc_custom_create(
84                                 p_this, sizeof( media_library_t ),
85                                 VLC_OBJECT_GENERIC, "media-library" );
86     if( !p_ml )
87     {
88         msg_Err( p_this, "unable to create media library object" );
89         return NULL;
90     }
91     vlc_object_attach( p_ml, p_this );
92
93     p_ml->p_module = module_need( p_ml, "media-library", psz_name, false );
94     if( !p_ml->p_module )
95     {
96         vlc_object_release( p_ml );
97         msg_Err( p_this, "Media Library provider not found" );
98         return NULL;
99     }
100
101     return p_ml;
102 }
103
104 /**
105  * @brief Acquire a reference to the media library singleton
106  * @param p_this Object that holds the reference
107  * @return media_library_t The ml object. NULL if not compiled with
108  * media library or if unable to load
109  */
110 media_library_t* __ml_Hold( vlc_object_t* p_this )
111 {
112     media_library_t* p_ml = NULL;
113     p_ml = libvlc_priv (p_this->p_libvlc)->p_ml;
114     assert( VLC_OBJECT( p_ml ) != p_this );
115     if( p_ml == NULL &&
116             var_GetBool( p_this->p_libvlc, "load-media-library-on-startup" ) == false )
117     {
118         libvlc_priv (p_this->p_libvlc)->p_ml
119             = __ml_Create( VLC_OBJECT( p_this->p_libvlc ), NULL );
120         p_ml = libvlc_priv (p_this->p_libvlc)->p_ml;
121     }
122     if( p_ml )
123         vlc_object_hold( p_ml );
124     return p_ml;
125 }
126
127 /**
128  * @brief Release a reference to the media library singleton
129  * @param p_this Object that holds the reference
130  */
131 void __ml_Release( vlc_object_t* p_this )
132 {
133     media_library_t* p_ml;
134     p_ml = libvlc_priv (p_this->p_libvlc)->p_ml;
135     if( p_ml == NULL )
136     {
137         msg_Warn( p_this->p_libvlc , "Spurious release ML called");
138         return;
139     }
140     assert( VLC_OBJECT( p_ml ) != p_this );
141     vlc_object_release( p_ml );
142 }
143
144 /**
145  * @brief Destructor for ml_media_t
146  */
147 static void media_Destroy( ml_gc_object_t *p_gc )
148 {
149     ml_media_t* p_media = ml_priv( p_gc, ml_media_t );
150     vlc_mutex_destroy( &p_media->lock );
151     ml_FreeMediaContent( p_media );
152     free( p_media );
153 }
154
155 /**
156  * @brief Object constructor for ml_media_t
157  * @param p_ml The media library object
158  * @param id If 0, this item isn't in database. If non zero, it is and
159  * it will be a singleton
160  * @param select Type of object
161  * @param reload Whether to reload from database
162  */
163 ml_media_t* media_New( media_library_t* p_ml, int id,
164         ml_select_e select, bool reload )
165 {
166     if( id == 0 )
167     {
168         ml_media_t* p_media = NULL;
169         p_media = ( ml_media_t* )calloc( 1, sizeof( ml_media_t ) );
170         ml_gc_init( &p_media->ml_gc_data, media_Destroy );
171         vlc_mutex_init( &p_media->lock );
172         return p_media;
173     }
174     else
175         return p_ml->functions.pf_GetMedia( p_ml, id, select, reload );
176 }
177
178 /**
179  * @brief Update a given table
180  * @param p_media_library The media library object
181  * @param selected_type The table to update
182  * @param psz_lvalue The role of the person if selected_type = ML_PEOPLE
183  * @param id The id of the row to update
184  * @param ... The update data. [SelectType [RoleType] Value] ... ML_END
185  */
186 int __ml_UpdateSimple( media_library_t *p_media_library,
187                                      ml_select_e selected_type,
188                                      const char* psz_lvalue,
189                                      int id, ... )
190 {
191     ml_element_t *update = NULL;
192     vlc_array_t *array = vlc_array_new();
193     int i_ret = VLC_SUCCESS;
194
195     va_list args;
196     va_start( args, id );
197
198     ml_select_e sel;
199     do {
200         update = ( ml_element_t* ) calloc( 1, sizeof( ml_element_t ) );
201         sel = ( ml_select_e ) va_arg( args, int );
202         update->criteria = sel;
203         if( sel == ML_PEOPLE )
204         {
205             update->lvalue.str = va_arg( args, char* );
206             update->value.str = va_arg( args, char* );
207             vlc_array_append( array, update );
208         }
209         else if( sel == ML_PEOPLE_ID )
210         {
211            update->lvalue.str = va_arg( args, char* );
212            update->value.i = va_arg( args, int );
213            vlc_array_append( array, update );
214         }
215         else if( sel == ML_PEOPLE_ROLE )
216         {
217 #ifndef NDEBUG
218             msg_Dbg( p_media_library,
219                      "this argument is invalid for Update: %d",
220                      (int)sel );
221 #endif
222         }
223         else
224         {
225             switch( ml_AttributeIsString( sel ) )
226             {
227                 case -1:
228                     if( sel != ML_END )
229                     {
230 #ifndef NDEBUG
231                         msg_Dbg( p_media_library,
232                                  "this argument is invalid for Update: %d",
233                                  (int)sel );
234 #endif
235                         i_ret = VLC_EGENERIC;
236                     }
237                     else if( sel == ML_END )
238                         vlc_array_append( array, update );
239                     break;
240                 case 0:
241                     update->value.str = va_arg( args, char* );
242                     vlc_array_append( array, update );
243                     break;
244                 case 1:
245                     update->value.i = va_arg( args, int );
246                     vlc_array_append( array, update );
247                     break;
248             }
249         }
250     } while( sel != ML_END );
251
252     va_end( args );
253
254     ml_ftree_t* p_where = NULL;
255     ml_ftree_t* find = ( ml_ftree_t* ) calloc( 1, sizeof( ml_ftree_t ) );
256     find->criteria = ML_ID;
257     find->value.i = id ;
258     find->comp = ML_COMP_EQUAL;
259     p_where = ml_FtreeFastAnd( p_where, find );
260
261     /* Let's update the database ! */
262     if( i_ret == VLC_SUCCESS )
263         i_ret = ml_Update( p_media_library, selected_type, psz_lvalue,
264                             p_where, array );
265
266     /* Destroying array */
267     for( int i = 0; i < vlc_array_count( array ); i++ )
268     {
269         free( vlc_array_item_at_index( array, i ) );
270     }
271     vlc_array_destroy( array );
272     ml_FreeFindTree( p_where );
273
274     return i_ret;
275 }
276
277 /**
278  * @brief Connect up a find tree
279  * @param op operator to connect with
280  * If op = ML_OP_NONE, then you are connecting to a tree consisting of
281  * only SPECIAL nodes.
282  * If op = ML_OP_NOT, then right MUST be NULL
283  * op must not be ML_OP_SPECIAL, @see __ml_FtreeSpec
284  * @param left part of the tree
285  * @param right part of the tree
286  * @return Pointer to new tree
287  * @note Use the helpers!
288  */
289 ml_ftree_t* ml_OpConnectChilds( ml_op_e op, ml_ftree_t* left,
290         ml_ftree_t* right )
291 {
292     /* Use this Op for fresh trees (with only special nodes/none at all!) */
293     if( op == ML_OP_NONE )
294     {
295         assert( ml_FtreeHasOp( left ) == 0 );
296         if( left == NULL )
297             return right;
298         /* Percolate down tree only for special nodes */
299         assert( left->op == ML_OP_SPECIAL );
300         if( left->left == NULL )
301         {
302             left->left = right;
303             return left;
304         }
305         else
306         {
307             return ml_OpConnectChilds( ML_OP_NONE, left->left, right );
308         }
309     }
310     else if( op == ML_OP_NOT )
311     {
312         assert( right == NULL && left != NULL );
313         assert( ml_FtreeHasOp( left ) > 0 );
314     }
315     else if( op == ML_OP_SPECIAL )
316     {
317         assert( 0 );
318     }
319     else
320     {
321         assert( right != NULL && left != NULL );
322         assert( ml_FtreeHasOp( left ) > 0 );
323         assert( ml_FtreeHasOp( right ) > 0 );
324     }
325     ml_ftree_t* p_parent = (ml_ftree_t *) calloc( 1, sizeof( ml_ftree_t ) );
326     p_parent->op = op;
327     p_parent->left = left;
328     p_parent->right = right;
329     return p_parent;
330 }
331
332 /**
333  * @brief Attaches a special node to a tree
334  * @param tree Tree to attach special node to
335  * @param crit Criteria may be SORT_ASC, SORT_DESC, LIMIT or DISTINCT
336  * @param limit Limit used if LIMIT criteria used
337  * @param Sort string used if SORT criteria is used
338  * @return Pointer to new tree
339  * @note Use the helpers
340  */
341 ml_ftree_t* __ml_FtreeSpec( ml_ftree_t* tree,
342                                           ml_select_e crit,
343                                           int limit,
344                                           char* sort )
345 {
346     assert( crit == ML_SORT_ASC || crit == ML_LIMIT || crit == ML_SORT_DESC ||
347             crit == ML_DISTINCT );
348     ml_ftree_t* right = ( ml_ftree_t* ) calloc( 1, sizeof( ml_ftree_t ) );
349     right->criteria = crit;
350     if( crit == ML_LIMIT )
351         right->value.i = limit;
352     else if( crit == ML_SORT_ASC || crit == ML_SORT_DESC )
353         right->value.str = strdup( sort );
354     right->op = ML_OP_NONE;
355     ml_ftree_t* p_parent = ( ml_ftree_t* ) calloc( 1, sizeof( ml_ftree_t ) );
356     p_parent->right = right;
357     p_parent->op = ML_OP_SPECIAL;
358     p_parent->left = tree;
359     return p_parent;
360 }
361
362
363 /**
364  * @brief Creates and adds the playlist based on a given find tree
365  * @param p_ml Media library object
366  * @param p_tree Find tree to create SELECT
367  */
368 void ml_PlaySmartPlaylistBasedOn( media_library_t* p_ml,
369                                                 ml_ftree_t* p_tree )
370 {
371     assert( p_tree );
372     vlc_array_t* p_results = vlc_array_new();
373     ml_FindAdv( p_ml, p_results, ML_ID, NULL, p_tree );
374     playlist_t* p_pl = pl_Get( p_ml );
375     playlist_Lock( p_pl );
376     playlist_Clear( p_pl, true );
377     for( int i = 0; i < vlc_array_count( p_results ); i++ )
378     {
379         ml_result_t* p_res = ( ml_result_t* ) vlc_array_item_at_index( p_results, i );
380         input_item_t* p_item;
381         if( p_res )
382         {
383             p_item = ml_CreateInputItem( p_ml, p_res->value.i );
384             playlist_AddInput( p_pl, p_item, PLAYLIST_APPEND,
385                            PLAYLIST_END, true, true );
386         }
387     }
388     playlist_Unlock( p_pl );
389     ml_DestroyResultArray( p_results );
390     vlc_array_destroy( p_results );
391 }
392
393 /**
394  * @brief Returns a person list of given type
395  * @param p_ml The ML object
396  * @param p_media The Media object
397  * @param i_type The person type
398  * @note This function is thread safe
399  */
400 ml_person_t*  ml_GetPersonsFromMedia( media_library_t* p_ml,
401                                                     ml_media_t* p_media,
402                                                     const char *psz_role )
403 {
404     VLC_UNUSED( p_ml );
405     assert( p_media != NULL );
406     ml_person_t* p_return = NULL;
407     ml_LockMedia( p_media );
408     ml_person_t* p_person = p_media->p_people;
409     while( p_person )
410     {
411         if( strcmp( p_person->psz_role, psz_role ) == 0 )
412         {
413             int i_ret = ml_CreateAppendPerson( &p_return, p_person );
414             if( i_ret != VLC_SUCCESS )
415             {
416                 ml_UnlockMedia( p_media );
417                 ml_FreePeople( p_return );
418                 return NULL;
419             }
420         }
421         p_person = p_person->p_next;
422     }
423     ml_UnlockMedia( p_media );
424     //TODO: Fill up empty names + clean up list
425     return p_return;
426 }
427
428 /**
429  * @brief Delete a certain type of people from a media
430  * @param p_media Media to delete from
431  * @param i_type Type of person to delete
432  * @note This function is threadsafe
433  */
434 void ml_DeletePersonTypeFromMedia( ml_media_t* p_media, const char *psz_role )
435 {
436     assert( p_media );
437     ml_LockMedia( p_media );
438     ml_person_t* p_prev = NULL;
439     ml_person_t* p_person = p_media->p_people;
440
441     while( p_person )
442     {
443         if( strcmp( p_person->psz_role, psz_role ) == 0 )
444         {
445             if( p_prev == NULL )
446             {
447                 p_media->p_people = p_person->p_next;
448                 p_person->p_next = NULL;
449                 ml_FreePeople( p_person );
450                 p_person = p_media->p_people;
451             }
452             else
453             {
454                 p_prev->p_next = p_person->p_next;
455                 p_person->p_next = NULL;
456                 ml_FreePeople( p_person );
457                 p_person = p_prev->p_next;
458             }
459         }
460         else
461         {
462             p_prev = p_person;
463             p_person = p_person->p_next;
464         }
465     }
466     ml_UnlockMedia( p_media );
467 }
468
469 #endif /* MEDIA_LIBRARY */