]> git.sesse.net Git - vlc/blob - modules/media_library/sql_media_library.h
Removed now useless include in goom.
[vlc] / modules / media_library / sql_media_library.h
1 /*****************************************************************************
2  * sql_media_library.h : Media Library Interface
3  *****************************************************************************
4  * Copyright (C) 2008-2010 the VideoLAN team and AUTHORS
5  * $Id$
6  *
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>
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 #ifndef SQL_MEDIA_LIBRARY_H
29 #define SQL_MEDIA_LIBRARY_H
30
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc/vlc.h>
38
39 #include <vlc_sql.h>
40 #include <vlc_media_library.h>
41
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>
49
50 #include <stdarg.h>
51 #include <assert.h>
52 #include <errno.h>
53
54 #include "item_list.h"
55
56 #ifdef HAVE_SYS_STAT_H
57 #   include <sys/stat.h>
58 #endif
59
60
61 /*****************************************************************************
62  * Static parameters
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 */
70
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;
76
77 struct ml_poolobject_t
78 {
79     ml_media_t* p_media;
80     ml_poolobject_t* p_next;
81 };
82
83 struct media_library_sys_t
84 {
85     /* Lock on the ML object */
86     vlc_mutex_t lock;
87
88     /* SQL object */
89     sql_t *p_sql;
90
91     /* Monitoring thread */
92     monitoring_thread_t *p_mon;
93
94     /* Watch thread */
95     watch_thread_t *p_watch;
96
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;
101
102     /* Info on update/collection rebuilding */
103     bool b_updating;
104     bool b_rebuilding;
105 };
106
107 /* Directory Monitoring thread */
108 struct monitoring_thread_t
109 {
110     VLC_COMMON_MEMBERS;
111
112     vlc_cond_t wait;
113     vlc_mutex_t lock;
114     vlc_thread_t thread;
115     media_library_t *p_ml;
116 };
117
118 /* Media status Watching thread */
119 struct watch_thread_t
120 {
121     media_library_t *p_ml;
122     vlc_thread_t thread;
123     vlc_cond_t cond;
124     vlc_mutex_t lock;
125
126     /* Input items watched */
127     struct item_list_t* p_hlist[ ML_ITEMLIST_HASH_LENGTH ];
128     vlc_mutex_t list_mutex;
129
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;
134 };
135
136
137
138 /*****************************************************************************
139  * Function headers
140  *****************************************************************************/
141 /* General functions */
142 int CreateEmptyDatabase( media_library_t *p_ml );
143 int InitDatabase( media_library_t *p_ml );
144
145 /* Module Control */
146 int Control( media_library_t *p_ml,
147              int i_query,
148              va_list args );
149
150 /* Add functions */
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 );
162
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,
167                                      int i_media );
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 );
172
173 /* Get functions */
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 );
179
180 /* Search in the database */
181 int BuildSelectVa( media_library_t *p_ml,
182                    char **ppsz_query,
183                    ml_result_type_e *p_result_type,
184                    va_list criterias );
185 int BuildSelect( media_library_t *p_ml,
186                  char **ppsz_query,
187                  ml_result_type_e *p_result_type,
188                  const char *psz_selected_type_lvalue,
189                  ml_select_e selected_type,
190                  ml_ftree_t *tree );
191 int Find( media_library_t *p_ml,
192           vlc_array_t *results,
193           ... );
194 int FindVa( media_library_t *p_ml,
195             vlc_array_t *results,
196             va_list criterias );
197 int FindAdv( media_library_t *p_ml,
198              vlc_array_t *results,
199              ml_select_e selected_type,
200              const char* psz_lvalue,
201              ml_ftree_t *tree );
202
203 /* Update the database */
204 int Update( media_library_t *p_ml,
205             ml_select_e selected_type,
206             const char* psz_lvalue,
207             ml_ftree_t *where,
208             vlc_array_t *changes );
209 int BuildUpdate( media_library_t *p_ml,
210                  char **ppsz_query,
211                  char **ppsz_id_query,
212                  const char *psz_lvalue,
213                  ml_select_e selected_type,
214                  ml_ftree_t* where,
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,
219                  int i_album_id,
220                  const char *psz_cover );
221
222 /* Delete medias in the database */
223 int Delete( media_library_t *p_ml, vlc_array_t *p_array );
224
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,
229            char ***ppp_res,
230            int *pi_rows,
231            int *pi_cols,
232            const char *psz_fmt,
233            ... );
234 int QueryVa( media_library_t *p_ml,
235              char ***ppp_res,
236              int *pi_rows,
237              int *pi_cols,
238              const char *psz_fmt,
239              va_list args );
240 int QuerySimpleVa( media_library_t *p_ml,
241                    const char *psz_fmt,
242                    va_list argp );
243
244 /* Convert SQL results to ML results */
245 int StringToResult( ml_result_t *res,
246                     const char *psz,
247                     const char *psz_id,
248                     ml_result_type_e result_type );
249 int SQLToMediaArray( media_library_t *p_ml,
250                      vlc_array_t *p_result_array,
251                      char **pp_results,
252                      int i_rows,
253                      int i_cols );
254 int SQLToResultArray( media_library_t *p_ml,
255                       vlc_array_t *p_result_array,
256                       char **pp_results,
257                       int i_rows,
258                       int i_cols,
259                       ml_result_type_e result_type );
260
261 /* Database locking functions */
262
263 /**
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.
269  */
270 static inline int Begin( media_library_t* p_ml )
271 {
272     return sql_BeginTransaction( p_ml->p_sys->p_sql );
273 }
274
275 /**
276  * @brief Commits the transaction
277  * @param p_ml The Media Library object
278  */
279 static inline void Commit( media_library_t* p_ml )
280 {
281     sql_CommitTransaction( p_ml->p_sys->p_sql );
282 }
283
284 /**
285  * @brief Rollback the transaction
286  * @param p_ml The Media Library Object
287  */
288 static inline void Rollback( media_library_t* p_ml )
289 {
290     sql_RollbackTransaction( p_ml->p_sys->p_sql );
291 }
292
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 );
303
304
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 );
311
312 /*****************************************************************************
313  * Items watching system
314  *****************************************************************************/
315 /* Watching thread */
316 #define watch_add_Item( a, b, c ) __watch_add_Item( a, b, c, false )
317
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 );
322
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 );
330
331 /*****************************************************************************
332  * Free result of ml_Query
333  *****************************************************************************/
334 static inline void FreeSQLResult( media_library_t *p_ml, char **ppsz_result )
335 {
336     if( ppsz_result )
337     {
338         sql_Free( p_ml->p_sys->p_sql, ppsz_result );
339     }
340 }
341
342 #endif /* SQL_MEDIA_LIBRARY_H */