libgnomevfs2-mythtv/modules/mythtv-method.c
author leo_sobral
Thu Nov 30 22:24:05 2006 +0000 (2006-11-30)
branchtrunk
changeset 161 a2308f2e6ed0
parent 149 4b1889efb573
child 164 81e9da9ad7db
permissions -rwxr-xr-x
[svn r162] testing
     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     //g_debug ("[%s] Calling do_open function", __FUNCTION__);
    73 	    
    74     myth_handle = g_new0 (MythtvHandle, 1);
    75 
    76     if (mode & GNOME_VFS_OPEN_WRITE) {
    77         return GNOME_VFS_ERROR_NOT_PERMITTED;
    78     }
    79 
    80     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    81     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    82     }
    83 
    84     /* Initialize mythtv handler*/
    85     myth_handle->file_transfer = NULL;
    86     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    87     myth_handle->bytes_read = 0;
    88     myth_handle->content_size = -1;
    89 
    90     /* Creates and fills out the backend info structure */
    91     backend_info = gmyth_backend_info_new_full (
    92 	gnome_vfs_uri_get_host_name (uri),
    93 	NULL, NULL, NULL,
    94 	gnome_vfs_uri_get_host_port (uri));
    95 
    96     /* Connect to the backend */
    97     myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
    98 
    99     g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   100 
   101     /* sets the Playback monitor connection */
   102     ret = gmyth_file_transfer_open (myth_handle->file_transfer, gnome_vfs_uri_get_path (uri));
   103     if (ret == FALSE) {
   104         g_printerr ("Mythtv FileTransfer open error\n");
   105         return GNOME_VFS_ERROR_NOT_OPEN;
   106     }
   107 
   108     // TODO: Verify if file exists in the backend
   109 
   110     myth_handle->content_size = myth_handle->file_transfer->filesize;
   111     
   112     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   113     myth_handle->buffer_remain = 0;
   114 
   115     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   116 
   117     return GNOME_VFS_OK;
   118 }
   119 
   120 static GnomeVFSResult
   121 do_read (GnomeVFSMethod *method,
   122          GnomeVFSMethodHandle *method_handle,
   123          gpointer buffer,
   124          GnomeVFSFileSize num_bytes,
   125          GnomeVFSFileSize *bytes_read,
   126          GnomeVFSContext *context)
   127 {
   128     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   129     GnomeVFSFileSize bytes_to_read;
   130 
   131     //g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_remain: %du", 
   132 	//	    myth_handle->content_size, myth_handle->bytes_read, num_bytes, 
   133 	//			myth_handle->buffer_remain);
   134 
   135     *bytes_read = 0;
   136 
   137     printf ("XXXXXXXXXXXXXXXXXXXX bytes_read: %d, content_size: %d\n", (int)myth_handle->bytes_read, (int)myth_handle->content_size);
   138     if (myth_handle->bytes_read >= myth_handle->content_size)
   139         return GNOME_VFS_ERROR_EOF;
   140 
   141     // fixme: change this to min math function
   142     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   143 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   144     else
   145 	    bytes_to_read = num_bytes;
   146 
   147     /* Loop sending the Myth File Transfer request:
   148     * Retry whilst authentication fails and we supply it. */
   149   
   150     if ( bytes_to_read > myth_handle->buffer_remain ) {
   151         GByteArray *tmp_buffer = g_byte_array_new();
   152 
   153         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   154               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   155 
   156 		if (len < 0) {
   157             g_byte_array_free (tmp_buffer, TRUE);
   158 		    return GNOME_VFS_ERROR_IO;
   159         }
   160 
   161         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   162     		tmp_buffer->data, len);
   163 
   164 		myth_handle->buffer_remain += len;
   165 
   166 		//g_debug ( "Reading %d data from backend", MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain );
   167 	
   168         g_byte_array_free (tmp_buffer, TRUE);
   169     	tmp_buffer = NULL;    	
   170     }
   171     
   172     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   173 
   174     //g_debug ("Returning %du bytes to gnomevfs", (gint) bytes_to_read);
   175 	  /* gets the first buffer_size bytes from the byte array buffer variable */ 
   176 
   177     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   178 
   179     myth_handle->bytes_read += bytes_to_read;
   180     myth_handle->buffer_remain -= bytes_to_read;    
   181 
   182   	/* flushs the newly buffer got from byte array */
   183   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   184     *bytes_read = bytes_to_read;
   185   
   186     return GNOME_VFS_OK;
   187 }
   188 
   189 static GnomeVFSResult
   190 do_close (GnomeVFSMethod *method,
   191           GnomeVFSMethodHandle *method_handle,
   192           GnomeVFSContext *context)
   193 {
   194 
   195     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   196 
   197     if (myth_handle->file_transfer) {
   198 	gmyth_file_transfer_close (myth_handle->file_transfer);
   199         g_object_unref (myth_handle->file_transfer);
   200 	myth_handle->file_transfer = NULL;
   201     }
   202     
   203     if (myth_handle->buffer) {
   204 			g_byte_array_free (myth_handle->buffer, TRUE);
   205 			myth_handle->buffer = NULL;
   206     }
   207 
   208     g_free (myth_handle);
   209 
   210     return GNOME_VFS_OK;
   211 }
   212 
   213 static GnomeVFSResult
   214 do_get_file_info (GnomeVFSMethod *method,
   215                   GnomeVFSURI *uri,
   216                   GnomeVFSFileInfo *file_info,
   217                   GnomeVFSFileInfoOptions options,
   218                   GnomeVFSContext *context)
   219 {
   220     file_info->name = g_strdup ("fixme.txt");
   221     file_info->valid_fields = file_info->valid_fields
   222         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   223         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   224         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   225     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   226     // fixme: get from file extension?
   227     file_info->mime_type = g_strdup ("video/x-nuv");
   228     file_info->permissions =
   229         GNOME_VFS_PERM_USER_READ |
   230         GNOME_VFS_PERM_OTHER_READ |
   231         GNOME_VFS_PERM_GROUP_READ;
   232 
   233     return GNOME_VFS_OK;
   234 }
   235 
   236 static gboolean
   237 do_is_local (GnomeVFSMethod *method,
   238              const GnomeVFSURI *uri)
   239 {
   240 	return FALSE;
   241 }
   242 
   243 static GnomeVFSMethod method = {
   244 	sizeof (GnomeVFSMethod),
   245 	do_open,
   246 	NULL,
   247 	do_close,
   248 	do_read,
   249 	NULL,
   250 	NULL,
   251 	NULL,
   252 	NULL,
   253 	NULL,
   254 	NULL,
   255 	NULL,
   256 	do_get_file_info,
   257 	NULL,
   258 	do_is_local,
   259 	NULL,
   260 	NULL,
   261 	NULL,
   262 	NULL,
   263 	NULL,
   264 	NULL,
   265 	NULL,
   266 	NULL,
   267 	NULL,
   268 	NULL,
   269 	NULL,
   270 	NULL,
   271 };
   272 
   273 
   274 GnomeVFSMethod *
   275 vfs_module_init (const char *method_name, const char *args)
   276 {
   277 	return &method;
   278 }
   279 
   280 void
   281 vfs_module_shutdown (GnomeVFSMethod *method)
   282 {
   283 }