libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Wed Feb 07 18:30:15 2007 +0000 (2007-02-07)
branchtrunk
changeset 331 f32837103d17
parent 328 73461d8db7ec
child 332 be533d944ceb
permissions -rwxr-xr-x
[svn r333] Fixed bug on do_get_file_info.
     1 /*
     2  * @author Hallyson Melo <hallyson.melo@indt.org.br>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU Lesser General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU Lesser General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    18 
    19 #ifdef HAVE_CONFIG_H
    20 #include <config.h>
    21 #endif
    22 
    23 #include <string.h>
    24 #include <glib.h>
    25 #include <math.h>
    26 
    27 #include <libgnomevfs/gnome-vfs-module.h>
    28 #include <libgnomevfs/gnome-vfs-utils.h>
    29 
    30 #include <gmyth/gmyth_file_transfer.h>
    31 #include <gmyth/gmyth_livetv.h>
    32 #include <gmyth/gmyth_uri.h>
    33 #include <gmyth/gmyth_recorder.h>
    34 #include <gmyth/gmyth_backendinfo.h>
    35 #include <gmyth/gmyth_util.h>
    36 
    37 #define GST_MYTHTV_ID_NUM               1
    38 #define MYTHTV_VERSION_DEFAULT          30
    39 #define MYTHTV_TRANSFER_MAX_WAITS       100
    40 
    41 #define MYTHTV_BUFFER_SIZE							1024*64
    42 
    43 static GnomeVFSResult do_read (GnomeVFSMethod *method,
    44                                GnomeVFSMethodHandle *method_handle,
    45                                gpointer buffer,
    46                                GnomeVFSFileSize num_bytes,
    47                                GnomeVFSFileSize *bytes_read,
    48                                GnomeVFSContext *context);
    49 
    50 typedef struct {
    51     GMythFileTransfer *file_transfer;
    52     GMythLiveTV 			*livetv;
    53     gchar 						*channel_name;
    54     
    55     gint mythtv_version;
    56     gint64 content_size;
    57     guint64 bytes_read;
    58 
    59     GByteArray *buffer;
    60     gsize buffer_remain;
    61 } MythtvHandle;
    62 
    63 static GnomeVFSResult
    64 do_open (GnomeVFSMethod *method,
    65          GnomeVFSMethodHandle **method_handle,
    66          GnomeVFSURI *uri,
    67          GnomeVFSOpenMode mode,
    68          GnomeVFSContext *context)
    69 {
    70     MythtvHandle *myth_handle;
    71     GMythBackendInfo *backend_info;
    72     GMythURI *gmyth_uri = NULL;
    73     gboolean ret = TRUE;
    74     gboolean is_livetv = FALSE;
    75 
    76     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    77     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    78 
    79     myth_handle = g_new0 (MythtvHandle, 1);
    80 
    81     if (mode & GNOME_VFS_OPEN_WRITE) {
    82         return GNOME_VFS_ERROR_NOT_PERMITTED;
    83     }
    84 
    85     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    86     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    87     }
    88 
    89     /* Initialize mythtv handler*/
    90     myth_handle->file_transfer = NULL;
    91     myth_handle->livetv = NULL;
    92     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    93     myth_handle->bytes_read = 0;
    94     myth_handle->content_size = (GnomeVFSFileSize) - 1;
    95 
    96     /* Creates and fills out the backend info structure */    
    97 	  backend_info = gmyth_backend_info_new_with_uri ( 
    98 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
    99 	  
   100 	  /* creates an instance of  */  
   101 	  gmyth_uri = gmyth_uri_new_with_value( 
   102 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   103 
   104 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   105 
   106 	  /* Connect to the backend */	  
   107 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   108 	    myth_handle->livetv = gmyth_livetv_new ();
   109 	    
   110 	    myth_handle->channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   111 	    
   112 	    g_print( "[%s] Channel name = %s", __FUNCTION__, myth_handle->channel_name );
   113 	
   114 	    if ( myth_handle->channel_name != NULL ) {
   115 	      if (gmyth_livetv_channel_name_setup (myth_handle->livetv, myth_handle->channel_name,
   116 	              backend_info) == FALSE) {
   117 	        g_object_unref( gmyth_uri );
   118 	        ret = FALSE;
   119 	      }
   120 	    } else {
   121 	      if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
   122 	      	g_object_unref( gmyth_uri );
   123 	        ret = FALSE;
   124 	      }
   125 	    }
   126 	
   127 	    myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
   128 	
   129 	    if (NULL == myth_handle->file_transfer) {
   130 	      ret = FALSE;
   131 	    }
   132 	    
   133   		if ( !gmyth_file_transfer_open( myth_handle->file_transfer, myth_handle->livetv->uri != NULL ? 
   134 								gmyth_uri_get_path(myth_handle->livetv->uri) : 
   135 								myth_handle->livetv->proginfo->pathname->str ) )
   136 			{
   137 				g_object_unref( myth_handle->file_transfer );
   138 				myth_handle->file_transfer = NULL;
   139 				ret = FALSE;
   140 			}
   141 	    
   142 	  } else {
   143 	
   144 	    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   145 	    
   146 	    /* Verifies if the file exists */
   147 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   148 	        g_object_unref (backend_info);
   149 					ret = FALSE;
   150 	    }
   151 	    
   152 	    /* sets the Playback monitor connection */
   153 	    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, 
   154 	    		gmyth_uri_get_path (gmyth_uri) );
   155 		
   156 	  } /* if - LiveTV or not? */
   157 	  
   158     if (ret == FALSE) {
   159 	    g_warning ("MythTV FileTransfer open error.\n");
   160 	    return GNOME_VFS_ERROR_NOT_OPEN;
   161 	  }
   162 
   163     g_object_unref (backend_info);
   164     
   165 	  //if ( gmyth_uri != NULL )
   166 	  //	g_object_unref( gmyth_uri );
   167 	  
   168 	  g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   169 	  
   170 	  if ( myth_handle->file_transfer->filesize < 0 && is_livetv ) {
   171 	  	myth_handle->content_size = (GnomeVFSFileSize) - 1;
   172 			myth_handle->content_size = gmyth_recorder_get_file_position( myth_handle->livetv->recorder );
   173 	  } else		
   174     	myth_handle->content_size = myth_handle->file_transfer->filesize;
   175     
   176     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   177     myth_handle->buffer_remain = 0;
   178 
   179     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   180 
   181     return GNOME_VFS_OK;
   182 }
   183 
   184 static GnomeVFSResult
   185 do_read (GnomeVFSMethod *method,
   186          GnomeVFSMethodHandle *method_handle,
   187          gpointer buffer,
   188          GnomeVFSFileSize num_bytes,
   189          GnomeVFSFileSize *bytes_read,
   190          GnomeVFSContext *context)
   191 {
   192   MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   193   GnomeVFSFileSize bytes_to_read;
   194 
   195   *bytes_read = 0;
   196 
   197   if (myth_handle->bytes_read >= myth_handle->content_size)
   198       return GNOME_VFS_ERROR_EOF;
   199 
   200   // fixme: change this to min math function
   201   if (myth_handle->content_size > 0 && num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ))
   202     bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   203   else
   204     bytes_to_read = num_bytes;
   205 
   206   /* Loop sending the Myth File Transfer request:
   207   * Retry whilst authentication fails and we supply it. */
   208   //if (myth_handle->buffer_remain  < MYTHTV_BUFFER_SIZE) {
   209   if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < MYTHTV_BUFFER_SIZE ) {
   210   	GByteArray *tmp_buffer = g_byte_array_new();
   211 
   212 		printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
   213 
   214     gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   215           tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   216 
   217 		if (len < 0) {
   218 	    g_byte_array_free (tmp_buffer, TRUE);
   219 	    g_warning ("Fail to read bytes");
   220 	    return GNOME_VFS_ERROR_IO;
   221 	  } /*else if (len == 0) {
   222 		    g_byte_array_free (tmp_buffer, TRUE);
   223 		    g_warning ("End of file probably achieved");
   224 		    return GNOME_VFS_ERROR_EOF;
   225 		}*/
   226 	
   227 	    myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   228 	    		tmp_buffer->data, len);
   229 	
   230 			myth_handle->buffer_remain += len;
   231 
   232       g_byte_array_free (tmp_buffer, TRUE);
   233     	tmp_buffer = NULL;
   234     }
   235     
   236     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   237     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   238 
   239     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   240 
   241     myth_handle->bytes_read += bytes_to_read;
   242     myth_handle->buffer_remain -= bytes_to_read;    
   243 
   244   	/* flushs the newly buffer got from byte array */
   245   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   246     *bytes_read = bytes_to_read;
   247   
   248     return GNOME_VFS_OK;
   249 }
   250 
   251 static GnomeVFSResult
   252 do_close (GnomeVFSMethod *method,
   253           GnomeVFSMethodHandle *method_handle,
   254           GnomeVFSContext *context)
   255 {
   256 
   257     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   258 
   259     if (myth_handle->file_transfer) {
   260 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   261         g_object_unref (myth_handle->file_transfer);
   262     	myth_handle->file_transfer = NULL;
   263     }
   264     
   265     if (myth_handle->livetv) {
   266       g_object_unref (myth_handle->livetv);
   267     	myth_handle->livetv = NULL;
   268     }
   269     
   270     if (myth_handle->buffer) {
   271 		g_byte_array_free (myth_handle->buffer, TRUE);
   272 		myth_handle->buffer = NULL;
   273     }
   274 
   275     g_free (myth_handle);
   276 
   277     return GNOME_VFS_OK;
   278 }
   279 
   280 static GnomeVFSResult
   281 do_get_file_info (GnomeVFSMethod *method,
   282                   GnomeVFSURI *uri,
   283                   GnomeVFSFileInfo *file_info,
   284                   GnomeVFSFileInfoOptions options,
   285                   GnomeVFSContext *context)
   286 {
   287     GMythFileTransfer *file_transfer = NULL;
   288     GMythLiveTV 			*livetv 			 = NULL;
   289     GMythBackendInfo  *backend_info  = NULL;
   290     GMythURI					*gmyth_uri		 = NULL;
   291     gboolean 					is_livetv 		 = FALSE;
   292     gboolean					ret						 = TRUE;
   293     
   294     /* Creates and fills out the backend info structure */    
   295 	  backend_info = gmyth_backend_info_new_with_uri ( 
   296 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   297 	  
   298 	  /* creates an instance of */  
   299 	  gmyth_uri = gmyth_uri_new_with_value( 
   300 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   301 
   302 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   303 
   304     file_info->valid_fields = file_info->valid_fields
   305         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   306         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   307         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   308     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   309     /* fixme: get from file extension? */
   310     file_info->mime_type = g_strdup ("video/x-nuv");
   311     file_info->permissions =
   312         GNOME_VFS_PERM_USER_READ |
   313         GNOME_VFS_PERM_OTHER_READ |
   314         GNOME_VFS_PERM_GROUP_READ;
   315         
   316     g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.",
   317     				gnome_vfs_uri_get_path (uri),
   318     				gmyth_uri_get_path (gmyth_uri) );
   319 
   320 	  /* Connect to the backend */
   321 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   322 	    livetv = gmyth_livetv_new ();	    
   323 	        
   324 	    gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   325 	
   326 	    if ( channel_name != NULL ) {
   327 	      if (gmyth_livetv_channel_name_setup (livetv, channel_name,
   328 	              backend_info) == FALSE) {
   329 	        g_object_unref( gmyth_uri );
   330 	        ret = FALSE;
   331 	      }
   332 	    } else {
   333 	      if ( gmyth_livetv_setup (livetv, backend_info) == FALSE ) {
   334 	      	g_object_unref( gmyth_uri );
   335 	        ret = FALSE;
   336 	      }
   337 	    }
   338 	    
   339 	    g_print( "path = %s",  gmyth_uri_get_path (livetv->uri) );
   340 	
   341 	    file_transfer = gmyth_livetv_create_file_transfer (livetv);	    
   342 	
   343 	    if (NULL == file_transfer) {
   344 	      ret = FALSE;
   345 	    }
   346 	    g_print( "path = %s",  gmyth_uri_get_path (livetv->uri) );
   347 	    
   348 	    file_info->name = g_strdup ( gmyth_uri_get_path (livetv->uri) );
   349 
   350 	  } else {
   351 	
   352 	    file_transfer = gmyth_file_transfer_new (backend_info);
   353 	    
   354 	    /* Verifies if the file exists */
   355 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   356 	        g_object_unref (backend_info);
   357 					return GNOME_VFS_ERROR_NOT_FOUND;
   358 	    }
   359 	    
   360 	    /* sets the Playback monitor connection */
   361 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
   362 	    
   363 	    file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );	    
   364 		
   365 	  } /* if - LiveTV or not? */
   366 	  
   367 	  /*	  
   368     if (ret == FALSE) {
   369 	    g_warning ("MythTV FileTransfer open error\n");
   370 	    return GNOME_VFS_ERROR_NOT_OPEN;
   371 	  }
   372 	  */
   373 	  if ( is_livetv )
   374 	  {
   375 	  	gint64 size = 0;
   376 	  	if ( file_transfer->filesize < 0 )
   377 	  	{
   378 	  		size = gmyth_recorder_get_file_position( livetv->recorder );
   379 	  		if( size < 0 ) {	  			
   380 	  			GMythProgramInfo *prog_info = gmyth_recorder_get_current_program_info ( livetv->recorder );
   381 	  			size = prog_info->filesize;
   382 	  			
   383 	  			if ( prog_info )
   384 	  				g_object_unref( prog_info );	  								
   385 				}
   386 	  	}
   387 			else if ( file_transfer != NULL )
   388 				size = gmyth_file_transfer_get_filesize (file_transfer);
   389 						
   390 			if ( size < 0 )
   391 				file_info->size =(GnomeVFSFileSize) - 1;
   392 			else
   393 				file_info->size = size;
   394 
   395 	    /*if ( file_transfer != NULL)
   396 	    	g_object_unref (file_transfer);*/
   397 				
   398 	    if ( livetv != NULL )
   399 	    	g_object_unref (livetv);
   400 	    	        
   401 	    /*if ( file_transfer != NULL)
   402 	    	g_object_unref (file_transfer);*/
   403 
   404 	  } else if ( ret == TRUE  && file_transfer != NULL ) {
   405     	file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   406 	    if ( file_transfer )
   407 	    	g_object_unref (file_transfer);
   408    	}
   409  	
   410     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   411 	  file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   412     
   413     if (backend_info)
   414    		g_object_unref (backend_info);
   415 
   416     return GNOME_VFS_OK;
   417 }
   418 
   419 static gboolean
   420 do_is_local (GnomeVFSMethod *method,
   421              const GnomeVFSURI *uri)
   422 {
   423 	return FALSE;
   424 }
   425 
   426 static GnomeVFSMethod method = {
   427 	sizeof (GnomeVFSMethod),
   428 	do_open,
   429 	NULL,
   430 	do_close,
   431 	do_read,
   432 	NULL,
   433 	NULL,
   434 	NULL,
   435 	NULL,
   436 	NULL,
   437 	NULL,
   438 	NULL,
   439 	do_get_file_info,
   440 	NULL,
   441 	do_is_local,
   442 	NULL,
   443 	NULL,
   444 	NULL,
   445 	NULL,
   446 	NULL,
   447 	NULL,
   448 	NULL,
   449 	NULL,
   450 	NULL,
   451 	NULL,
   452 	NULL,
   453 	NULL,
   454 };
   455 
   456 
   457 GnomeVFSMethod *
   458 vfs_module_init (const char *method_name, const char *args)
   459 {
   460 	return &method;
   461 }
   462 
   463 void
   464 vfs_module_shutdown (GnomeVFSMethod *method)
   465 {
   466 }