[svn r116] A lot of API changes.
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;
59 do_open (GnomeVFSMethod *method,
60 GnomeVFSMethodHandle **method_handle,
62 GnomeVFSOpenMode mode,
63 GnomeVFSContext *context)
66 const gchar *user, *password, *host, *transfer_uri;
67 MythtvHandle *myth_handle;
74 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
75 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
77 g_debug ("[%s] Calling do_open function", __FUNCTION__);
79 myth_handle = g_new0 (MythtvHandle, 1);
81 if (mode & GNOME_VFS_OPEN_WRITE) {
82 return GNOME_VFS_ERROR_NOT_PERMITTED;
85 if ((host = gnome_vfs_uri_get_host_name (uri)) == NULL) {
86 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
90 path = gnome_vfs_unescape_string (uri->text, NULL);
91 port = gnome_vfs_uri_get_host_port (uri);
92 user = gnome_vfs_uri_get_user_name (uri);
93 password = gnome_vfs_uri_get_password (uri);
95 transfer_uri = gnome_vfs_uri_to_string (uri,
96 GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD);
98 /* Initialize mythtv handler*/
99 myth_handle->file_transfer = NULL;
100 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
101 myth_handle->bytes_read = 0;
102 myth_handle->content_size = -1;
104 /* Connect to the backend */
105 myth_handle->file_transfer = gmyth_file_transfer_new ();
107 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
109 /* sets the Playback monitor connection */
110 uri_str = g_string_new (transfer_uri);
111 ret = gmyth_file_transfer_open (myth_handle->file_transfer, uri_str);
113 g_printerr ("Mythtv FileTransfer open error\n");
114 return GNOME_VFS_ERROR_NOT_OPEN;
116 g_string_free (uri_str, TRUE);
118 // TODO: Verify if file exists in the backend
120 myth_handle->content_size = myth_handle->file_transfer->filesize;
122 myth_handle->buffer = g_malloc0 (MYTHTV_BUFFER_SIZE);
123 myth_handle->buffer_offset = 0;
124 myth_handle->buffer_remain = 0;
126 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
131 static GnomeVFSResult
132 do_read (GnomeVFSMethod *method,
133 GnomeVFSMethodHandle *method_handle,
135 GnomeVFSFileSize num_bytes,
136 GnomeVFSFileSize *bytes_read,
137 GnomeVFSContext *context)
139 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
140 GnomeVFSFileSize bytes_to_read;
142 g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_offset: %du, buffer_remain: %du",
143 myth_handle->content_size, myth_handle->bytes_read, num_bytes,
144 myth_handle->buffer_offset, myth_handle->buffer_remain);
148 if (myth_handle->bytes_read >= myth_handle->content_size)
149 return GNOME_VFS_ERROR_EOF;
151 // fixme: change this to min math function
152 if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
153 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
155 bytes_to_read = num_bytes;
157 /* Loop sending the Myth File Transfer request:
158 * Retry whilst authentication fails and we supply it. */
160 if (bytes_to_read > myth_handle->buffer_remain ) {
161 guint8 *tmp_buffer = g_malloc0 (MYTHTV_BUFFER_SIZE);
164 if (myth_handle->buffer_remain > (MYTHTV_BUFFER_SIZE >> 1)) {
165 // Avoid overlap creating another buffer, in the case the remaining data is bigger than half buffer size
166 tmp_buffer = g_malloc0 (MYTHTV_BUFFER_SIZE);
167 memcpy (tmp_buffer, myth_handle->buffer + myth_handle->buffer_offset, myth_handle->buffer_remain);
168 g_free (myth_handle->buffer);
169 myth_handle->buffer = tmp_buffer;
171 memcpy (myth_handle->buffer, myth_handle->buffer + myth_handle->buffer_offset, myth_handle->buffer_remain);
174 g_debug ("Reading %d data from backend", MYTHTV_BUFFER_SIZE- myth_handle->buffer_remain);
176 len = gmyth_file_transfer_read( myth_handle->file_transfer,
177 myth_handle->buffer + myth_handle->buffer_remain,
178 MYTHTV_BUFFER_SIZE- myth_handle->buffer_remain, TRUE );
181 return GNOME_VFS_ERROR_IO;
183 myth_handle->buffer_offset = 0;
184 myth_handle->buffer_remain += len;
188 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
190 g_debug ("Returning %du bytes to gnomevfs", (int) bytes_to_read);
191 memcpy (buffer, myth_handle->buffer + myth_handle->buffer_offset, bytes_to_read);
193 myth_handle->bytes_read += bytes_to_read;
194 myth_handle->buffer_offset += bytes_to_read;
195 myth_handle->buffer_remain -= bytes_to_read;
196 *bytes_read = bytes_to_read;
201 static GnomeVFSResult
202 do_close (GnomeVFSMethod *method,
203 GnomeVFSMethodHandle *method_handle,
204 GnomeVFSContext *context)
207 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
209 if (myth_handle->file_transfer) {
210 gmyth_file_transfer_close (myth_handle->file_transfer);
211 g_object_unref (myth_handle->file_transfer);
212 myth_handle->file_transfer = NULL;
215 g_free (myth_handle);
220 static GnomeVFSResult
221 do_get_file_info (GnomeVFSMethod *method,
223 GnomeVFSFileInfo *file_info,
224 GnomeVFSFileInfoOptions options,
225 GnomeVFSContext *context)
227 file_info->name = g_strdup ("fixme.txt");
228 file_info->valid_fields = file_info->valid_fields
229 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
230 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
231 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
232 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
233 // fixme: get from file extension?
234 file_info->mime_type = g_strdup ("video/x-nuv");
235 file_info->permissions =
236 GNOME_VFS_PERM_USER_READ |
237 GNOME_VFS_PERM_OTHER_READ |
238 GNOME_VFS_PERM_GROUP_READ;
244 do_is_local (GnomeVFSMethod *method,
245 const GnomeVFSURI *uri)
250 static GnomeVFSMethod method = {
251 sizeof (GnomeVFSMethod),
282 vfs_module_init (const char *method_name, const char *args)
288 vfs_module_shutdown (GnomeVFSMethod *method)