libgnomevfs2-mythtv/modules/mythtv-method.c
author renatofilho
Mon Dec 04 22:01:41 2006 +0000 (2006-12-04)
branchtrunk
changeset 188 944deaac3f01
parent 180 60033c7d3bab
child 190 201327c993e5
permissions -rwxr-xr-x
[svn r189] fixed gmyth_file_transfer compatibilit
     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_file_transfer.h"
    31 #include "gmyth_backendinfo.h"
    32 
    33 #define GST_MYTHTV_ID_NUM               1
    34 #define MYTHTV_VERSION_DEFAULT          30
    35 
    36 #define MYTHTV_BUFFER_SIZE		1024*64
    37 
    38 static GnomeVFSResult do_read (GnomeVFSMethod *method,
    39                                GnomeVFSMethodHandle *method_handle,
    40                                gpointer buffer,
    41                                GnomeVFSFileSize num_bytes,
    42                                GnomeVFSFileSize *bytes_read,
    43                                GnomeVFSContext *context);
    44 
    45 typedef struct {
    46     GMythFileTransfer *file_transfer;
    47     
    48     gint mythtv_version;
    49     guint64 content_size;
    50     guint64 bytes_read;
    51 
    52     GByteArray *buffer;
    53     gsize buffer_remain;
    54 } MythtvHandle;
    55 
    56 
    57 
    58 static GnomeVFSResult
    59 do_open (GnomeVFSMethod *method,
    60          GnomeVFSMethodHandle **method_handle,
    61          GnomeVFSURI *uri,
    62          GnomeVFSOpenMode mode,
    63          GnomeVFSContext *context)
    64 {
    65     MythtvHandle *myth_handle;
    66     GMythBackendInfo *backend_info;
    67     gboolean ret;
    68 
    69     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    70     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    71 
    72     myth_handle = g_new0 (MythtvHandle, 1);
    73 
    74     if (mode & GNOME_VFS_OPEN_WRITE) {
    75         return GNOME_VFS_ERROR_NOT_PERMITTED;
    76     }
    77 
    78     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    79     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    80     }
    81 
    82     /* Initialize mythtv handler*/
    83     myth_handle->file_transfer = NULL;
    84     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    85     myth_handle->bytes_read = 0;
    86     myth_handle->content_size = -1;
    87 
    88     /* Creates and fills out the backend info structure */
    89     backend_info = gmyth_backend_info_new_full (
    90         gnome_vfs_uri_get_host_name (uri),
    91 	    NULL, NULL, NULL,
    92 	    gnome_vfs_uri_get_host_port (uri));
    93 	    
    94 	  gmyth_backend_info_set_path ( backend_info, gnome_vfs_uri_get_path (uri) );
    95 
    96     /* Connect to the backend */
    97     myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
    98     g_object_unref (backend_info);
    99 
   100     g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   101 
   102     /* sets the Playback monitor connection */
   103     ret = gmyth_file_transfer_open (myth_handle->file_transfer, gnome_vfs_uri_get_path (uri));
   104     if (ret == FALSE) {
   105         g_warning ("Mythtv FileTransfer open error\n");
   106         return GNOME_VFS_ERROR_NOT_OPEN;
   107     }
   108 
   109     // TODO: Verify if file exists in the backend
   110 
   111     myth_handle->content_size = myth_handle->file_transfer->filesize;
   112     
   113     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   114     myth_handle->buffer_remain = 0;
   115 
   116     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   117 
   118     return GNOME_VFS_OK;
   119 }
   120 
   121 static GnomeVFSResult
   122 do_read (GnomeVFSMethod *method,
   123          GnomeVFSMethodHandle *method_handle,
   124          gpointer buffer,
   125          GnomeVFSFileSize num_bytes,
   126          GnomeVFSFileSize *bytes_read,
   127          GnomeVFSContext *context)
   128 {
   129     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   130     GnomeVFSFileSize bytes_to_read;
   131 
   132     *bytes_read = 0;
   133 
   134     if (myth_handle->bytes_read >= myth_handle->content_size)
   135         return GNOME_VFS_ERROR_EOF;
   136 
   137     // fixme: change this to min math function
   138     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   139 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   140     else
   141 	    bytes_to_read = num_bytes;
   142 
   143     /* Loop sending the Myth File Transfer request:
   144     * Retry whilst authentication fails and we supply it. */
   145   
   146     if ( bytes_to_read > myth_handle->buffer_remain ) {
   147         GByteArray *tmp_buffer = g_byte_array_new();
   148 
   149         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   150               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   151 
   152 		if (len < 0) {
   153             g_byte_array_free (tmp_buffer, TRUE);
   154             g_warning ("Fail to read bytes");
   155 		    return GNOME_VFS_ERROR_IO;
   156         }
   157 
   158         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   159     		tmp_buffer->data, len);
   160 
   161 		myth_handle->buffer_remain += len;
   162 
   163         g_byte_array_free (tmp_buffer, TRUE);
   164     	tmp_buffer = NULL;    	
   165     }
   166     
   167     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   168     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   169 
   170     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   171 
   172     myth_handle->bytes_read += bytes_to_read;
   173     myth_handle->buffer_remain -= bytes_to_read;    
   174 
   175   	/* flushs the newly buffer got from byte array */
   176   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   177     *bytes_read = bytes_to_read;
   178   
   179     return GNOME_VFS_OK;
   180 }
   181 
   182 static GnomeVFSResult
   183 do_close (GnomeVFSMethod *method,
   184           GnomeVFSMethodHandle *method_handle,
   185           GnomeVFSContext *context)
   186 {
   187 
   188     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   189 
   190     if (myth_handle->file_transfer) {
   191 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   192         g_object_unref (myth_handle->file_transfer);
   193     	myth_handle->file_transfer = NULL;
   194     }
   195     
   196     if (myth_handle->buffer) {
   197 		g_byte_array_free (myth_handle->buffer, TRUE);
   198 		myth_handle->buffer = NULL;
   199     }
   200 
   201     g_free (myth_handle);
   202 
   203     return GNOME_VFS_OK;
   204 }
   205 
   206 static GnomeVFSResult
   207 do_get_file_info (GnomeVFSMethod *method,
   208                   GnomeVFSURI *uri,
   209                   GnomeVFSFileInfo *file_info,
   210                   GnomeVFSFileInfoOptions options,
   211                   GnomeVFSContext *context)
   212 {
   213     GMythFileTransfer *file_transfer = NULL;
   214     GMythBackendInfo *backend_info = NULL;
   215 
   216     file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
   217     file_info->valid_fields = file_info->valid_fields
   218         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   219         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   220         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   221     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   222     // fixme: get from file extension?
   223     file_info->mime_type = g_strdup ("video/x-nuv");
   224     file_info->permissions =
   225         GNOME_VFS_PERM_USER_READ |
   226         GNOME_VFS_PERM_OTHER_READ |
   227         GNOME_VFS_PERM_GROUP_READ;
   228 
   229     backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
   230             gnome_vfs_uri_get_user_name (uri),
   231             gnome_vfs_uri_get_password (uri),
   232             NULL,
   233             gnome_vfs_uri_get_host_port (uri));
   234 
   235     file_transfer = gmyth_file_transfer_new (backend_info);
   236     if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
   237         file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   238         file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   239         file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   240     }
   241 
   242     g_object_unref (file_transfer);
   243     g_object_unref (backend_info);
   244     return GNOME_VFS_OK;
   245 }
   246 
   247 static gboolean
   248 do_is_local (GnomeVFSMethod *method,
   249              const GnomeVFSURI *uri)
   250 {
   251 	return FALSE;
   252 }
   253 
   254 static GnomeVFSMethod method = {
   255 	sizeof (GnomeVFSMethod),
   256 	do_open,
   257 	NULL,
   258 	do_close,
   259 	do_read,
   260 	NULL,
   261 	NULL,
   262 	NULL,
   263 	NULL,
   264 	NULL,
   265 	NULL,
   266 	NULL,
   267 	do_get_file_info,
   268 	NULL,
   269 	do_is_local,
   270 	NULL,
   271 	NULL,
   272 	NULL,
   273 	NULL,
   274 	NULL,
   275 	NULL,
   276 	NULL,
   277 	NULL,
   278 	NULL,
   279 	NULL,
   280 	NULL,
   281 	NULL,
   282 };
   283 
   284 
   285 GnomeVFSMethod *
   286 vfs_module_init (const char *method_name, const char *args)
   287 {
   288 	return &method;
   289 }
   290 
   291 void
   292 vfs_module_shutdown (GnomeVFSMethod *method)
   293 {
   294 }