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);
154 if (myth_handle == NULL) {
158 tmp_str1 = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
159 tmp_str2 = gnome_vfs_unescape_string (tmp_str1, "");
161 backend_info = gmyth_backend_info_new_with_uri (tmp_str2);
162 gmyth_uri = gmyth_uri_new_with_value (tmp_str2);
167 backend_info = g_object_ref (myth_handle->backend_info);
168 gmyth_uri = g_object_ref (myth_handle->gmyth_uri);
171 info->valid_fields = 0;
172 info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE |
173 GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE |
174 GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
176 info->type = GNOME_VFS_FILE_TYPE_REGULAR;
178 /* fixme: get from file extension? */
179 info->mime_type = g_strdup ("video/x-nuv");
180 info->permissions = GNOME_VFS_PERM_USER_READ |
181 GNOME_VFS_PERM_OTHER_READ |
182 GNOME_VFS_PERM_GROUP_READ;
185 info->name = g_strdup (gmyth_uri_get_path (gmyth_uri));
187 /* file size for remote files */
188 if (gmyth_uri_is_livetv (gmyth_uri) == FALSE) {
189 GMythFileTransfer *file_transfer = gmyth_file_transfer_new (backend_info);
191 /* Verifies if the file exists */
192 if (!gmyth_util_file_exists (backend_info,
193 gmyth_uri_get_path (gmyth_uri))) {
194 g_object_unref (file_transfer);
195 g_object_unref (backend_info);
196 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
197 return GNOME_VFS_ERROR_NOT_FOUND;
200 if (!gmyth_file_transfer_open (file_transfer, gmyth_uri_get_path (gmyth_uri))) {
201 g_object_unref (file_transfer);
202 g_object_unref (backend_info);
203 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
204 return GNOME_VFS_ERROR_NOT_FOUND;
207 info->size = gmyth_file_transfer_get_filesize (file_transfer);
208 info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE;
209 g_object_unref (file_transfer);
212 g_object_unref (backend_info);
213 g_object_unref (gmyth_uri);
218 static GnomeVFSResult
219 myth_connection_start (MythtvHandle * method_handle)
221 GnomeVFSResult result = GNOME_VFS_OK;
223 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
224 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle->backend_info != NULL);
226 /* Connect to the backend */
227 if (gmyth_uri_is_livetv (method_handle->gmyth_uri) == TRUE) {
228 method_handle->livetv = gmyth_livetv_new (method_handle->backend_info);
229 method_handle->channel_name = gmyth_uri_get_channel_name (method_handle->gmyth_uri);
231 if (method_handle->channel_name != NULL) {
232 if (gmyth_livetv_channel_name_setup (method_handle->livetv,
233 method_handle->channel_name) == FALSE) {
234 result = GNOME_VFS_ERROR_INVALID_URI;
237 } else if (gmyth_livetv_setup (method_handle->livetv) == FALSE) {
238 result = GNOME_VFS_ERROR_INVALID_URI;
243 method_handle->file_transfer =
244 gmyth_livetv_create_file_transfer (method_handle->livetv);
246 if (method_handle->file_transfer == NULL) {
247 result = GNOME_VFS_ERROR_INVALID_URI;
248 g_debug ("MythTV FileTransfer is NULL!\n");
252 if (!gmyth_file_transfer_open (method_handle->file_transfer,
253 method_handle->livetv->uri != NULL ?
254 gmyth_uri_get_path (method_handle->livetv->uri) :
255 method_handle->livetv->proginfo->pathname->str)) {
257 g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
258 result = GNOME_VFS_ERROR_NOT_OPEN;
263 method_handle->file_transfer =
264 gmyth_file_transfer_new (method_handle->backend_info);
266 /* Verifies if the file exists */
267 if (!gmyth_util_file_exists (method_handle->backend_info,
268 gmyth_uri_get_path (method_handle->gmyth_uri))) {
270 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
274 /* sets the Playback monitor connection */
275 if (!gmyth_file_transfer_open (method_handle->file_transfer,
276 gmyth_uri_get_path (method_handle->gmyth_uri))) {
278 g_debug ("NOT FOUND %s/%d", __FUNCTION__, __LINE__);
279 result = GNOME_VFS_ERROR_NOT_FOUND;
282 } /* if - LiveTV or not? */
284 method_handle->configured = TRUE;
286 if (method_handle->file_transfer == NULL) {
287 result = GNOME_VFS_ERROR_NOT_OPEN;
295 static GnomeVFSResult
296 do_open (GnomeVFSMethod * method,
297 GnomeVFSMethodHandle ** method_handle,
299 GnomeVFSOpenMode mode, GnomeVFSContext * context)
301 MythtvHandle *myth_handle = NULL;
302 GnomeVFSResult result = GNOME_VFS_OK;
304 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
305 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
307 if (mode & GNOME_VFS_OPEN_WRITE) {
308 return GNOME_VFS_ERROR_INVALID_OPEN_MODE;
311 result = myth_handle_new (uri, &myth_handle);
312 if (result != GNOME_VFS_OK)
315 result = myth_connection_start (myth_handle);
316 if (result != GNOME_VFS_OK) {
317 myth_destroy_handle (myth_handle);
322 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
327 static GnomeVFSResult
328 do_create (GnomeVFSMethod *method,
329 GnomeVFSMethodHandle **method_handle,
331 GnomeVFSOpenMode mode,
334 GnomeVFSContext *context)
336 return GNOME_VFS_ERROR_NOT_SUPPORTED;
339 static GnomeVFSResult
340 do_close (GnomeVFSMethod * method,
341 GnomeVFSMethodHandle * method_handle,
342 GnomeVFSContext * context)
344 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
346 myth_destroy_handle (myth_handle);
352 static GnomeVFSResult
353 do_read (GnomeVFSMethod * method,
354 GnomeVFSMethodHandle * method_handle,
356 GnomeVFSFileSize num_bytes,
357 GnomeVFSFileSize * bytes_read,
358 GnomeVFSContext * context)
360 GnomeVFSResult result;
361 MythtvHandle *myth_handle;
362 gint64 total_read = 0;
363 GByteArray *myth_buffer = g_byte_array_new ();
365 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
367 myth_handle = (MythtvHandle *) method_handle;
368 result = GNOME_VFS_OK;
370 total_read = gmyth_file_transfer_read (myth_handle->file_transfer,
372 num_bytes, gmyth_uri_is_livetv (myth_handle->gmyth_uri));
375 if (total_read == -1) {
376 result = GNOME_VFS_ERROR_IO;
380 if (total_read < num_bytes) {
381 result = GNOME_VFS_ERROR_EOF;
384 if (total_read > 0) {
385 g_memmove (buffer, myth_buffer->data, total_read);
386 g_byte_array_free (myth_buffer, TRUE);
387 myth_handle->offset += total_read;
390 *bytes_read = (GnomeVFSFileSize) total_read;
395 static GnomeVFSResult
396 do_write (GnomeVFSMethod *method,
397 GnomeVFSMethodHandle *method_handle,
398 gconstpointer buffer,
399 GnomeVFSFileSize num_bytes,
400 GnomeVFSFileSize *bytes_written,
401 GnomeVFSContext *context)
403 return GNOME_VFS_ERROR_NOT_SUPPORTED;
406 static GnomeVFSResult
407 do_seek (GnomeVFSMethod *method,
408 GnomeVFSMethodHandle *method_handle,
409 GnomeVFSSeekPosition whence,
410 GnomeVFSFileOffset offset,
411 GnomeVFSContext *context)
413 MythtvHandle *myth_handle;
414 //guint64 whence_p = 0;
415 //gint64 new_offset =0;
417 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
419 myth_handle = (MythtvHandle *) method_handle;
421 g_debug ("seek offset%"G_GINT64_FORMAT" whence %d", offset, whence);
423 return GNOME_VFS_ERROR_NOT_SUPPORTED;
425 if (gmyth_uri_is_livetv (myth_handle->gmyth_uri))
429 case GNOME_VFS_SEEK_START:
432 case GNOME_VFS_SEEK_CURRENT:
433 whence_p = myth_handle->offset;
435 case GNOME_VFS_SEEK_END:
436 return GNOME_VFS_ERROR_NOT_SUPPORTED;
439 new_offset = gmyth_file_transfer_seek (myth_handle->file_transfer, offset, whence_p);
440 if (new_offset != 0) {
441 myth_handle->offset = new_offset;
445 return GNOME_VFS_ERROR_NOT_SUPPORTED;
449 static GnomeVFSResult
450 do_tell (GnomeVFSMethod *method,
451 GnomeVFSMethodHandle *method_handle,
452 GnomeVFSFileSize *offset_return)
454 MythtvHandle *myth_handle = NULL;
456 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
458 myth_handle = (MythtvHandle *) method_handle;
459 *offset_return = myth_handle->offset;
464 static GnomeVFSResult
465 do_truncate_handle (GnomeVFSMethod *method,
466 GnomeVFSMethodHandle *method_handle,
467 GnomeVFSFileSize where,
468 GnomeVFSContext *context)
470 return GNOME_VFS_ERROR_READ_ONLY;
473 static GnomeVFSResult
474 do_open_directory (GnomeVFSMethod *method,
475 GnomeVFSMethodHandle **method_handle,
477 GnomeVFSFileInfoOptions options,
478 GnomeVFSContext *context)
480 return GNOME_VFS_ERROR_NOT_SUPPORTED;
483 static GnomeVFSResult
484 do_close_directory (GnomeVFSMethod *method,
485 GnomeVFSMethodHandle *method_handle,
486 GnomeVFSContext *context)
488 return GNOME_VFS_ERROR_NOT_SUPPORTED;
491 static GnomeVFSResult
492 do_read_directory (GnomeVFSMethod *method,
493 GnomeVFSMethodHandle *method_handle,
494 GnomeVFSFileInfo *file_info,
495 GnomeVFSContext *context)
497 return GNOME_VFS_ERROR_NOT_SUPPORTED;
501 static GnomeVFSResult
502 do_get_file_info (GnomeVFSMethod * method,
504 GnomeVFSFileInfo * file_info,
505 GnomeVFSFileInfoOptions options,
506 GnomeVFSContext * context)
508 return myth_get_file_info (NULL, uri, file_info);
511 static GnomeVFSResult
512 do_get_file_info_from_handle (GnomeVFSMethod *method,
513 GnomeVFSMethodHandle *method_handle,
514 GnomeVFSFileInfo *file_info,
515 GnomeVFSFileInfoOptions options,
516 GnomeVFSContext *context)
518 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
520 return myth_get_file_info (myth_handle, NULL, file_info);
524 do_is_local (GnomeVFSMethod * method, const GnomeVFSURI * uri)
529 static GnomeVFSResult
530 do_make_directory (GnomeVFSMethod *method,
533 GnomeVFSContext *context)
535 return GNOME_VFS_ERROR_READ_ONLY;
538 static GnomeVFSResult
539 do_remove_directory (GnomeVFSMethod *method,
541 GnomeVFSContext *context)
543 return GNOME_VFS_ERROR_READ_ONLY;
546 static GnomeVFSResult
547 do_move (GnomeVFSMethod *method,
548 GnomeVFSURI *old_uri,
549 GnomeVFSURI *new_uri,
550 gboolean force_replace,
551 GnomeVFSContext *context)
553 return GNOME_VFS_ERROR_READ_ONLY;
556 static GnomeVFSResult
557 do_unlink (GnomeVFSMethod *method,
559 GnomeVFSContext *context)
561 return GNOME_VFS_ERROR_READ_ONLY;
564 static GnomeVFSResult
565 do_check_same_fs (GnomeVFSMethod *method,
568 gboolean *same_fs_return,
569 GnomeVFSContext *context)
571 return GNOME_VFS_ERROR_NOT_SUPPORTED;
574 static GnomeVFSResult
575 do_set_file_info (GnomeVFSMethod *method,
577 const GnomeVFSFileInfo *info,
578 GnomeVFSSetFileInfoMask mask,
579 GnomeVFSContext *context)
581 return GNOME_VFS_ERROR_READ_ONLY;
584 static GnomeVFSResult
585 do_truncate (GnomeVFSMethod *method,
587 GnomeVFSFileSize where,
588 GnomeVFSContext *context)
590 return GNOME_VFS_ERROR_READ_ONLY;
593 static GnomeVFSResult
594 do_find_directory (GnomeVFSMethod *method,
595 GnomeVFSURI *near_uri,
596 GnomeVFSFindDirectoryKind kind,
597 GnomeVFSURI **result_uri,
598 gboolean create_if_needed,
599 gboolean find_if_needed,
601 GnomeVFSContext *context)
603 return GNOME_VFS_ERROR_NOT_SUPPORTED;
606 static GnomeVFSResult
607 do_create_symbolic_link (GnomeVFSMethod *method,
609 const char *target_reference,
610 GnomeVFSContext *context)
612 return GNOME_VFS_ERROR_READ_ONLY;
615 static GnomeVFSResult
616 do_monitor_add (GnomeVFSMethod *method,
617 GnomeVFSMethodHandle **method_handle_return,
619 GnomeVFSMonitorType monitor_type)
621 return GNOME_VFS_ERROR_NOT_SUPPORTED;
624 static GnomeVFSResult
625 do_monitor_cancel (GnomeVFSMethod *method,
626 GnomeVFSMethodHandle *method_handle)
628 return GNOME_VFS_ERROR_NOT_SUPPORTED;
631 static GnomeVFSResult
632 do_file_control (GnomeVFSMethod *method,
633 GnomeVFSMethodHandle *method_handle,
634 const char *operation,
635 gpointer operation_data,
636 GnomeVFSContext *context)
638 return GNOME_VFS_ERROR_NOT_SUPPORTED;
641 static GnomeVFSMethod method = {
642 sizeof (GnomeVFSMethod),
655 do_get_file_info_from_handle,
665 do_create_symbolic_link,
673 vfs_module_init (const char *method_name, const char *args)
679 vfs_module_shutdown (GnomeVFSMethod * method)