[svn r465] removed seek imp.
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
25 #include <glib/gprintf.h>
26 #include <glib/gstdio.h>
29 #include <libgnomevfs/gnome-vfs-module.h>
30 #include <libgnomevfs/gnome-vfs-utils.h>
32 #include <gmyth/gmyth_file_transfer.h>
33 #include <gmyth/gmyth_livetv.h>
34 #include <gmyth/gmyth_uri.h>
35 #include <gmyth/gmyth_recorder.h>
36 #include <gmyth/gmyth_backendinfo.h>
37 #include <gmyth/gmyth_util.h>
38 #include <gmyth/gmyth_remote_util.h>
39 #include <gmyth/gmyth_tvchain.h>
40 #include <gmyth/gmyth_programinfo.h>
42 #define GST_MYTHTV_ID_NUM 1
43 #define MYTHTV_VERSION_DEFAULT 30
44 #define MYTHTV_TRANSFER_MAX_WAITS 100
46 /* internal GnomeVFS plug-in buffer size ( 120 Kbytes ) */
47 #define MYTHTV_BUFFER_SIZE 80*1024
48 /* internally sized GnomeVFS plug-in buffer ( 4 Kbytes ) */
49 #define MYTHTV_MAX_VFS_BUFFER_SIZE 4096
50 /* maximum number of bytes to be requested to the MythTV backend ( 64 Kbytes ) */
51 #define MYTHTV_MAX_REQUEST_SIZE 64*1024
54 GMythFileTransfer *file_transfer;
56 GMythBackendInfo *backend_info;
58 GMythRecorder *live_recorder;
68 static GnomeVFSResult do_read (GnomeVFSMethod * method,
69 GnomeVFSMethodHandle * method_handle,
71 GnomeVFSFileSize num_bytes,
72 GnomeVFSFileSize * bytes_read,
73 GnomeVFSContext * context);
75 static GnomeVFSResult myth_connection_start (MythtvHandle * method_handle);
76 static void myth_destroy_handle (MythtvHandle * method_handle);
77 static GnomeVFSResult myth_handle_new (GnomeVFSURI * uri,
78 MythtvHandle ** method_handle);
79 static GnomeVFSResult myth_get_file_info (MythtvHandle * myth_handle,
81 GnomeVFSFileInfo * info);
84 myth_handle_new (GnomeVFSURI * uri,
85 MythtvHandle ** method_handle)
90 _GNOME_VFS_METHOD_PARAM_CHECK (*method_handle == NULL);
92 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
93 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
96 *method_handle = g_new0 (MythtvHandle, 1);
97 (*method_handle)->mythtv_version = MYTHTV_VERSION_DEFAULT;
99 tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
100 tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
102 (*method_handle)->backend_info = gmyth_backend_info_new_with_uri (tmp_str2);
103 (*method_handle)->gmyth_uri = gmyth_uri_new_with_value (tmp_str2);
111 myth_destroy_handle (MythtvHandle * method_handle)
113 //TODO: abort if in tranfer state
115 if (method_handle->backend_info != NULL) {
116 g_object_unref (method_handle->backend_info);
117 method_handle->backend_info = NULL;
120 if (method_handle->channel_name != NULL) {
121 g_free (method_handle->channel_name);
122 method_handle->channel_name = NULL;
125 if (method_handle->livetv != NULL) {
126 g_object_unref (method_handle->livetv);
127 method_handle->livetv = NULL;
130 if (method_handle->file_transfer != NULL) {
131 g_object_unref (method_handle->file_transfer);
132 method_handle->file_transfer = NULL;
135 if (method_handle->gmyth_uri != NULL) {
136 g_object_unref (method_handle->gmyth_uri);
137 method_handle->gmyth_uri = NULL;
140 g_free (method_handle);
143 static GnomeVFSResult
144 myth_get_file_info (MythtvHandle * myth_handle,
146 GnomeVFSFileInfo * info)
149 GMythBackendInfo *backend_info;
151 _GNOME_VFS_METHOD_PARAM_CHECK (info != NULL);
153 if (myth_handle == NULL) {
157 tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
158 tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
160 backend_info = gmyth_backend_info_new_with_uri (tmp_str2);
161 gmyth_uri = gmyth_uri_new_with_value (tmp_str2);
166 backend_info = g_object_ref (myth_handle->backend_info);
167 gmyth_uri = g_object_ref (myth_handle->gmyth_uri);
170 info->valid_fields = 0;
171 info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE |
172 GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE |
173 GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
175 info->type = GNOME_VFS_FILE_TYPE_REGULAR;
177 /* fixme: get from file extension? */
178 info->mime_type = g_strdup ("video/x-nuv");
179 info->permissions = GNOME_VFS_PERM_USER_READ |
180 GNOME_VFS_PERM_OTHER_READ |
181 GNOME_VFS_PERM_GROUP_READ;
184 info->name = g_strdup (gmyth_uri_get_path (gmyth_uri));
186 /* file size for remote files */
187 if (gmyth_uri_is_livetv (gmyth_uri) == FALSE) {
188 GMythFileTransfer *file_transfer = gmyth_file_transfer_new (backend_info);
190 /* Verifies if the file exists */
191 if (!gmyth_util_file_exists (backend_info,
192 gmyth_uri_get_path (gmyth_uri))) {
193 g_object_unref (backend_info);
194 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
195 return GNOME_VFS_ERROR_NOT_FOUND;
198 info->size = gmyth_file_transfer_get_filesize (file_transfer);
199 info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE;
200 g_object_unref (file_transfer);
203 g_object_unref (backend_info);
204 g_object_unref (gmyth_uri);
209 static GnomeVFSResult
210 myth_connection_start (MythtvHandle * method_handle)
212 GnomeVFSResult result = GNOME_VFS_OK;
214 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
215 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle->backend_info != NULL);
218 /* Connect to the backend */
219 if (gmyth_uri_is_livetv (method_handle->gmyth_uri) == TRUE) {
222 method_handle->livetv = gmyth_livetv_new (method_handle->backend_info);
223 method_handle->channel_name = gmyth_uri_get_channel_name (method_handle->gmyth_uri);
225 g_debug ("[%s] Channel name = %s\n", __FUNCTION__,
226 method_handle->channel_name);
228 if (method_handle->channel_name != NULL) {
229 if (gmyth_livetv_channel_name_setup (method_handle->livetv,
230 method_handle->channel_name) == FALSE) {
231 result = GNOME_VFS_ERROR_INVALID_URI;
234 } else if (gmyth_livetv_setup (method_handle->livetv) == FALSE) {
235 result = GNOME_VFS_ERROR_INVALID_URI;
240 method_handle->file_transfer =
241 gmyth_livetv_create_file_transfer (method_handle->livetv);
243 if (method_handle->file_transfer == NULL) {
244 result = GNOME_VFS_ERROR_INVALID_URI;
245 g_debug ("MythTV FileTransfer is NULL!\n");
249 if (!gmyth_file_transfer_open (method_handle->file_transfer,
250 method_handle->livetv->uri != NULL ?
251 gmyth_uri_get_path (method_handle->livetv->uri) :
252 method_handle->livetv->proginfo->pathname->str)) {
254 g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
255 result = GNOME_VFS_ERROR_NOT_OPEN;
260 method_handle->file_transfer =
261 gmyth_file_transfer_new (method_handle->backend_info);
263 /* Verifies if the file exists */
264 if (!gmyth_util_file_exists (method_handle->backend_info,
265 gmyth_uri_get_path (method_handle->gmyth_uri))) {
267 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
268 result = GNOME_VFS_ERROR_NOT_FOUND;
272 /* sets the Playback monitor connection */
273 result = gmyth_file_transfer_open (method_handle->file_transfer,
274 gmyth_uri_get_path (method_handle->gmyth_uri));
275 } /* if - LiveTV or not? */
277 method_handle->configured = TRUE;
279 if (method_handle->file_transfer == NULL) {
280 result = GNOME_VFS_ERROR_NOT_OPEN;
283 g_return_val_if_fail (method_handle->file_transfer != NULL,
284 GNOME_VFS_ERROR_NOT_OPEN);
291 static GnomeVFSResult
292 do_open (GnomeVFSMethod * method,
293 GnomeVFSMethodHandle ** method_handle,
295 GnomeVFSOpenMode mode, GnomeVFSContext * context)
297 MythtvHandle *myth_handle = NULL;
298 GnomeVFSResult result = GNOME_VFS_OK;
300 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
301 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
303 if (mode & GNOME_VFS_OPEN_WRITE) {
304 return GNOME_VFS_ERROR_INVALID_OPEN_MODE;
307 result = myth_handle_new (uri, &myth_handle);
308 if (result != GNOME_VFS_OK)
311 result = myth_connection_start (myth_handle);
312 if (result != GNOME_VFS_OK) {
313 myth_destroy_handle (myth_handle);
317 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
322 static GnomeVFSResult
323 do_create (GnomeVFSMethod *method,
324 GnomeVFSMethodHandle **method_handle,
326 GnomeVFSOpenMode mode,
329 GnomeVFSContext *context)
331 return GNOME_VFS_ERROR_NOT_SUPPORTED;
334 static GnomeVFSResult
335 do_close (GnomeVFSMethod * method,
336 GnomeVFSMethodHandle * method_handle,
337 GnomeVFSContext * context)
339 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
341 myth_destroy_handle (myth_handle);
347 static GnomeVFSResult
348 do_read (GnomeVFSMethod * method,
349 GnomeVFSMethodHandle * method_handle,
351 GnomeVFSFileSize num_bytes,
352 GnomeVFSFileSize * bytes_read,
353 GnomeVFSContext * context)
355 GnomeVFSResult result;
356 MythtvHandle *myth_handle;
357 gint64 total_read = 0;
358 GByteArray *myth_buffer = g_byte_array_new ();
360 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
363 myth_handle = (MythtvHandle *) method_handle;
364 result = GNOME_VFS_OK;
366 total_read = gmyth_file_transfer_read (myth_handle->file_transfer,
368 num_bytes, gmyth_uri_is_livetv (myth_handle->gmyth_uri));
371 if (total_read == -1) {
372 result = GNOME_VFS_ERROR_IO;
376 if (total_read < num_bytes) {
377 result = GNOME_VFS_ERROR_EOF;
380 if (total_read > 0) {
381 g_memmove (buffer, myth_buffer->data, total_read);
382 g_byte_array_free (myth_buffer, TRUE);
383 myth_handle->offset += total_read;
386 *bytes_read = (GnomeVFSFileSize) total_read;
391 static GnomeVFSResult
392 do_write (GnomeVFSMethod *method,
393 GnomeVFSMethodHandle *method_handle,
394 gconstpointer buffer,
395 GnomeVFSFileSize num_bytes,
396 GnomeVFSFileSize *bytes_written,
397 GnomeVFSContext *context)
399 return GNOME_VFS_ERROR_NOT_SUPPORTED;
402 static GnomeVFSResult
403 do_seek (GnomeVFSMethod *method,
404 GnomeVFSMethodHandle *method_handle,
405 GnomeVFSSeekPosition whence,
406 GnomeVFSFileOffset offset,
407 GnomeVFSContext *context)
409 MythtvHandle *myth_handle;
410 //guint64 whence_p = 0;
411 //gint64 new_offset =0;
413 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
415 myth_handle = (MythtvHandle *) method_handle;
417 g_debug ("seek offset%"G_GINT64_FORMAT" whence %d", offset, whence);
419 return GNOME_VFS_ERROR_NOT_SUPPORTED;
421 if (gmyth_uri_is_livetv (myth_handle->gmyth_uri))
425 case GNOME_VFS_SEEK_START:
428 case GNOME_VFS_SEEK_CURRENT:
429 whence_p = myth_handle->offset;
431 case GNOME_VFS_SEEK_END:
432 return GNOME_VFS_ERROR_NOT_SUPPORTED;
435 new_offset = gmyth_file_transfer_seek (myth_handle->file_transfer, offset, whence_p);
436 if (new_offset != 0) {
437 myth_handle->offset = new_offset;
441 return GNOME_VFS_ERROR_NOT_SUPPORTED;
445 static GnomeVFSResult
446 do_tell (GnomeVFSMethod *method,
447 GnomeVFSMethodHandle *method_handle,
448 GnomeVFSFileSize *offset_return)
450 MythtvHandle *myth_handle = NULL;
452 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
454 myth_handle = (MythtvHandle *) method_handle;
455 *offset_return = myth_handle->offset;
460 static GnomeVFSResult
461 do_truncate_handle (GnomeVFSMethod *method,
462 GnomeVFSMethodHandle *method_handle,
463 GnomeVFSFileSize where,
464 GnomeVFSContext *context)
466 return GNOME_VFS_ERROR_READ_ONLY;
469 static GnomeVFSResult
470 do_open_directory (GnomeVFSMethod *method,
471 GnomeVFSMethodHandle **method_handle,
473 GnomeVFSFileInfoOptions options,
474 GnomeVFSContext *context)
476 return GNOME_VFS_ERROR_NOT_SUPPORTED;
479 static GnomeVFSResult
480 do_close_directory (GnomeVFSMethod *method,
481 GnomeVFSMethodHandle *method_handle,
482 GnomeVFSContext *context)
484 return GNOME_VFS_ERROR_NOT_SUPPORTED;
487 static GnomeVFSResult
488 do_read_directory (GnomeVFSMethod *method,
489 GnomeVFSMethodHandle *method_handle,
490 GnomeVFSFileInfo *file_info,
491 GnomeVFSContext *context)
493 return GNOME_VFS_ERROR_NOT_SUPPORTED;
497 static GnomeVFSResult
498 do_get_file_info (GnomeVFSMethod * method,
500 GnomeVFSFileInfo * file_info,
501 GnomeVFSFileInfoOptions options,
502 GnomeVFSContext * context)
504 return myth_get_file_info (NULL, uri, file_info);
507 static GnomeVFSResult
508 do_get_file_info_from_handle (GnomeVFSMethod *method,
509 GnomeVFSMethodHandle *method_handle,
510 GnomeVFSFileInfo *file_info,
511 GnomeVFSFileInfoOptions options,
512 GnomeVFSContext *context)
514 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
516 return myth_get_file_info (myth_handle, NULL, file_info);
520 do_is_local (GnomeVFSMethod * method, const GnomeVFSURI * uri)
525 static GnomeVFSResult
526 do_make_directory (GnomeVFSMethod *method,
529 GnomeVFSContext *context)
531 return GNOME_VFS_ERROR_READ_ONLY;
534 static GnomeVFSResult
535 do_remove_directory (GnomeVFSMethod *method,
537 GnomeVFSContext *context)
539 return GNOME_VFS_ERROR_READ_ONLY;
542 static GnomeVFSResult
543 do_move (GnomeVFSMethod *method,
544 GnomeVFSURI *old_uri,
545 GnomeVFSURI *new_uri,
546 gboolean force_replace,
547 GnomeVFSContext *context)
549 return GNOME_VFS_ERROR_READ_ONLY;
552 static GnomeVFSResult
553 do_unlink (GnomeVFSMethod *method,
555 GnomeVFSContext *context)
557 return GNOME_VFS_ERROR_READ_ONLY;
560 static GnomeVFSResult
561 do_check_same_fs (GnomeVFSMethod *method,
564 gboolean *same_fs_return,
565 GnomeVFSContext *context)
567 return GNOME_VFS_ERROR_NOT_SUPPORTED;
570 static GnomeVFSResult
571 do_set_file_info (GnomeVFSMethod *method,
573 const GnomeVFSFileInfo *info,
574 GnomeVFSSetFileInfoMask mask,
575 GnomeVFSContext *context)
577 return GNOME_VFS_ERROR_READ_ONLY;
580 static GnomeVFSResult
581 do_truncate (GnomeVFSMethod *method,
583 GnomeVFSFileSize where,
584 GnomeVFSContext *context)
586 return GNOME_VFS_ERROR_READ_ONLY;
589 static GnomeVFSResult
590 do_find_directory (GnomeVFSMethod *method,
591 GnomeVFSURI *near_uri,
592 GnomeVFSFindDirectoryKind kind,
593 GnomeVFSURI **result_uri,
594 gboolean create_if_needed,
595 gboolean find_if_needed,
597 GnomeVFSContext *context)
599 return GNOME_VFS_ERROR_NOT_SUPPORTED;
602 static GnomeVFSResult
603 do_create_symbolic_link (GnomeVFSMethod *method,
605 const char *target_reference,
606 GnomeVFSContext *context)
608 return GNOME_VFS_ERROR_READ_ONLY;
611 static GnomeVFSResult
612 do_monitor_add (GnomeVFSMethod *method,
613 GnomeVFSMethodHandle **method_handle_return,
615 GnomeVFSMonitorType monitor_type)
617 return GNOME_VFS_ERROR_NOT_SUPPORTED;
620 static GnomeVFSResult
621 do_monitor_cancel (GnomeVFSMethod *method,
622 GnomeVFSMethodHandle *method_handle)
624 return GNOME_VFS_ERROR_NOT_SUPPORTED;
627 static GnomeVFSResult
628 do_file_control (GnomeVFSMethod *method,
629 GnomeVFSMethodHandle *method_handle,
630 const char *operation,
631 gpointer operation_data,
632 GnomeVFSContext *context)
634 return GNOME_VFS_ERROR_NOT_SUPPORTED;
637 static GnomeVFSMethod method = {
638 sizeof (GnomeVFSMethod),
651 do_get_file_info_from_handle,
661 do_create_symbolic_link,
669 vfs_module_init (const char *method_name, const char *args)
675 vfs_module_shutdown (GnomeVFSMethod * method)