1 /*****************************************************************************
2 * sql_media_library.h : Media Library Interface
3 *****************************************************************************
4 * Copyright (C) 2008-2010 the VideoLAN team and AUTHORS
7 * Authors: Antoine Lejeune <phytos@videolan.org>
8 * Jean-Philippe André <jpeg@videolan.org>
9 * Rémi Duraffort <ivoire@videolan.org>
10 * Adrien Maglo <magsoft@videolan.org>
11 * Srikanth Raju <srikiraju at gmail dot com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #ifndef SQL_MEDIA_LIBRARY_H
29 #define SQL_MEDIA_LIBRARY_H
36 #include <vlc_common.h>
40 #include <vlc_media_library.h>
42 #include <vlc_playlist.h>
43 #include <vlc_input.h>
44 #include <vlc_arrays.h>
45 #include <vlc_charset.h>
46 #include <vlc_plugin.h>
47 #include <vlc_interface.h>
48 #include <vlc_modules.h>
54 #include "item_list.h"
56 #ifdef HAVE_SYS_STAT_H
57 # include <sys/stat.h>
61 /*****************************************************************************
63 *****************************************************************************/
64 #define THREAD_SLEEP_DELAY 2 /* Time between two calls to item_list_loop */
65 #define MONITORING_DELAY 30 /* Media library updates interval */
66 #define ITEM_LOOP_UPDATE 1 /* An item is updated after 1 loop */
67 #define ITEM_LOOP_MAX_AGE 10 /* An item is deleted after 10 loops */
68 #define ML_DBVERSION 1 /* The current version of the database */
69 #define ML_MEDIAPOOL_HASH_LENGTH 100 /* The length of the media pool hash */
71 /*****************************************************************************
72 * Structures and types definitions
73 *****************************************************************************/
74 typedef struct monitoring_thread_t monitoring_thread_t;
75 typedef struct ml_poolobject_t ml_poolobject_t;
77 struct ml_poolobject_t
80 ml_poolobject_t* p_next;
83 struct media_library_sys_t
85 /* Lock on the ML object */
91 /* Monitoring thread */
92 monitoring_thread_t *p_mon;
95 watch_thread_t *p_watch;
97 /* Holds all medias */
98 DECL_ARRAY( ml_media_t* ) mediapool;
99 ml_poolobject_t* p_mediapool[ ML_MEDIAPOOL_HASH_LENGTH ];
100 vlc_mutex_t pool_mutex;
102 /* Info on update/collection rebuilding */
107 /* Directory Monitoring thread */
108 struct monitoring_thread_t
115 media_library_t *p_ml;
118 /* Media status Watching thread */
119 struct watch_thread_t
121 media_library_t *p_ml;
126 /* Input items watched */
127 struct item_list_t* p_hlist[ ML_ITEMLIST_HASH_LENGTH ];
128 vlc_mutex_t list_mutex;
130 /* List of items to check */
131 input_item_t** item_append_queue;
132 vlc_mutex_t item_append_queue_lock;
133 int item_append_queue_count;
138 /*****************************************************************************
140 *****************************************************************************/
141 /* General functions */
142 int CreateEmptyDatabase( media_library_t *p_ml );
143 int InitDatabase( media_library_t *p_ml );
146 int Control( media_library_t *p_ml,
151 int AddMedia( media_library_t *p_ml,
152 ml_media_t *p_media );
153 int AddAlbum( media_library_t *p_ml, const char *psz_title,
154 const char *psz_cover, const int i_album_artist );
155 int AddPeople( media_library_t *p_ml,
156 const char *psz_name,
157 const char *psz_role );
158 int AddPlaylistItem( media_library_t *p_ml,
159 playlist_item_t *p_playlist_item );
160 int AddInputItem( media_library_t *p_ml,
161 input_item_t *p_input );
163 /* Create and Copy functions */
164 ml_media_t* GetMedia( media_library_t* p_ml, int id,
165 ml_select_e select, bool reload );
166 input_item_t* GetInputItemFromMedia( media_library_t *p_ml,
168 void CopyInputItemToMedia( ml_media_t *p_media,
169 input_item_t *p_item );
170 void CopyMediaToInputItem( input_item_t *p_item,
171 ml_media_t *p_media );
174 int GetDatabaseVersion( media_library_t *p_ml );
175 int GetMediaIdOfInputItem( media_library_t *p_ml,
176 input_item_t *p_item );
177 int GetMediaIdOfURI( media_library_t *p_ml,
178 const char *psz_uri );
180 /* Search in the database */
181 int BuildSelectVa( media_library_t *p_ml,
183 ml_result_type_e *p_result_type,
185 int BuildSelect( media_library_t *p_ml,
187 ml_result_type_e *p_result_type,
188 const char *psz_selected_type_lvalue,
189 ml_select_e selected_type,
191 int Find( media_library_t *p_ml,
192 vlc_array_t *results,
194 int FindVa( media_library_t *p_ml,
195 vlc_array_t *results,
197 int FindAdv( media_library_t *p_ml,
198 vlc_array_t *results,
199 ml_select_e selected_type,
200 const char* psz_lvalue,
203 /* Update the database */
204 int Update( media_library_t *p_ml,
205 ml_select_e selected_type,
206 const char* psz_lvalue,
208 vlc_array_t *changes );
209 int BuildUpdate( media_library_t *p_ml,
211 char **ppsz_id_query,
212 const char *psz_lvalue,
213 ml_select_e selected_type,
215 vlc_array_t *changes );
216 int UpdateMedia( media_library_t *p_ml,
217 ml_media_t *p_media );
218 int SetArtCover( media_library_t *p_ml,
220 const char *psz_cover );
222 /* Delete medias in the database */
223 int Delete( media_library_t *p_ml, vlc_array_t *p_array );
225 /* Do some query on the database */
226 int QuerySimple( media_library_t *p_ml,
227 const char *psz_fmt, ... );
228 int Query( media_library_t *p_ml,
234 int QueryVa( media_library_t *p_ml,
240 int QuerySimpleVa( media_library_t *p_ml,
244 /* Convert SQL results to ML results */
245 int StringToResult( ml_result_t *res,
248 ml_result_type_e result_type );
249 int SQLToMediaArray( media_library_t *p_ml,
250 vlc_array_t *p_result_array,
254 int SQLToResultArray( media_library_t *p_ml,
255 vlc_array_t *p_result_array,
259 ml_result_type_e result_type );
261 /* Database locking functions */
264 * @brief Begin a transaction
265 * @param p_ml The Media Library object
266 * @return VLC_SUCCESS and VLC_EGENERIC
267 * @note This creates a SHARED lock in SQLITE. All queries made between
268 * a Begin and Commit/Rollback will be transactional.
270 static inline int Begin( media_library_t* p_ml )
272 return sql_BeginTransaction( p_ml->p_sys->p_sql );
276 * @brief Commits the transaction
277 * @param p_ml The Media Library object
279 static inline void Commit( media_library_t* p_ml )
281 sql_CommitTransaction( p_ml->p_sys->p_sql );
285 * @brief Rollback the transaction
286 * @param p_ml The Media Library Object
288 static inline void Rollback( media_library_t* p_ml )
290 sql_RollbackTransaction( p_ml->p_sys->p_sql );
293 /****************************************************************************
294 * Scanning/monitoring functions
295 *****************************************************************************/
296 void *RunMonitoringThread( void *p_mon );
297 int AddDirToMonitor( media_library_t *p_ml,
298 const char *psz_dir );
299 int ListMonitoredDirs( media_library_t *p_ml,
300 vlc_array_t *p_array );
301 int RemoveDirToMonitor( media_library_t *p_ml,
302 const char *psz_dir );
305 /*****************************************************************************
306 * Media pool functions
307 *****************************************************************************/
308 ml_media_t* pool_GetMedia( media_library_t* p_ml, int media_id );
309 int pool_InsertMedia( media_library_t* p_ml, ml_media_t* media, bool locked );
310 void pool_GC( media_library_t* p_ml );
312 /*****************************************************************************
313 * Items watching system
314 *****************************************************************************/
315 /* Watching thread */
316 #define watch_add_Item( a, b, c ) __watch_add_Item( a, b, c, false )
318 int watch_Init( media_library_t *p_ml );
319 void watch_Close( media_library_t *p_ml );
320 int __watch_add_Item( media_library_t *p_ml, input_item_t *p_item,
321 ml_media_t* p_media, bool locked );
323 #define watch_del_Item( a, b ) __watch_del_Item( a, b, false )
324 int __watch_del_Item( media_library_t *p_ml, input_item_t *p_item, bool locked );
325 int watch_del_MediaById( media_library_t* p_ml, int i_media_id );
326 input_item_t* watch_get_itemOfMediaId( media_library_t *p_ml, int i_media_id );
327 ml_media_t* watch_get_mediaOfMediaId( media_library_t* p_ml, int i_media_id );
328 int watch_get_mediaIdOfItem( media_library_t *p_ml, input_item_t *p_item );
329 void watch_Force_Update( media_library_t* p_ml );
331 /*****************************************************************************
332 * Free result of ml_Query
333 *****************************************************************************/
334 static inline void FreeSQLResult( media_library_t *p_ml, char **ppsz_result )
338 sql_Free( p_ml->p_sys->p_sql, ppsz_result );
342 #endif /* SQL_MEDIA_LIBRARY_H */