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