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