melunko@38: /* melunko@38: * @author Hallyson Melo melunko@38: * melunko@38: * This program is free software; you can redistribute it and/or modify melunko@38: * it under the terms of the GNU Lesser General Public License as published by melunko@38: * the Free Software Foundation; either version 2 of the License, or melunko@38: * (at your option) any later version. melunko@38: * melunko@38: * This program is distributed in the hope that it will be useful, melunko@38: * but WITHOUT ANY WARRANTY; without even the implied warranty of melunko@38: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the melunko@38: * GNU General Public License for more details. melunko@38: * melunko@38: * You should have received a copy of the GNU Lesser General Public License melunko@38: * along with this program; if not, write to the Free Software melunko@38: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA melunko@38: */ melunko@38: melunko@38: #ifdef HAVE_CONFIG_H melunko@38: #include melunko@38: #endif melunko@38: melunko@38: #include melunko@38: #include melunko@38: #include melunko@38: melunko@38: #include melunko@38: #include melunko@38: melunko@38: #include "myth_file_transfer.h" melunko@38: melunko@38: #define GST_MYTHTV_ID_NUM 1 melunko@38: #define MYTHTV_VERSION_DEFAULT 30 melunko@38: melunko@38: static GnomeVFSResult do_read (GnomeVFSMethod *method, melunko@38: GnomeVFSMethodHandle *method_handle, melunko@38: gpointer buffer, melunko@38: GnomeVFSFileSize num_bytes, melunko@38: GnomeVFSFileSize *bytes_read, melunko@38: GnomeVFSContext *context); melunko@38: melunko@38: typedef struct { melunko@38: MythFileTransfer *file_transfer; melunko@38: melunko@38: gint mythtv_version; melunko@38: guint64 content_size; melunko@38: guint64 bytes_read; melunko@38: guint64 read_offset; melunko@38: gboolean live_tv; melunko@38: } MythtvHandle; melunko@38: melunko@38: melunko@38: melunko@38: static GnomeVFSResult melunko@38: do_open (GnomeVFSMethod *method, melunko@38: GnomeVFSMethodHandle **method_handle, melunko@38: GnomeVFSURI *uri, melunko@38: GnomeVFSOpenMode mode, melunko@38: GnomeVFSContext *context) melunko@38: { melunko@38: MythtvHandle *myth_handle = g_new0 (MythtvHandle, 1); melunko@38: melunko@38: const gchar *user, *password, *host, *transfer_uri; melunko@38: guint port; melunko@38: gchar *path; melunko@38: gboolean ret; melunko@38: melunko@38: _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL); melunko@38: _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL); melunko@38: melunko@38: if (mode & GNOME_VFS_OPEN_WRITE) { melunko@38: return GNOME_VFS_ERROR_NOT_PERMITTED; melunko@38: } melunko@38: melunko@38: if ((host = gnome_vfs_uri_get_host_name (uri)) == NULL) { melunko@38: return GNOME_VFS_ERROR_INVALID_HOST_NAME; melunko@38: } melunko@38: melunko@38: /* Parse URI */ melunko@38: path = gnome_vfs_unescape_string (uri->text, NULL); melunko@38: port = gnome_vfs_uri_get_host_port (uri); melunko@38: user = gnome_vfs_uri_get_user_name (uri); melunko@38: password = gnome_vfs_uri_get_password (uri); melunko@38: melunko@38: transfer_uri = gnome_vfs_uri_to_string (uri, melunko@38: GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD); melunko@38: melunko@38: /* Initialize mythtv handler*/ melunko@38: myth_handle->file_transfer = NULL; melunko@38: myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT; melunko@38: myth_handle->bytes_read = 0; melunko@38: myth_handle->content_size = -1; melunko@38: myth_handle->read_offset = 0; melunko@38: myth_handle->live_tv = FALSE; melunko@38: melunko@38: /* Connect to the backend */ melunko@38: myth_handle->file_transfer = myth_file_transfer_new ( GST_MYTHTV_ID_NUM /*mythtv->live_tv_id*/, melunko@38: g_string_new( transfer_uri ), -1, myth_handle->mythtv_version ); melunko@38: melunko@38: if ( myth_handle->file_transfer == NULL ) { melunko@38: return GNOME_VFS_ERROR_NOT_OPEN; melunko@38: } melunko@38: melunko@38: /* sets the Playback monitor connection */ melunko@38: ret = myth_file_transfer_playback_setup( &(myth_handle->file_transfer), myth_handle->live_tv ); melunko@38: if (ret == FALSE) { melunko@38: g_printerr ("Mythtv FileTransfer playback setup error\n"); melunko@38: return GNOME_VFS_ERROR_NOT_OPEN; melunko@38: } melunko@38: melunko@38: /* sets the FileTransfer instance connection (video/audio download) */ melunko@38: ret = myth_file_transfer_setup( &(myth_handle->file_transfer), myth_handle->live_tv ); melunko@38: melunko@38: if ( ret == FALSE ) { melunko@38: g_printerr ("MythTV FileTransfer request failed when setting up socket connection!\n" ); melunko@38: return GNOME_VFS_ERROR_NOT_OPEN; melunko@38: } melunko@38: melunko@38: // TODO: Verify if file exists in the backend melunko@38: melunko@38: myth_handle->content_size = myth_handle->file_transfer->filesize; melunko@38: melunko@38: *method_handle = (GnomeVFSMethodHandle *) myth_handle; melunko@38: melunko@38: return GNOME_VFS_OK; melunko@38: } melunko@38: melunko@38: static GnomeVFSResult melunko@38: do_read (GnomeVFSMethod *method, melunko@38: GnomeVFSMethodHandle *method_handle, melunko@38: gpointer buffer, melunko@38: GnomeVFSFileSize num_bytes, melunko@38: GnomeVFSFileSize *bytes_read, melunko@38: GnomeVFSContext *context) melunko@38: { melunko@38: MythtvHandle *myth_handle = (MythtvHandle *) method_handle; melunko@38: guint len = 0, offset = 0; melunko@38: GnomeVFSFileSize bytes_to_read; melunko@38: melunko@38: g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu", myth_handle->content_size, melunko@38: myth_handle->bytes_read, num_bytes); melunko@38: melunko@38: *bytes_read = 0; melunko@38: melunko@38: if (myth_handle->bytes_read >= myth_handle->content_size) melunko@38: return GNOME_VFS_ERROR_EOF; melunko@38: melunko@38: // fixme: change this to min math function melunko@38: if (num_bytes > myth_handle->content_size - myth_handle->bytes_read) melunko@38: bytes_to_read = myth_handle->content_size - myth_handle->bytes_read; melunko@38: else melunko@38: bytes_to_read = num_bytes; melunko@38: melunko@38: /* Loop sending the Myth File Transfer request: melunko@38: * Retry whilst authentication fails and we supply it. */ melunko@38: melunko@38: // lock here ??? melunko@38: while ( bytes_to_read > 0 ) { melunko@38: len = myth_file_transfer_read( myth_handle->file_transfer, melunko@38: buffer + offset, bytes_to_read, TRUE ); melunko@38: melunko@38: if ( len > 0 ) { melunko@38: offset += len; melunko@38: bytes_to_read -= len; melunko@38: } else { melunko@38: break; melunko@38: } melunko@38: melunko@38: if ( offset == num_bytes ) melunko@38: break; melunko@38: } melunko@38: melunko@38: if (( offset <= 0 ) && !myth_handle->live_tv ) melunko@38: return GNOME_VFS_ERROR_EOF; melunko@38: melunko@38: myth_handle->read_offset += offset; melunko@38: myth_handle->bytes_read += offset; melunko@38: *bytes_read = offset; melunko@38: melunko@38: return GNOME_VFS_OK; melunko@38: } melunko@38: melunko@38: static GnomeVFSResult melunko@38: do_close (GnomeVFSMethod *method, melunko@38: GnomeVFSMethodHandle *method_handle, melunko@38: GnomeVFSContext *context) melunko@38: { melunko@38: MythtvHandle *myth_handle = (MythtvHandle *) method_handle; melunko@38: melunko@38: if (myth_handle->file_transfer) melunko@38: g_object_unref (myth_handle->file_transfer); melunko@38: melunko@38: g_free (myth_handle); melunko@38: melunko@38: return GNOME_VFS_OK; melunko@38: } melunko@38: melunko@38: static GnomeVFSResult melunko@38: do_get_file_info (GnomeVFSMethod *method, melunko@38: GnomeVFSURI *uri, melunko@38: GnomeVFSFileInfo *file_info, melunko@38: GnomeVFSFileInfoOptions options, melunko@38: GnomeVFSContext *context) melunko@38: { melunko@38: GString *name = g_string_new ("hallyson"); melunko@38: file_info->name = g_string_free (name, FALSE);//"hallyson.txt";//get_base_from_uri (uri); melunko@38: file_info->valid_fields = file_info->valid_fields melunko@38: | GNOME_VFS_FILE_INFO_FIELDS_TYPE melunko@38: | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE melunko@38: | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS; melunko@38: file_info->type = GNOME_VFS_FILE_TYPE_REGULAR; melunko@38: file_info->mime_type = "video/x-nuv"; //g_strdup (gnome_vfs_mime_type_from_name (file_info->name)); melunko@38: file_info->permissions = melunko@38: GNOME_VFS_PERM_USER_READ | melunko@38: GNOME_VFS_PERM_OTHER_READ | melunko@38: GNOME_VFS_PERM_GROUP_READ; melunko@38: melunko@38: return GNOME_VFS_OK; melunko@38: } melunko@38: melunko@38: static gboolean melunko@38: do_is_local (GnomeVFSMethod *method, melunko@38: const GnomeVFSURI *uri) melunko@38: { melunko@38: return FALSE; melunko@38: } melunko@38: melunko@38: static GnomeVFSMethod method = { melunko@38: sizeof (GnomeVFSMethod), melunko@38: do_open, melunko@38: NULL, melunko@38: do_close, melunko@38: do_read, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: do_get_file_info, melunko@38: NULL, melunko@38: do_is_local, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: NULL, melunko@38: }; melunko@38: melunko@38: melunko@38: GnomeVFSMethod * melunko@38: vfs_module_init (const char *method_name, const char *args) melunko@38: { melunko@38: return &method; melunko@38: } melunko@38: melunko@38: void melunko@38: vfs_module_shutdown (GnomeVFSMethod *method) melunko@38: { melunko@38: }