libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Fri Oct 27 23:42:34 2006 +0100 (2006-10-27)
branchtrunk
changeset 62 2a552f003c4e
parent 38 d5f5855e7800
child 111 dfa72795bd32
permissions -rwxr-xr-x
[svn r63] Some fixes.
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@38
    35
static GnomeVFSResult do_read (GnomeVFSMethod *method,
melunko@38
    36
                               GnomeVFSMethodHandle *method_handle,
melunko@38
    37
                               gpointer buffer,
melunko@38
    38
                               GnomeVFSFileSize num_bytes,
melunko@38
    39
                               GnomeVFSFileSize *bytes_read,
melunko@38
    40
                               GnomeVFSContext *context);
melunko@38
    41
melunko@38
    42
typedef struct {
melunko@48
    43
    GMythFileTransfer *file_transfer;
melunko@38
    44
    
melunko@38
    45
    gint mythtv_version;
melunko@38
    46
    guint64 content_size;
melunko@38
    47
    guint64 bytes_read;
melunko@38
    48
    guint64 read_offset;
melunko@38
    49
    gboolean live_tv;
melunko@38
    50
} MythtvHandle;
melunko@38
    51
melunko@38
    52
melunko@38
    53
melunko@38
    54
static GnomeVFSResult
melunko@38
    55
do_open (GnomeVFSMethod *method,
melunko@38
    56
         GnomeVFSMethodHandle **method_handle,
melunko@38
    57
         GnomeVFSURI *uri,
melunko@38
    58
         GnomeVFSOpenMode mode,
melunko@38
    59
         GnomeVFSContext *context)
melunko@38
    60
{
melunko@38
    61
    MythtvHandle *myth_handle = g_new0 (MythtvHandle, 1);
melunko@38
    62
melunko@38
    63
    const gchar *user, *password, *host, *transfer_uri;
melunko@38
    64
    guint port;
melunko@38
    65
    gchar *path;
melunko@38
    66
    gboolean ret;
melunko@38
    67
melunko@38
    68
    _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
melunko@38
    69
    _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
melunko@38
    70
melunko@38
    71
    if (mode & GNOME_VFS_OPEN_WRITE) {
melunko@38
    72
        return GNOME_VFS_ERROR_NOT_PERMITTED;
melunko@38
    73
    }
melunko@38
    74
melunko@38
    75
    if ((host = gnome_vfs_uri_get_host_name (uri)) == NULL) {
melunko@38
    76
	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
melunko@38
    77
    }
melunko@38
    78
melunko@38
    79
    /* Parse URI */
melunko@38
    80
    path = gnome_vfs_unescape_string (uri->text, NULL);
melunko@38
    81
    port = gnome_vfs_uri_get_host_port (uri);
melunko@38
    82
    user = gnome_vfs_uri_get_user_name (uri);
melunko@38
    83
    password = gnome_vfs_uri_get_password (uri);
melunko@38
    84
melunko@38
    85
    transfer_uri = gnome_vfs_uri_to_string (uri, 
melunko@38
    86
		    GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD);
melunko@38
    87
   
melunko@38
    88
    /* Initialize mythtv handler*/
melunko@38
    89
    myth_handle->file_transfer = NULL;
melunko@38
    90
    myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
melunko@38
    91
    myth_handle->bytes_read = 0;
melunko@38
    92
    myth_handle->content_size = -1;
melunko@38
    93
    myth_handle->read_offset = 0;
melunko@38
    94
    myth_handle->live_tv = FALSE;
melunko@38
    95
melunko@38
    96
    /* Connect to the backend */
melunko@48
    97
    myth_handle->file_transfer = gmyth_file_transfer_new ( GST_MYTHTV_ID_NUM /*mythtv->live_tv_id*/,
melunko@38
    98
        g_string_new( transfer_uri ), -1, myth_handle->mythtv_version );
melunko@38
    99
melunko@38
   100
    if ( myth_handle->file_transfer == NULL ) {
melunko@38
   101
	return GNOME_VFS_ERROR_NOT_OPEN;
melunko@38
   102
    }
melunko@38
   103
melunko@38
   104
    /* sets the Playback monitor connection */
melunko@48
   105
    ret = gmyth_file_transfer_playback_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
melunko@38
   106
    if (ret == FALSE) {
melunko@38
   107
        g_printerr ("Mythtv FileTransfer playback setup error\n");
melunko@38
   108
	return GNOME_VFS_ERROR_NOT_OPEN;
melunko@38
   109
    }
melunko@38
   110
      
melunko@38
   111
    /* sets the FileTransfer instance connection (video/audio download) */
melunko@48
   112
    ret = gmyth_file_transfer_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
melunko@38
   113
melunko@38
   114
    if ( ret == FALSE ) {
melunko@38
   115
      g_printerr ("MythTV FileTransfer request failed when setting up socket connection!\n" );
melunko@38
   116
      return GNOME_VFS_ERROR_NOT_OPEN;
melunko@38
   117
    }
melunko@38
   118
melunko@38
   119
    // TODO: Verify if file exists in the backend
melunko@38
   120
melunko@38
   121
    myth_handle->content_size = myth_handle->file_transfer->filesize;
melunko@38
   122
melunko@38
   123
    *method_handle = (GnomeVFSMethodHandle *) myth_handle;
melunko@38
   124
melunko@38
   125
    return GNOME_VFS_OK;
melunko@38
   126
}
melunko@38
   127
melunko@38
   128
static GnomeVFSResult
melunko@38
   129
do_read (GnomeVFSMethod *method,
melunko@38
   130
         GnomeVFSMethodHandle *method_handle,
melunko@38
   131
         gpointer buffer,
melunko@38
   132
         GnomeVFSFileSize num_bytes,
melunko@38
   133
         GnomeVFSFileSize *bytes_read,
melunko@38
   134
         GnomeVFSContext *context)
melunko@38
   135
{
melunko@38
   136
    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
melunko@38
   137
    guint len = 0, offset = 0;
melunko@38
   138
    GnomeVFSFileSize bytes_to_read;
melunko@38
   139
melunko@38
   140
    g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu", myth_handle->content_size,
melunko@38
   141
		    myth_handle->bytes_read, num_bytes);
melunko@38
   142
melunko@38
   143
    *bytes_read = 0;
melunko@38
   144
melunko@38
   145
    if (myth_handle->bytes_read >= myth_handle->content_size)
melunko@38
   146
        return GNOME_VFS_ERROR_EOF;
melunko@38
   147
melunko@38
   148
    // fixme: change this to min math function
melunko@38
   149
    if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
melunko@38
   150
	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
melunko@38
   151
    else
melunko@38
   152
	    bytes_to_read = num_bytes;
melunko@38
   153
melunko@38
   154
    /* Loop sending the Myth File Transfer request:
melunko@38
   155
    * Retry whilst authentication fails and we supply it. */
melunko@38
   156
melunko@38
   157
    // lock here ???
melunko@38
   158
    while ( bytes_to_read > 0 ) {
melunko@48
   159
  	len = gmyth_file_transfer_read( myth_handle->file_transfer,
melunko@38
   160
			buffer + offset, bytes_to_read, TRUE );
melunko@38
   161
melunko@38
   162
        if ( len > 0 ) {
melunko@38
   163
            offset += len;      
melunko@38
   164
            bytes_to_read -= len;
melunko@38
   165
        } else {
melunko@38
   166
	    break;
melunko@38
   167
        }
melunko@38
   168
    
melunko@38
   169
        if ( offset == num_bytes )
melunko@38
   170
          break;
melunko@38
   171
    }
melunko@38
   172
  
melunko@38
   173
    if (( offset <= 0 ) && !myth_handle->live_tv )
melunko@38
   174
        return GNOME_VFS_ERROR_EOF;
melunko@38
   175
  
melunko@38
   176
    myth_handle->read_offset += offset;
melunko@38
   177
    myth_handle->bytes_read += offset;
melunko@38
   178
    *bytes_read = offset;
melunko@38
   179
  
melunko@38
   180
    return GNOME_VFS_OK;
melunko@38
   181
}
melunko@38
   182
melunko@38
   183
static GnomeVFSResult
melunko@38
   184
do_close (GnomeVFSMethod *method,
melunko@38
   185
          GnomeVFSMethodHandle *method_handle,
melunko@38
   186
          GnomeVFSContext *context)
melunko@38
   187
{
melunko@38
   188
    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
melunko@38
   189
melunko@38
   190
    if (myth_handle->file_transfer)
melunko@38
   191
        g_object_unref (myth_handle->file_transfer);
melunko@38
   192
melunko@38
   193
    g_free (myth_handle);
melunko@38
   194
melunko@38
   195
    return GNOME_VFS_OK;
melunko@38
   196
}
melunko@38
   197
melunko@38
   198
static GnomeVFSResult
melunko@38
   199
do_get_file_info (GnomeVFSMethod *method,
melunko@38
   200
                  GnomeVFSURI *uri,
melunko@38
   201
                  GnomeVFSFileInfo *file_info,
melunko@38
   202
                  GnomeVFSFileInfoOptions options,
melunko@38
   203
                  GnomeVFSContext *context)
melunko@38
   204
{
melunko@38
   205
    GString *name = g_string_new ("hallyson");	
melunko@38
   206
    file_info->name = g_string_free (name, FALSE);//"hallyson.txt";//get_base_from_uri (uri);
melunko@38
   207
    file_info->valid_fields = file_info->valid_fields
melunko@38
   208
        | GNOME_VFS_FILE_INFO_FIELDS_TYPE
melunko@38
   209
        | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
melunko@38
   210
        | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
melunko@38
   211
    file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
melunko@38
   212
    file_info->mime_type = "video/x-nuv"; //g_strdup (gnome_vfs_mime_type_from_name (file_info->name));
melunko@38
   213
    file_info->permissions =
melunko@38
   214
        GNOME_VFS_PERM_USER_READ |
melunko@38
   215
        GNOME_VFS_PERM_OTHER_READ |
melunko@38
   216
        GNOME_VFS_PERM_GROUP_READ;
melunko@38
   217
melunko@38
   218
    return GNOME_VFS_OK;
melunko@38
   219
}
melunko@38
   220
melunko@38
   221
static gboolean
melunko@38
   222
do_is_local (GnomeVFSMethod *method,
melunko@38
   223
             const GnomeVFSURI *uri)
melunko@38
   224
{
melunko@38
   225
	return FALSE;
melunko@38
   226
}
melunko@38
   227
melunko@38
   228
static GnomeVFSMethod method = {
melunko@38
   229
	sizeof (GnomeVFSMethod),
melunko@38
   230
	do_open,
melunko@38
   231
	NULL,
melunko@38
   232
	do_close,
melunko@38
   233
	do_read,
melunko@38
   234
	NULL,
melunko@38
   235
	NULL,
melunko@38
   236
	NULL,
melunko@38
   237
	NULL,
melunko@38
   238
	NULL,
melunko@38
   239
	NULL,
melunko@38
   240
	NULL,
melunko@38
   241
	do_get_file_info,
melunko@38
   242
	NULL,
melunko@38
   243
	do_is_local,
melunko@38
   244
	NULL,
melunko@38
   245
	NULL,
melunko@38
   246
	NULL,
melunko@38
   247
	NULL,
melunko@38
   248
	NULL,
melunko@38
   249
	NULL,
melunko@38
   250
	NULL,
melunko@38
   251
	NULL,
melunko@38
   252
	NULL,
melunko@38
   253
	NULL,
melunko@38
   254
	NULL,
melunko@38
   255
	NULL,
melunko@38
   256
};
melunko@38
   257
melunko@38
   258
melunko@38
   259
GnomeVFSMethod *
melunko@38
   260
vfs_module_init (const char *method_name, const char *args)
melunko@38
   261
{
melunko@38
   262
	return &method;
melunko@38
   263
}
melunko@38
   264
melunko@38
   265
void
melunko@38
   266
vfs_module_shutdown (GnomeVFSMethod *method)
melunko@38
   267
{
melunko@38
   268
}