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