1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-12 23:23:54 +00:00

Move main model into a new TrCore wrapper class.

This commit is contained in:
Josh Elsasser 2007-05-23 00:49:31 +00:00
parent dfdfe11e19
commit 89cc48f0e8
7 changed files with 258 additions and 49 deletions

View file

@ -32,6 +32,7 @@
#include "conf.h"
#include "dialogs.h"
#include "tr_cell_renderer_progress.h"
#include "tr_core.h"
#include "tr_icon.h"
#include "tr_prefs.h"
#include "util.h"

View file

@ -42,6 +42,7 @@
#include "msgwin.h"
#include "tr_backend.h"
#include "tr_cell_renderer_progress.h"
#include "tr_core.h"
#include "tr_icon.h"
#include "tr_prefs.h"
#include "tr_torrent.h"
@ -67,7 +68,7 @@
struct cbdata {
TrBackend * back;
GtkWindow * wind;
GtkTreeModel * model;
TrCore * core;
TrIcon * icon;
TrPrefs * prefs;
guint timer;
@ -371,32 +372,14 @@ gtksetup( int * argc, char *** argv )
static void
appsetup( TrWindow * wind, benc_val_t * state, GList * args, gboolean paused )
{
GType types[] =
{
/* info->name, info->totalSize, status, error, errorString, */
G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING,
/* progress, rateDownload, rateUpload, eta, peersTotal, */
G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_INT, G_TYPE_INT,
/* peersUploading, peersDownloading, seeders, leechers */
G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,
/* completedFromTracker, downloaded, uploaded left */
G_TYPE_INT, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT64,
/* tracker, the TrTorrent object */
TR_TRACKER_BOXED_TYPE, TR_TORRENT_TYPE,
};
struct cbdata * cbdata;
GtkListStore * store;
guint flags;
/* create the model used to store torrent data */
g_assert( ALEN( types ) == MC_ROW_COUNT );
store = gtk_list_store_newv( MC_ROW_COUNT, types );
/* fill out cbdata */
cbdata = g_new0( struct cbdata, 1 );
cbdata->back = tr_backend_new();
cbdata->wind = NULL;
cbdata->model = GTK_TREE_MODEL(store);
cbdata->core = tr_core_new();
cbdata->icon = NULL;
cbdata->prefs = NULL;
cbdata->timer = 0;
@ -445,7 +428,7 @@ winsetup( struct cbdata * cbdata, TrWindow * wind )
gettext( actions[ii].tooltip ),
actions[ii].key );
}
g_object_set( wind, "model", cbdata->model,
g_object_set( wind, "model", tr_core_model( cbdata->core ),
"double-click-action", ACT_INFO, NULL);
g_signal_connect( wind, "action", G_CALLBACK( windact ), cbdata );
@ -523,6 +506,7 @@ wannaquit( void * vdata )
{
struct cbdata * data;
struct exitdata *edata;
GtkTreeModel * model;
GtkTreeIter iter;
TrTorrent *tor;
@ -544,10 +528,14 @@ wannaquit( void * vdata )
because a reference is added when a torrent is removed
from the model and tr_torrent_stop_polite() is called on it.
*/
if(gtk_tree_model_get_iter_first(data->model, &iter)) {
do
gtk_tree_model_get(data->model, &iter, MC_TORRENT, &tor, -1);
while(gtk_tree_model_iter_next(data->model, &iter));
model = tr_core_model( data->core );
if( gtk_tree_model_get_iter_first( model, &iter) )
{
do
{
gtk_tree_model_get( model, &iter, MC_TORRENT, &tor, -1 );
}
while( gtk_tree_model_iter_next( model, &iter ) );
}
/* try to politely stop all the torrents */
@ -616,7 +604,7 @@ exitcheck( gpointer gdata )
{
gtk_widget_destroy( GTK_WIDGET( cbdata->wind ) );
}
g_object_unref( cbdata->model );
g_object_unref( cbdata->core );
if( NULL != cbdata->icon )
{
g_object_unref( cbdata->icon );
@ -826,6 +814,7 @@ setpex( tr_torrent_t * tor, void * arg )
gboolean
updatemodel(gpointer gdata) {
struct cbdata *data = gdata;
GtkTreeModel * model;
TrTorrent *tor;
tr_stat_t *st;
GtkTreeIter iter;
@ -837,13 +826,15 @@ updatemodel(gpointer gdata) {
return FALSE;
}
if(gtk_tree_model_get_iter_first(data->model, &iter)) {
model = tr_core_model( data->core );
if( gtk_tree_model_get_iter_first( model, &iter ) )
{
do {
gtk_tree_model_get(data->model, &iter, MC_TORRENT, &tor, -1);
gtk_tree_model_get( model, &iter, MC_TORRENT, &tor, -1);
st = tr_torrent_stat(tor);
g_object_unref(tor);
/* XXX find out if setting the same data emits changed signal */
gtk_list_store_set( GTK_LIST_STORE( data->model ), &iter,
gtk_list_store_set( GTK_LIST_STORE( model ), &iter,
MC_STAT, st->status, MC_ERR, st->error,
MC_TERR, st->errorString, MC_PROG, st->progress,
MC_DRATE, st->rateDownload, MC_URATE, st->rateUpload,
@ -853,7 +844,8 @@ updatemodel(gpointer gdata) {
MC_DONE, st->completedFromTracker, MC_TRACKER, st->tracker,
MC_DOWN, st->downloaded, MC_UP, st->uploaded,
MC_LEFT, st->left, -1);
} while(gtk_tree_model_iter_next(data->model, &iter));
}
while( gtk_tree_model_iter_next( model, &iter ) );
}
/* update the main window's statusbar and toolbar buttons */
@ -905,7 +897,7 @@ getselection( struct cbdata * cbdata )
rows = gtk_tree_selection_get_selected_rows( sel, NULL );
for( ii = rows; NULL != ii; ii = ii->next )
{
ref = gtk_tree_row_reference_new( cbdata->model, ii->data );
ref = gtk_tree_row_reference_new( tr_core_model( cbdata->core ), ii->data );
gtk_tree_path_free( ii->data );
ii->data = ref;
}
@ -917,6 +909,7 @@ static void
handleaction( struct cbdata * data, int act )
{
GList *rows, *ii;
GtkTreeModel * model;
GtkTreePath *path;
GtkTreeIter iter;
TrTorrent *tor;
@ -979,12 +972,14 @@ handleaction( struct cbdata * data, int act )
/* get a list of references to selected rows */
rows = getselection( data );
model = tr_core_model( data->core );
changed = FALSE;
for(ii = rows; NULL != ii; ii = ii->next) {
if(NULL != (path = gtk_tree_row_reference_get_path(ii->data)) &&
gtk_tree_model_get_iter(data->model, &iter, path)) {
gtk_tree_model_get(data->model, &iter, MC_TORRENT, &tor,
MC_STAT, &status, -1);
gtk_tree_model_get_iter( model, &iter, path ) )
{
gtk_tree_model_get( model , &iter, MC_TORRENT, &tor,
MC_STAT, &status, -1 );
if( ACT_ISAVAIL( actions[act].flags, status ) )
{
@ -1006,12 +1001,11 @@ handleaction( struct cbdata * data, int act )
{
tr_torrentRemoveSaved( tr_torrent_handle( tor ) );
}
gtk_list_store_remove( GTK_LIST_STORE( data->model ),
&iter );
gtk_list_store_remove( GTK_LIST_STORE( model ), &iter );
changed = TRUE;
break;
case ACT_INFO:
makeinfowind( data->wind, data->model, path, tor );
makeinfowind( data->wind, model, path, tor );
break;
case ACT_OPEN:
case ACT_PREF:
@ -1047,6 +1041,7 @@ addtorrents(void *vdata, void *state, GList *files,
GList *torlist, *errlist, *ii;
char *errstr;
TrTorrent *tor;
GtkTreeModel * model;
GtkTreeIter iter;
const char * pref;
tr_info_t *in;
@ -1081,11 +1076,12 @@ addtorrents(void *vdata, void *state, GList *files,
}
}
model = tr_core_model( data->core );
for( ii = g_list_first( torlist ); NULL != ii; ii = ii->next )
{
gtk_list_store_append( GTK_LIST_STORE( data->model ), &iter );
gtk_list_store_append( GTK_LIST_STORE( model ), &iter );
in = tr_torrent_info( ii->data );
gtk_list_store_set( GTK_LIST_STORE( data->model ), &iter,
gtk_list_store_set( GTK_LIST_STORE( model ), &iter,
MC_NAME, in->name,
MC_SIZE, in->totalSize,
MC_TORRENT, ii->data, -1);

137
gtk/tr_core.c Normal file
View file

@ -0,0 +1,137 @@
/******************************************************************************
* $Id$
*
* Copyright (c) 2007 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include <string.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "tr_core.h"
#include "tr_torrent.h"
#include "util.h"
static void
tr_core_init( GTypeInstance * instance, gpointer g_class );
static void
tr_core_class_init( gpointer g_class, gpointer g_class_data );
static void
tr_core_dispose( GObject * obj );
GType
tr_core_get_type( void )
{
static GType type = 0;
if( 0 == type )
{
static const GTypeInfo info =
{
sizeof( TrCoreClass ),
NULL, /* base_init */
NULL, /* base_finalize */
tr_core_class_init, /* class_init */
NULL, /* class_finalize */
NULL, /* class_data */
sizeof( TrCore ),
0, /* n_preallocs */
tr_core_init, /* instance_init */
NULL,
};
type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 );
}
return type;
}
static void
tr_core_class_init( gpointer g_class, gpointer g_class_data SHUTUP )
{
GObjectClass * gobject_class;
gobject_class = G_OBJECT_CLASS( g_class );
gobject_class->dispose = tr_core_dispose;
}
static void
tr_core_init( GTypeInstance * instance, gpointer g_class SHUTUP )
{
TrCore * self = (TrCore *) instance;
GtkListStore * store;
/* column types for the model used to store torrent information */
/* keep this in sync with the enum near the bottom of tr_core.h */
GType types[] =
{
/* info->name, info->totalSize, status, error, errorString, */
G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING,
/* progress, rateDownload, rateUpload, eta, peersTotal, */
G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_INT, G_TYPE_INT,
/* peersUploading, peersDownloading, seeders, leechers */
G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,
/* completedFromTracker, downloaded, uploaded left */
G_TYPE_INT, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT64,
/* tracker, the TrTorrent object */
TR_TRACKER_BOXED_TYPE, TR_TORRENT_TYPE,
};
/* create the model used to store torrent data */
g_assert( ALEN( types ) == MC_ROW_COUNT );
store = gtk_list_store_newv( MC_ROW_COUNT, types );
self->model = GTK_TREE_MODEL( store );
self->disposed = FALSE;
}
static void
tr_core_dispose( GObject * obj )
{
TrCore * self = (TrCore *) obj;
GObjectClass * parent;
if( self->disposed )
{
return;
}
self->disposed = TRUE;
g_object_unref( self->model );
/* Chain up to the parent class */
parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) );
parent->dispose( obj );
}
TrCore *
tr_core_new( void )
{
return g_object_new( TR_CORE_TYPE, NULL );
}
GtkTreeModel *
tr_core_model( TrCore * self )
{
TR_IS_CORE( self );
return self->model;
}

82
gtk/tr_core.h Normal file
View file

@ -0,0 +1,82 @@
/******************************************************************************
* $Id$
*
* Copyright (c) 2007 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#ifndef TR_CORE_H
#define TR_CORE_H
#include <glib-object.h>
#include <gtk/gtk.h>
#define TR_CORE_TYPE ( tr_core_get_type() )
#define TR_CORE( obj ) \
( G_TYPE_CHECK_INSTANCE_CAST( (obj), TR_CORE_TYPE, TrCore ) )
#define TR_CORE_CLASS( class ) \
( G_TYPE_CHECK_CLASS_CAST( (class), TR_CORE_TYPE, TrCoreClass ) )
#define TR_IS_CORE( obj ) \
( G_TYPE_CHECK_INSTANCE_TYPE( (obj), TR_CORE_TYPE ) )
#define TR_IS_CORE_CLASS( class ) \
( G_TYPE_CHECK_CLASS_TYPE( (class), TR_CORE_TYPE ) )
#define TR_CORE_GET_CLASS( obj ) \
( G_TYPE_INSTANCE_GET_CLASS( (obj), TR_CORE_TYPE, TrCoreClass ) )
typedef struct _TrCore TrCore;
typedef struct _TrCoreClass TrCoreClass;
/* treat the contents of this structure as private */
struct _TrCore
{
GObject parent;
GtkTreeModel * model;
gboolean disposed;
};
struct _TrCoreClass
{
GObjectClass parent;
};
GType
tr_core_get_type( void );
TrCore *
tr_core_new( void );
GtkTreeModel *
tr_core_model( TrCore * self );
/* column names for the model used to store torrent information */
/* keep this in sync with the type array in tr_core_init() in tr_core.c */
enum {
MC_NAME, MC_SIZE, MC_STAT, MC_ERR, MC_TERR,
MC_PROG, MC_DRATE, MC_URATE, MC_ETA, MC_PEERS,
MC_UPEERS, MC_DPEERS, MC_SEED, MC_LEECH, MC_DONE,
MC_DOWN, MC_UP, MC_LEFT, MC_TRACKER, MC_TORRENT, MC_ROW_COUNT,
};
#endif

View file

@ -30,6 +30,7 @@
#include "transmission.h"
#include "tr_cell_renderer_progress.h"
#include "tr_core.h"
#include "tr_torrent.h"
#include "tr_window.h"
#include "util.h"

View file

@ -61,14 +61,6 @@ typedef void (*callbackfunc_t)(void*);
( ACTF_INACTIVE & (flags) && TR_STATUS_INACTIVE & (status) ) || \
ACTF_ALWAYS & (flags) )
/* column names for the model used to store torrent information */
enum {
MC_NAME, MC_SIZE, MC_STAT, MC_ERR, MC_TERR,
MC_PROG, MC_DRATE, MC_URATE, MC_ETA, MC_PEERS,
MC_UPEERS, MC_DPEERS, MC_SEED, MC_LEECH, MC_DONE,
MC_DOWN, MC_UP, MC_LEFT, MC_TRACKER, MC_TORRENT, MC_ROW_COUNT,
};
/* try to interpret a string as a textual representation of a boolean */
/* note that this isn't localized */
gboolean

View file

@ -4,8 +4,8 @@ include ../mk/config.mk
include ../mk/common.mk
SRCS = conf.c dialogs.c io.c ipc.c main.c msgwin.c tr_backend.c \
tr_cell_renderer_progress.c tr_icon.c tr_prefs.c tr_torrent.c \
tr_window.c util.c
tr_cell_renderer_progress.c tr_core.c tr_icon.c tr_prefs.c \
tr_torrent.c tr_window.c util.c
OBJS = $(SRCS:%.c=%.o)
CFLAGS += $(CFLAGS_GTK) -I../libtransmission