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 "gmyth_file_transfer.h"
31 #include "gmyth_backendinfo.h"
33 #define GST_MYTHTV_ID_NUM 1
34 #define MYTHTV_VERSION_DEFAULT 30
36 #define MYTHTV_BUFFER_SIZE 1024*64
38 static GnomeVFSResult do_read (GnomeVFSMethod *method,
39 GnomeVFSMethodHandle *method_handle,
41 GnomeVFSFileSize num_bytes,
42 GnomeVFSFileSize *bytes_read,
43 GnomeVFSContext *context);
46 GMythFileTransfer *file_transfer;
59 do_open (GnomeVFSMethod *method,
60 GnomeVFSMethodHandle **method_handle,
62 GnomeVFSOpenMode mode,
63 GnomeVFSContext *context)
65 MythtvHandle *myth_handle;
66 GMythBackendInfo *backend_info;
69 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
70 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
72 myth_handle = g_new0 (MythtvHandle, 1);
74 if (mode & GNOME_VFS_OPEN_WRITE) {
75 return GNOME_VFS_ERROR_NOT_PERMITTED;
78 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
79 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
82 /* Initialize mythtv handler*/
83 myth_handle->file_transfer = NULL;
84 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
85 myth_handle->bytes_read = 0;
86 myth_handle->content_size = -1;
88 /* Creates and fills out the backend info structure */
89 backend_info = gmyth_backend_info_new_full (
90 gnome_vfs_uri_get_host_name (uri),
92 gnome_vfs_uri_get_host_port (uri));
94 /* Connect to the backend */
95 myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
96 g_object_unref (backend_info);
98 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
100 /* sets the Playback monitor connection */
101 ret = gmyth_file_transfer_open (myth_handle->file_transfer, gnome_vfs_uri_get_path (uri));
103 g_warning ("Mythtv FileTransfer open error\n");
104 return GNOME_VFS_ERROR_NOT_OPEN;
107 // TODO: Verify if file exists in the backend
109 myth_handle->content_size = myth_handle->file_transfer->filesize;
111 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
112 myth_handle->buffer_remain = 0;
114 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
119 static GnomeVFSResult
120 do_read (GnomeVFSMethod *method,
121 GnomeVFSMethodHandle *method_handle,
123 GnomeVFSFileSize num_bytes,
124 GnomeVFSFileSize *bytes_read,
125 GnomeVFSContext *context)
127 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
128 GnomeVFSFileSize bytes_to_read;
132 if (myth_handle->bytes_read >= myth_handle->content_size)
133 return GNOME_VFS_ERROR_EOF;
135 // fixme: change this to min math function
136 if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
137 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
139 bytes_to_read = num_bytes;
141 /* Loop sending the Myth File Transfer request:
142 * Retry whilst authentication fails and we supply it. */
144 if ( bytes_to_read > myth_handle->buffer_remain ) {
145 GByteArray *tmp_buffer = g_byte_array_new();
147 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
148 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
151 g_byte_array_free (tmp_buffer, TRUE);
152 g_warning ("Fail to read bytes");
153 return GNOME_VFS_ERROR_IO;
156 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
157 tmp_buffer->data, len);
159 myth_handle->buffer_remain += len;
161 g_byte_array_free (tmp_buffer, TRUE);
165 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
166 /* gets the first buffer_size bytes from the byte array buffer variable */
168 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
170 myth_handle->bytes_read += bytes_to_read;
171 myth_handle->buffer_remain -= bytes_to_read;
173 /* flushs the newly buffer got from byte array */
174 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
175 *bytes_read = bytes_to_read;
180 static GnomeVFSResult
181 do_close (GnomeVFSMethod *method,
182 GnomeVFSMethodHandle *method_handle,
183 GnomeVFSContext *context)
186 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
188 if (myth_handle->file_transfer) {
189 gmyth_file_transfer_close (myth_handle->file_transfer);
190 g_object_unref (myth_handle->file_transfer);
191 myth_handle->file_transfer = NULL;
194 if (myth_handle->buffer) {
195 g_byte_array_free (myth_handle->buffer, TRUE);
196 myth_handle->buffer = NULL;
199 g_free (myth_handle);
204 static GnomeVFSResult
205 do_get_file_info (GnomeVFSMethod *method,
207 GnomeVFSFileInfo *file_info,
208 GnomeVFSFileInfoOptions options,
209 GnomeVFSContext *context)
211 GMythFileTransfer *file_transfer = NULL;
212 GMythBackendInfo *backend_info = NULL;
214 file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
215 file_info->valid_fields = file_info->valid_fields
216 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
217 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
218 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
219 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
220 // fixme: get from file extension?
221 file_info->mime_type = g_strdup ("video/x-nuv");
222 file_info->permissions =
223 GNOME_VFS_PERM_USER_READ |
224 GNOME_VFS_PERM_OTHER_READ |
225 GNOME_VFS_PERM_GROUP_READ;
227 backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
228 gnome_vfs_uri_get_user_name (uri),
229 gnome_vfs_uri_get_password (uri),
231 gnome_vfs_uri_get_host_port (uri));
233 file_transfer = gmyth_file_transfer_new (backend_info);
234 if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
235 file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
236 file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
237 file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
240 g_object_unref (file_transfer);
241 g_object_unref (backend_info);
246 do_is_local (GnomeVFSMethod *method,
247 const GnomeVFSURI *uri)
252 static GnomeVFSMethod method = {
253 sizeof (GnomeVFSMethod),
284 vfs_module_init (const char *method_name, const char *args)
290 vfs_module_shutdown (GnomeVFSMethod *method)