1 /*****************************************************************************
2 * media_library.c: SQL-based media library: ML creators and destructors
3 *****************************************************************************
4 * Copyright (C) 2009-2010 the VideoLAN team and AUTHORS
7 * Authors: Srikanth Raju <srikiraju at gmail dot com>
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.
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.
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 *****************************************************************************/
28 #if defined(MEDIA_LIBRARY)
31 #include <vlc_media_library.h>
32 #include <vlc_modules.h>
33 #include "../libvlc.h"
36 * @brief Destroy the medialibrary object
37 * @param Parent object that holds the media library object
39 void ml_Destroy( vlc_object_t * p_this )
41 media_library_t* p_ml = ( media_library_t* )p_this;
42 module_unneed( p_ml, p_ml->p_module );
47 * Atomically set the reference count to 1.
48 * @param p_gc reference counted object
49 * @param pf_destruct destruction calback
52 static void *ml_gc_init (ml_gc_object_t *p_gc, void (*pf_destruct) (ml_gc_object_t *))
54 /* There is no point in using the GC if there is no destructor... */
56 p_gc->pf_destructor = pf_destruct;
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);
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
76 media_library_t *ml_Create( vlc_object_t *p_this, char *psz_name )
78 media_library_t *p_ml;
80 p_ml = ( media_library_t * ) vlc_custom_create(
81 p_this, sizeof( media_library_t ),
85 msg_Err( p_this, "unable to create media library object" );
89 p_ml->p_module = module_need( p_ml, "media-library", psz_name, false );
92 vlc_object_release( p_ml );
93 msg_Err( p_this, "Media Library provider not found" );
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
107 media_library_t* ml_Get( vlc_object_t* p_this )
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 );
114 !var_GetBool( p_this->p_libvlc, "load-media-library-on-startup" ) )
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;
120 vlc_mutex_unlock( &( libvlc_priv( p_this->p_libvlc )->ml_lock ) );
125 * @brief Destructor for ml_media_t
127 static void media_Destroy( ml_gc_object_t *p_gc )
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 );
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
143 ml_media_t* media_New( media_library_t* p_ml, int id,
144 ml_select_e select, bool reload )
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 );
155 return p_ml->functions.pf_GetMedia( p_ml, id, select, reload );
158 #undef ml_UpdateSimple
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
167 int ml_UpdateSimple( media_library_t *p_media_library,
168 ml_select_e selected_type,
169 const char* psz_lvalue,
172 ml_element_t *update;
173 vlc_array_t *array = vlc_array_new();
174 int i_ret = VLC_SUCCESS;
177 va_start( args, id );
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 )
186 update->lvalue.str = va_arg( args, char* );
187 update->value.str = va_arg( args, char* );
188 vlc_array_append( array, update );
190 else if( sel == ML_PEOPLE_ID )
192 update->lvalue.str = va_arg( args, char* );
193 update->value.i = va_arg( args, int );
194 vlc_array_append( array, update );
196 else if( sel == ML_PEOPLE_ROLE )
199 msg_Dbg( p_media_library,
200 "this argument is invalid for Update: %d",
206 switch( ml_AttributeIsString( sel ) )
212 msg_Dbg( p_media_library,
213 "this argument is invalid for Update: %d",
216 i_ret = VLC_EGENERIC;
218 else if( sel == ML_END )
219 vlc_array_append( array, update );
222 update->value.str = va_arg( args, char* );
223 vlc_array_append( array, update );
226 update->value.i = va_arg( args, int );
227 vlc_array_append( array, update );
231 } while( sel != ML_END );
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;
239 find->comp = ML_COMP_EQUAL;
240 p_where = ml_FtreeFastAnd( p_where, find );
242 /* Let's update the database ! */
243 if( i_ret == VLC_SUCCESS )
244 i_ret = ml_Update( p_media_library, selected_type, psz_lvalue,
247 /* Destroying array */
248 for( int i = 0; i < vlc_array_count( array ); i++ )
250 free( vlc_array_item_at_index( array, i ) );
252 vlc_array_destroy( array );
253 ml_FreeFindTree( p_where );
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!
270 ml_ftree_t* ml_OpConnectChilds( ml_op_e op, ml_ftree_t* left,
273 /* Use this Op for fresh trees (with only special nodes/none at all!) */
274 if( op == ML_OP_NONE )
276 assert( ml_FtreeHasOp( left ) == 0 );
279 /* Percolate down tree only for special nodes */
280 assert( left->op == ML_OP_SPECIAL );
281 if( left->left == NULL )
288 return ml_OpConnectChilds( ML_OP_NONE, left->left, right );
291 else if( op == ML_OP_NOT )
293 assert( right == NULL && left != NULL );
294 assert( ml_FtreeHasOp( left ) > 0 );
296 else if( op == ML_OP_SPECIAL )
302 assert( right != NULL && left != NULL );
303 assert( ml_FtreeHasOp( left ) > 0 );
304 assert( ml_FtreeHasOp( right ) > 0 );
306 ml_ftree_t* p_parent = (ml_ftree_t *) calloc( 1, sizeof( ml_ftree_t ) );
308 p_parent->left = left;
309 p_parent->right = right;
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
323 ml_ftree_t* ml_FtreeSpec( ml_ftree_t* tree,
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;
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
350 void ml_PlaySmartPlaylistBasedOn( media_library_t* p_ml,
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++ )
361 ml_result_t* p_res = ( ml_result_t* ) vlc_array_item_at_index( p_results, i );
362 input_item_t* p_item;
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 );
370 playlist_Unlock( p_pl );
371 ml_DestroyResultArray( p_results );
372 vlc_array_destroy( p_results );
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
382 ml_person_t* ml_GetPersonsFromMedia( media_library_t* p_ml,
384 const char *psz_role )
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;
393 if( strcmp( p_person->psz_role, psz_role ) == 0 )
395 int i_ret = ml_CreateAppendPerson( &p_return, p_person );
396 if( i_ret != VLC_SUCCESS )
398 ml_UnlockMedia( p_media );
399 ml_FreePeople( p_return );
403 p_person = p_person->p_next;
405 ml_UnlockMedia( p_media );
406 //TODO: Fill up empty names + clean up list
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
416 void ml_DeletePersonTypeFromMedia( ml_media_t* p_media, const char *psz_role )
419 ml_LockMedia( p_media );
420 ml_person_t* p_prev = NULL;
421 ml_person_t* p_person = p_media->p_people;
425 if( strcmp( p_person->psz_role, psz_role ) == 0 )
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;
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;
445 p_person = p_person->p_next;
448 ml_UnlockMedia( p_media );
451 #endif /* MEDIA_LIBRARY */