[gnome-network]Re: download manager



Hi

Ok, so there are 3 people trying an implementation of the download
manager, and I am in the middle :-)

So, please, have a discussion (on the list, please :-) to see what is
wrong/good in current implementations (Manuel's downman), what we want
and what we are going to do.

I am myself only looking for the following:

	* basic functionality in libgnetwork
	* try to push that library to be used in other apps
	* have a nice GUI

As you see, I ask very little :-) The Bonobo-based architecture plan
seems good to me, and having the libgnetwork code be just a wrapper that
accesses the Bonobo component, also looks good.

Attached code is what Tapia has been doing. I haven't look at it much
(sorry Tapia :-).

cheers

On Sun, 2003-09-21 at 21:23, tapia wrote:
> Hola, Rodrigo.
> 
> Me estoy poniendo con el download manager. Te mando lo que tengo hecho.
> 
> Pero tengo una pregunta. Mientras leo de la url de origen, el bucle es
> algo parecido a esto:
> 
> while (!download->priv->paused)
> {
>    // leo del origen
> 
>    // escribo en el destino
> }
> 
> Pero para el método gnetwork_download_manager_pause lo suyo sería hacer
> 
> download->priv->paused = TRUE
> 
> pero claro, tal como lo tengo hecho, no sale de ese bucle while hasta
> que termine.
> 
> Lo suyo sería hacerlo con hilos, así no hay problema, pero siempre me
> decís que los hilos mejor no tocarlos. ¿Cómo te parece que puedo
> solucionar esto de una forma adecuada?
> 
> Un saludo.
 /* 
  * GNOME NETWORK library
  * Download Manager
  *
  * AUTHORS:
  *      Cesar Garcia Tapia <cesar garcia tapia hispalinux es>
  *
  * Copyright (C) 2003 The GNOME Foundation.
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
  *
  * This Library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this Library; see the file COPYING.LIB.  If not,
  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */

#include "gnetwork-downloader.h"
#include <libgnomevfs/gnome-vfs.h>

#define PARENT_TYPE G_TYPE_OBJECT
#define BYTES_TO_PROCESS 256

struct _GNetworkDownloadManagerPrivate {
	GNetworkDownloadManagerConfig *config;
	GnomeVFSURI                   *origin_uri;
	GnomeVFSURI                   *destination_uri;
	GnomeVFSHandle                *read_handle;
	GnomeVFSHandle                *write_handle;
	GnomeVFSFileSize              bytes_read;
	GnomeVFSFileSize              bytes_written;
	guint buffer[BYTES_TO_PROCESS];
	gboolean                      paused;
};

GType
gnetwork_download_manager_get_type (void)
{
	static GType type = 0;
	
	if (!type) {
		static const GTypeInfo info = {
			sizeof (GNetworkDownloadManagerClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) gnetwork_download_manager_class_init,
			(GClassFinalizeFunc) NULL,
			(gconstpointer) NULL,
			sizeof (GNetworkDownloadManager),
			(guint16) 0,
			(GInstanceInitFunc) gnetwork_donwload_manager_init
		};
		
		type = g_type_register_static (PARENT_TYPE,
				               "GNetworkDownloadManager",
				               &info, 0);
	}
	return type;
}

static void
gnetwork_download_manager_init (GNetworkDownloadManager *download)
{
	g_return_if_fail (GNETWORK_IS_DOWNLOAD_MANAGER (download));
	g_return_if_fail (gnome_vfs_init ());
	
	donwload->priv = g_new0 (GNetworkDownloadManagerPrivate, 1);
}

static void
gnetwork_download_manager_class_init (GNetworkDownloadManager *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	
	parent_class = g_type_class_peek_parent (klass);
	object_class->finalize = gnetwork_download_manager_finalize;
}

static void
gnetwork_download_manager_finalize (GObject *object)
{
	GNetworkDownloadManager *download = GNETWORK_DOWNLOAD_MANAGER (object);
	
	if (server->priv) {
		g_free (server->priv);
		server->priv = NULL;
	}
	
	if (G_OBJECT_CLASS (parent_class)->finalize) {
		(* G_OBJECT_CLASS (parent_class)->finalize) (object);
	}
}

GNetworkDownloadManager*     
gnetwork_download_manager_new (GNetworkDownloadManagerConfig *config)
{
	GNetworkDownloadManager *download;
	
	download = g_object_new (GNETWORK_TYPE_DOWNLOAD_MANAGER, NULL);
	if (config) {
		gnetwork_download_manager_set_config (download, config);
	}

	download->priv->bytes_read    = (GnomeVFSFileSize) 0;
	download->priv->bytes_written = (GnomeVFSFileSize) 0;
	
	return download;
}

gboolean                  
gnetwork_download_manager_set_config (GNetworkDownloadManager *download, GNetworkDownloadManagerConfig *config)
{
	g_return_val_if_fail (GNETWORK_IS_DOWNLOAD_MANAGER (download), FALSE);
	g_return_val_if_fail (config != NULL, FALSE);
	
	download->priv->config = config;
	return TRUE;
}

GNetworkDownloadManagerConfig*
gnetwork_download_manager_get_config (GNetworkDownloadManager *download)
{
	g_return_val_if_fail (GNETWORK_IS_DOWNLOAD_MANAGER (download), NULL);
	
	return download->priv->config;
}

gboolean 
gnetwork_download_manager_set_origin_url (GNetworkDownloadManager *download, gstring *url)
{
	GnomeVFSURI uri = gnome_vfs_uri_new (url);

	g_return_val_if_fail (uri |= NULL, FALSE);

	download->priv->origin_uri = uri;
	return TRUE;
}

gstring
gnetwork_download_manager_get_origin_url (GNetworkDownloadManager *download)
{
	g_return_val_if_fail (GNETWORK_IS_DOWNLOAD_MANAGER (download), NULL);
	
	return gnome_vfs_uri_to_string(download->priv->origin_uri, GNOME_VFS_URI_HIDE_NONE);
}

gboolean  
gnetwork_download_manager_set_destination_url (GNetworkDownloadManager *download, gstring *url)
{
	GnomeVFSURI uri = gnome_vfs_uri_new (url);

	g_return_val_if_fail (uri != NULL, FALSE);

	download->priv->destination_uri = uri;
	return TRUE;

}

gstring 
gnetwork_download_manager_get_destination_url (GnetworkDownloadManager *download)
{
	g_return_val_if_fail (GNETWORK_IS_DOWNLOAD_MANAGER (download), NULL);
	
	return gnome_vfs_uri_to_string(download->priv->destination_uri, GNOME_VFS_URI_HIDE_NONE);
}

gboolean
gnetwork_download_manager_start (GNetworkDownloadManager *download)
{
	gnome_vfs_open_uri (download->priv->read_handle,
	                    download->priv->origin_uri,
			    GNOME_VFS_OPEN_READ);

	gnome_vfs_create_uri (download->priv->write_handle,
			            download->priv->destination_uri,
				    GNOME_VFS_OPEN_WRITE,
				    FALSE,
				    0x644);

	download->priv->paused = FALSE;
	gnetwork_download_manager_read (download);
}

void gnetwork_download_manager_read (GNetworkDownloadManager *download)
{
	GnomeVFSFileSize bytes_read_this_time;
	GnomeVFSFileSize bytes_written_this_time;

	gnome_vfs_seek (download->priv->read_handle,
			(GnomeVFSSeekPosition)download->priv_bytes_written,
			(GnomeVFSFileOffset) 0);
	
	while (!download->priv->paused)
	{
		gnome_vfs_read (download->priv->read_handle,
			        download->priv->buffer,
			        (GnomeVFSFileSize) BYTES_TO_PROCCESS,
				*bytes_read_this_time);

		gnome_vfs_write (download->priv->write_handle,
				 download->priv->buffer,
				 (GnomeVFSFileSize) BYTES_TO_PROCCESS,
				 *bytes_written_this_time);
	}
}

void 
gnetwork_download_manager_stop (GNetworkDownloadManager *download)
{
}

gboolean
gnetwork_download_manager_pause (GNetworkDownloadManager *download)
{
}
 /* 
  * GNOME NETWORK library
  * Download Manager
  *
  * AUTHORS:
  *      Cesar Garcia Tapia <cesar garcia tapia hispalinux es>
  *
  * Copyright (C) 2003 The GNOME Foundation.
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
  *
  * This Library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this Library; see the file COPYING.LIB.  If not,
  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */

#ifndef __GNETWORK_DOWNLOAD_MANAGER_H__
#define __GNETWORK_DOWNLOAD_MANAGER_H__

#include <glib-object.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define GNETWORK_TYPE_DOWNLOAD_MANAGER            (gnetwork_download_manager_get_type())
#define GNETWORK_DOWNLOAD_MANAGER(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GNETWORK_TYPE_DOWNLOAD_MANAGER, GNetworkDownloadManager))
#define GNETWORK_DOWNLOAD_MANAGER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GNETWORK_TYPE_DOWNLOAD_MANAGER, GNetworkDownloadManagerClass))
#define GNETWORK_IS_DOWNLOAD_MANAGER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, GNETWORK_TYPE_DOWNLOAD_MANAGER))
#define GNETWORK_IS_DOWNLOAD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNETWORK_TYPE_DOWNLOAD_MANAGER))

typedef struct _GNetworkDownloadManager        GNetworkDownloadManager;
typedef struct _GNetworkDownloadManagerClass   GNetworkDownloadManagerClass;
typedef struct _GNetworkDownloadManagerPrivate GNetworkDownloadManagerPrivate;
typedef struct _GNetworkDownloadManagerConfig  GNetworkDownloadManagerConfig;
 
struct _GNetworkDownloadManager {
	GObject parent;
	GNetworkDownloadManagerPrivate *priv;
};

struct _GNetworkDownloadManagerClass {
	GObjectClass parent_class;
};

struct _GNetworkDownloadManagerConfig {
	gchar    *download_directory;
};

GType                          gnetwork_download_manager_get_type   (void);
GNetworkDownloadManager*       gnetwork_download_manager_new                 (GNetworkDownloadManagerConfig *config);
gboolean                       gnetwork_download_manager_set_config          (GNetworkDownloadManager *download, GNetworkDownloadManagerConfig *config);
GNetworkDownloadManagerConfig* gnetwork_download_manager_get_config          (GNetworkDownloadManager *download);
gboolean                       gnetwork_download_manager_set_origin_url      (GNetworkDownloadManager *download, gstring *url);
gstring                        gnetwork_download_manager_get_origin_url      (GNetworkDownloadManager *download);
gboolean                       gnetwork_download_manager_set_destination_url (GNetworkDownloadManager *download, gstring *url);
gstring                        gnetwork_download_manager_get_destination_url (GnetworkDownloadManager *download);
gboolean                       gnetwork_download_manager_start               (GNetworkDownloadManager *download, GnomeVFSSeekPosition start_position);
void                           gnetwork_download_manager_stop                (GNetworkDownloadManager *download);
gboolean                       gnetwork_download_manager_pause               (GNetworkDownloadManager *download);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __GNETWORK_DOWNLOAD_MANAGER_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]