2 * @author Hallyson Melo <hallyson.melo@indt.org.br>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <libgnomevfs/gnome-vfs-module.h>
28 #include <libgnomevfs/gnome-vfs-utils.h>
30 #include "myth_file_transfer.h"
32 #define GST_MYTHTV_ID_NUM 1
33 #define MYTHTV_VERSION_DEFAULT 30
35 static GnomeVFSResult do_read (GnomeVFSMethod *method,
36 GnomeVFSMethodHandle *method_handle,
38 GnomeVFSFileSize num_bytes,
39 GnomeVFSFileSize *bytes_read,
40 GnomeVFSContext *context);
43 MythFileTransfer *file_transfer;
55 do_open (GnomeVFSMethod *method,
56 GnomeVFSMethodHandle **method_handle,
58 GnomeVFSOpenMode mode,
59 GnomeVFSContext *context)
61 MythtvHandle *myth_handle = g_new0 (MythtvHandle, 1);
63 const gchar *user, *password, *host, *transfer_uri;
68 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
69 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
71 if (mode & GNOME_VFS_OPEN_WRITE) {
72 return GNOME_VFS_ERROR_NOT_PERMITTED;
75 if ((host = gnome_vfs_uri_get_host_name (uri)) == NULL) {
76 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
80 path = gnome_vfs_unescape_string (uri->text, NULL);
81 port = gnome_vfs_uri_get_host_port (uri);
82 user = gnome_vfs_uri_get_user_name (uri);
83 password = gnome_vfs_uri_get_password (uri);
85 transfer_uri = gnome_vfs_uri_to_string (uri,
86 GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD);
88 /* Initialize mythtv handler*/
89 myth_handle->file_transfer = NULL;
90 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
91 myth_handle->bytes_read = 0;
92 myth_handle->content_size = -1;
93 myth_handle->read_offset = 0;
94 myth_handle->live_tv = FALSE;
96 /* Connect to the backend */
97 myth_handle->file_transfer = myth_file_transfer_new ( GST_MYTHTV_ID_NUM /*mythtv->live_tv_id*/,
98 g_string_new( transfer_uri ), -1, myth_handle->mythtv_version );
100 if ( myth_handle->file_transfer == NULL ) {
101 return GNOME_VFS_ERROR_NOT_OPEN;
104 /* sets the Playback monitor connection */
105 ret = myth_file_transfer_playback_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
107 g_printerr ("Mythtv FileTransfer playback setup error\n");
108 return GNOME_VFS_ERROR_NOT_OPEN;
111 /* sets the FileTransfer instance connection (video/audio download) */
112 ret = myth_file_transfer_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
114 if ( ret == FALSE ) {
115 g_printerr ("MythTV FileTransfer request failed when setting up socket connection!\n" );
116 return GNOME_VFS_ERROR_NOT_OPEN;
119 // TODO: Verify if file exists in the backend
121 myth_handle->content_size = myth_handle->file_transfer->filesize;
123 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
128 static GnomeVFSResult
129 do_read (GnomeVFSMethod *method,
130 GnomeVFSMethodHandle *method_handle,
132 GnomeVFSFileSize num_bytes,
133 GnomeVFSFileSize *bytes_read,
134 GnomeVFSContext *context)
136 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
137 guint len = 0, offset = 0;
138 GnomeVFSFileSize bytes_to_read;
140 g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu", myth_handle->content_size,
141 myth_handle->bytes_read, num_bytes);
145 if (myth_handle->bytes_read >= myth_handle->content_size)
146 return GNOME_VFS_ERROR_EOF;
148 // fixme: change this to min math function
149 if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
150 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
152 bytes_to_read = num_bytes;
154 /* Loop sending the Myth File Transfer request:
155 * Retry whilst authentication fails and we supply it. */
158 while ( bytes_to_read > 0 ) {
159 len = myth_file_transfer_read( myth_handle->file_transfer,
160 buffer + offset, bytes_to_read, TRUE );
164 bytes_to_read -= len;
169 if ( offset == num_bytes )
173 if (( offset <= 0 ) && !myth_handle->live_tv )
174 return GNOME_VFS_ERROR_EOF;
176 myth_handle->read_offset += offset;
177 myth_handle->bytes_read += offset;
178 *bytes_read = offset;
183 static GnomeVFSResult
184 do_close (GnomeVFSMethod *method,
185 GnomeVFSMethodHandle *method_handle,
186 GnomeVFSContext *context)
188 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
190 if (myth_handle->file_transfer)
191 g_object_unref (myth_handle->file_transfer);
193 g_free (myth_handle);
198 static GnomeVFSResult
199 do_get_file_info (GnomeVFSMethod *method,
201 GnomeVFSFileInfo *file_info,
202 GnomeVFSFileInfoOptions options,
203 GnomeVFSContext *context)
205 GString *name = g_string_new ("hallyson");
206 file_info->name = g_string_free (name, FALSE);//"hallyson.txt";//get_base_from_uri (uri);
207 file_info->valid_fields = file_info->valid_fields
208 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
209 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
210 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
211 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
212 file_info->mime_type = "video/x-nuv"; //g_strdup (gnome_vfs_mime_type_from_name (file_info->name));
213 file_info->permissions =
214 GNOME_VFS_PERM_USER_READ |
215 GNOME_VFS_PERM_OTHER_READ |
216 GNOME_VFS_PERM_GROUP_READ;
222 do_is_local (GnomeVFSMethod *method,
223 const GnomeVFSURI *uri)
228 static GnomeVFSMethod method = {
229 sizeof (GnomeVFSMethod),
260 vfs_module_init (const char *method_name, const char *args)
266 vfs_module_shutdown (GnomeVFSMethod *method)