libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Tue Jan 23 21:37:27 2007 +0000 (2007-01-23)
branchtrunk
changeset 291 73fa3a0f2f08
parent 281 08c4bc759e7a
child 294 388fc8b452c2
permissions -rwxr-xr-x
[svn r292] GObject format to the GMythProgramInfo, new actions such as GET_PROGRAM_INFO and GET_CURRENT_RECORDER.
     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     gint 							channel_num;
    54     
    55     gint mythtv_version;
    56     guint64 content_size;
    57     guint64 bytes_read;
    58 
    59     GByteArray *buffer;
    60     gsize buffer_remain;
    61 } MythtvHandle;
    62 
    63 
    64 
    65 static GnomeVFSResult
    66 do_open (GnomeVFSMethod *method,
    67          GnomeVFSMethodHandle **method_handle,
    68          GnomeVFSURI *uri,
    69          GnomeVFSOpenMode mode,
    70          GnomeVFSContext *context)
    71 {
    72     MythtvHandle *myth_handle;
    73     GMythBackendInfo *backend_info;
    74     GMythURI *gmyth_uri = NULL;
    75     gboolean ret = TRUE;
    76     gboolean is_livetv = FALSE;
    77     gint wait_to_transfer = 0;
    78 
    79     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    80     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    81 
    82     myth_handle = g_new0 (MythtvHandle, 1);
    83 
    84     if (mode & GNOME_VFS_OPEN_WRITE) {
    85         return GNOME_VFS_ERROR_NOT_PERMITTED;
    86     }
    87 
    88     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    89     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    90     }
    91 
    92     /* Initialize mythtv handler*/
    93     myth_handle->file_transfer = NULL;
    94     myth_handle->livetv = NULL;
    95     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    96     myth_handle->bytes_read = 0;
    97     myth_handle->content_size = -1;
    98 
    99     /* Creates and fills out the backend info structure */    
   100 	  backend_info = gmyth_backend_info_new_with_uri ( 
   101 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   102 	  
   103 	  /* creates an instance of  */  
   104 	  gmyth_uri = gmyth_uri_new_with_value( 
   105 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   106 	  
   107 	  /* Connect to the backend */	  
   108 	  if ( gmyth_uri != NULL && ( is_livetv = gmyth_uri_is_livetv( gmyth_uri ) ) == TRUE ) {
   109 	    myth_handle->livetv = gmyth_livetv_new ();
   110 	    
   111 	    myth_handle->channel_num = gmyth_uri_get_channel_num( gmyth_uri );
   112 	
   113 	    if ( myth_handle->channel_num != -1 ) {
   114 	      if (gmyth_livetv_channel_setup (myth_handle->livetv, myth_handle->channel_num,
   115 	              backend_info) == FALSE) {
   116 	        g_object_unref( gmyth_uri );
   117 	        ret = FALSE;
   118 	      }
   119 	    } else {
   120 	      if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
   121 	      	g_object_unref( gmyth_uri );
   122 	        ret = FALSE;
   123 	      }
   124 	    }
   125 	
   126 	    myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
   127 	
   128 	    if (NULL == myth_handle->file_transfer) {
   129 	      ret = FALSE;
   130 	    }
   131 	    
   132 	    if ( gmyth_uri != NULL )
   133 	    	g_object_unref( gmyth_uri );
   134 	    	
   135 	  } else {
   136 	
   137 	    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   138 	    
   139 	    /* Verifies if the file exists */
   140 	    if (!gmyth_util_file_exists (backend_info, gnome_vfs_uri_get_path (uri))) {
   141 	        g_object_unref (backend_info);
   142 					return GNOME_VFS_ERROR_NOT_FOUND;
   143 	    }
   144 	    
   145 	    /* sets the Playback monitor connection */
   146 	    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, gnome_vfs_uri_get_path (uri) );
   147 		
   148 	  } /* if - LiveTV or not? */
   149 	  
   150     if (ret == FALSE) {
   151 	    g_warning ("MythTV FileTransfer open error\n");
   152 	    return GNOME_VFS_ERROR_NOT_OPEN;
   153 	  } 
   154 
   155     g_object_unref (backend_info);
   156 
   157     g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   158 
   159     myth_handle->content_size = myth_handle->file_transfer->filesize;
   160     
   161     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   162     myth_handle->buffer_remain = 0;
   163 
   164     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   165 
   166     return GNOME_VFS_OK;
   167 }
   168 
   169 static GnomeVFSResult
   170 do_read (GnomeVFSMethod *method,
   171          GnomeVFSMethodHandle *method_handle,
   172          gpointer buffer,
   173          GnomeVFSFileSize num_bytes,
   174          GnomeVFSFileSize *bytes_read,
   175          GnomeVFSContext *context)
   176 {
   177     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   178     GnomeVFSFileSize bytes_to_read;
   179 
   180     *bytes_read = 0;
   181 
   182     if (myth_handle->bytes_read >= myth_handle->content_size)
   183         return GNOME_VFS_ERROR_EOF;
   184 
   185     // fixme: change this to min math function
   186     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   187 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   188     else
   189 	    bytes_to_read = num_bytes;
   190 
   191     /* Loop sending the Myth File Transfer request:
   192     * Retry whilst authentication fails and we supply it. */
   193     if ((myth_handle->buffer_remain = myth_handle->buffer->len) < MYTHTV_BUFFER_SIZE) {
   194     //if ( bytes_to_read > myth_handle->buffer_remain ) {
   195         GByteArray *tmp_buffer = g_byte_array_new();
   196 
   197         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   198               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   199 
   200 	if (len < 0) {
   201             g_byte_array_free (tmp_buffer, TRUE);
   202             g_warning ("Fail to read bytes");
   203 	    return GNOME_VFS_ERROR_IO;
   204         } /*else if (len == 0) {
   205 	    g_byte_array_free (tmp_buffer, TRUE);
   206 	    g_warning ("End of file probably achieved");
   207 	    return GNOME_VFS_ERROR_EOF;
   208 	}*/
   209 
   210         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   211     		tmp_buffer->data, len);
   212 
   213 		myth_handle->buffer_remain += len;
   214 
   215         g_byte_array_free (tmp_buffer, TRUE);
   216     	tmp_buffer = NULL;    	
   217     }
   218     
   219     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   220     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   221 
   222     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   223 
   224     myth_handle->bytes_read += bytes_to_read;
   225     myth_handle->buffer_remain -= bytes_to_read;    
   226 
   227   	/* flushs the newly buffer got from byte array */
   228   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   229     *bytes_read = bytes_to_read;
   230   
   231     return GNOME_VFS_OK;
   232 }
   233 
   234 static GnomeVFSResult
   235 do_close (GnomeVFSMethod *method,
   236           GnomeVFSMethodHandle *method_handle,
   237           GnomeVFSContext *context)
   238 {
   239 
   240     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   241 
   242     if (myth_handle->file_transfer) {
   243 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   244         g_object_unref (myth_handle->file_transfer);
   245     	myth_handle->file_transfer = NULL;
   246     }
   247     
   248     if (myth_handle->livetv) {
   249       g_object_unref (myth_handle->livetv);
   250     	myth_handle->livetv = NULL;
   251     }
   252     
   253     if (myth_handle->buffer) {
   254 		g_byte_array_free (myth_handle->buffer, TRUE);
   255 		myth_handle->buffer = NULL;
   256     }
   257 
   258     g_free (myth_handle);
   259 
   260     return GNOME_VFS_OK;
   261 }
   262 
   263 static GnomeVFSResult
   264 do_get_file_info (GnomeVFSMethod *method,
   265                   GnomeVFSURI *uri,
   266                   GnomeVFSFileInfo *file_info,
   267                   GnomeVFSFileInfoOptions options,
   268                   GnomeVFSContext *context)
   269 {
   270     GMythFileTransfer *file_transfer = NULL;
   271     GMythBackendInfo *backend_info = NULL;
   272 
   273     file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
   274     file_info->valid_fields = file_info->valid_fields
   275         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   276         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   277         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   278     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   279     // fixme: get from file extension?
   280     file_info->mime_type = g_strdup ("video/x-nuv");
   281     file_info->permissions =
   282         GNOME_VFS_PERM_USER_READ |
   283         GNOME_VFS_PERM_OTHER_READ |
   284         GNOME_VFS_PERM_GROUP_READ;
   285 
   286     backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
   287             gnome_vfs_uri_get_user_name (uri),
   288             gnome_vfs_uri_get_password (uri),
   289             NULL,
   290             gnome_vfs_uri_get_host_port (uri));
   291 
   292     file_transfer = gmyth_file_transfer_new (backend_info);
   293     if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
   294         file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   295         file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   296         file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   297     }
   298 
   299     g_object_unref (file_transfer);
   300     g_object_unref (backend_info);
   301     return GNOME_VFS_OK;
   302 }
   303 
   304 static gboolean
   305 do_is_local (GnomeVFSMethod *method,
   306              const GnomeVFSURI *uri)
   307 {
   308 	return FALSE;
   309 }
   310 
   311 static GnomeVFSMethod method = {
   312 	sizeof (GnomeVFSMethod),
   313 	do_open,
   314 	NULL,
   315 	do_close,
   316 	do_read,
   317 	NULL,
   318 	NULL,
   319 	NULL,
   320 	NULL,
   321 	NULL,
   322 	NULL,
   323 	NULL,
   324 	do_get_file_info,
   325 	NULL,
   326 	do_is_local,
   327 	NULL,
   328 	NULL,
   329 	NULL,
   330 	NULL,
   331 	NULL,
   332 	NULL,
   333 	NULL,
   334 	NULL,
   335 	NULL,
   336 	NULL,
   337 	NULL,
   338 	NULL,
   339 };
   340 
   341 
   342 GnomeVFSMethod *
   343 vfs_module_init (const char *method_name, const char *args)
   344 {
   345 	return &method;
   346 }
   347 
   348 void
   349 vfs_module_shutdown (GnomeVFSMethod *method)
   350 {
   351 }