libgnomevfs2-mythtv/modules/mythtv-method.c
author renatofilho
Fri Dec 01 19:41:39 2006 +0000 (2006-12-01)
branchtrunk
changeset 171 dda330ac986f
parent 164 81e9da9ad7db
child 180 60033c7d3bab
permissions -rwxr-xr-x
[svn r172]
     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     /* Connect to the backend */
    95     myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
    96 
    97     g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
    98 
    99     /* sets the Playback monitor connection */
   100     ret = gmyth_file_transfer_open (myth_handle->file_transfer, gnome_vfs_uri_get_path (uri));
   101     if (ret == FALSE) {
   102         g_warning ("Mythtv FileTransfer open error\n");
   103         return GNOME_VFS_ERROR_NOT_OPEN;
   104     }
   105 
   106     // TODO: Verify if file exists in the backend
   107 
   108     myth_handle->content_size = myth_handle->file_transfer->filesize;
   109     
   110     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   111     myth_handle->buffer_remain = 0;
   112 
   113     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   114 
   115     return GNOME_VFS_OK;
   116 }
   117 
   118 static GnomeVFSResult
   119 do_read (GnomeVFSMethod *method,
   120          GnomeVFSMethodHandle *method_handle,
   121          gpointer buffer,
   122          GnomeVFSFileSize num_bytes,
   123          GnomeVFSFileSize *bytes_read,
   124          GnomeVFSContext *context)
   125 {
   126     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   127     GnomeVFSFileSize bytes_to_read;
   128 
   129     *bytes_read = 0;
   130 
   131     if (myth_handle->bytes_read >= myth_handle->content_size)
   132         return GNOME_VFS_ERROR_EOF;
   133 
   134     // fixme: change this to min math function
   135     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   136 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   137     else
   138 	    bytes_to_read = num_bytes;
   139 
   140     /* Loop sending the Myth File Transfer request:
   141     * Retry whilst authentication fails and we supply it. */
   142   
   143     if ( bytes_to_read > myth_handle->buffer_remain ) {
   144         GByteArray *tmp_buffer = g_byte_array_new();
   145 
   146         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   147               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   148 
   149 		if (len < 0) {
   150             g_byte_array_free (tmp_buffer, TRUE);
   151             g_warning ("Fail to read bytes");
   152 		    return GNOME_VFS_ERROR_IO;
   153         }
   154 
   155         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   156     		tmp_buffer->data, len);
   157 
   158 		myth_handle->buffer_remain += len;
   159 
   160         g_byte_array_free (tmp_buffer, TRUE);
   161     	tmp_buffer = NULL;    	
   162     }
   163     
   164     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   165     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   166 
   167     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   168 
   169     myth_handle->bytes_read += bytes_to_read;
   170     myth_handle->buffer_remain -= bytes_to_read;    
   171 
   172   	/* flushs the newly buffer got from byte array */
   173   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   174     *bytes_read = bytes_to_read;
   175   
   176     return GNOME_VFS_OK;
   177 }
   178 
   179 static GnomeVFSResult
   180 do_close (GnomeVFSMethod *method,
   181           GnomeVFSMethodHandle *method_handle,
   182           GnomeVFSContext *context)
   183 {
   184 
   185     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   186 
   187     if (myth_handle->file_transfer) {
   188 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   189         g_object_unref (myth_handle->file_transfer);
   190     	myth_handle->file_transfer = NULL;
   191     }
   192     
   193     if (myth_handle->buffer) {
   194 		g_byte_array_free (myth_handle->buffer, TRUE);
   195 		myth_handle->buffer = NULL;
   196     }
   197 
   198     g_free (myth_handle);
   199 
   200     return GNOME_VFS_OK;
   201 }
   202 
   203 static GnomeVFSResult
   204 do_get_file_info (GnomeVFSMethod *method,
   205                   GnomeVFSURI *uri,
   206                   GnomeVFSFileInfo *file_info,
   207                   GnomeVFSFileInfoOptions options,
   208                   GnomeVFSContext *context)
   209 {
   210     file_info->name = g_strdup ("fixme.txt");
   211     file_info->valid_fields = file_info->valid_fields
   212         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   213         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   214         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   215     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   216     // fixme: get from file extension?
   217     file_info->mime_type = g_strdup ("video/x-nuv");
   218     file_info->permissions =
   219         GNOME_VFS_PERM_USER_READ |
   220         GNOME_VFS_PERM_OTHER_READ |
   221         GNOME_VFS_PERM_GROUP_READ;
   222 
   223     return GNOME_VFS_OK;
   224 }
   225 
   226 static gboolean
   227 do_is_local (GnomeVFSMethod *method,
   228              const GnomeVFSURI *uri)
   229 {
   230 	return FALSE;
   231 }
   232 
   233 static GnomeVFSMethod method = {
   234 	sizeof (GnomeVFSMethod),
   235 	do_open,
   236 	NULL,
   237 	do_close,
   238 	do_read,
   239 	NULL,
   240 	NULL,
   241 	NULL,
   242 	NULL,
   243 	NULL,
   244 	NULL,
   245 	NULL,
   246 	do_get_file_info,
   247 	NULL,
   248 	do_is_local,
   249 	NULL,
   250 	NULL,
   251 	NULL,
   252 	NULL,
   253 	NULL,
   254 	NULL,
   255 	NULL,
   256 	NULL,
   257 	NULL,
   258 	NULL,
   259 	NULL,
   260 	NULL,
   261 };
   262 
   263 
   264 GnomeVFSMethod *
   265 vfs_module_init (const char *method_name, const char *args)
   266 {
   267 	return &method;
   268 }
   269 
   270 void
   271 vfs_module_shutdown (GnomeVFSMethod *method)
   272 {
   273 }