libgnomevfs2-mythtv/modules/mythtv-method.c
branchtrunk
changeset 45 5c83cb526118
child 48 a30f59f76bda
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libgnomevfs2-mythtv/modules/mythtv-method.c	Tue Oct 24 14:19:49 2006 +0100
     1.3 @@ -0,0 +1,268 @@
     1.4 +/*
     1.5 + * @author Hallyson Melo <hallyson.melo@indt.org.br>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU Lesser General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU Lesser General Public License
    1.18 + * along with this program; if not, write to the Free Software
    1.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 + */
    1.21 +
    1.22 +#ifdef HAVE_CONFIG_H
    1.23 +#include <config.h>
    1.24 +#endif
    1.25 +
    1.26 +#include <string.h>
    1.27 +#include <glib.h>
    1.28 +#include <math.h>
    1.29 +
    1.30 +#include <libgnomevfs/gnome-vfs-module.h>
    1.31 +#include <libgnomevfs/gnome-vfs-utils.h>
    1.32 +
    1.33 +#include "myth_file_transfer.h"
    1.34 +
    1.35 +#define GST_MYTHTV_ID_NUM               1
    1.36 +#define MYTHTV_VERSION_DEFAULT          30
    1.37 +
    1.38 +static GnomeVFSResult do_read (GnomeVFSMethod *method,
    1.39 +                               GnomeVFSMethodHandle *method_handle,
    1.40 +                               gpointer buffer,
    1.41 +                               GnomeVFSFileSize num_bytes,
    1.42 +                               GnomeVFSFileSize *bytes_read,
    1.43 +                               GnomeVFSContext *context);
    1.44 +
    1.45 +typedef struct {
    1.46 +    MythFileTransfer *file_transfer;
    1.47 +    
    1.48 +    gint mythtv_version;
    1.49 +    guint64 content_size;
    1.50 +    guint64 bytes_read;
    1.51 +    guint64 read_offset;
    1.52 +    gboolean live_tv;
    1.53 +} MythtvHandle;
    1.54 +
    1.55 +
    1.56 +
    1.57 +static GnomeVFSResult
    1.58 +do_open (GnomeVFSMethod *method,
    1.59 +         GnomeVFSMethodHandle **method_handle,
    1.60 +         GnomeVFSURI *uri,
    1.61 +         GnomeVFSOpenMode mode,
    1.62 +         GnomeVFSContext *context)
    1.63 +{
    1.64 +    MythtvHandle *myth_handle = g_new0 (MythtvHandle, 1);
    1.65 +
    1.66 +    const gchar *user, *password, *host, *transfer_uri;
    1.67 +    guint port;
    1.68 +    gchar *path;
    1.69 +    gboolean ret;
    1.70 +
    1.71 +    _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    1.72 +    _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    1.73 +
    1.74 +    if (mode & GNOME_VFS_OPEN_WRITE) {
    1.75 +        return GNOME_VFS_ERROR_NOT_PERMITTED;
    1.76 +    }
    1.77 +
    1.78 +    if ((host = gnome_vfs_uri_get_host_name (uri)) == NULL) {
    1.79 +	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    1.80 +    }
    1.81 +
    1.82 +    /* Parse URI */
    1.83 +    path = gnome_vfs_unescape_string (uri->text, NULL);
    1.84 +    port = gnome_vfs_uri_get_host_port (uri);
    1.85 +    user = gnome_vfs_uri_get_user_name (uri);
    1.86 +    password = gnome_vfs_uri_get_password (uri);
    1.87 +
    1.88 +    transfer_uri = gnome_vfs_uri_to_string (uri, 
    1.89 +		    GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD);
    1.90 +   
    1.91 +    /* Initialize mythtv handler*/
    1.92 +    myth_handle->file_transfer = NULL;
    1.93 +    myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    1.94 +    myth_handle->bytes_read = 0;
    1.95 +    myth_handle->content_size = -1;
    1.96 +    myth_handle->read_offset = 0;
    1.97 +    myth_handle->live_tv = FALSE;
    1.98 +
    1.99 +    /* Connect to the backend */
   1.100 +    myth_handle->file_transfer = myth_file_transfer_new ( GST_MYTHTV_ID_NUM /*mythtv->live_tv_id*/,
   1.101 +        g_string_new( transfer_uri ), -1, myth_handle->mythtv_version );
   1.102 +
   1.103 +    if ( myth_handle->file_transfer == NULL ) {
   1.104 +	return GNOME_VFS_ERROR_NOT_OPEN;
   1.105 +    }
   1.106 +
   1.107 +    /* sets the Playback monitor connection */
   1.108 +    ret = myth_file_transfer_playback_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
   1.109 +    if (ret == FALSE) {
   1.110 +        g_printerr ("Mythtv FileTransfer playback setup error\n");
   1.111 +	return GNOME_VFS_ERROR_NOT_OPEN;
   1.112 +    }
   1.113 +      
   1.114 +    /* sets the FileTransfer instance connection (video/audio download) */
   1.115 +    ret = myth_file_transfer_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
   1.116 +
   1.117 +    if ( ret == FALSE ) {
   1.118 +      g_printerr ("MythTV FileTransfer request failed when setting up socket connection!\n" );
   1.119 +      return GNOME_VFS_ERROR_NOT_OPEN;
   1.120 +    }
   1.121 +
   1.122 +    // TODO: Verify if file exists in the backend
   1.123 +
   1.124 +    myth_handle->content_size = myth_handle->file_transfer->filesize;
   1.125 +
   1.126 +    *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   1.127 +
   1.128 +    return GNOME_VFS_OK;
   1.129 +}
   1.130 +
   1.131 +static GnomeVFSResult
   1.132 +do_read (GnomeVFSMethod *method,
   1.133 +         GnomeVFSMethodHandle *method_handle,
   1.134 +         gpointer buffer,
   1.135 +         GnomeVFSFileSize num_bytes,
   1.136 +         GnomeVFSFileSize *bytes_read,
   1.137 +         GnomeVFSContext *context)
   1.138 +{
   1.139 +    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   1.140 +    guint len = 0, offset = 0;
   1.141 +    GnomeVFSFileSize bytes_to_read;
   1.142 +
   1.143 +    g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu", myth_handle->content_size,
   1.144 +		    myth_handle->bytes_read, num_bytes);
   1.145 +
   1.146 +    *bytes_read = 0;
   1.147 +
   1.148 +    if (myth_handle->bytes_read >= myth_handle->content_size)
   1.149 +        return GNOME_VFS_ERROR_EOF;
   1.150 +
   1.151 +    // fixme: change this to min math function
   1.152 +    if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   1.153 +	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   1.154 +    else
   1.155 +	    bytes_to_read = num_bytes;
   1.156 +
   1.157 +    /* Loop sending the Myth File Transfer request:
   1.158 +    * Retry whilst authentication fails and we supply it. */
   1.159 +
   1.160 +    // lock here ???
   1.161 +    while ( bytes_to_read > 0 ) {
   1.162 +  	len = myth_file_transfer_read( myth_handle->file_transfer,
   1.163 +			buffer + offset, bytes_to_read, TRUE );
   1.164 +
   1.165 +        if ( len > 0 ) {
   1.166 +            offset += len;      
   1.167 +            bytes_to_read -= len;
   1.168 +        } else {
   1.169 +	    break;
   1.170 +        }
   1.171 +    
   1.172 +        if ( offset == num_bytes )
   1.173 +          break;
   1.174 +    }
   1.175 +  
   1.176 +    if (( offset <= 0 ) && !myth_handle->live_tv )
   1.177 +        return GNOME_VFS_ERROR_EOF;
   1.178 +  
   1.179 +    myth_handle->read_offset += offset;
   1.180 +    myth_handle->bytes_read += offset;
   1.181 +    *bytes_read = offset;
   1.182 +  
   1.183 +    return GNOME_VFS_OK;
   1.184 +}
   1.185 +
   1.186 +static GnomeVFSResult
   1.187 +do_close (GnomeVFSMethod *method,
   1.188 +          GnomeVFSMethodHandle *method_handle,
   1.189 +          GnomeVFSContext *context)
   1.190 +{
   1.191 +    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   1.192 +
   1.193 +    if (myth_handle->file_transfer)
   1.194 +        g_object_unref (myth_handle->file_transfer);
   1.195 +
   1.196 +    g_free (myth_handle);
   1.197 +
   1.198 +    return GNOME_VFS_OK;
   1.199 +}
   1.200 +
   1.201 +static GnomeVFSResult
   1.202 +do_get_file_info (GnomeVFSMethod *method,
   1.203 +                  GnomeVFSURI *uri,
   1.204 +                  GnomeVFSFileInfo *file_info,
   1.205 +                  GnomeVFSFileInfoOptions options,
   1.206 +                  GnomeVFSContext *context)
   1.207 +{
   1.208 +    GString *name = g_string_new ("hallyson");	
   1.209 +    file_info->name = g_string_free (name, FALSE);//"hallyson.txt";//get_base_from_uri (uri);
   1.210 +    file_info->valid_fields = file_info->valid_fields
   1.211 +        | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   1.212 +        | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   1.213 +        | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   1.214 +    file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   1.215 +    file_info->mime_type = "video/x-nuv"; //g_strdup (gnome_vfs_mime_type_from_name (file_info->name));
   1.216 +    file_info->permissions =
   1.217 +        GNOME_VFS_PERM_USER_READ |
   1.218 +        GNOME_VFS_PERM_OTHER_READ |
   1.219 +        GNOME_VFS_PERM_GROUP_READ;
   1.220 +
   1.221 +    return GNOME_VFS_OK;
   1.222 +}
   1.223 +
   1.224 +static gboolean
   1.225 +do_is_local (GnomeVFSMethod *method,
   1.226 +             const GnomeVFSURI *uri)
   1.227 +{
   1.228 +	return FALSE;
   1.229 +}
   1.230 +
   1.231 +static GnomeVFSMethod method = {
   1.232 +	sizeof (GnomeVFSMethod),
   1.233 +	do_open,
   1.234 +	NULL,
   1.235 +	do_close,
   1.236 +	do_read,
   1.237 +	NULL,
   1.238 +	NULL,
   1.239 +	NULL,
   1.240 +	NULL,
   1.241 +	NULL,
   1.242 +	NULL,
   1.243 +	NULL,
   1.244 +	do_get_file_info,
   1.245 +	NULL,
   1.246 +	do_is_local,
   1.247 +	NULL,
   1.248 +	NULL,
   1.249 +	NULL,
   1.250 +	NULL,
   1.251 +	NULL,
   1.252 +	NULL,
   1.253 +	NULL,
   1.254 +	NULL,
   1.255 +	NULL,
   1.256 +	NULL,
   1.257 +	NULL,
   1.258 +	NULL,
   1.259 +};
   1.260 +
   1.261 +
   1.262 +GnomeVFSMethod *
   1.263 +vfs_module_init (const char *method_name, const char *args)
   1.264 +{
   1.265 +	return &method;
   1.266 +}
   1.267 +
   1.268 +void
   1.269 +vfs_module_shutdown (GnomeVFSMethod *method)
   1.270 +{
   1.271 +}