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
melunko@38
     1
/*
melunko@38
     2
 * @author Hallyson Melo <hallyson.melo@indt.org.br>
melunko@38
     3
 *
melunko@38
     4
 * This program is free software; you can redistribute it and/or modify
melunko@38
     5
 * it under the terms of the GNU Lesser General Public License as published by
melunko@38
     6
 * the Free Software Foundation; either version 2 of the License, or
melunko@38
     7
 * (at your option) any later version.
melunko@38
     8
 *
melunko@38
     9
 * This program is distributed in the hope that it will be useful,
melunko@38
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
melunko@38
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
melunko@38
    12
 * GNU General Public License for more details.
melunko@38
    13
 *
melunko@38
    14
 * You should have received a copy of the GNU Lesser General Public License
melunko@38
    15
 * along with this program; if not, write to the Free Software
melunko@38
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
melunko@38
    17
 */
melunko@38
    18
melunko@38
    19
#ifdef HAVE_CONFIG_H
melunko@38
    20
#include <config.h>
melunko@38
    21
#endif
melunko@38
    22
melunko@38
    23
#include <string.h>
melunko@38
    24
#include <glib.h>
melunko@38
    25
#include <math.h>
melunko@38
    26
melunko@38
    27
#include <libgnomevfs/gnome-vfs-module.h>
melunko@38
    28
#include <libgnomevfs/gnome-vfs-utils.h>
melunko@38
    29
melunko@48
    30
#include "gmyth_file_transfer.h"
melunko@160
    31
#include "gmyth_backendinfo.h"
melunko@38
    32
melunko@38
    33
#define GST_MYTHTV_ID_NUM               1
melunko@38
    34
#define MYTHTV_VERSION_DEFAULT          30
melunko@38
    35
melunko@111
    36
#define MYTHTV_BUFFER_SIZE		1024*64
melunko@111
    37
melunko@38
    38
static GnomeVFSResult do_read (GnomeVFSMethod *method,
melunko@38
    39
                               GnomeVFSMethodHandle *method_handle,
melunko@38
    40
                               gpointer buffer,
melunko@38
    41
                               GnomeVFSFileSize num_bytes,
melunko@38
    42
                               GnomeVFSFileSize *bytes_read,
melunko@38
    43
                               GnomeVFSContext *context);
melunko@38
    44
melunko@38
    45
typedef struct {
melunko@48
    46
    GMythFileTransfer *file_transfer;
melunko@38
    47
    
melunko@38
    48
    gint mythtv_version;
melunko@38
    49
    guint64 content_size;
melunko@38
    50
    guint64 bytes_read;
melunko@111
    51
rosfran@116
    52
    GByteArray *buffer;
melunko@111
    53
    gsize buffer_remain;
melunko@38
    54
} MythtvHandle;
melunko@38
    55
melunko@38
    56
melunko@38
    57
melunko@38
    58
static GnomeVFSResult
melunko@38
    59
do_open (GnomeVFSMethod *method,
melunko@38
    60
         GnomeVFSMethodHandle **method_handle,
melunko@38
    61
         GnomeVFSURI *uri,
melunko@38
    62
         GnomeVFSOpenMode mode,
melunko@38
    63
         GnomeVFSContext *context)
melunko@38
    64
{
melunko@111
    65
    MythtvHandle *myth_handle;
melunko@160
    66
    GMythBackendInfo *backend_info;
melunko@38
    67
    gboolean ret;
melunko@38
    68
melunko@38
    69
    _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
melunko@38
    70
    _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
melunko@38
    71
melunko@111
    72
    myth_handle = g_new0 (MythtvHandle, 1);
melunko@111
    73
melunko@38
    74
    if (mode & GNOME_VFS_OPEN_WRITE) {
melunko@38
    75
        return GNOME_VFS_ERROR_NOT_PERMITTED;
melunko@38
    76
    }
melunko@38
    77
renatofilho@149
    78
    if (gnome_vfs_uri_get_host_name (uri) == NULL) {
renatofilho@149
    79
    	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
melunko@38
    80
    }
melunko@38
    81
melunko@38
    82
    /* Initialize mythtv handler*/
melunko@38
    83
    myth_handle->file_transfer = NULL;
melunko@38
    84
    myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
melunko@38
    85
    myth_handle->bytes_read = 0;
melunko@38
    86
    myth_handle->content_size = -1;
melunko@38
    87
melunko@160
    88
    /* Creates and fills out the backend info structure */
melunko@160
    89
    backend_info = gmyth_backend_info_new_full (
renatofilho@164
    90
        gnome_vfs_uri_get_host_name (uri),
renatofilho@164
    91
	    NULL, NULL, NULL,
renatofilho@164
    92
	    gnome_vfs_uri_get_host_port (uri));
rosfran@180
    93
	    
rosfran@180
    94
	  gmyth_backend_info_set_path ( backend_info, gnome_vfs_uri_get_path (uri) );
melunko@160
    95
melunko@38
    96
    /* Connect to the backend */
melunko@160
    97
    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
renatofilho@188
    98
    g_object_unref (backend_info);
melunko@38
    99
melunko@111
   100
    g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
melunko@38
   101
melunko@38
   102
    /* sets the Playback monitor connection */
renatofilho@188
   103
    ret = gmyth_file_transfer_open (myth_handle->file_transfer, gnome_vfs_uri_get_path (uri));
melunko@38
   104
    if (ret == FALSE) {
renatofilho@164
   105
        g_warning ("Mythtv FileTransfer open error\n");
melunko@111
   106
        return GNOME_VFS_ERROR_NOT_OPEN;
melunko@38
   107
    }
melunko@38
   108
melunko@38
   109
    // TODO: Verify if file exists in the backend
melunko@38
   110
melunko@38
   111
    myth_handle->content_size = myth_handle->file_transfer->filesize;
melunko@111
   112
    
rosfran@116
   113
    myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
melunko@111
   114
    myth_handle->buffer_remain = 0;
melunko@38
   115
melunko@38
   116
    *method_handle = (GnomeVFSMethodHandle *) myth_handle;
melunko@38
   117
melunko@38
   118
    return GNOME_VFS_OK;
melunko@38
   119
}
melunko@38
   120
melunko@38
   121
static GnomeVFSResult
melunko@38
   122
do_read (GnomeVFSMethod *method,
melunko@38
   123
         GnomeVFSMethodHandle *method_handle,
melunko@38
   124
         gpointer buffer,
melunko@38
   125
         GnomeVFSFileSize num_bytes,
melunko@38
   126
         GnomeVFSFileSize *bytes_read,
melunko@38
   127
         GnomeVFSContext *context)
melunko@38
   128
{
melunko@38
   129
    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
melunko@38
   130
    GnomeVFSFileSize bytes_to_read;
melunko@38
   131
melunko@38
   132
    *bytes_read = 0;
melunko@38
   133
melunko@38
   134
    if (myth_handle->bytes_read >= myth_handle->content_size)
melunko@38
   135
        return GNOME_VFS_ERROR_EOF;
melunko@38
   136
melunko@38
   137
    // fixme: change this to min math function
melunko@38
   138
    if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
melunko@38
   139
	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
melunko@38
   140
    else
melunko@38
   141
	    bytes_to_read = num_bytes;
melunko@38
   142
melunko@38
   143
    /* Loop sending the Myth File Transfer request:
melunko@38
   144
    * Retry whilst authentication fails and we supply it. */
rosfran@116
   145
  
renatofilho@149
   146
    if ( bytes_to_read > myth_handle->buffer_remain ) {
renatofilho@149
   147
        GByteArray *tmp_buffer = g_byte_array_new();
melunko@38
   148
renatofilho@149
   149
        gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
renatofilho@149
   150
              tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
melunko@38
   151
renatofilho@149
   152
		if (len < 0) {
renatofilho@149
   153
            g_byte_array_free (tmp_buffer, TRUE);
renatofilho@164
   154
            g_warning ("Fail to read bytes");
rosfran@116
   155
		    return GNOME_VFS_ERROR_IO;
renatofilho@149
   156
        }
melunko@111
   157
renatofilho@149
   158
        myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
renatofilho@149
   159
    		tmp_buffer->data, len);
rosfran@116
   160
rosfran@116
   161
		myth_handle->buffer_remain += len;
rosfran@116
   162
renatofilho@169
   163
        g_byte_array_free (tmp_buffer, TRUE);
rosfran@116
   164
    	tmp_buffer = NULL;    	
rosfran@116
   165
    }
melunko@38
   166
    
melunko@111
   167
    bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
renatofilho@164
   168
    /* gets the first buffer_size bytes from the byte array buffer variable */ 
rosfran@116
   169
renatofilho@149
   170
    g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
rosfran@116
   171
melunko@111
   172
    myth_handle->bytes_read += bytes_to_read;
rosfran@116
   173
    myth_handle->buffer_remain -= bytes_to_read;    
rosfran@116
   174
rosfran@116
   175
  	/* flushs the newly buffer got from byte array */
renatofilho@149
   176
  	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
melunko@111
   177
    *bytes_read = bytes_to_read;
melunko@38
   178
  
melunko@38
   179
    return GNOME_VFS_OK;
melunko@38
   180
}
melunko@38
   181
melunko@38
   182
static GnomeVFSResult
melunko@38
   183
do_close (GnomeVFSMethod *method,
melunko@38
   184
          GnomeVFSMethodHandle *method_handle,
melunko@38
   185
          GnomeVFSContext *context)
melunko@38
   186
{
melunko@111
   187
melunko@38
   188
    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
melunko@38
   189
melunko@111
   190
    if (myth_handle->file_transfer) {
renatofilho@164
   191
	    gmyth_file_transfer_close (myth_handle->file_transfer);
melunko@38
   192
        g_object_unref (myth_handle->file_transfer);
renatofilho@164
   193
    	myth_handle->file_transfer = NULL;
melunko@111
   194
    }
rosfran@127
   195
    
rosfran@127
   196
    if (myth_handle->buffer) {
renatofilho@164
   197
		g_byte_array_free (myth_handle->buffer, TRUE);
renatofilho@164
   198
		myth_handle->buffer = NULL;
rosfran@127
   199
    }
melunko@38
   200
melunko@38
   201
    g_free (myth_handle);
melunko@38
   202
melunko@38
   203
    return GNOME_VFS_OK;
melunko@38
   204
}
melunko@38
   205
melunko@38
   206
static GnomeVFSResult
melunko@38
   207
do_get_file_info (GnomeVFSMethod *method,
melunko@38
   208
                  GnomeVFSURI *uri,
melunko@38
   209
                  GnomeVFSFileInfo *file_info,
melunko@38
   210
                  GnomeVFSFileInfoOptions options,
melunko@38
   211
                  GnomeVFSContext *context)
melunko@38
   212
{
renatofilho@188
   213
    GMythFileTransfer *file_transfer = NULL;
renatofilho@188
   214
    GMythBackendInfo *backend_info = NULL;
renatofilho@188
   215
renatofilho@188
   216
    file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
melunko@38
   217
    file_info->valid_fields = file_info->valid_fields
melunko@38
   218
        | GNOME_VFS_FILE_INFO_FIELDS_TYPE
melunko@38
   219
        | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
melunko@38
   220
        | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
melunko@38
   221
    file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
melunko@111
   222
    // fixme: get from file extension?
melunko@111
   223
    file_info->mime_type = g_strdup ("video/x-nuv");
melunko@38
   224
    file_info->permissions =
melunko@38
   225
        GNOME_VFS_PERM_USER_READ |
melunko@38
   226
        GNOME_VFS_PERM_OTHER_READ |
melunko@38
   227
        GNOME_VFS_PERM_GROUP_READ;
melunko@38
   228
renatofilho@188
   229
    backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
renatofilho@188
   230
            gnome_vfs_uri_get_user_name (uri),
renatofilho@188
   231
            gnome_vfs_uri_get_password (uri),
renatofilho@188
   232
            NULL,
renatofilho@188
   233
            gnome_vfs_uri_get_host_port (uri));
renatofilho@188
   234
renatofilho@188
   235
    file_transfer = gmyth_file_transfer_new (backend_info);
renatofilho@188
   236
    if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
renatofilho@188
   237
        file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
renatofilho@188
   238
        file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
renatofilho@188
   239
        file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
renatofilho@188
   240
    }
renatofilho@188
   241
renatofilho@188
   242
    g_object_unref (file_transfer);
renatofilho@188
   243
    g_object_unref (backend_info);
melunko@38
   244
    return GNOME_VFS_OK;
melunko@38
   245
}
melunko@38
   246
melunko@38
   247
static gboolean
melunko@38
   248
do_is_local (GnomeVFSMethod *method,
melunko@38
   249
             const GnomeVFSURI *uri)
melunko@38
   250
{
melunko@38
   251
	return FALSE;
melunko@38
   252
}
melunko@38
   253
melunko@38
   254
static GnomeVFSMethod method = {
melunko@38
   255
	sizeof (GnomeVFSMethod),
melunko@38
   256
	do_open,
melunko@38
   257
	NULL,
melunko@38
   258
	do_close,
melunko@38
   259
	do_read,
melunko@38
   260
	NULL,
melunko@38
   261
	NULL,
melunko@38
   262
	NULL,
melunko@38
   263
	NULL,
melunko@38
   264
	NULL,
melunko@38
   265
	NULL,
melunko@38
   266
	NULL,
melunko@38
   267
	do_get_file_info,
melunko@38
   268
	NULL,
melunko@38
   269
	do_is_local,
melunko@38
   270
	NULL,
melunko@38
   271
	NULL,
melunko@38
   272
	NULL,
melunko@38
   273
	NULL,
melunko@38
   274
	NULL,
melunko@38
   275
	NULL,
melunko@38
   276
	NULL,
melunko@38
   277
	NULL,
melunko@38
   278
	NULL,
melunko@38
   279
	NULL,
melunko@38
   280
	NULL,
melunko@38
   281
	NULL,
melunko@38
   282
};
melunko@38
   283
melunko@38
   284
melunko@38
   285
GnomeVFSMethod *
melunko@38
   286
vfs_module_init (const char *method_name, const char *args)
melunko@38
   287
{
melunko@38
   288
	return &method;
melunko@38
   289
}
melunko@38
   290
melunko@38
   291
void
melunko@38
   292
vfs_module_shutdown (GnomeVFSMethod *method)
melunko@38
   293
{
melunko@38
   294
}