]> git.sesse.net Git - vlc/blob - modules/access/gnomevfs.c
* Support URIs with non ascii characters
[vlc] / modules / access / gnomevfs.c
1 /*****************************************************************************
2  * gnomevfs.c: GnomeVFS input
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben -AT- videolan -DOT- org>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include <libgnomevfs/gnome-vfs.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "charset.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 #define CACHING_TEXT N_("Caching value in ms")
44 #define CACHING_LONGTEXT N_( \
45     "Allows you to modify the default caching value for GnomeVFS streams."\
46     "This value should be set in millisecond units." )
47
48 vlc_module_begin();
49     set_description( _("GnomeVFS filesystem file input") );
50     set_shortname( _("GnomeVFS") );
51     set_category( CAT_INPUT );
52     set_subcategory( SUBCAT_INPUT_ACCESS );
53     add_integer( "gnomevfs-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
54     set_capability( "access2", 10 );
55     add_shortcut( "gnomevfs" );
56     set_callbacks( Open, Close );
57 vlc_module_end();
58
59
60 /*****************************************************************************
61  * Exported prototypes
62  *****************************************************************************/
63 static int  Seek( access_t *, int64_t );
64 static int  Read( access_t *, uint8_t *, int );
65 static int  Control( access_t *, int, va_list );
66
67 struct access_sys_t
68 {
69     unsigned int i_nb_reads;
70     char *psz_name;
71
72     GnomeVFSHandle *p_handle;
73     GnomeVFSFileInfo *p_file_info;
74
75     vlc_bool_t b_local;
76     vlc_bool_t b_seekable;
77     vlc_bool_t b_pace_control;
78 };
79
80 /*****************************************************************************
81  * Open: open the file
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     access_t     *p_access = (access_t*)p_this;
86     access_sys_t *p_sys;
87     char *psz_name;
88     char *psz, *psz_uri;
89     GnomeVFSURI         *p_uri;
90     GnomeVFSResult      i_ret;
91
92     if( !(gnome_vfs_init()) )
93     {
94         msg_Warn( p_access, "couldn't initilize GnomeVFS" );
95         return VLC_EGENERIC;
96     }
97     /* FIXME
98        Since GnomeVFS segfaults on exit if we initialize it without trying to
99        open a file with a valid protocol, try top open at least file:// */
100     gnome_vfs_open( &(p_sys->p_handle), "file://", 5 );
101
102     p_access->pf_read = Read;
103     p_access->pf_block = NULL;
104     p_access->pf_seek = Seek;
105     p_access->pf_control = Control;
106     p_access->info.i_update = 0;
107     p_access->info.i_size = 0;
108     p_access->info.i_pos = 0;
109     p_access->info.b_eof = VLC_FALSE;
110     p_access->info.i_title = 0;
111     p_access->info.i_seekpoint = 0;
112     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
113     p_sys->i_nb_reads = 0;
114     p_sys->b_pace_control = VLC_TRUE;
115
116
117     if( strcmp( "gnomevfs", p_access->psz_access ) &&
118                                             *(p_access->psz_access) != '\0')
119     {
120         psz_name = malloc( strlen( p_access->psz_access ) +
121                                             strlen( p_access->psz_path ) + 3 );
122         strcpy( psz_name, p_access->psz_access );
123         strcat( psz_name, "://" );
124         strcat( psz_name, p_access->psz_path );
125     }
126     else
127     {
128         psz_name = strdup( p_access->psz_path );
129     }
130
131     psz = ToLocale( psz_name );
132
133     /* Use gnome_vfs_make_uri_from_input_with_dirs for local paths, as it deals
134        relative directories, and gnome_vfs_make_uri_from_input_with_dirs
135        otherwise */
136     psz_uri = gnome_vfs_make_uri_from_input_with_dirs( psz,
137                                     GNOME_VFS_MAKE_URI_DIR_CURRENT);
138     if( *psz_uri != '/' )
139     {
140         g_free( psz_uri );
141         psz_uri = gnome_vfs_escape_host_and_path_string( psz );
142     }
143
144     p_uri = gnome_vfs_uri_new( psz_uri );
145
146     if( p_uri )
147     {
148         p_sys->p_file_info = gnome_vfs_file_info_new();
149         i_ret = gnome_vfs_get_file_info_uri( p_uri,
150                                                 p_sys->p_file_info, 8 ); 
151
152         if( i_ret )
153         {
154             msg_Err( p_access, "cannot get file info (%s)", 
155                                     gnome_vfs_result_to_string( i_ret ) );
156             gnome_vfs_file_info_unref( p_sys->p_file_info );
157             gnome_vfs_uri_unref( p_uri);
158             free( p_sys );
159             free( psz_uri );
160             free( psz_name );
161             return VLC_EGENERIC;
162         }
163     }
164     else
165     {
166         msg_Warn( p_access, "cannot parse MRL %s or unsupported protocol", psz_name );
167         LocaleFree( psz );
168         g_free( psz_uri );
169         free( p_sys );
170         free( psz_name );
171         return VLC_EGENERIC;
172     }
173     LocaleFree( psz );
174
175     msg_Dbg( p_access, "opening file `%s'", psz_uri );
176     i_ret = gnome_vfs_open( &(p_sys->p_handle), psz_uri, 5 );
177     if( i_ret )
178     {
179         msg_Warn( p_access, "cannot open file %s: %s", psz_uri,
180                                 gnome_vfs_result_to_string( i_ret ) );
181
182         LocaleFree( psz );
183         g_free( psz_uri );
184         gnome_vfs_uri_unref( p_uri);
185         free( p_sys ); 
186         free( psz_uri );
187         free( psz_name );
188         return VLC_EGENERIC;
189     }
190
191     if (GNOME_VFS_FILE_INFO_LOCAL( p_sys->p_file_info ))
192     {
193         p_sys->b_local = VLC_TRUE;
194     }
195
196     if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_REGULAR || 
197         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE ||
198         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_BLOCK_DEVICE )
199     {
200         p_sys->b_seekable = VLC_TRUE;
201         p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
202     }
203     else if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_FIFO
204               || p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_SOCKET )
205     {
206         p_sys->b_seekable = VLC_FALSE;
207     }
208     else
209     {
210         msg_Err( p_access, "unknown file type for `%s'", psz_name );
211         return VLC_EGENERIC;
212     }
213
214     if( p_sys->b_seekable && !p_access->info.i_size )
215     {
216         /* FIXME that's bad because all others access will be probed */
217         msg_Err( p_access, "file %s is empty, aborting", psz_name );
218         gnome_vfs_file_info_unref( p_sys->p_file_info );
219         free( p_sys );
220         free( psz_uri );
221         free( psz_name );
222         return VLC_EGENERIC;
223     }
224
225     /* Update default_pts to a suitable value for file access */
226     var_Create( p_access, "gnomevfs-caching",
227                                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
228
229     g_free( psz_uri );
230     p_sys->psz_name = psz_name;
231     gnome_vfs_uri_unref( p_uri);
232     return VLC_SUCCESS;
233
234 }
235
236 /*****************************************************************************
237  * Close: close the target
238  *****************************************************************************/
239 static void Close( vlc_object_t * p_this )
240 {
241     access_t     *p_access = (access_t*)p_this;
242     access_sys_t *p_sys = p_access->p_sys;
243     int i_result;
244
245     i_result = gnome_vfs_close( p_sys->p_handle );
246     if( i_result )
247     {
248          msg_Err( p_access, "cannot close %s: %s", p_sys->psz_name,
249                                 gnome_vfs_result_to_string( i_result ) );
250     }
251
252     gnome_vfs_file_info_unref( p_sys->p_file_info );
253
254     free( p_sys->psz_name );
255     free( p_sys );
256 }
257
258 /*****************************************************************************
259  * Read: standard read on a file descriptor.
260  *****************************************************************************/
261 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
262 {
263     access_sys_t *p_sys = p_access->p_sys;
264     int i_ret;
265     GnomeVFSFileSize i_read_len;
266
267     i_ret = gnome_vfs_read( p_sys->p_handle, p_buffer,
268                                   (GnomeVFSFileSize)i_len, &i_read_len );
269
270     if( i_ret )
271     {
272         p_access->info.b_eof = VLC_TRUE;
273         if( i_ret != GNOME_VFS_ERROR_EOF )
274         {
275             msg_Err( p_access, "read failed (%s)",
276                                     gnome_vfs_result_to_string( i_ret ) );
277         }
278     }
279     else
280     {
281         p_sys->i_nb_reads++;
282         if( p_access->info.i_size != 0 &&
283             (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 &&
284             p_sys->b_local )
285         {
286             gnome_vfs_file_info_clear( p_sys->p_file_info );
287             i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
288                                                 p_sys->p_file_info, 8 );
289             if( i_ret )
290             {
291                 msg_Warn( p_access, "couldn't get file properties again (%s)",
292                                         gnome_vfs_result_to_string( i_ret ) );
293             }
294             else
295             {
296                 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
297             }
298         }
299     }
300
301     p_access->info.i_pos += (int64_t)i_read_len;
302
303     /* Some Acces (http) never return EOF and loop on the file */
304     if( p_access->info.i_pos > p_access->info.i_size )
305     {
306         p_access->info.b_eof = VLC_TRUE;
307         return 0;
308     }
309     return (int)i_read_len;
310 }
311
312 /*****************************************************************************
313  * Seek: seek to a specific location in a file
314  *****************************************************************************/
315 static int Seek( access_t *p_access, int64_t i_pos )
316 {
317     access_sys_t *p_sys = p_access->p_sys;
318     int i_ret;
319
320     i_ret = gnome_vfs_seek( p_sys->p_handle, GNOME_VFS_SEEK_START,
321                                             (GnomeVFSFileOffset)i_pos);
322
323     if ( !i_ret )
324     {
325         p_access->info.i_pos = i_pos;
326     }
327     else
328     {
329         GnomeVFSFileSize i_offset;
330         msg_Err( p_access, "cannot seek (%s)",
331                                         gnome_vfs_result_to_string( i_ret ) );
332         i_ret = gnome_vfs_tell( p_sys->p_handle, &i_offset );
333         if( !i_ret )
334         {
335             msg_Err( p_access, "cannot tell the current position (%s)",
336                                         gnome_vfs_result_to_string( i_ret ) );
337             return VLC_EGENERIC;
338         }
339     }
340     /* Reset eof */
341     p_access->info.b_eof = VLC_FALSE;
342
343     /* FIXME */
344     return VLC_SUCCESS;
345 }
346
347 /*****************************************************************************
348  * Control:
349  *****************************************************************************/
350 static int Control( access_t *p_access, int i_query, va_list args )
351 {
352     access_sys_t *p_sys = p_access->p_sys;
353     vlc_bool_t   *pb_bool;
354     int          *pi_int;
355     int64_t      *pi_64;
356
357     switch( i_query )
358     {
359         /* */
360         case ACCESS_CAN_SEEK:
361         case ACCESS_CAN_FASTSEEK:
362             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
363             *pb_bool = p_sys->b_seekable;
364             break;
365
366         case ACCESS_CAN_PAUSE:
367         case ACCESS_CAN_CONTROL_PACE:
368             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
369             *pb_bool = p_sys->b_pace_control;
370             break;
371
372         /* */
373         case ACCESS_GET_MTU:
374             pi_int = (int*)va_arg( args, int * );
375             *pi_int = 0;
376             break;
377
378         case ACCESS_GET_PTS_DELAY:
379             pi_64 = (int64_t*)va_arg( args, int64_t * );
380             *pi_64 = var_GetInteger( p_access,
381                                         "gnomevfs-caching" ) * I64C(1000);
382             break;
383
384         /* */
385         case ACCESS_SET_PAUSE_STATE:
386             /* Nothing to do */
387             break;
388
389         case ACCESS_GET_TITLE_INFO:
390         case ACCESS_SET_TITLE:
391         case ACCESS_SET_SEEKPOINT:
392         case ACCESS_SET_PRIVATE_ID_STATE:
393         case ACCESS_GET_META:
394             return VLC_EGENERIC;
395
396         default:
397             msg_Warn( p_access, "unimplemented query in control" );
398             return VLC_EGENERIC;
399
400     }
401     return VLC_SUCCESS;
402 }
403