]> git.sesse.net Git - vlc/blob - src/misc/media_library.c
ML: Remove leading underscores from core functions
[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 #undef ml_Hold
105 /**
106  * @brief Acquire a reference to the media library singleton
107  * @param p_this Object that holds the reference
108  * @return media_library_t The ml object. NULL if not compiled with
109  * media library or if unable to load
110  */
111 media_library_t* ml_Hold( vlc_object_t* p_this )
112 {
113     media_library_t* p_ml = NULL;
114     p_ml = libvlc_priv (p_this->p_libvlc)->p_ml;
115     assert( VLC_OBJECT( p_ml ) != p_this );
116     if( p_ml == NULL &&
117             var_GetBool( p_this->p_libvlc, "load-media-library-on-startup" ) == false )
118     {
119         libvlc_priv (p_this->p_libvlc)->p_ml
120             = ml_Create( VLC_OBJECT( p_this->p_libvlc ), NULL );
121         p_ml = libvlc_priv (p_this->p_libvlc)->p_ml;
122     }
123     if( p_ml )
124         vlc_object_hold( p_ml );
125     return p_ml;
126 }
127
128 #undef ml_Release
129 /**
130  * @brief Release a reference to the media library singleton
131  * @param p_this Object that holds the reference
132  */
133 void ml_Release( vlc_object_t* p_this )
134 {
135     media_library_t* p_ml;
136     p_ml = libvlc_priv (p_this->p_libvlc)->p_ml;
137     if( p_ml == NULL )
138     {
139         msg_Warn( p_this->p_libvlc , "Spurious release ML called");
140         return;
141     }
142     assert( VLC_OBJECT( p_ml ) != p_this );
143     vlc_object_release( p_ml );
144 }
145
146 /**
147  * @brief Destructor for ml_media_t
148  */
149 static void media_Destroy( ml_gc_object_t *p_gc )
150 {
151     ml_media_t* p_media = ml_priv( p_gc, ml_media_t );
152     vlc_mutex_destroy( &p_media->lock );
153     ml_FreeMediaContent( p_media );
154     free( p_media );
155 }
156
157 /**
158  * @brief Object constructor for ml_media_t
159  * @param p_ml The media library object
160  * @param id If 0, this item isn't in database. If non zero, it is and
161  * it will be a singleton
162  * @param select Type of object
163  * @param reload Whether to reload from database
164  */
165 ml_media_t* media_New( media_library_t* p_ml, int id,
166         ml_select_e select, bool reload )
167 {
168     if( id == 0 )
169     {
170         ml_media_t* p_media = NULL;
171         p_media = ( ml_media_t* )calloc( 1, sizeof( ml_media_t ) );
172         ml_gc_init( &p_media->ml_gc_data, media_Destroy );
173         vlc_mutex_init( &p_media->lock );
174         return p_media;
175     }
176     else
177         return p_ml->functions.pf_GetMedia( p_ml, id, select, reload );
178 }
179
180 #undef ml_UpdateSimple
181 /**
182  * @brief Update a given table
183  * @param p_media_library The media library object
184  * @param selected_type The table to update
185  * @param psz_lvalue The role of the person if selected_type = ML_PEOPLE
186  * @param id The id of the row to update
187  * @param ... The update data. [SelectType [RoleType] Value] ... ML_END
188  */
189 int ml_UpdateSimple( media_library_t *p_media_library,
190                                      ml_select_e selected_type,
191                                      const char* psz_lvalue,
192                                      int id, ... )
193 {
194     ml_element_t *update = NULL;
195     vlc_array_t *array = vlc_array_new();
196     int i_ret = VLC_SUCCESS;
197
198     va_list args;
199     va_start( args, id );
200
201     ml_select_e sel;
202     do {
203         update = ( ml_element_t* ) calloc( 1, sizeof( ml_element_t ) );
204         sel = ( ml_select_e ) va_arg( args, int );
205         update->criteria = sel;
206         if( sel == ML_PEOPLE )
207         {
208             update->lvalue.str = va_arg( args, char* );
209             update->value.str = va_arg( args, char* );
210             vlc_array_append( array, update );
211         }
212         else if( sel == ML_PEOPLE_ID )
213         {
214            update->lvalue.str = va_arg( args, char* );
215            update->value.i = va_arg( args, int );
216            vlc_array_append( array, update );
217         }
218         else if( sel == ML_PEOPLE_ROLE )
219         {
220 #ifndef NDEBUG
221             msg_Dbg( p_media_library,
222                      "this argument is invalid for Update: %d",
223                      (int)sel );
224 #endif
225         }
226         else
227         {
228             switch( ml_AttributeIsString( sel ) )
229             {
230                 case -1:
231                     if( sel != ML_END )
232                     {
233 #ifndef NDEBUG
234                         msg_Dbg( p_media_library,
235                                  "this argument is invalid for Update: %d",
236                                  (int)sel );
237 #endif
238                         i_ret = VLC_EGENERIC;
239                     }
240                     else if( sel == ML_END )
241                         vlc_array_append( array, update );
242                     break;
243                 case 0:
244                     update->value.str = va_arg( args, char* );
245                     vlc_array_append( array, update );
246                     break;
247                 case 1:
248                     update->value.i = va_arg( args, int );
249                     vlc_array_append( array, update );
250                     break;
251             }
252         }
253     } while( sel != ML_END );
254
255     va_end( args );
256
257     ml_ftree_t* p_where = NULL;
258     ml_ftree_t* find = ( ml_ftree_t* ) calloc( 1, sizeof( ml_ftree_t ) );
259     find->criteria = ML_ID;
260     find->value.i = id ;
261     find->comp = ML_COMP_EQUAL;
262     p_where = ml_FtreeFastAnd( p_where, find );
263
264     /* Let's update the database ! */
265     if( i_ret == VLC_SUCCESS )
266         i_ret = ml_Update( p_media_library, selected_type, psz_lvalue,
267                             p_where, array );
268
269     /* Destroying array */
270     for( int i = 0; i < vlc_array_count( array ); i++ )
271     {
272         free( vlc_array_item_at_index( array, i ) );
273     }
274     vlc_array_destroy( array );
275     ml_FreeFindTree( p_where );
276
277     return i_ret;
278 }
279
280 /**
281  * @brief Connect up a find tree
282  * @param op operator to connect with
283  * If op = ML_OP_NONE, then you are connecting to a tree consisting of
284  * only SPECIAL nodes.
285  * If op = ML_OP_NOT, then right MUST be NULL
286  * op must not be ML_OP_SPECIAL, @see ml_FtreeSpec
287  * @param left part of the tree
288  * @param right part of the tree
289  * @return Pointer to new tree
290  * @note Use the helpers!
291  */
292 ml_ftree_t* ml_OpConnectChilds( ml_op_e op, ml_ftree_t* left,
293         ml_ftree_t* right )
294 {
295     /* Use this Op for fresh trees (with only special nodes/none at all!) */
296     if( op == ML_OP_NONE )
297     {
298         assert( ml_FtreeHasOp( left ) == 0 );
299         if( left == NULL )
300             return right;
301         /* Percolate down tree only for special nodes */
302         assert( left->op == ML_OP_SPECIAL );
303         if( left->left == NULL )
304         {
305             left->left = right;
306             return left;
307         }
308         else
309         {
310             return ml_OpConnectChilds( ML_OP_NONE, left->left, right );
311         }
312     }
313     else if( op == ML_OP_NOT )
314     {
315         assert( right == NULL && left != NULL );
316         assert( ml_FtreeHasOp( left ) > 0 );
317     }
318     else if( op == ML_OP_SPECIAL )
319     {
320         assert( 0 );
321     }
322     else
323     {
324         assert( right != NULL && left != NULL );
325         assert( ml_FtreeHasOp( left ) > 0 );
326         assert( ml_FtreeHasOp( right ) > 0 );
327     }
328     ml_ftree_t* p_parent = (ml_ftree_t *) calloc( 1, sizeof( ml_ftree_t ) );
329     p_parent->op = op;
330     p_parent->left = left;
331     p_parent->right = right;
332     return p_parent;
333 }
334
335 #undef ml_FtreeSpec
336 /**
337  * @brief Attaches a special node to a tree
338  * @param tree Tree to attach special node to
339  * @param crit Criteria may be SORT_ASC, SORT_DESC, LIMIT or DISTINCT
340  * @param limit Limit used if LIMIT criteria used
341  * @param Sort string used if SORT criteria is used
342  * @return Pointer to new tree
343  * @note Use the helpers
344  */
345 ml_ftree_t* ml_FtreeSpec( ml_ftree_t* tree,
346                                           ml_select_e crit,
347                                           int limit,
348                                           char* sort )
349 {
350     assert( crit == ML_SORT_ASC || crit == ML_LIMIT || crit == ML_SORT_DESC ||
351             crit == ML_DISTINCT );
352     ml_ftree_t* right = ( ml_ftree_t* ) calloc( 1, sizeof( ml_ftree_t ) );
353     right->criteria = crit;
354     if( crit == ML_LIMIT )
355         right->value.i = limit;
356     else if( crit == ML_SORT_ASC || crit == ML_SORT_DESC )
357         right->value.str = strdup( sort );
358     right->op = ML_OP_NONE;
359     ml_ftree_t* p_parent = ( ml_ftree_t* ) calloc( 1, sizeof( ml_ftree_t ) );
360     p_parent->right = right;
361     p_parent->op = ML_OP_SPECIAL;
362     p_parent->left = tree;
363     return p_parent;
364 }
365
366
367 /**
368  * @brief Creates and adds the playlist based on a given find tree
369  * @param p_ml Media library object
370  * @param p_tree Find tree to create SELECT
371  */
372 void ml_PlaySmartPlaylistBasedOn( media_library_t* p_ml,
373                                                 ml_ftree_t* p_tree )
374 {
375     assert( p_tree );
376     vlc_array_t* p_results = vlc_array_new();
377     ml_FindAdv( p_ml, p_results, ML_ID, NULL, p_tree );
378     playlist_t* p_pl = pl_Get( p_ml );
379     playlist_Lock( p_pl );
380     playlist_Clear( p_pl, true );
381     for( int i = 0; i < vlc_array_count( p_results ); i++ )
382     {
383         ml_result_t* p_res = ( ml_result_t* ) vlc_array_item_at_index( p_results, i );
384         input_item_t* p_item;
385         if( p_res )
386         {
387             p_item = ml_CreateInputItem( p_ml, p_res->value.i );
388             playlist_AddInput( p_pl, p_item, PLAYLIST_APPEND,
389                            PLAYLIST_END, true, true );
390         }
391     }
392     playlist_Unlock( p_pl );
393     ml_DestroyResultArray( p_results );
394     vlc_array_destroy( p_results );
395 }
396
397 /**
398  * @brief Returns a person list of given type
399  * @param p_ml The ML object
400  * @param p_media The Media object
401  * @param i_type The person type
402  * @note This function is thread safe
403  */
404 ml_person_t*  ml_GetPersonsFromMedia( media_library_t* p_ml,
405                                                     ml_media_t* p_media,
406                                                     const char *psz_role )
407 {
408     VLC_UNUSED( p_ml );
409     assert( p_media != NULL );
410     ml_person_t* p_return = NULL;
411     ml_LockMedia( p_media );
412     ml_person_t* p_person = p_media->p_people;
413     while( p_person )
414     {
415         if( strcmp( p_person->psz_role, psz_role ) == 0 )
416         {
417             int i_ret = ml_CreateAppendPerson( &p_return, p_person );
418             if( i_ret != VLC_SUCCESS )
419             {
420                 ml_UnlockMedia( p_media );
421                 ml_FreePeople( p_return );
422                 return NULL;
423             }
424         }
425         p_person = p_person->p_next;
426     }
427     ml_UnlockMedia( p_media );
428     //TODO: Fill up empty names + clean up list
429     return p_return;
430 }
431
432 /**
433  * @brief Delete a certain type of people from a media
434  * @param p_media Media to delete from
435  * @param i_type Type of person to delete
436  * @note This function is threadsafe
437  */
438 void ml_DeletePersonTypeFromMedia( ml_media_t* p_media, const char *psz_role )
439 {
440     assert( p_media );
441     ml_LockMedia( p_media );
442     ml_person_t* p_prev = NULL;
443     ml_person_t* p_person = p_media->p_people;
444
445     while( p_person )
446     {
447         if( strcmp( p_person->psz_role, psz_role ) == 0 )
448         {
449             if( p_prev == NULL )
450             {
451                 p_media->p_people = p_person->p_next;
452                 p_person->p_next = NULL;
453                 ml_FreePeople( p_person );
454                 p_person = p_media->p_people;
455             }
456             else
457             {
458                 p_prev->p_next = p_person->p_next;
459                 p_person->p_next = NULL;
460                 ml_FreePeople( p_person );
461                 p_person = p_prev->p_next;
462             }
463         }
464         else
465         {
466             p_prev = p_person;
467             p_person = p_person->p_next;
468         }
469     }
470     ml_UnlockMedia( p_media );
471 }
472
473 #endif /* MEDIA_LIBRARY */