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