# HG changeset patch # User renatofilho # Date 1175006182 -3600 # Node ID 96a51561bed61be0d0cd0e62eca3883131985dea # Parent 10c21aebb0ffd783bbb58e2629aa93c8ab6861e9 [svn r461] code rewrite diff -r 10c21aebb0ff -r 96a51561bed6 libgnomevfs2-mythtv/modules/mythtv-method.c --- a/libgnomevfs2-mythtv/modules/mythtv-method.c Tue Mar 27 15:35:49 2007 +0100 +++ b/libgnomevfs2-mythtv/modules/mythtv-method.c Tue Mar 27 15:36:22 2007 +0100 @@ -50,6 +50,21 @@ /* maximum number of bytes to be requested to the MythTV backend ( 64 Kbytes ) */ #define MYTHTV_MAX_REQUEST_SIZE 64*1024 +typedef struct { + GMythFileTransfer *file_transfer; + GMythLiveTV *livetv; + GMythBackendInfo *backend_info; + GMythURI *gmyth_uri; + GMythRecorder *live_recorder; + gint64 offset; + + gchar *channel_name; + + gint mythtv_version; + gboolean configured; +} MythtvHandle; + + static GnomeVFSResult do_read (GnomeVFSMethod * method, GnomeVFSMethodHandle * method_handle, gpointer buffer, @@ -57,29 +72,230 @@ GnomeVFSFileSize * bytes_read, GnomeVFSContext * context); -typedef struct { - GMythFileTransfer *file_transfer; - GMythLiveTV *livetv; - gchar *channel_name; +static GnomeVFSResult myth_connection_start (MythtvHandle * method_handle); +static void myth_destroy_handle (MythtvHandle * method_handle); +static GnomeVFSResult myth_handle_new (GnomeVFSURI * uri, + MythtvHandle ** method_handle); +static GnomeVFSResult myth_get_file_info (MythtvHandle * myth_handle, + GnomeVFSURI * uri, + GnomeVFSFileInfo * info); - gint mythtv_version; - gint64 content_size; - guint64 bytes_read; - - GByteArray *buffer; - gsize buffer_remain; - gboolean is_livetv; - - gboolean configured; - -} MythtvHandle; - -//static MythtvHandle *myth_handle = NULL; #ifdef DEBUG static FILE *fpout = NULL; static gboolean first = TRUE; #endif +static GnomeVFSResult +myth_handle_new (GnomeVFSURI * uri, + MythtvHandle ** method_handle) +{ + gchar *tmp_str1; + gchar *tmp_str2; + + _GNOME_VFS_METHOD_PARAM_CHECK (*method_handle == NULL); + + if (gnome_vfs_uri_get_host_name (uri) == NULL) { + return GNOME_VFS_ERROR_INVALID_HOST_NAME; + } + + *method_handle = g_new0 (MythtvHandle, 1); + (*method_handle)->mythtv_version = MYTHTV_VERSION_DEFAULT; + + tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE); + tmp_str2 = gnome_vfs_unescape_string (tmp_str1, ""); + + (*method_handle)->backend_info = gmyth_backend_info_new_with_uri (tmp_str2); + (*method_handle)->gmyth_uri = gmyth_uri_new_with_value (tmp_str2); + g_free (tmp_str1); + g_free (tmp_str2); + + return GNOME_VFS_OK; +} + +static void +myth_destroy_handle (MythtvHandle * method_handle) +{ + //TODO: abort if in tranfer state + + if (method_handle->backend_info != NULL) { + g_object_unref (method_handle->backend_info); + method_handle->backend_info = NULL; + } + + if (method_handle->channel_name != NULL) { + g_free (method_handle->channel_name); + method_handle->channel_name = NULL; + } + + if (method_handle->livetv != NULL) { + g_object_unref (method_handle->livetv); + method_handle->livetv = NULL; + } + + if (method_handle->file_transfer != NULL) { + g_object_unref (method_handle->file_transfer); + method_handle->file_transfer = NULL; + } + + if (method_handle->gmyth_uri != NULL) { + g_object_unref (method_handle->gmyth_uri); + method_handle->gmyth_uri = NULL; + } + + g_free (method_handle); +} + +static GnomeVFSResult +myth_get_file_info (MythtvHandle * myth_handle, + GnomeVFSURI * uri, + GnomeVFSFileInfo * info) +{ + GMythURI *gmyth_uri; + GMythBackendInfo *backend_info; + + _GNOME_VFS_METHOD_PARAM_CHECK (info != NULL); + + if (myth_handle == NULL) { + gchar *tmp_str1; + gchar *tmp_str2; + + tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE); + tmp_str2 = gnome_vfs_unescape_string (tmp_str1, ""); + + backend_info = gmyth_backend_info_new_with_uri (tmp_str2); + gmyth_uri = gmyth_uri_new_with_value (tmp_str2); + + g_free (tmp_str1); + g_free (tmp_str2); + } else { + backend_info = g_object_ref (myth_handle->backend_info); + gmyth_uri = g_object_ref (myth_handle->gmyth_uri); + } + + info->valid_fields = 0; + info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE | + GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE | + GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS; + + info->type = GNOME_VFS_FILE_TYPE_REGULAR; + + /* fixme: get from file extension? */ + info->mime_type = g_strdup ("video/x-nuv"); + info->permissions = GNOME_VFS_PERM_USER_READ | + GNOME_VFS_PERM_OTHER_READ | + GNOME_VFS_PERM_GROUP_READ; + + + info->name = g_strdup (gmyth_uri_get_path (gmyth_uri)); + + /* file size for remote files */ + if (gmyth_uri_is_livetv (gmyth_uri) == FALSE) { + GMythFileTransfer *file_transfer = gmyth_file_transfer_new (backend_info); + + /* Verifies if the file exists */ + if (!gmyth_util_file_exists (backend_info, + gmyth_uri_get_path (gmyth_uri))) { + g_object_unref (backend_info); + g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__); + return GNOME_VFS_ERROR_NOT_FOUND; + } + + info->size = gmyth_file_transfer_get_filesize (file_transfer); + info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE; + g_object_unref (file_transfer); + } + + g_object_unref (backend_info); + g_object_unref (gmyth_uri); + + return GNOME_VFS_OK; +} + +static GnomeVFSResult +myth_connection_start (MythtvHandle * method_handle) +{ + GnomeVFSResult result = GNOME_VFS_OK; + + _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL); + _GNOME_VFS_METHOD_PARAM_CHECK (method_handle->backend_info != NULL); + + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); + + /* Connect to the backend */ + if (gmyth_uri_is_livetv (method_handle->gmyth_uri) == TRUE) { + + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); + + method_handle->livetv = gmyth_livetv_new (method_handle->backend_info); + method_handle->channel_name = gmyth_uri_get_channel_name (method_handle->gmyth_uri); + + g_debug ("[%s] Channel name = %s\n", __FUNCTION__, + method_handle->channel_name); + + if (method_handle->channel_name != NULL) { + if (gmyth_livetv_channel_name_setup (method_handle->livetv, + method_handle->channel_name) == FALSE) { + result = GNOME_VFS_ERROR_INVALID_URI; + goto error; + } + } else if (gmyth_livetv_setup (method_handle->livetv) == FALSE) { + result = GNOME_VFS_ERROR_INVALID_URI; + goto error; + } + + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); + + method_handle->file_transfer = + gmyth_livetv_create_file_transfer (method_handle->livetv); + + if (method_handle->file_transfer == NULL) { + result = GNOME_VFS_ERROR_INVALID_URI; + g_debug ("MythTV FileTransfer is NULL!\n"); + goto error; + } + + if (!gmyth_file_transfer_open (method_handle->file_transfer, + method_handle->livetv->uri != NULL ? + gmyth_uri_get_path (method_handle->livetv->uri) : + method_handle->livetv->proginfo->pathname->str)) { + + g_debug ("Couldn't open MythTV FileTransfer is NULL!\n"); + result = GNOME_VFS_ERROR_NOT_OPEN; + goto error; + } + } + else { + method_handle->file_transfer = + gmyth_file_transfer_new (method_handle->backend_info); + + /* Verifies if the file exists */ + if (!gmyth_util_file_exists (method_handle->backend_info, + gmyth_uri_get_path (method_handle->gmyth_uri))) { + + g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__); + result = GNOME_VFS_ERROR_NOT_FOUND; + goto error; + } + + /* sets the Playback monitor connection */ + result = gmyth_file_transfer_open (method_handle->file_transfer, + gmyth_uri_get_path (method_handle->gmyth_uri)); + } /* if - LiveTV or not? */ + + method_handle->configured = TRUE; + + if (method_handle->file_transfer == NULL) { + result = GNOME_VFS_ERROR_NOT_OPEN; + goto error; + } + g_return_val_if_fail (method_handle->file_transfer != NULL, + GNOME_VFS_ERROR_NOT_OPEN); + +error: + + return result; +} + static GnomeVFSResult do_open (GnomeVFSMethod * method, GnomeVFSMethodHandle ** method_handle, @@ -87,484 +303,240 @@ GnomeVFSOpenMode mode, GnomeVFSContext * context) { MythtvHandle *myth_handle = NULL; - GMythBackendInfo *backend_info = NULL; - GMythURI *gmyth_uri = NULL; - gboolean ret = TRUE; - gchar *tmp_str1; - gchar *tmp_str2; + GnomeVFSResult result = GNOME_VFS_OK; + + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL); _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL); if (mode & GNOME_VFS_OPEN_WRITE) { - return GNOME_VFS_ERROR_NOT_PERMITTED; + return GNOME_VFS_ERROR_INVALID_OPEN_MODE; } - if (gnome_vfs_uri_get_host_name (uri) == NULL) { - return GNOME_VFS_ERROR_INVALID_HOST_NAME; - } + result = myth_handle_new (uri, &myth_handle); + if (result != GNOME_VFS_OK) + return result; - // FIXME: myth_handle is always NULL here - if ((NULL == myth_handle) || !myth_handle->configured) { - myth_handle = g_new0 (MythtvHandle, 1); - - myth_handle->configured = FALSE; - myth_handle->is_livetv = FALSE; + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); + result = myth_connection_start (myth_handle); + if (result != GNOME_VFS_OK) { + myth_destroy_handle (myth_handle); + myth_handle = NULL; + return result; + } + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); - /* Initialize mythtv handler */ - myth_handle->file_transfer = NULL; - myth_handle->livetv = NULL; - myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT; - myth_handle->bytes_read = 0; - myth_handle->content_size = (GnomeVFSFileSize) - 1; - - /* Creates and fills out the backend info structure */ - tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE); - tmp_str2 = gnome_vfs_unescape_string (tmp_str1, ""); + *method_handle = (GnomeVFSMethodHandle *) myth_handle; + + return result; +} - backend_info = gmyth_backend_info_new_with_uri (tmp_str2); +static GnomeVFSResult +do_create (GnomeVFSMethod *method, + GnomeVFSMethodHandle **method_handle, + GnomeVFSURI *uri, + GnomeVFSOpenMode mode, + gboolean exclusive, + guint perm, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} - g_free (tmp_str1); - g_free (tmp_str2); - - /* creates an instance of */ - tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE); - tmp_str2 = gnome_vfs_unescape_string (tmp_str1, ""); +static GnomeVFSResult +do_close (GnomeVFSMethod * method, + GnomeVFSMethodHandle * method_handle, + GnomeVFSContext * context) +{ + MythtvHandle *myth_handle = (MythtvHandle *) method_handle; - gmyth_uri = gmyth_uri_new_with_value (tmp_str2); - - g_free (tmp_str1); - g_free (tmp_str2); - - myth_handle->is_livetv = gmyth_uri_is_livetv (gmyth_uri); - - /* Connect to the backend */ - if (gmyth_uri != NULL && myth_handle->is_livetv == TRUE) { - - if (NULL == myth_handle->livetv) { - myth_handle->livetv = gmyth_livetv_new (backend_info); - - myth_handle->channel_name = - gmyth_uri_get_channel_name (gmyth_uri); - - g_debug ("[%s] Channel name = %s\n", - __FUNCTION__, - myth_handle->channel_name); - - if (myth_handle->channel_name != NULL) { - if (gmyth_livetv_channel_name_setup (myth_handle->livetv, - myth_handle->channel_name) == FALSE) { - g_object_unref (gmyth_uri); - ret = FALSE; - } - } else { - if (gmyth_livetv_setup (myth_handle->livetv) == FALSE) { - g_object_unref (gmyth_uri); - ret = FALSE; - } - } - } - - if (NULL == myth_handle->file_transfer) { - myth_handle->file_transfer = - gmyth_livetv_create_file_transfer (myth_handle->livetv); - - if (NULL == myth_handle->file_transfer) { - ret = FALSE; - g_debug ("MythTV FileTransfer is NULL!\n"); - return GNOME_VFS_ERROR_NOT_OPEN; - } - - if (!gmyth_file_transfer_open (myth_handle->file_transfer, - myth_handle->livetv->uri != NULL ? - gmyth_uri_get_path (myth_handle->livetv->uri) : - myth_handle->livetv->proginfo->pathname->str)) { - g_debug ("Couldn't open MythTV FileTransfer is NULL!\n"); - g_object_unref (myth_handle->file_transfer); - myth_handle->file_transfer = NULL; - ret = FALSE; - } - } /* if - FileTransfer is NULL, or not */ - } else { - if (NULL == myth_handle->file_transfer) { - - myth_handle->file_transfer = - gmyth_file_transfer_new (backend_info); - - /* Verifies if the file exists */ - if (!gmyth_util_file_exists (backend_info, - gmyth_uri_get_path (gmyth_uri))) { - g_object_unref (backend_info); - ret = FALSE; - } - - /* sets the Playback monitor connection */ - ret = gmyth_file_transfer_open (myth_handle->file_transfer, - gmyth_uri_get_path (gmyth_uri)); - } - } /* if - LiveTV or not? */ - - if (ret == FALSE) { - g_debug ("MythTV FileTransfer open error.\n"); - return GNOME_VFS_ERROR_NOT_OPEN; - } - - myth_handle->configured = TRUE; - - g_object_unref (backend_info); - - if (gmyth_uri != NULL) - g_object_unref (gmyth_uri); - - myth_handle->buffer = - g_byte_array_sized_new (MYTHTV_BUFFER_SIZE); - myth_handle->buffer_remain = 0; - - g_return_val_if_fail (myth_handle->file_transfer != NULL, - GNOME_VFS_ERROR_NOT_OPEN); - - if ( /*myth_handle->file_transfer->filesize <= 0 && */ - myth_handle->is_livetv) { - myth_handle->content_size = - gmyth_recorder_get_file_position (myth_handle->livetv->recorder); - } else { - myth_handle->content_size = - myth_handle->file_transfer->filesize; - } - - } - /* if - configured or not? */ - *method_handle = (GnomeVFSMethodHandle *) myth_handle; - + myth_destroy_handle (myth_handle); + return GNOME_VFS_OK; } + static GnomeVFSResult do_read (GnomeVFSMethod * method, GnomeVFSMethodHandle * method_handle, gpointer buffer, GnomeVFSFileSize num_bytes, GnomeVFSFileSize * bytes_read, - GnomeVFSContext * context) + GnomeVFSContext * context) { - MythtvHandle *myth_handle = (MythtvHandle *) method_handle; - GnomeVFSFileSize bytes_to_read = num_bytes; + GnomeVFSResult result; + MythtvHandle *myth_handle; + gint64 total_read = 0; + GByteArray *myth_buffer = g_byte_array_new (); - if (!myth_handle->is_livetv - && (myth_handle->bytes_read >= myth_handle->content_size)) - return GNOME_VFS_ERROR_EOF; + _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL); - /* fixme: change this to min math function */ - if ((myth_handle->content_size > 0)) { - if (!myth_handle->is_livetv && - (num_bytes > - (myth_handle->content_size - - myth_handle->bytes_read))) { - bytes_to_read = - myth_handle->content_size - - myth_handle->bytes_read; - } - } + + myth_handle = (MythtvHandle *) method_handle; + result = GNOME_VFS_OK; - /* Loop sending the Myth File Transfer request: - * Retry whilst authentication fails and we supply it. */ - if ((myth_handle->buffer_remain = - myth_handle->buffer->len) < bytes_to_read) { - gint buffer_size; + g_debug ("Reading %"G_GUINT64_FORMAT" bytes", num_bytes); + + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); - while (MYTHTV_BUFFER_SIZE != myth_handle->buffer_remain) { - /* resize buffer length request to no more than MYTHTV_MAX_REQUEST_SIZE */ - if ((MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain) <= - MYTHTV_MAX_REQUEST_SIZE) - buffer_size = MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain; - else - buffer_size = MYTHTV_MAX_REQUEST_SIZE; + total_read = gmyth_file_transfer_read (myth_handle->file_transfer, + myth_buffer, + num_bytes, gmyth_uri_is_livetv (myth_handle->gmyth_uri)); - GByteArray *tmp_buffer = g_byte_array_new (); - g_debug ("Asking %d bytes (there is %d bytes in the buffer)\n", - buffer_size, myth_handle->buffer_remain); + g_debug ("total read %"G_GINT64_FORMAT, total_read); - gint len = gmyth_file_transfer_read (myth_handle->file_transfer, - tmp_buffer, - buffer_size, TRUE); + if (total_read == -1) { + result = GNOME_VFS_ERROR_IO; + total_read = 0; + } - if ( !myth_handle->is_livetv ) { - if ( len < 0 ) { - g_byte_array_free (tmp_buffer, TRUE); - g_debug ("Fail to read bytes"); - return GNOME_VFS_ERROR_IO; - } else if ( len == 0 ) { - g_byte_array_free (tmp_buffer, TRUE); - g_warning ("End of file probably achieved"); - return GNOME_VFS_ERROR_EOF; - } - } /* if */ - - myth_handle->buffer = - g_byte_array_append (myth_handle->buffer, - tmp_buffer->data, len); + if (total_read < num_bytes) { + result = GNOME_VFS_ERROR_EOF; + } - myth_handle->buffer_remain += len; + g_debug ("F: %s L: %d", __FUNCTION__, __LINE__); - if (tmp_buffer != NULL) { - g_byte_array_free (tmp_buffer, TRUE); - tmp_buffer = NULL; - } - } /* while - iterates until fills the internal buffer */ + + if (total_read > 0) { + g_memmove (buffer, myth_buffer->data, total_read); + g_byte_array_free (myth_buffer, TRUE); + myth_handle->offset += total_read; + } else { + g_debug ("Nao leu nada"); + } - } - - /* if - got from the network, or not */ - bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? - myth_handle->buffer_remain : bytes_to_read; - /* gets the first buffer_size bytes from the byte array buffer variable */ - g_memmove (buffer, myth_handle->buffer->data, bytes_to_read); + *bytes_read = (GnomeVFSFileSize) total_read; - myth_handle->bytes_read += bytes_to_read; - myth_handle->buffer_remain -= bytes_to_read; - - /* flushs the newly buffer got from byte array */ - myth_handle->buffer = - g_byte_array_remove_range (myth_handle->buffer, 0, - bytes_to_read); - g_debug - ("Got from %llu bytes from internal buffer. (there are %d bytes in the buffer, from a total of %llu dispatched.)\n", - bytes_to_read, myth_handle->buffer_remain, - myth_handle->bytes_read); - - *bytes_read = bytes_to_read; - - return GNOME_VFS_OK; + return result; } static GnomeVFSResult -do_close (GnomeVFSMethod * method, - GnomeVFSMethodHandle * method_handle, GnomeVFSContext * context) +do_write (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + gconstpointer buffer, + GnomeVFSFileSize num_bytes, + GnomeVFSFileSize *bytes_written, + GnomeVFSContext *context) { + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} - MythtvHandle *myth_handle = (MythtvHandle *) method_handle; +static GnomeVFSResult +do_seek (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + GnomeVFSSeekPosition whence, + GnomeVFSFileOffset offset, + GnomeVFSContext *context) +{ + MythtvHandle *myth_handle; + guint64 whence_p = 0; + gint64 new_offset =0; - //if ( NULL == myth_handle || myth_handle->configured ) { + _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL); - if (myth_handle->is_livetv && myth_handle->livetv != NULL) { - g_object_unref (myth_handle->livetv); - myth_handle->livetv = NULL; - } - - if (myth_handle->file_transfer != NULL) { - g_object_unref (myth_handle->file_transfer); - myth_handle->file_transfer = NULL; - } + myth_handle = (MythtvHandle *) method_handle; + + g_debug ("seek offset%"G_GINT64_FORMAT" whence %d", offset, whence); - if (myth_handle->buffer) { - g_byte_array_free (myth_handle->buffer, TRUE); - myth_handle->buffer = NULL; - } + if (gmyth_uri_is_livetv (myth_handle->gmyth_uri)) + return GNOME_VFS_ERROR_NOT_SUPPORTED; + + switch (whence) + { + case GNOME_VFS_SEEK_START: + whence_p = 0; + break; + case GNOME_VFS_SEEK_CURRENT: + whence_p = myth_handle->offset; + break; + case GNOME_VFS_SEEK_END: + return GNOME_VFS_ERROR_NOT_SUPPORTED; + } + + new_offset = gmyth_file_transfer_seek (myth_handle->file_transfer, offset, whence_p); + if (new_offset != 0) { + myth_handle->offset = new_offset; + return GNOME_VFS_OK; + } - myth_handle->configured = FALSE; + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} - g_free (myth_handle->channel_name); +static GnomeVFSResult +do_tell (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + GnomeVFSFileSize *offset_return) +{ + MythtvHandle *myth_handle = NULL; - g_free (myth_handle); + _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL); + + myth_handle = (MythtvHandle *) method_handle; + *offset_return = myth_handle->offset; - myth_handle = NULL; + return GNOME_VFS_OK; +} - // } +static GnomeVFSResult +do_truncate_handle (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + GnomeVFSFileSize where, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} - return GNOME_VFS_OK; +static GnomeVFSResult +do_open_directory (GnomeVFSMethod *method, + GnomeVFSMethodHandle **method_handle, + GnomeVFSURI *uri, + GnomeVFSFileInfoOptions options, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; } static GnomeVFSResult +do_close_directory (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} + +static GnomeVFSResult +do_read_directory (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + GnomeVFSFileInfo *file_info, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} + + +static GnomeVFSResult do_get_file_info (GnomeVFSMethod * method, GnomeVFSURI * uri, GnomeVFSFileInfo * file_info, GnomeVFSFileInfoOptions options, GnomeVFSContext * context) { - GMythFileTransfer *file_transfer = NULL; - GMythRecorder *recorder = NULL; - GMythTVChain *tvchain = NULL; - GMythBackendInfo *backend_info = NULL; - GMythURI *gmyth_uri = NULL; - GMythSocket *socket = NULL; - gboolean is_livetv = FALSE; - gchar *tmp_str1; - gchar *tmp_str2; + return myth_get_file_info (NULL, uri, file_info); +} - gboolean ret = TRUE; - gboolean res = TRUE; +static GnomeVFSResult +do_get_file_info_from_handle (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + GnomeVFSFileInfo *file_info, + GnomeVFSFileInfoOptions options, + GnomeVFSContext *context) +{ + MythtvHandle *myth_handle = (MythtvHandle *) method_handle; - /* Creates and fills out the backend info structure */ - tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE); - tmp_str2 = gnome_vfs_unescape_string (tmp_str1, ""); - - backend_info = gmyth_backend_info_new_with_uri (tmp_str2); - g_free (tmp_str1); - g_free (tmp_str2); - - /* creates an instance of */ - tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE); - tmp_str2 = gnome_vfs_unescape_string (tmp_str1, ""); - - gmyth_uri = gmyth_uri_new_with_value (tmp_str2); - g_free (tmp_str1); - g_free (tmp_str2); - - is_livetv = gmyth_uri_is_livetv (gmyth_uri); - - file_info->valid_fields = 0; - file_info->valid_fields = file_info->valid_fields - | GNOME_VFS_FILE_INFO_FIELDS_TYPE - | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE - | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS; - file_info->type = GNOME_VFS_FILE_TYPE_REGULAR; - /* fixme: get from file extension? */ - file_info->mime_type = g_strdup ("video/x-nuv"); - file_info->permissions = - GNOME_VFS_PERM_USER_READ | - GNOME_VFS_PERM_OTHER_READ | GNOME_VFS_PERM_GROUP_READ; - - g_debug ("gnome_vfs_uri == %s | gmyth_uri == %s.\n", - gnome_vfs_uri_get_path (uri), - gmyth_uri_get_path (gmyth_uri)); - - /* Connect to the backend */ - if (gmyth_uri != NULL && is_livetv == TRUE) { - - /* start to get file info from LiveTV remote encoder */ - socket = gmyth_socket_new (); - - /* FIME: Implement this at gmyth_socket */ - res = - gmyth_socket_connect_to_backend (socket, - backend_info->hostname, - backend_info->port, - TRUE); - if (!res) { - g_debug ("[%s] LiveTV can not connect to backend", __FUNCTION__); - res = FALSE; - goto error; - } - - if (gmyth_remote_util_get_free_recorder_count (socket) <= 0) { - g_debug ("No free remote encoder available."); - res = FALSE; - goto error; - } - - /* Gets the recorder num */ - recorder = remote_request_next_free_recorder (socket, -1); - - if (socket != NULL) - g_object_unref (socket); - - if (recorder == NULL) { - g_debug ("[%s] None remote encoder available", __FUNCTION__); - res = FALSE; - goto error; - } - - /* Init remote encoder. Opens its control socket. */ - res = gmyth_recorder_setup (recorder); - if (!res) { - g_debug ("[%s] Fail while setting remote encoder\n", __FUNCTION__); - res = FALSE; - goto error; - } - - /* Creates livetv chain handler */ - tvchain = gmyth_tvchain_new (); - gmyth_tvchain_initialize (tvchain, backend_info); - - if (tvchain == NULL || tvchain->tvchain_id == NULL) { - res = FALSE; - goto error; - } - /* Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly) */ - res = gmyth_recorder_spawntv (recorder, gmyth_tvchain_get_id(tvchain)); - if (!res) { - g_warning ("[%s] Fail while spawn tv\n", - __FUNCTION__); - res = FALSE; - goto error; - } - - sleep(1); - - /* DEBUG message */ - GMythProgramInfo *prog_info = - gmyth_recorder_get_current_program_info (recorder); - - if (prog_info != NULL) { - - g_debug ("path = %s", prog_info->pathname->str); - - file_info->name = - g_strdup (g_strrstr - (prog_info->pathname->str, "/")); - - } else { - file_info->name = g_strdup ("LiveTV.nuv"); - /* Size being overrided below ... */ - //file_info->size = gmyth_recorder_get_file_position (recorder); - } - - if (recorder != NULL) - g_object_unref (recorder); - - if (prog_info != NULL) - g_object_unref (prog_info); - - if (tvchain != NULL) - g_object_unref (tvchain); - - //file_info->size = (GnomeVFSFileSize) - 1; - - } else { - - /* start to get file info from remote file encoder */ - file_transfer = gmyth_file_transfer_new (backend_info); - - /* Verifies if the file exists */ - if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) { - g_object_unref (backend_info); - return GNOME_VFS_ERROR_NOT_FOUND; - } - - /* sets the Playback monitor connection */ - ret = gmyth_file_transfer_open (file_transfer, - gmyth_uri_get_path (gmyth_uri)); - - file_info->name = g_strdup (gnome_vfs_uri_get_path (uri)); - - } /* if - LiveTV or not? */ - - if (ret == FALSE) { - g_debug ("MythTV FileTransfer open error\n"); - return GNOME_VFS_ERROR_NOT_OPEN; - } - - /* Just for recorded content */ - if (ret == TRUE && file_transfer != NULL) { - file_info->size = - gmyth_file_transfer_get_filesize (file_transfer); - - file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE; - - if (file_transfer) - g_object_unref (file_transfer); - } - -error: - if (backend_info) - g_object_unref (backend_info); - - if (!res) - return GNOME_VFS_ERROR_IO; - - return GNOME_VFS_OK; + return myth_get_file_info (myth_handle, NULL, file_info); } static gboolean @@ -573,34 +545,146 @@ return FALSE; } +static GnomeVFSResult +do_make_directory (GnomeVFSMethod *method, + GnomeVFSURI *uri, + guint perm, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} + +static GnomeVFSResult +do_remove_directory (GnomeVFSMethod *method, + GnomeVFSURI *uri, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} + +static GnomeVFSResult +do_move (GnomeVFSMethod *method, + GnomeVFSURI *old_uri, + GnomeVFSURI *new_uri, + gboolean force_replace, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} + +static GnomeVFSResult +do_unlink (GnomeVFSMethod *method, + GnomeVFSURI *uri, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} + +static GnomeVFSResult +do_check_same_fs (GnomeVFSMethod *method, + GnomeVFSURI *a, + GnomeVFSURI *b, + gboolean *same_fs_return, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} + +static GnomeVFSResult +do_set_file_info (GnomeVFSMethod *method, + GnomeVFSURI *uri, + const GnomeVFSFileInfo *info, + GnomeVFSSetFileInfoMask mask, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} + +static GnomeVFSResult +do_truncate (GnomeVFSMethod *method, + GnomeVFSURI *uri, + GnomeVFSFileSize where, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} + +static GnomeVFSResult +do_find_directory (GnomeVFSMethod *method, + GnomeVFSURI *near_uri, + GnomeVFSFindDirectoryKind kind, + GnomeVFSURI **result_uri, + gboolean create_if_needed, + gboolean find_if_needed, + guint permissions, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} + +static GnomeVFSResult +do_create_symbolic_link (GnomeVFSMethod *method, + GnomeVFSURI *uri, + const char *target_reference, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_READ_ONLY; +} + +static GnomeVFSResult +do_monitor_add (GnomeVFSMethod *method, + GnomeVFSMethodHandle **method_handle_return, + GnomeVFSURI *uri, + GnomeVFSMonitorType monitor_type) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} + +static GnomeVFSResult +do_monitor_cancel (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} + +static GnomeVFSResult +do_file_control (GnomeVFSMethod *method, + GnomeVFSMethodHandle *method_handle, + const char *operation, + gpointer operation_data, + GnomeVFSContext *context) +{ + return GNOME_VFS_ERROR_NOT_SUPPORTED; +} + static GnomeVFSMethod method = { sizeof (GnomeVFSMethod), - do_open, - NULL, + do_open, + do_create, do_close, - do_read, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, + do_read, + do_write, + do_seek, + do_tell, + do_truncate_handle, + do_open_directory, + do_close_directory, + do_read_directory, do_get_file_info, - NULL, - do_is_local, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, + do_get_file_info_from_handle, + do_is_local, + do_make_directory, + do_remove_directory, + do_move, + do_unlink, + do_check_same_fs, + do_set_file_info, + do_truncate, + do_find_directory, + do_create_symbolic_link, + do_monitor_add, + do_monitor_cancel, + do_file_control };