libgnomevfs2-mythtv/modules/mythtv-method.c
author morphbr
Tue Mar 27 00:13:27 2007 +0100 (2007-03-27)
branchtrunk
changeset 453 a806d8ad0ff0
parent 441 f310d1d086b5
child 456 96a51561bed6
permissions -rwxr-xr-x
[svn r458]
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>
leo_sobral@441
    25
#include <glib/gprintf.h>
leo_sobral@441
    26
#include <glib/gstdio.h>
melunko@38
    27
#include <math.h>
melunko@38
    28
melunko@38
    29
#include <libgnomevfs/gnome-vfs-module.h>
melunko@38
    30
#include <libgnomevfs/gnome-vfs-utils.h>
melunko@38
    31
rosfran@277
    32
#include <gmyth/gmyth_file_transfer.h>
rosfran@277
    33
#include <gmyth/gmyth_livetv.h>
rosfran@277
    34
#include <gmyth/gmyth_uri.h>
rosfran@277
    35
#include <gmyth/gmyth_recorder.h>
rosfran@277
    36
#include <gmyth/gmyth_backendinfo.h>
rosfran@277
    37
#include <gmyth/gmyth_util.h>
rosfran@332
    38
#include <gmyth/gmyth_remote_util.h>
rosfran@332
    39
#include <gmyth/gmyth_tvchain.h>
rosfran@357
    40
#include <gmyth/gmyth_programinfo.h>
melunko@38
    41
melunko@38
    42
#define GST_MYTHTV_ID_NUM               1
melunko@38
    43
#define MYTHTV_VERSION_DEFAULT          30
rosfran@277
    44
#define MYTHTV_TRANSFER_MAX_WAITS       100
melunko@38
    45
rosfran@361
    46
/* internal GnomeVFS plug-in buffer size ( 120 Kbytes ) */
leo_sobral@441
    47
#define MYTHTV_BUFFER_SIZE                          80*1024
rosfran@361
    48
/* internally sized GnomeVFS plug-in buffer ( 4 Kbytes ) */
leo_sobral@441
    49
#define MYTHTV_MAX_VFS_BUFFER_SIZE                  4096
rosfran@361
    50
/* maximum number of bytes to be requested to the MythTV backend ( 64 Kbytes ) */
leo_sobral@441
    51
#define MYTHTV_MAX_REQUEST_SIZE                     64*1024
melunko@111
    52
renatofilho@367
    53
static GnomeVFSResult do_read (GnomeVFSMethod * method,
renatofilho@367
    54
			       GnomeVFSMethodHandle * method_handle,
renatofilho@367
    55
			       gpointer buffer,
renatofilho@367
    56
			       GnomeVFSFileSize num_bytes,
renatofilho@367
    57
			       GnomeVFSFileSize * bytes_read,
renatofilho@367
    58
			       GnomeVFSContext * context);
melunko@38
    59
melunko@38
    60
typedef struct {
renatofilho@367
    61
	GMythFileTransfer *file_transfer;
renatofilho@367
    62
	GMythLiveTV *livetv;
renatofilho@367
    63
	gchar *channel_name;
melunko@111
    64
renatofilho@367
    65
	gint mythtv_version;
renatofilho@367
    66
	gint64 content_size;
renatofilho@367
    67
	guint64 bytes_read;
renatofilho@367
    68
renatofilho@367
    69
	GByteArray *buffer;
renatofilho@367
    70
	gsize buffer_remain;
renatofilho@367
    71
	gboolean is_livetv;
renatofilho@367
    72
renatofilho@367
    73
	gboolean configured;
renatofilho@367
    74
melunko@38
    75
} MythtvHandle;
melunko@38
    76
rosfran@357
    77
//static MythtvHandle *myth_handle = NULL;
leo_sobral@441
    78
#ifdef DEBUG
leo_sobral@441
    79
static FILE *fpout = NULL;
leo_sobral@441
    80
static gboolean first = TRUE;
leo_sobral@441
    81
#endif
rosfran@357
    82
melunko@38
    83
static GnomeVFSResult
renatofilho@367
    84
do_open (GnomeVFSMethod * method,
renatofilho@367
    85
	 GnomeVFSMethodHandle ** method_handle,
renatofilho@367
    86
	 GnomeVFSURI * uri,
renatofilho@367
    87
	 GnomeVFSOpenMode mode, GnomeVFSContext * context)
renatofilho@367
    88
{
renatofilho@367
    89
	MythtvHandle *myth_handle = NULL;
renatofilho@367
    90
	GMythBackendInfo *backend_info = NULL;
renatofilho@367
    91
	GMythURI *gmyth_uri = NULL;
renatofilho@367
    92
	gboolean ret = TRUE;
leo_sobral@447
    93
    gchar *tmp_str1;
leo_sobral@447
    94
    gchar *tmp_str2;
melunko@38
    95
renatofilho@367
    96
	_GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
renatofilho@367
    97
	_GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
melunko@38
    98
renatofilho@367
    99
	if (mode & GNOME_VFS_OPEN_WRITE) {
renatofilho@367
   100
		return GNOME_VFS_ERROR_NOT_PERMITTED;
renatofilho@367
   101
	}
melunko@38
   102
renatofilho@367
   103
	if (gnome_vfs_uri_get_host_name (uri) == NULL) {
renatofilho@367
   104
		return GNOME_VFS_ERROR_INVALID_HOST_NAME;
renatofilho@367
   105
	}
renatofilho@367
   106
leo_sobral@441
   107
    // FIXME: myth_handle is always NULL here
renatofilho@367
   108
	if ((NULL == myth_handle) || !myth_handle->configured) {
leo_sobral@447
   109
        myth_handle = g_new0 (MythtvHandle, 1);
leo_sobral@447
   110
        
leo_sobral@447
   111
        myth_handle->configured = FALSE;        
leo_sobral@447
   112
        myth_handle->is_livetv = FALSE;
renatofilho@367
   113
renatofilho@367
   114
		/* Initialize mythtv handler */
leo_sobral@447
   115
        myth_handle->file_transfer = NULL;
leo_sobral@447
   116
        myth_handle->livetv = NULL;
leo_sobral@447
   117
        myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
leo_sobral@447
   118
        myth_handle->bytes_read = 0;
leo_sobral@447
   119
        myth_handle->content_size = (GnomeVFSFileSize) - 1;
leo_sobral@447
   120
        
leo_sobral@447
   121
		/* Creates and fills out the backend info structure */
leo_sobral@447
   122
        tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
leo_sobral@447
   123
        tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
renatofilho@367
   124
leo_sobral@447
   125
        backend_info = gmyth_backend_info_new_with_uri (tmp_str2);
renatofilho@367
   126
leo_sobral@447
   127
        g_free (tmp_str1);
leo_sobral@447
   128
        g_free (tmp_str2);
leo_sobral@447
   129
   
renatofilho@367
   130
		/* creates an instance of  */
leo_sobral@447
   131
        tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
leo_sobral@447
   132
        tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
leo_sobral@447
   133
leo_sobral@447
   134
		gmyth_uri = gmyth_uri_new_with_value (tmp_str2);
leo_sobral@447
   135
leo_sobral@447
   136
        g_free (tmp_str1);
leo_sobral@447
   137
        g_free (tmp_str2);
renatofilho@367
   138
renatofilho@367
   139
		myth_handle->is_livetv = gmyth_uri_is_livetv (gmyth_uri);
renatofilho@367
   140
renatofilho@367
   141
		/* Connect to the backend */
renatofilho@367
   142
		if (gmyth_uri != NULL && myth_handle->is_livetv == TRUE) {
renatofilho@367
   143
renatofilho@367
   144
			if (NULL == myth_handle->livetv) {
leo_sobral@441
   145
				myth_handle->livetv = gmyth_livetv_new (backend_info);
renatofilho@367
   146
renatofilho@367
   147
				myth_handle->channel_name =
renatofilho@367
   148
				    gmyth_uri_get_channel_name (gmyth_uri);
renatofilho@367
   149
renatofilho@367
   150
				g_debug ("[%s] Channel name = %s\n",
renatofilho@367
   151
					 __FUNCTION__,
renatofilho@367
   152
					 myth_handle->channel_name);
renatofilho@367
   153
renatofilho@367
   154
				if (myth_handle->channel_name != NULL) {
leo_sobral@441
   155
					if (gmyth_livetv_channel_name_setup (myth_handle->livetv,
leo_sobral@441
   156
    					     myth_handle->channel_name) == FALSE) {
renatofilho@367
   157
						g_object_unref (gmyth_uri);
rosfran@357
   158
						ret = FALSE;
rosfran@357
   159
					}
renatofilho@367
   160
				} else {
leo_sobral@441
   161
					if (gmyth_livetv_setup (myth_handle->livetv) == FALSE) {
renatofilho@367
   162
						g_object_unref (gmyth_uri);
renatofilho@367
   163
						ret = FALSE;
renatofilho@367
   164
					}
renatofilho@367
   165
				}
renatofilho@367
   166
			}
melunko@38
   167
renatofilho@367
   168
			if (NULL == myth_handle->file_transfer) {
renatofilho@367
   169
				myth_handle->file_transfer =
leo_sobral@441
   170
				    gmyth_livetv_create_file_transfer (myth_handle->livetv);
renatofilho@367
   171
renatofilho@367
   172
				if (NULL == myth_handle->file_transfer) {
renatofilho@367
   173
					ret = FALSE;
leo_sobral@447
   174
					g_debug ("MythTV FileTransfer is NULL!\n");
renatofilho@367
   175
					return GNOME_VFS_ERROR_NOT_OPEN;
renatofilho@367
   176
				}
renatofilho@367
   177
leo_sobral@447
   178
				if (!gmyth_file_transfer_open (myth_handle->file_transfer,
leo_sobral@447
   179
                      				     myth_handle->livetv->uri != NULL ?
leo_sobral@447
   180
                      				     gmyth_uri_get_path (myth_handle->livetv->uri) :
leo_sobral@447
   181
                    				     myth_handle->livetv->proginfo->pathname->str)) {
leo_sobral@447
   182
					g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
leo_sobral@447
   183
					g_object_unref (myth_handle->file_transfer);
renatofilho@367
   184
					myth_handle->file_transfer = NULL;
renatofilho@367
   185
					ret = FALSE;
renatofilho@367
   186
				}
leo_sobral@441
   187
			} /* if - FileTransfer is NULL, or not */
renatofilho@367
   188
		} else {
renatofilho@367
   189
			if (NULL == myth_handle->file_transfer) {
renatofilho@367
   190
renatofilho@367
   191
				myth_handle->file_transfer =
renatofilho@367
   192
				    gmyth_file_transfer_new (backend_info);
renatofilho@367
   193
renatofilho@367
   194
				/* Verifies if the file exists */
leo_sobral@447
   195
				if (!gmyth_util_file_exists (backend_info,
leo_sobral@447
   196
    				     gmyth_uri_get_path (gmyth_uri))) {
renatofilho@367
   197
					g_object_unref (backend_info);
renatofilho@367
   198
					ret = FALSE;
renatofilho@367
   199
				}
renatofilho@367
   200
renatofilho@367
   201
				/* sets the Playback monitor connection */
leo_sobral@447
   202
				ret = gmyth_file_transfer_open (myth_handle->file_transfer,
leo_sobral@447
   203
                       					      gmyth_uri_get_path (gmyth_uri));
renatofilho@367
   204
			}
leo_sobral@441
   205
		} /* if - LiveTV or not? */
renatofilho@367
   206
renatofilho@367
   207
		if (ret == FALSE) {
renatofilho@367
   208
			g_debug ("MythTV FileTransfer open error.\n");
renatofilho@367
   209
			return GNOME_VFS_ERROR_NOT_OPEN;
renatofilho@367
   210
		}
renatofilho@367
   211
renatofilho@367
   212
		myth_handle->configured = TRUE;
renatofilho@367
   213
renatofilho@367
   214
		g_object_unref (backend_info);
renatofilho@367
   215
renatofilho@367
   216
		if (gmyth_uri != NULL)
renatofilho@367
   217
			g_object_unref (gmyth_uri);
renatofilho@367
   218
renatofilho@367
   219
		myth_handle->buffer =
renatofilho@367
   220
		    g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
renatofilho@367
   221
		myth_handle->buffer_remain = 0;
renatofilho@367
   222
renatofilho@367
   223
		g_return_val_if_fail (myth_handle->file_transfer != NULL,
renatofilho@367
   224
				      GNOME_VFS_ERROR_NOT_OPEN);
renatofilho@367
   225
renatofilho@367
   226
		if ( /*myth_handle->file_transfer->filesize <= 0 && */
renatofilho@367
   227
		    myth_handle->is_livetv) {
renatofilho@367
   228
			myth_handle->content_size =
leo_sobral@441
   229
			    gmyth_recorder_get_file_position (myth_handle->livetv->recorder);
renatofilho@367
   230
		} else {
renatofilho@367
   231
			myth_handle->content_size =
renatofilho@367
   232
			    myth_handle->file_transfer->filesize;
renatofilho@367
   233
		}
renatofilho@367
   234
renatofilho@367
   235
	}
renatofilho@367
   236
	/* if - configured or not? */
renatofilho@367
   237
	*method_handle = (GnomeVFSMethodHandle *) myth_handle;
renatofilho@367
   238
renatofilho@367
   239
	return GNOME_VFS_OK;
melunko@38
   240
}
melunko@38
   241
melunko@38
   242
static GnomeVFSResult
renatofilho@367
   243
do_read (GnomeVFSMethod * method,
renatofilho@367
   244
	 GnomeVFSMethodHandle * method_handle,
renatofilho@367
   245
	 gpointer buffer,
renatofilho@367
   246
	 GnomeVFSFileSize num_bytes,
leo_sobral@441
   247
	 GnomeVFSFileSize * bytes_read, 
leo_sobral@441
   248
     GnomeVFSContext * context)
melunko@38
   249
{
renatofilho@367
   250
	MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
renatofilho@367
   251
	GnomeVFSFileSize bytes_to_read = num_bytes;
melunko@38
   252
renatofilho@367
   253
	if (!myth_handle->is_livetv
renatofilho@367
   254
	    && (myth_handle->bytes_read >= myth_handle->content_size))
renatofilho@367
   255
		return GNOME_VFS_ERROR_EOF;
melunko@38
   256
renatofilho@367
   257
	/* fixme: change this to min math function */
renatofilho@367
   258
	if ((myth_handle->content_size > 0)) {
renatofilho@367
   259
		if (!myth_handle->is_livetv &&
renatofilho@367
   260
		    (num_bytes >
renatofilho@367
   261
		     (myth_handle->content_size -
renatofilho@367
   262
		      myth_handle->bytes_read))) {
renatofilho@367
   263
			bytes_to_read =
renatofilho@367
   264
			    myth_handle->content_size -
renatofilho@367
   265
			    myth_handle->bytes_read;
renatofilho@367
   266
		}
renatofilho@367
   267
	}
melunko@38
   268
renatofilho@367
   269
	/* Loop sending the Myth File Transfer request:
renatofilho@367
   270
	 * Retry whilst authentication fails and we supply it. */
renatofilho@367
   271
	if ((myth_handle->buffer_remain =
renatofilho@367
   272
	     myth_handle->buffer->len) < bytes_to_read) {
renatofilho@367
   273
		gint buffer_size;
renatofilho@367
   274
renatofilho@367
   275
		while (MYTHTV_BUFFER_SIZE != myth_handle->buffer_remain) {
renatofilho@367
   276
			/* resize buffer length request to no more than MYTHTV_MAX_REQUEST_SIZE */
leo_sobral@447
   277
			if ((MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain) <=
leo_sobral@447
   278
    			    MYTHTV_MAX_REQUEST_SIZE)
leo_sobral@447
   279
				buffer_size = MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain;
renatofilho@367
   280
			else
renatofilho@367
   281
				buffer_size = MYTHTV_MAX_REQUEST_SIZE;
renatofilho@367
   282
renatofilho@367
   283
			GByteArray *tmp_buffer = g_byte_array_new ();
renatofilho@367
   284
leo_sobral@441
   285
			g_debug ("Asking %d bytes (there is %d bytes in the buffer)\n",
renatofilho@367
   286
			     buffer_size, myth_handle->buffer_remain);
renatofilho@367
   287
leo_sobral@447
   288
			gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
leo_sobral@447
   289
                    					         tmp_buffer,
leo_sobral@447
   290
					                             buffer_size, TRUE);
renatofilho@367
   291
rosfran@408
   292
			if ( !myth_handle->is_livetv ) {
rosfran@408
   293
				if ( len < 0 ) {
rosfran@408
   294
					g_byte_array_free (tmp_buffer, TRUE);
rosfran@408
   295
					g_debug ("Fail to read bytes");
rosfran@408
   296
					return GNOME_VFS_ERROR_IO;
rosfran@408
   297
				} else if ( len == 0 ) {
rosfran@408
   298
				   g_byte_array_free (tmp_buffer, TRUE);
rosfran@408
   299
				   g_warning ("End of file probably achieved");
rosfran@408
   300
				   return GNOME_VFS_ERROR_EOF;
rosfran@408
   301
			   }
rosfran@408
   302
			} /* if */
rosfran@408
   303
			
renatofilho@367
   304
			myth_handle->buffer =
renatofilho@367
   305
			    g_byte_array_append (myth_handle->buffer,
renatofilho@367
   306
						 tmp_buffer->data, len);
renatofilho@367
   307
rosfran@327
   308
			myth_handle->buffer_remain += len;
melunko@111
   309
renatofilho@367
   310
			if (tmp_buffer != NULL) {
renatofilho@367
   311
				g_byte_array_free (tmp_buffer, TRUE);
renatofilho@367
   312
				tmp_buffer = NULL;
rosfran@354
   313
			}
renatofilho@367
   314
		}		/* while - iterates until fills the internal buffer */
rosfran@116
   315
renatofilho@367
   316
	}
leo_sobral@441
   317
        
renatofilho@367
   318
	/* if - got from the network, or not */
leo_sobral@447
   319
	bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? 
leo_sobral@447
   320
                        myth_handle->buffer_remain : bytes_to_read;
renatofilho@367
   321
	/* gets the first buffer_size bytes from the byte array buffer variable */
rosfran@116
   322
renatofilho@367
   323
	g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
renatofilho@367
   324
renatofilho@367
   325
	myth_handle->bytes_read += bytes_to_read;
renatofilho@367
   326
	myth_handle->buffer_remain -= bytes_to_read;
rosfran@116
   327
rosfran@355
   328
	/* flushs the newly buffer got from byte array */
renatofilho@367
   329
	myth_handle->buffer =
renatofilho@367
   330
	    g_byte_array_remove_range (myth_handle->buffer, 0,
renatofilho@367
   331
				       bytes_to_read);
renatofilho@367
   332
	g_debug
renatofilho@367
   333
	    ("Got from %llu bytes from internal buffer. (there are %d bytes in the buffer, from a total of %llu dispatched.)\n",
renatofilho@367
   334
	     bytes_to_read, myth_handle->buffer_remain,
renatofilho@367
   335
	     myth_handle->bytes_read);
rosfran@355
   336
renatofilho@367
   337
	*bytes_read = bytes_to_read;
renatofilho@367
   338
renatofilho@367
   339
	return GNOME_VFS_OK;
melunko@38
   340
}
melunko@38
   341
melunko@38
   342
static GnomeVFSResult
renatofilho@367
   343
do_close (GnomeVFSMethod * method,
renatofilho@367
   344
	  GnomeVFSMethodHandle * method_handle, GnomeVFSContext * context)
melunko@38
   345
{
melunko@111
   346
renatofilho@367
   347
	MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
renatofilho@367
   348
renatofilho@367
   349
	//if ( NULL == myth_handle || myth_handle->configured ) {
renatofilho@367
   350
leo_sobral@441
   351
	if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
leo_sobral@441
   352
		g_object_unref (myth_handle->livetv);
leo_sobral@441
   353
		myth_handle->livetv = NULL;
leo_sobral@441
   354
	}
leo_sobral@441
   355
   
renatofilho@367
   356
	if (myth_handle->file_transfer != NULL) {
renatofilho@367
   357
		g_object_unref (myth_handle->file_transfer);
renatofilho@367
   358
		myth_handle->file_transfer = NULL;
renatofilho@367
   359
	}
renatofilho@367
   360
renatofilho@367
   361
	if (myth_handle->buffer) {
renatofilho@367
   362
		g_byte_array_free (myth_handle->buffer, TRUE);
renatofilho@367
   363
		myth_handle->buffer = NULL;
renatofilho@367
   364
	}
renatofilho@367
   365
renatofilho@367
   366
	myth_handle->configured = FALSE;
renatofilho@367
   367
leo_sobral@441
   368
	g_free (myth_handle->channel_name);
leo_sobral@441
   369
    
renatofilho@367
   370
	g_free (myth_handle);
renatofilho@367
   371
renatofilho@367
   372
	myth_handle = NULL;
renatofilho@367
   373
renatofilho@367
   374
	// }
renatofilho@367
   375
renatofilho@367
   376
	return GNOME_VFS_OK;
melunko@38
   377
}
melunko@38
   378
melunko@38
   379
static GnomeVFSResult
renatofilho@367
   380
do_get_file_info (GnomeVFSMethod * method,
renatofilho@367
   381
		  GnomeVFSURI * uri,
renatofilho@367
   382
		  GnomeVFSFileInfo * file_info,
renatofilho@367
   383
		  GnomeVFSFileInfoOptions options,
renatofilho@367
   384
		  GnomeVFSContext * context)
melunko@38
   385
{
renatofilho@367
   386
	GMythFileTransfer *file_transfer = NULL;
renatofilho@367
   387
	GMythRecorder *recorder = NULL;
renatofilho@367
   388
	GMythTVChain *tvchain = NULL;
renatofilho@367
   389
	GMythBackendInfo *backend_info = NULL;
renatofilho@367
   390
	GMythURI *gmyth_uri = NULL;
renatofilho@367
   391
	GMythSocket *socket = NULL;
renatofilho@367
   392
	gboolean is_livetv = FALSE;
leo_sobral@447
   393
    gchar *tmp_str1;
leo_sobral@447
   394
    gchar *tmp_str2;
leo_sobral@447
   395
renatofilho@367
   396
	gboolean ret = TRUE;
renatofilho@367
   397
	gboolean res = TRUE;
renatofilho@188
   398
leo_sobral@447
   399
	/* Creates and fills out the backend info structure */        
leo_sobral@447
   400
    tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
leo_sobral@447
   401
    tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
leo_sobral@447
   402
leo_sobral@447
   403
	backend_info = gmyth_backend_info_new_with_uri (tmp_str2);
leo_sobral@447
   404
    g_free (tmp_str1);
leo_sobral@447
   405
    g_free (tmp_str2);
rosfran@297
   406
renatofilho@367
   407
	/* creates an instance of */
leo_sobral@447
   408
    tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
leo_sobral@447
   409
    tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
leo_sobral@447
   410
leo_sobral@447
   411
	gmyth_uri = gmyth_uri_new_with_value (tmp_str2);
leo_sobral@447
   412
    g_free (tmp_str1);
leo_sobral@447
   413
    g_free (tmp_str2);
melunko@38
   414
renatofilho@367
   415
	is_livetv = gmyth_uri_is_livetv (gmyth_uri);
rosfran@332
   416
leo_sobral@441
   417
    file_info->valid_fields = 0;
renatofilho@367
   418
	file_info->valid_fields = file_info->valid_fields
renatofilho@367
   419
	    | GNOME_VFS_FILE_INFO_FIELDS_TYPE
renatofilho@367
   420
	    | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
renatofilho@367
   421
	    | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
renatofilho@367
   422
	file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
renatofilho@367
   423
	/* fixme: get from file extension? */
renatofilho@367
   424
	file_info->mime_type = g_strdup ("video/x-nuv");
renatofilho@367
   425
	file_info->permissions =
renatofilho@367
   426
	    GNOME_VFS_PERM_USER_READ |
renatofilho@367
   427
	    GNOME_VFS_PERM_OTHER_READ | GNOME_VFS_PERM_GROUP_READ;
rosfran@346
   428
rosfran@368
   429
	g_debug ("gnome_vfs_uri == %s | gmyth_uri == %s.\n",
renatofilho@367
   430
		 gnome_vfs_uri_get_path (uri),
renatofilho@367
   431
		 gmyth_uri_get_path (gmyth_uri));
rosfran@331
   432
renatofilho@367
   433
	/* Connect to the backend */
renatofilho@367
   434
	if (gmyth_uri != NULL && is_livetv == TRUE) {
rosfran@338
   435
renatofilho@367
   436
		/* start to get file info from LiveTV remote encoder */
renatofilho@367
   437
		socket = gmyth_socket_new ();
rosfran@301
   438
renatofilho@367
   439
		/* FIME: Implement this at gmyth_socket */
renatofilho@367
   440
		res =
renatofilho@367
   441
		    gmyth_socket_connect_to_backend (socket,
leo_sobral@441
   442
						     backend_info->hostname,
renatofilho@367
   443
						     backend_info->port,
renatofilho@367
   444
						     TRUE);
renatofilho@367
   445
		if (!res) {
leo_sobral@441
   446
			g_debug ("[%s] LiveTV can not connect to backend",  __FUNCTION__);
renatofilho@367
   447
			res = FALSE;
renatofilho@367
   448
			goto error;
renatofilho@367
   449
		}
renatofilho@367
   450
leo_sobral@441
   451
		if (gmyth_remote_util_get_free_recorder_count (socket) <= 0) {
rosfran@368
   452
			g_debug ("No free remote encoder available.");
renatofilho@367
   453
			res = FALSE;
renatofilho@367
   454
			goto error;
renatofilho@367
   455
		}
renatofilho@367
   456
renatofilho@367
   457
		/* Gets the recorder num */
renatofilho@367
   458
		recorder = remote_request_next_free_recorder (socket, -1);
renatofilho@367
   459
leo_sobral@441
   460
		if (socket != NULL)
rosfran@368
   461
			g_object_unref (socket);
renatofilho@367
   462
renatofilho@367
   463
		if (recorder == NULL) {
leo_sobral@441
   464
			g_debug ("[%s] None remote encoder available", __FUNCTION__);
renatofilho@367
   465
			res = FALSE;
renatofilho@367
   466
			goto error;
renatofilho@367
   467
		}
renatofilho@367
   468
renatofilho@367
   469
		/* Init remote encoder. Opens its control socket. */
renatofilho@367
   470
		res = gmyth_recorder_setup (recorder);
renatofilho@367
   471
		if (!res) {
leo_sobral@441
   472
			g_debug ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
renatofilho@367
   473
			res = FALSE;
renatofilho@367
   474
			goto error;
renatofilho@367
   475
		}
renatofilho@367
   476
renatofilho@367
   477
		/* Creates livetv chain handler */
renatofilho@367
   478
		tvchain = gmyth_tvchain_new ();
renatofilho@367
   479
		gmyth_tvchain_initialize (tvchain, backend_info);
renatofilho@367
   480
renatofilho@367
   481
		if (tvchain == NULL || tvchain->tvchain_id == NULL) {
renatofilho@367
   482
			res = FALSE;
renatofilho@367
   483
			goto error;
renatofilho@367
   484
		}
rosfran@368
   485
		/* Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly) */
leo_sobral@441
   486
		res = gmyth_recorder_spawntv (recorder, gmyth_tvchain_get_id(tvchain));
renatofilho@367
   487
		if (!res) {
renatofilho@367
   488
			g_warning ("[%s] Fail while spawn tv\n",
renatofilho@367
   489
				   __FUNCTION__);
renatofilho@367
   490
			res = FALSE;
renatofilho@367
   491
			goto error;
renatofilho@367
   492
		}
rosfran@372
   493
		
rosfran@372
   494
		sleep(1);
renatofilho@367
   495
renatofilho@367
   496
		/* DEBUG message */
renatofilho@367
   497
		GMythProgramInfo *prog_info =
renatofilho@367
   498
		    gmyth_recorder_get_current_program_info (recorder);
renatofilho@367
   499
renatofilho@367
   500
		if (prog_info != NULL) {
renatofilho@367
   501
rosfran@368
   502
			g_debug ("path = %s", prog_info->pathname->str);
renatofilho@367
   503
renatofilho@367
   504
			file_info->name =
renatofilho@367
   505
			    g_strdup (g_strrstr
renatofilho@367
   506
				      (prog_info->pathname->str, "/"));
renatofilho@367
   507
renatofilho@367
   508
		} else {
renatofilho@367
   509
			file_info->name = g_strdup ("LiveTV.nuv");
leo_sobral@441
   510
            /* Size being overrided below ... */
leo_sobral@441
   511
			//file_info->size = gmyth_recorder_get_file_position (recorder);
renatofilho@367
   512
		}
renatofilho@367
   513
renatofilho@367
   514
		if (recorder != NULL)
renatofilho@367
   515
			g_object_unref (recorder);
renatofilho@367
   516
renatofilho@367
   517
		if (prog_info != NULL)
renatofilho@367
   518
			g_object_unref (prog_info);
renatofilho@367
   519
renatofilho@367
   520
		if (tvchain != NULL)
renatofilho@367
   521
			g_object_unref (tvchain);
renatofilho@367
   522
leo_sobral@441
   523
		//file_info->size = (GnomeVFSFileSize) - 1;
renatofilho@367
   524
renatofilho@367
   525
	} else {
renatofilho@367
   526
leo_sobral@441
   527
        /* start to get file info from remote file encoder */
leo_sobral@441
   528
        file_transfer = gmyth_file_transfer_new (backend_info);
renatofilho@367
   529
renatofilho@367
   530
		/* Verifies if the file exists */
leo_sobral@441
   531
    	if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
leo_sobral@441
   532
            g_object_unref (backend_info);
leo_sobral@441
   533
            return GNOME_VFS_ERROR_NOT_FOUND;
renatofilho@367
   534
		}
renatofilho@367
   535
renatofilho@367
   536
		/* sets the Playback monitor connection */
leo_sobral@441
   537
		ret = gmyth_file_transfer_open (file_transfer,
leo_sobral@441
   538
					      gmyth_uri_get_path (gmyth_uri));
renatofilho@367
   539
renatofilho@367
   540
		file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
renatofilho@367
   541
leo_sobral@441
   542
	} /* if - LiveTV or not? */
renatofilho@367
   543
renatofilho@367
   544
	if (ret == FALSE) {
renatofilho@367
   545
		g_debug ("MythTV FileTransfer open error\n");
renatofilho@367
   546
		return GNOME_VFS_ERROR_NOT_OPEN;
renatofilho@367
   547
	}
renatofilho@367
   548
leo_sobral@441
   549
    /* Just for recorded content */
renatofilho@367
   550
	if (ret == TRUE && file_transfer != NULL) {
renatofilho@367
   551
		file_info->size =
renatofilho@367
   552
		    gmyth_file_transfer_get_filesize (file_transfer);
leo_sobral@441
   553
leo_sobral@441
   554
        file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE;
leo_sobral@441
   555
        
renatofilho@367
   556
		if (file_transfer)
renatofilho@367
   557
			g_object_unref (file_transfer);
renatofilho@367
   558
	}
renatofilho@367
   559
leo_sobral@441
   560
error:
renatofilho@367
   561
	if (backend_info)
renatofilho@367
   562
		g_object_unref (backend_info);
renatofilho@367
   563
renatofilho@367
   564
	if (!res)
renatofilho@367
   565
		return GNOME_VFS_ERROR_IO;
renatofilho@367
   566
renatofilho@367
   567
	return GNOME_VFS_OK;
melunko@38
   568
}
melunko@38
   569
melunko@38
   570
static gboolean
renatofilho@367
   571
do_is_local (GnomeVFSMethod * method, const GnomeVFSURI * uri)
melunko@38
   572
{
melunko@38
   573
	return FALSE;
melunko@38
   574
}
melunko@38
   575
melunko@38
   576
static GnomeVFSMethod method = {
melunko@38
   577
	sizeof (GnomeVFSMethod),
melunko@38
   578
	do_open,
melunko@38
   579
	NULL,
melunko@38
   580
	do_close,
melunko@38
   581
	do_read,
melunko@38
   582
	NULL,
melunko@38
   583
	NULL,
melunko@38
   584
	NULL,
melunko@38
   585
	NULL,
melunko@38
   586
	NULL,
melunko@38
   587
	NULL,
melunko@38
   588
	NULL,
melunko@38
   589
	do_get_file_info,
melunko@38
   590
	NULL,
melunko@38
   591
	do_is_local,
melunko@38
   592
	NULL,
melunko@38
   593
	NULL,
melunko@38
   594
	NULL,
melunko@38
   595
	NULL,
melunko@38
   596
	NULL,
melunko@38
   597
	NULL,
melunko@38
   598
	NULL,
melunko@38
   599
	NULL,
melunko@38
   600
	NULL,
melunko@38
   601
	NULL,
melunko@38
   602
	NULL,
melunko@38
   603
	NULL,
melunko@38
   604
};
melunko@38
   605
melunko@38
   606
melunko@38
   607
GnomeVFSMethod *
melunko@38
   608
vfs_module_init (const char *method_name, const char *args)
melunko@38
   609
{
melunko@38
   610
	return &method;
melunko@38
   611
}
melunko@38
   612
melunko@38
   613
void
renatofilho@367
   614
vfs_module_shutdown (GnomeVFSMethod * method)
melunko@38
   615
{
melunko@38
   616
}