[svn r121] Some changes to the BackendInfo.
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"
32 #define GST_MYTHTV_ID_NUM 1
33 #define MYTHTV_VERSION_DEFAULT 30
35 #define MYTHTV_BUFFER_SIZE 1024*64
37 static GnomeVFSResult do_read (GnomeVFSMethod *method,
38 GnomeVFSMethodHandle *method_handle,
40 GnomeVFSFileSize num_bytes,
41 GnomeVFSFileSize *bytes_read,
42 GnomeVFSContext *context);
45 GMythFileTransfer *file_transfer;
58 do_open (GnomeVFSMethod *method,
59 GnomeVFSMethodHandle **method_handle,
61 GnomeVFSOpenMode mode,
62 GnomeVFSContext *context)
65 const gchar *user, *password, *host, *transfer_uri;
66 MythtvHandle *myth_handle;
73 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
74 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
76 g_debug ("[%s] Calling do_open function", __FUNCTION__);
78 myth_handle = g_new0 (MythtvHandle, 1);
80 if (mode & GNOME_VFS_OPEN_WRITE) {
81 return GNOME_VFS_ERROR_NOT_PERMITTED;
84 if ((host = gnome_vfs_uri_get_host_name (uri)) == NULL) {
85 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
89 path = gnome_vfs_unescape_string (uri->text, NULL);
90 port = gnome_vfs_uri_get_host_port (uri);
91 user = gnome_vfs_uri_get_user_name (uri);
92 password = gnome_vfs_uri_get_password (uri);
94 transfer_uri = gnome_vfs_uri_to_string (uri,
95 GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD);
97 /* Initialize mythtv handler*/
98 myth_handle->file_transfer = NULL;
99 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
100 myth_handle->bytes_read = 0;
101 myth_handle->content_size = -1;
103 /* Connect to the backend */
104 myth_handle->file_transfer = gmyth_file_transfer_new ();
106 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
108 /* sets the Playback monitor connection */
109 uri_str = g_string_new (transfer_uri);
110 ret = gmyth_file_transfer_open (myth_handle->file_transfer, uri_str);
112 g_printerr ("Mythtv FileTransfer open error\n");
113 return GNOME_VFS_ERROR_NOT_OPEN;
115 g_string_free (uri_str, TRUE);
117 // TODO: Verify if file exists in the backend
119 myth_handle->content_size = myth_handle->file_transfer->filesize;
121 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
122 myth_handle->buffer_remain = 0;
124 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
129 static GnomeVFSResult
130 do_read (GnomeVFSMethod *method,
131 GnomeVFSMethodHandle *method_handle,
133 GnomeVFSFileSize num_bytes,
134 GnomeVFSFileSize *bytes_read,
135 GnomeVFSContext *context)
137 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
138 GnomeVFSFileSize bytes_to_read;
140 g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_remain: %du",
141 myth_handle->content_size, myth_handle->bytes_read, num_bytes,
142 myth_handle->buffer_remain);
146 if (myth_handle->bytes_read >= myth_handle->content_size)
147 return GNOME_VFS_ERROR_EOF;
149 // fixme: change this to min math function
150 if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
151 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
153 bytes_to_read = num_bytes;
155 /* Loop sending the Myth File Transfer request:
156 * Retry whilst authentication fails and we supply it. */
158 if ( bytes_to_read > myth_handle->buffer_remain ) {
159 GByteArray *tmp_buffer = g_byte_array_new();
161 gint len = gmyth_file_transfer_read( myth_handle->file_transfer,
162 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE );
165 return GNOME_VFS_ERROR_IO;
167 myth_handle->buffer = g_byte_array_append( myth_handle->buffer,
168 g_memdup( tmp_buffer->data, len ), len );
170 myth_handle->buffer_remain += len;
172 g_debug ( "Reading %d data from backend", MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain );
174 if ( tmp_buffer != NULL ) {
175 g_byte_array_free( tmp_buffer, TRUE );
181 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
183 g_debug ("Returning %du bytes to gnomevfs", (gint) bytes_to_read);
184 /* gets the first buffer_size bytes from the byte array buffer variable */
186 g_memmove( buffer, myth_handle->buffer->data, bytes_to_read );
188 myth_handle->bytes_read += bytes_to_read;
189 myth_handle->buffer_remain -= bytes_to_read;
191 /* flushs the newly buffer got from byte array */
192 myth_handle->buffer = g_byte_array_remove_range( myth_handle->buffer, 0, bytes_to_read );
193 *bytes_read = bytes_to_read;
198 static GnomeVFSResult
199 do_close (GnomeVFSMethod *method,
200 GnomeVFSMethodHandle *method_handle,
201 GnomeVFSContext *context)
204 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
206 if (myth_handle->file_transfer) {
207 gmyth_file_transfer_close (myth_handle->file_transfer);
208 g_object_unref (myth_handle->file_transfer);
209 myth_handle->file_transfer = NULL;
212 g_free (myth_handle);
217 static GnomeVFSResult
218 do_get_file_info (GnomeVFSMethod *method,
220 GnomeVFSFileInfo *file_info,
221 GnomeVFSFileInfoOptions options,
222 GnomeVFSContext *context)
224 file_info->name = g_strdup ("fixme.txt");
225 file_info->valid_fields = file_info->valid_fields
226 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
227 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
228 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
229 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
230 // fixme: get from file extension?
231 file_info->mime_type = g_strdup ("video/x-nuv");
232 file_info->permissions =
233 GNOME_VFS_PERM_USER_READ |
234 GNOME_VFS_PERM_OTHER_READ |
235 GNOME_VFS_PERM_GROUP_READ;
241 do_is_local (GnomeVFSMethod *method,
242 const GnomeVFSURI *uri)
247 static GnomeVFSMethod method = {
248 sizeof (GnomeVFSMethod),
279 vfs_module_init (const char *method_name, const char *args)
285 vfs_module_shutdown (GnomeVFSMethod *method)