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;
69 static GnomeVFSResult do_read (GnomeVFSMethod * method,
70 GnomeVFSMethodHandle * method_handle,
72 GnomeVFSFileSize num_bytes,
73 GnomeVFSFileSize * bytes_read,
74 GnomeVFSContext * context);
76 static GnomeVFSResult myth_connection_start (MythtvHandle * method_handle);
77 static void myth_destroy_handle (MythtvHandle * method_handle);
78 static GnomeVFSResult myth_handle_new (GnomeVFSURI * uri,
79 MythtvHandle ** method_handle);
80 static GnomeVFSResult myth_get_file_info (MythtvHandle * myth_handle,
82 GnomeVFSFileInfo * info);
85 myth_handle_new (GnomeVFSURI * uri,
86 MythtvHandle ** method_handle)
91 _GNOME_VFS_METHOD_PARAM_CHECK (*method_handle == NULL);
93 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
94 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
97 *method_handle = g_new0 (MythtvHandle, 1);
98 (*method_handle)->mythtv_version = MYTHTV_VERSION_DEFAULT;
100 tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
101 tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
103 (*method_handle)->backend_info = gmyth_backend_info_new_with_uri (tmp_str2);
104 (*method_handle)->gmyth_uri = gmyth_uri_new_with_value (tmp_str2);
112 myth_destroy_handle (MythtvHandle * method_handle)
114 //TODO: abort if in tranfer state
116 if (method_handle->backend_info != NULL) {
117 g_object_unref (method_handle->backend_info);
118 method_handle->backend_info = NULL;
121 if (method_handle->channel_name != NULL) {
122 g_free (method_handle->channel_name);
123 method_handle->channel_name = NULL;
126 if (method_handle->livetv != NULL) {
127 g_object_unref (method_handle->livetv);
128 method_handle->livetv = NULL;
131 if (method_handle->file_transfer != NULL) {
132 g_object_unref (method_handle->file_transfer);
133 method_handle->file_transfer = NULL;
136 if (method_handle->gmyth_uri != NULL) {
137 g_object_unref (method_handle->gmyth_uri);
138 method_handle->gmyth_uri = NULL;
141 g_free (method_handle);
144 static GnomeVFSResult
145 myth_get_file_info (MythtvHandle * myth_handle,
147 GnomeVFSFileInfo * info)
150 GMythBackendInfo *backend_info;
152 _GNOME_VFS_METHOD_PARAM_CHECK (info != NULL);
155 if (myth_handle == NULL) {
159 tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
160 tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
162 backend_info = gmyth_backend_info_new_with_uri (tmp_str2);
163 gmyth_uri = gmyth_uri_new_with_value (tmp_str2);
168 backend_info = g_object_ref (myth_handle->backend_info);
169 gmyth_uri = g_object_ref (myth_handle->gmyth_uri);
172 info->valid_fields = 0;
173 info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE |
174 GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE |
175 GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
177 info->type = GNOME_VFS_FILE_TYPE_REGULAR;
179 /* fixme: get from file extension? */
180 info->mime_type = g_strdup ("video/x-nuv");
181 info->permissions = GNOME_VFS_PERM_USER_READ |
182 GNOME_VFS_PERM_OTHER_READ |
183 GNOME_VFS_PERM_GROUP_READ;
186 info->name = g_strdup (gmyth_uri_get_path (gmyth_uri));
188 /* file size for remote files */
189 if (gmyth_uri_is_livetv (gmyth_uri) == FALSE) {
190 GMythFileTransfer *file_transfer = gmyth_file_transfer_new (backend_info);
192 /* Verifies if the file exists */
193 if (!gmyth_util_file_exists (backend_info,
194 gmyth_uri_get_path (gmyth_uri))) {
195 g_object_unref (file_transfer);
196 g_object_unref (backend_info);
197 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
198 return GNOME_VFS_ERROR_NOT_FOUND;
201 if (!gmyth_file_transfer_open (file_transfer, gmyth_uri_get_path (gmyth_uri))) {
202 g_object_unref (file_transfer);
203 g_object_unref (backend_info);
204 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
205 return GNOME_VFS_ERROR_NOT_FOUND;
208 info->size = gmyth_file_transfer_get_filesize (file_transfer);
209 info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE;
210 g_object_unref (file_transfer);
213 g_object_unref (backend_info);
214 g_object_unref (gmyth_uri);
219 static GnomeVFSResult
220 myth_connection_start (MythtvHandle * method_handle)
222 GnomeVFSResult result = GNOME_VFS_OK;
224 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
225 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle->backend_info != NULL);
227 /* Connect to the backend */
228 if (gmyth_uri_is_livetv (method_handle->gmyth_uri) == TRUE) {
229 method_handle->livetv = gmyth_livetv_new (method_handle->backend_info);
230 method_handle->channel_name = gmyth_uri_get_channel_name (method_handle->gmyth_uri);
232 if (method_handle->channel_name != NULL) {
233 if (gmyth_livetv_channel_name_setup (method_handle->livetv,
234 method_handle->channel_name) == FALSE) {
235 result = GNOME_VFS_ERROR_INVALID_URI;
238 } else if (gmyth_livetv_setup (method_handle->livetv) == FALSE) {
239 result = GNOME_VFS_ERROR_INVALID_URI;
244 method_handle->file_transfer =
245 gmyth_livetv_create_file_transfer (method_handle->livetv);
247 if (method_handle->file_transfer == NULL) {
248 result = GNOME_VFS_ERROR_INVALID_URI;
249 g_debug ("MythTV FileTransfer is NULL!\n");
253 if (!gmyth_file_transfer_open (method_handle->file_transfer,
254 method_handle->livetv->uri != NULL ?
255 gmyth_uri_get_path (method_handle->livetv->uri) :
256 method_handle->livetv->proginfo->pathname->str)) {
258 g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
259 result = GNOME_VFS_ERROR_NOT_OPEN;
264 method_handle->file_transfer =
265 gmyth_file_transfer_new (method_handle->backend_info);
267 /* Verifies if the file exists */
268 if (!gmyth_util_file_exists (method_handle->backend_info,
269 gmyth_uri_get_path (method_handle->gmyth_uri))) {
271 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
275 /* sets the Playback monitor connection */
276 if (!gmyth_file_transfer_open (method_handle->file_transfer,
277 gmyth_uri_get_path (method_handle->gmyth_uri))) {
279 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
280 result = GNOME_VFS_ERROR_NOT_FOUND;
283 } /* if - LiveTV or not? */
285 method_handle->configured = TRUE;
287 if (method_handle->file_transfer == NULL) {
288 result = GNOME_VFS_ERROR_NOT_OPEN;
296 static GnomeVFSResult
297 do_open (GnomeVFSMethod * method,
298 GnomeVFSMethodHandle ** method_handle,
300 GnomeVFSOpenMode mode, GnomeVFSContext * context)
302 MythtvHandle *myth_handle = NULL;
303 GnomeVFSResult result = GNOME_VFS_OK;
305 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
306 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
308 if (mode & GNOME_VFS_OPEN_WRITE) {
309 return GNOME_VFS_ERROR_INVALID_OPEN_MODE;
312 result = myth_handle_new (uri, &myth_handle);
313 if (result != GNOME_VFS_OK)
316 result = myth_connection_start (myth_handle);
317 if (result != GNOME_VFS_OK) {
318 myth_destroy_handle (myth_handle);
323 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
328 static GnomeVFSResult
329 do_create (GnomeVFSMethod *method,
330 GnomeVFSMethodHandle **method_handle,
332 GnomeVFSOpenMode mode,
335 GnomeVFSContext *context)
337 return GNOME_VFS_ERROR_NOT_SUPPORTED;
340 static GnomeVFSResult
341 do_close (GnomeVFSMethod * method,
342 GnomeVFSMethodHandle * method_handle,
343 GnomeVFSContext * context)
345 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
347 myth_destroy_handle (myth_handle);
353 static GnomeVFSResult
354 do_read (GnomeVFSMethod * method,
355 GnomeVFSMethodHandle * method_handle,
357 GnomeVFSFileSize num_bytes,
358 GnomeVFSFileSize * bytes_read,
359 GnomeVFSContext * context)
361 GnomeVFSResult retval = GNOME_VFS_OK;
362 MythtvHandle *myth_handle;
363 GMythFileTransferReadResult result;
364 GByteArray *myth_buffer = g_byte_array_new ();
366 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
368 myth_handle = (MythtvHandle *) method_handle;
369 result = gmyth_file_transfer_read (myth_handle->file_transfer,
371 num_bytes, gmyth_uri_is_livetv (myth_handle->gmyth_uri));
374 if (result == GMYTH_FILE_TRANSFER_READ_ERROR) {
375 retval = GNOME_VFS_ERROR_IO;
378 if (result == GMYTH_FILE_TRANSFER_READ_EOF) {
379 retval = GNOME_VFS_ERROR_EOF;
382 if (myth_buffer->len > 0) {
383 g_memmove (buffer, myth_buffer->data, myth_buffer->len);
384 *bytes_read = (GnomeVFSFileSize) myth_buffer->len;
385 myth_handle->offset += myth_buffer->len;
386 g_byte_array_free (myth_buffer, TRUE);
392 static GnomeVFSResult
393 do_write (GnomeVFSMethod *method,
394 GnomeVFSMethodHandle *method_handle,
395 gconstpointer buffer,
396 GnomeVFSFileSize num_bytes,
397 GnomeVFSFileSize *bytes_written,
398 GnomeVFSContext *context)
400 return GNOME_VFS_ERROR_NOT_SUPPORTED;
403 static GnomeVFSResult
404 do_seek (GnomeVFSMethod *method,
405 GnomeVFSMethodHandle *method_handle,
406 GnomeVFSSeekPosition whence,
407 GnomeVFSFileOffset offset,
408 GnomeVFSContext *context)
410 MythtvHandle *myth_handle;
411 //guint64 whence_p = 0;
412 //gint64 new_offset =0;
414 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
416 myth_handle = (MythtvHandle *) method_handle;
418 g_debug ("seek offset%"G_GINT64_FORMAT" whence %d", offset, whence);
420 return GNOME_VFS_ERROR_NOT_SUPPORTED;
422 if (gmyth_uri_is_livetv (myth_handle->gmyth_uri))
426 case GNOME_VFS_SEEK_START:
429 case GNOME_VFS_SEEK_CURRENT:
430 whence_p = myth_handle->offset;
432 case GNOME_VFS_SEEK_END:
433 return GNOME_VFS_ERROR_NOT_SUPPORTED;
436 new_offset = gmyth_file_transfer_seek (myth_handle->file_transfer, offset, whence_p);
437 if (new_offset != 0) {
438 myth_handle->offset = new_offset;
442 return GNOME_VFS_ERROR_NOT_SUPPORTED;
446 static GnomeVFSResult
447 do_tell (GnomeVFSMethod *method,
448 GnomeVFSMethodHandle *method_handle,
449 GnomeVFSFileSize *offset_return)
451 MythtvHandle *myth_handle = NULL;
453 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
455 myth_handle = (MythtvHandle *) method_handle;
456 *offset_return = myth_handle->offset;
461 static GnomeVFSResult
462 do_truncate_handle (GnomeVFSMethod *method,
463 GnomeVFSMethodHandle *method_handle,
464 GnomeVFSFileSize where,
465 GnomeVFSContext *context)
467 return GNOME_VFS_ERROR_READ_ONLY;
470 static GnomeVFSResult
471 do_open_directory (GnomeVFSMethod *method,
472 GnomeVFSMethodHandle **method_handle,
474 GnomeVFSFileInfoOptions options,
475 GnomeVFSContext *context)
477 return GNOME_VFS_ERROR_NOT_SUPPORTED;
480 static GnomeVFSResult
481 do_close_directory (GnomeVFSMethod *method,
482 GnomeVFSMethodHandle *method_handle,
483 GnomeVFSContext *context)
485 return GNOME_VFS_ERROR_NOT_SUPPORTED;
488 static GnomeVFSResult
489 do_read_directory (GnomeVFSMethod *method,
490 GnomeVFSMethodHandle *method_handle,
491 GnomeVFSFileInfo *file_info,
492 GnomeVFSContext *context)
494 return GNOME_VFS_ERROR_NOT_SUPPORTED;
498 static GnomeVFSResult
499 do_get_file_info (GnomeVFSMethod * method,
501 GnomeVFSFileInfo * file_info,
502 GnomeVFSFileInfoOptions options,
503 GnomeVFSContext * context)
505 return myth_get_file_info (NULL, uri, file_info);
508 static GnomeVFSResult
509 do_get_file_info_from_handle (GnomeVFSMethod *method,
510 GnomeVFSMethodHandle *method_handle,
511 GnomeVFSFileInfo *file_info,
512 GnomeVFSFileInfoOptions options,
513 GnomeVFSContext *context)
515 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
517 return myth_get_file_info (myth_handle, NULL, file_info);
521 do_is_local (GnomeVFSMethod * method, const GnomeVFSURI * uri)
526 static GnomeVFSResult
527 do_make_directory (GnomeVFSMethod *method,
530 GnomeVFSContext *context)
532 return GNOME_VFS_ERROR_READ_ONLY;
535 static GnomeVFSResult
536 do_remove_directory (GnomeVFSMethod *method,
538 GnomeVFSContext *context)
540 return GNOME_VFS_ERROR_READ_ONLY;
543 static GnomeVFSResult
544 do_move (GnomeVFSMethod *method,
545 GnomeVFSURI *old_uri,
546 GnomeVFSURI *new_uri,
547 gboolean force_replace,
548 GnomeVFSContext *context)
550 return GNOME_VFS_ERROR_READ_ONLY;
553 static GnomeVFSResult
554 do_unlink (GnomeVFSMethod *method,
556 GnomeVFSContext *context)
558 return GNOME_VFS_ERROR_READ_ONLY;
561 static GnomeVFSResult
562 do_check_same_fs (GnomeVFSMethod *method,
565 gboolean *same_fs_return,
566 GnomeVFSContext *context)
568 return GNOME_VFS_ERROR_NOT_SUPPORTED;
571 static GnomeVFSResult
572 do_set_file_info (GnomeVFSMethod *method,
574 const GnomeVFSFileInfo *info,
575 GnomeVFSSetFileInfoMask mask,
576 GnomeVFSContext *context)
578 return GNOME_VFS_ERROR_READ_ONLY;
581 static GnomeVFSResult
582 do_truncate (GnomeVFSMethod *method,
584 GnomeVFSFileSize where,
585 GnomeVFSContext *context)
587 return GNOME_VFS_ERROR_READ_ONLY;
590 static GnomeVFSResult
591 do_find_directory (GnomeVFSMethod *method,
592 GnomeVFSURI *near_uri,
593 GnomeVFSFindDirectoryKind kind,
594 GnomeVFSURI **result_uri,
595 gboolean create_if_needed,
596 gboolean find_if_needed,
598 GnomeVFSContext *context)
600 return GNOME_VFS_ERROR_NOT_SUPPORTED;
603 static GnomeVFSResult
604 do_create_symbolic_link (GnomeVFSMethod *method,
606 const char *target_reference,
607 GnomeVFSContext *context)
609 return GNOME_VFS_ERROR_READ_ONLY;
612 static GnomeVFSResult
613 do_monitor_add (GnomeVFSMethod *method,
614 GnomeVFSMethodHandle **method_handle_return,
616 GnomeVFSMonitorType monitor_type)
618 return GNOME_VFS_ERROR_NOT_SUPPORTED;
621 static GnomeVFSResult
622 do_monitor_cancel (GnomeVFSMethod *method,
623 GnomeVFSMethodHandle *method_handle)
625 return GNOME_VFS_ERROR_NOT_SUPPORTED;
628 static GnomeVFSResult
629 do_file_control (GnomeVFSMethod *method,
630 GnomeVFSMethodHandle *method_handle,
631 const char *operation,
632 gpointer operation_data,
633 GnomeVFSContext *context)
635 return GNOME_VFS_ERROR_NOT_SUPPORTED;
638 static GnomeVFSMethod method = {
639 sizeof (GnomeVFSMethod),
652 do_get_file_info_from_handle,
662 do_create_symbolic_link,
670 vfs_module_init (const char *method_name, const char *args)
676 vfs_module_shutdown (GnomeVFSMethod * method)