1.1 --- a/gmyth/src/gmyth_file_local.c Thu Apr 12 13:22:53 2007 +0100
1.2 +++ b/gmyth/src/gmyth_file_local.c Wed May 23 16:11:29 2007 +0100
1.3 @@ -9,23 +9,23 @@
1.4 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
1.5 * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
1.6 *
1.7 - *//*
1.8 - *
1.9 - * This program is free software; you can redistribute it and/or modify
1.10 - * it under the terms of the GNU Lesser General Public License as published by
1.11 - * the Free Software Foundation; either version 2 of the License, or
1.12 - * (at your option) any later version.
1.13 - *
1.14 - * This program is distributed in the hope that it will be useful,
1.15 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 - * GNU General Public License for more details.
1.18 - *
1.19 - * You should have received a copy of the GNU Lesser General Public License
1.20 - * along with this program; if not, write to the Free Software
1.21 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.22 - */
1.23 -
1.24 + *//*
1.25 + *
1.26 + * This program is free software; you can redistribute it and/or modify
1.27 + * it under the terms of the GNU Lesser General Public License as published by
1.28 + * the Free Software Foundation; either version 2 of the License, or
1.29 + * (at your option) any later version.
1.30 + *
1.31 + * This program is distributed in the hope that it will be useful,
1.32 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.33 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.34 + * GNU General Public License for more details.
1.35 + *
1.36 + * You should have received a copy of the GNU Lesser General Public License
1.37 + * along with this program; if not, write to the Free Software
1.38 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.39 + */
1.40 +
1.41 #ifdef HAVE_CONFIG_H
1.42 #include "config.h"
1.43 #endif
1.44 @@ -53,31 +53,31 @@
1.45 #define GMYTH_FILE_LOCAL_GET_PRIVATE(obj) \
1.46 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_FILE_LOCAL_TYPE, GMythFileLocalPrivate))
1.47
1.48 -struct _GMythFileLocalPrivate {
1.49 -
1.50 - gboolean disposed;
1.51 -
1.52 - GMutex *mutex;
1.53 -
1.54 - gint fd;
1.55 -
1.56 - GIOChannel *file_io;
1.57 +struct _GMythFileLocalPrivate
1.58 +{
1.59 +
1.60 + gboolean disposed;
1.61 +
1.62 + GMutex *mutex;
1.63 +
1.64 + gint fd;
1.65 +
1.66 + GIOChannel *file_io;
1.67
1.68 };
1.69
1.70 -static void gmyth_file_local_class_init (GMythFileLocalClass *klass);
1.71 -static void gmyth_file_local_init (GMythFileLocal *object);
1.72 -static void gmyth_file_local_dispose (GObject *object);
1.73 -static void gmyth_file_local_finalize (GObject *object);
1.74 +static void gmyth_file_local_class_init (GMythFileLocalClass * klass);
1.75 +static void gmyth_file_local_init (GMythFileLocal * object);
1.76 +static void gmyth_file_local_dispose (GObject * object);
1.77 +static void gmyth_file_local_finalize (GObject * object);
1.78
1.79 -static gboolean _control_acquire_context( GMythFileLocal *file_local, gboolean do_wait );
1.80 +static gboolean _control_acquire_context (GMythFileLocal * file_local,
1.81 + gboolean do_wait);
1.82
1.83 -static gboolean _control_release_context( GMythFileLocal *file_local );
1.84 +static gboolean _control_release_context (GMythFileLocal * file_local);
1.85
1.86 -G_DEFINE_TYPE(GMythFileLocal, gmyth_file_local, GMYTH_FILE_TYPE)
1.87 -
1.88 -static void
1.89 -gmyth_file_local_class_init (GMythFileLocalClass *klass)
1.90 +G_DEFINE_TYPE (GMythFileLocal, gmyth_file_local, GMYTH_FILE_TYPE)
1.91 + static void gmyth_file_local_class_init (GMythFileLocalClass * klass)
1.92 {
1.93 GObjectClass *gobject_class;
1.94 GMythFileLocalClass *gtransfer_class;
1.95 @@ -85,49 +85,50 @@
1.96 gobject_class = (GObjectClass *) klass;
1.97 gtransfer_class = (GMythFileLocalClass *) gobject_class;
1.98
1.99 - gobject_class->dispose = gmyth_file_local_dispose;
1.100 + gobject_class->dispose = gmyth_file_local_dispose;
1.101 gobject_class->finalize = gmyth_file_local_finalize;
1.102 -
1.103 +
1.104 g_type_class_add_private (gobject_class, sizeof (GMythFileLocalPrivate));
1.105
1.106 }
1.107
1.108 static void
1.109 -gmyth_file_local_init (GMythFileLocal *file_local)
1.110 -{
1.111 +gmyth_file_local_init (GMythFileLocal * file_local)
1.112 +{
1.113 GMythFileLocalPrivate *priv;
1.114 - g_return_if_fail( file_local != NULL );
1.115
1.116 - priv = GMYTH_FILE_LOCAL_GET_PRIVATE(file_local);
1.117 -
1.118 - priv->mutex = g_mutex_new();
1.119 + g_return_if_fail (file_local != NULL);
1.120 +
1.121 + priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local);
1.122 +
1.123 + priv->mutex = g_mutex_new ();
1.124 }
1.125
1.126 static void
1.127 -gmyth_file_local_dispose (GObject *object)
1.128 -{
1.129 +gmyth_file_local_dispose (GObject * object)
1.130 +{
1.131 GMythFileLocalPrivate *priv;
1.132 - GMythFileLocal *file_local = GMYTH_FILE_LOCAL(object);
1.133 + GMythFileLocal *file_local = GMYTH_FILE_LOCAL (object);
1.134
1.135 - g_return_if_fail( file_local != NULL );
1.136 + g_return_if_fail (file_local != NULL);
1.137
1.138 - priv = GMYTH_FILE_LOCAL_GET_PRIVATE(file_local);
1.139 + priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local);
1.140
1.141 if (priv->disposed) {
1.142 /* If dispose did already run, return. */
1.143 return;
1.144 }
1.145 -
1.146 +
1.147 /* Make sure dispose does not run twice. */
1.148 priv->disposed = TRUE;
1.149 -
1.150 - if (priv->mutex != NULL ) {
1.151 - g_mutex_free (priv->mutex );
1.152 +
1.153 + if (priv->mutex != NULL) {
1.154 + g_mutex_free (priv->mutex);
1.155 priv->mutex = NULL;
1.156 }
1.157 -
1.158 - if (priv->file_io != NULL ) {
1.159 - g_io_channel_unref (priv->file_io );
1.160 +
1.161 + if (priv->file_io != NULL) {
1.162 + g_io_channel_unref (priv->file_io);
1.163 priv->file_io = NULL;
1.164 }
1.165
1.166 @@ -135,11 +136,11 @@
1.167 }
1.168
1.169 static void
1.170 -gmyth_file_local_finalize (GObject *object)
1.171 +gmyth_file_local_finalize (GObject * object)
1.172 {
1.173 - g_signal_handlers_destroy (object);
1.174 + g_signal_handlers_destroy (object);
1.175
1.176 - G_OBJECT_CLASS (gmyth_file_local_parent_class)->finalize (object);
1.177 + G_OBJECT_CLASS (gmyth_file_local_parent_class)->finalize (object);
1.178 }
1.179
1.180 /**
1.181 @@ -150,12 +151,13 @@
1.182 *
1.183 * @return a new instance of the File Transfer.
1.184 */
1.185 -GMythFileLocal*
1.186 -gmyth_file_local_new (GMythBackendInfo *backend_info)
1.187 +GMythFileLocal *
1.188 +gmyth_file_local_new (GMythBackendInfo * backend_info)
1.189 {
1.190 - GMythFileLocal *file_local = GMYTH_FILE_LOCAL (g_object_new (GMYTH_FILE_LOCAL_TYPE, NULL));
1.191 + GMythFileLocal *file_local =
1.192 + GMYTH_FILE_LOCAL (g_object_new (GMYTH_FILE_LOCAL_TYPE, NULL));
1.193
1.194 - g_object_set ( GMYTH_FILE( file_local ), "backend-info", &backend_info, NULL );
1.195 + g_object_set (GMYTH_FILE (file_local), "backend-info", &backend_info, NULL);
1.196
1.197 return file_local;
1.198 }
1.199 @@ -167,34 +169,35 @@
1.200 *
1.201 * @return a new instance of the File Transfer.
1.202 */
1.203 -GMythFileLocal*
1.204 -gmyth_file_local_new_with_uri (const gchar* uri_str)
1.205 +GMythFileLocal *
1.206 +gmyth_file_local_new_with_uri (const gchar * uri_str)
1.207 {
1.208 - GMythFileLocal *file_local = GMYTH_FILE_LOCAL (g_object_new (GMYTH_FILE_LOCAL_TYPE, NULL));
1.209 - GMythURI *uri = gmyth_uri_new_with_value(uri_str);
1.210 -
1.211 - gmyth_debug( "GMythURI path segment = %s", gmyth_uri_get_path(uri) );
1.212 -
1.213 - g_object_set( GMYTH_FILE( file_local),
1.214 - "backend-info", gmyth_backend_info_new_with_uri( uri_str ),
1.215 - "filename", g_strdup( gmyth_uri_get_path(uri) ),
1.216 - NULL );
1.217 -
1.218 - g_object_unref( uri );
1.219 -
1.220 + GMythFileLocal *file_local =
1.221 + GMYTH_FILE_LOCAL (g_object_new (GMYTH_FILE_LOCAL_TYPE, NULL));
1.222 + GMythURI *uri = gmyth_uri_new_with_value (uri_str);
1.223 +
1.224 + gmyth_debug ("GMythURI path segment = %s", gmyth_uri_get_path (uri));
1.225 +
1.226 + g_object_set (GMYTH_FILE (file_local),
1.227 + "backend-info", gmyth_backend_info_new_with_uri (uri_str),
1.228 + "filename", g_strdup (gmyth_uri_get_path (uri)), NULL);
1.229 +
1.230 + g_object_unref (uri);
1.231 +
1.232 return file_local;
1.233 }
1.234
1.235 -gchar*
1.236 -gmyth_file_local_get_file_name (GMythFileLocal *file_local)
1.237 +gchar *
1.238 +gmyth_file_local_get_file_name (GMythFileLocal * file_local)
1.239 {
1.240 - return gmyth_file_get_file_name( GMYTH_FILE( file_local ) );
1.241 + return gmyth_file_get_file_name (GMYTH_FILE (file_local));
1.242 }
1.243
1.244 void
1.245 -gmyth_file_local_set_file_name (GMythFileLocal *file_local, const gchar* filename)
1.246 +gmyth_file_local_set_file_name (GMythFileLocal * file_local,
1.247 + const gchar * filename)
1.248 {
1.249 - gmyth_file_set_file_name( GMYTH_FILE( file_local ), filename );
1.250 + gmyth_file_set_file_name (GMYTH_FILE (file_local), filename);
1.251 }
1.252
1.253 /**
1.254 @@ -205,24 +208,24 @@
1.255 * @return <code>true</code>, if the connection opening had been done successfully.
1.256 */
1.257 gboolean
1.258 -gmyth_file_local_open (GMythFileLocal *file_local)
1.259 +gmyth_file_local_open (GMythFileLocal * file_local)
1.260 {
1.261 gboolean ret = TRUE;
1.262 GMythFileLocalPrivate *priv;
1.263 gchar *file_name_uri = NULL;
1.264 -
1.265 +
1.266 g_return_val_if_fail (file_local != NULL, FALSE);
1.267
1.268 priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local);
1.269 - file_name_uri = gmyth_file_local_get_file_name(file_local);
1.270 + file_name_uri = gmyth_file_local_get_file_name (file_local);
1.271
1.272 - if ( file_name_uri != NULL )
1.273 - {
1.274 - priv->file_io = g_io_channel_new_file( g_strdup( file_name_uri ), "r+", NULL );
1.275 - g_free( file_name_uri );
1.276 + if (file_name_uri != NULL) {
1.277 + priv->file_io =
1.278 + g_io_channel_new_file (g_strdup (file_name_uri), "r+", NULL);
1.279 + g_free (file_name_uri);
1.280 }
1.281
1.282 - if ( priv->file_io < 0 )
1.283 + if (priv->file_io < 0)
1.284 ret = FALSE;
1.285
1.286 return ret;
1.287 @@ -234,7 +237,7 @@
1.288 * @param file_local The actual File Transfer instance.
1.289 */
1.290 void
1.291 -gmyth_file_local_close (GMythFileLocal *file_local )
1.292 +gmyth_file_local_close (GMythFileLocal * file_local)
1.293 {
1.294 g_return_if_fail (file_local != NULL);
1.295 }
1.296 @@ -247,10 +250,10 @@
1.297 *
1.298 * @return <code>true</code>, if the acquire had been got.
1.299 */
1.300 -static gboolean
1.301 -_control_acquire_context( GMythFileLocal *file_local, gboolean do_wait )
1.302 +static gboolean
1.303 +_control_acquire_context (GMythFileLocal * file_local, gboolean do_wait)
1.304 {
1.305 - gboolean ret = TRUE;
1.306 + gboolean ret = TRUE;
1.307 GMythFileLocalPrivate *priv;
1.308
1.309 g_return_val_if_fail (file_local != NULL, FALSE);
1.310 @@ -267,17 +270,17 @@
1.311 *
1.312 * @return <code>true</code>, if the local file read/write permissions had been releaseds.
1.313 */
1.314 -static gboolean
1.315 -_control_release_context( GMythFileLocal *file_local )
1.316 +static gboolean
1.317 +_control_release_context (GMythFileLocal * file_local)
1.318 {
1.319 gboolean ret = TRUE;
1.320 GMythFileLocalPrivate *priv;
1.321
1.322 g_return_val_if_fail (file_local != NULL, FALSE);
1.323 priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local);
1.324 -
1.325 - g_mutex_unlock (priv->mutex );
1.326 -
1.327 +
1.328 + g_mutex_unlock (priv->mutex);
1.329 +
1.330 return ret;
1.331 }
1.332
1.333 @@ -294,8 +297,9 @@
1.334 * @return The actual block size (in bytes) returned by REQUEST_BLOCK message,
1.335 * or the error code.
1.336 */
1.337 -GMythFileReadResult
1.338 -gmyth_file_local_read(GMythFileLocal *file_local, GByteArray *data, gint size, gboolean read_unlimited)
1.339 +GMythFileReadResult
1.340 +gmyth_file_local_read (GMythFileLocal * file_local, GByteArray * data,
1.341 + gint size, gboolean read_unlimited)
1.342 {
1.343 gsize bytes_read = 0;
1.344 gint64 total_read = 0;
1.345 @@ -312,21 +316,19 @@
1.346
1.347 priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local);
1.348
1.349 - io_status = g_io_channel_set_encoding( priv->file_io, NULL, &error );
1.350 - if ( io_status == G_IO_STATUS_NORMAL )
1.351 - gmyth_debug ( "Setting encoding to binary file data stream.\n" );
1.352 + io_status = g_io_channel_set_encoding (priv->file_io, NULL, &error);
1.353 + if (io_status == G_IO_STATUS_NORMAL)
1.354 + gmyth_debug ("Setting encoding to binary file data stream.\n");
1.355
1.356 - io_cond = g_io_channel_get_buffer_condition( priv->file_io );
1.357 + io_cond = g_io_channel_get_buffer_condition (priv->file_io);
1.358
1.359 - _control_acquire_context (file_local, TRUE );
1.360 + _control_acquire_context (file_local, TRUE);
1.361
1.362 if (size > 0) {
1.363 gchar *data_buffer = g_new0 (gchar, size);
1.364 - io_status = g_io_channel_read_chars (priv->file_io,
1.365 - data_buffer,
1.366 - (gsize) size,
1.367 - &bytes_read,
1.368 - &error);
1.369 +
1.370 + io_status = g_io_channel_read_chars (priv->file_io,
1.371 + data_buffer, (gsize) size, &bytes_read, &error);
1.372
1.373 if (io_status != G_IO_STATUS_NORMAL) {
1.374 gmyth_debug ("Error on io_channel");
1.375 @@ -336,55 +338,60 @@
1.376 }
1.377
1.378 /* append new data to the increasing byte array */
1.379 - data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
1.380 + data =
1.381 + g_byte_array_append (data, (const guint8 *) data_buffer,
1.382 + bytes_read);
1.383 total_read += bytes_read;
1.384 -
1.385 - if (!read_unlimited && ( gmyth_file_local_get_filesize(file_local) > 0) &&
1.386 - (gmyth_file_local_get_offset(file_local) == gmyth_file_local_get_filesize(file_local))) {
1.387 +
1.388 + if (!read_unlimited && (gmyth_file_local_get_filesize (file_local) > 0)
1.389 + && (gmyth_file_local_get_offset (file_local) ==
1.390 + gmyth_file_local_get_filesize (file_local))) {
1.391 retval = GMYTH_FILE_READ_EOF;
1.392 goto error;
1.393 }
1.394 -
1.395 +
1.396 g_free (data_buffer);
1.397 - } else {
1.398 + } else {
1.399 retval = GMYTH_FILE_READ_ERROR;
1.400 }
1.401
1.402 -error:
1.403 + error:
1.404 _control_release_context (file_local);
1.405
1.406 - if ( error != NULL ) {
1.407 - gmyth_debug ("Cleaning-up ERROR: [msg = %s, code = %d]\n", error->message,
1.408 - error->code);
1.409 + if (error != NULL) {
1.410 + gmyth_debug ("Cleaning-up ERROR: [msg = %s, code = %d]\n",
1.411 + error->message, error->code);
1.412 g_error_free (error);
1.413 }
1.414 -
1.415 - if ( total_read > 0 )
1.416 - gmyth_file_local_set_offset(file_local, ( gmyth_file_local_get_offset(file_local) + total_read ) );
1.417 +
1.418 + if (total_read > 0)
1.419 + gmyth_file_local_set_offset (file_local,
1.420 + (gmyth_file_local_get_offset (file_local) + total_read));
1.421
1.422 return retval;
1.423 }
1.424
1.425 gint64
1.426 -gmyth_file_local_seek(GMythFileLocal *file_local, gint64 pos, GSeekType whence)
1.427 +gmyth_file_local_seek (GMythFileLocal * file_local, gint64 pos,
1.428 + GSeekType whence)
1.429 {
1.430 GMythFileLocalPrivate *priv;
1.431 -
1.432 +
1.433 GError *error;
1.434 -
1.435 +
1.436 GIOStatus io_status = G_IO_STATUS_NORMAL;
1.437 -
1.438 +
1.439 g_return_val_if_fail (file_local != NULL, -1);
1.440 -
1.441 +
1.442 priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local);
1.443 -
1.444 +
1.445 io_status = g_io_channel_seek_position (priv->file_io, pos, whence, &error);
1.446 -
1.447 - if ( io_status == G_IO_STATUS_ERROR )
1.448 +
1.449 + if (io_status == G_IO_STATUS_ERROR)
1.450 pos = -1;
1.451 -
1.452 +
1.453 return pos;
1.454 -
1.455 +
1.456 }
1.457
1.458 /**
1.459 @@ -395,11 +402,11 @@
1.460 * @return The actual file_local size in bytes.
1.461 */
1.462 guint64
1.463 -gmyth_file_local_get_filesize (GMythFileLocal *file_local)
1.464 +gmyth_file_local_get_filesize (GMythFileLocal * file_local)
1.465 {
1.466 g_return_val_if_fail (file_local != NULL, 0);
1.467
1.468 - return gmyth_file_get_filesize( GMYTH_FILE(file_local) );
1.469 + return gmyth_file_get_filesize (GMYTH_FILE (file_local));
1.470 }
1.471
1.472 /**
1.473 @@ -409,11 +416,11 @@
1.474 * @param filesize The actual File Transfer size, in bytes.
1.475 */
1.476 void
1.477 -gmyth_file_local_set_filesize (GMythFileLocal *file_local, guint64 filesize)
1.478 +gmyth_file_local_set_filesize (GMythFileLocal * file_local, guint64 filesize)
1.479 {
1.480 g_return_if_fail (file_local != NULL);
1.481
1.482 - gmyth_file_set_filesize( GMYTH_FILE(file_local), filesize );
1.483 + gmyth_file_set_filesize (GMYTH_FILE (file_local), filesize);
1.484 }
1.485
1.486 /**
1.487 @@ -424,11 +431,11 @@
1.488 * @return The actual file offset in bytes.
1.489 */
1.490 gint64
1.491 -gmyth_file_local_get_offset (GMythFileLocal *file_local)
1.492 +gmyth_file_local_get_offset (GMythFileLocal * file_local)
1.493 {
1.494 g_return_val_if_fail (file_local != NULL, 0);
1.495
1.496 - return gmyth_file_get_offset ( GMYTH_FILE(file_local) );
1.497 + return gmyth_file_get_offset (GMYTH_FILE (file_local));
1.498 }
1.499
1.500 /**
1.501 @@ -438,10 +445,9 @@
1.502 * @param offset The actual File Local offset, in bytes.
1.503 */
1.504 void
1.505 -gmyth_file_local_set_offset (GMythFileLocal *file_local, gint64 offset)
1.506 +gmyth_file_local_set_offset (GMythFileLocal * file_local, gint64 offset)
1.507 {
1.508 g_return_if_fail (file_local != NULL);
1.509
1.510 - gmyth_file_set_offset( GMYTH_FILE( file_local ), offset );
1.511 + gmyth_file_set_offset (GMYTH_FILE (file_local), offset);
1.512 }
1.513 -