00001 #include <string.h>
00002
00003 #include <gtask/gtask-factory.h>
00004
00007
00008
00009 enum {
00010 PROP_0,
00011 PROP_APP,
00012 PROP_PROGRAM,
00013 PROP_CONN_GROUP
00014 };
00015
00016 static gpointer parent_class;
00017 static GTaskFactory *default_factory = NULL;
00018
00019 static void
00020 gtask_factory_class_init( GTaskFactoryClass *factory );
00021
00022 static void
00023 gtask_factory_init( GTaskFactory *factory );
00024
00025 static void
00026 gtask_factory_dispose( GObject *obj );
00027
00028 static void
00029 gtask_factory_finalize( GObject *obj );
00030
00031 static gchar *
00032 gtask_factory_next_task_id( GTaskFactory *factory );
00033
00034 static void
00035 gtask_factory_set_property( GObject *object,
00036 guint param_id,
00037 const GValue *value,
00038 GParamSpec *pspec );
00039
00040 static void
00041 gtask_factory_get_property( GObject *object,
00042 guint param_id,
00043 GValue *value,
00044 GParamSpec *pspec );
00045
00046 static GTaskFactory *
00047 gtask_factory_default( void );
00048
00049 GType gtask_factory_get_type( ) {
00050 static GType type = 0;
00051
00052 if( type == 0 ) {
00053 static const GTypeInfo info = {
00054 sizeof( GTaskFactoryClass ),
00055 NULL,
00056 NULL,
00057 (GClassInitFunc) gtask_factory_class_init,
00058 NULL,
00059 NULL,
00060 sizeof( GTaskFactory ),
00061 0,
00062 (GInstanceInitFunc) gtask_factory_init
00063 };
00064
00065 type = g_type_register_static( G_TYPE_OBJECT,
00066 "GTaskFactory",
00067 &info,
00068 0 );
00069 }
00070
00071 return type;
00072 }
00073
00074 static void
00075 gtask_factory_class_init( GTaskFactoryClass *klass ) {
00076 GObjectClass *gobject_class = G_OBJECT_CLASS( klass );
00077
00078 parent_class = g_type_class_peek_parent( klass );
00079
00080 gobject_class->dispose = gtask_factory_dispose;
00081 gobject_class->finalize = gtask_factory_finalize;
00082 gobject_class->get_property= gtask_factory_get_property;
00083 gobject_class->set_property= gtask_factory_set_property;
00084
00085 g_object_class_install_property( (GObjectClass *) klass,
00086 PROP_PROGRAM,
00087 g_param_spec_string( "program",
00088 "Program",
00089 "The name of the program (used as a key in for program preferences, etc).",
00090 "",
00091 G_PARAM_READWRITE ) );
00092
00093 g_object_class_install_property( (GObjectClass *) klass,
00094 PROP_APP,
00095 g_param_spec_string( "application",
00096 "Application",
00097 "A human readable name of the application.",
00098 "",
00099 G_PARAM_READWRITE ) );
00100
00101 g_object_class_install_property( (GObjectClass *) klass,
00102 PROP_CONN_GROUP,
00103 g_param_spec_object( "connection-group",
00104 "Connection Group",
00105 "The GTaskConnectionGroup that all objects created through the factory will serialize to by default.",
00106 GTASK_CONNECTION_GROUP_TYPE,
00107 G_PARAM_READWRITE ) );
00108 }
00109
00110
00118 static void
00119 gtask_factory_init( GTaskFactory *factory ) {
00120 GTaskFactoryPrivate *private = g_new0( GTaskFactoryPrivate, 1 );
00121
00122 factory->private = private;
00123
00126 if( !g_get_application_name( ) )
00127 private->application = g_string_new( "No Application Name Specified" );
00128 else
00129 private->application = g_string_new( g_get_application_name( ) );
00130
00131 if( !g_get_prgname( ) )
00132 private->program = g_string_new( "-unspecified-program-" );
00133 else
00134 private->program = g_string_new( g_get_prgname( ) );
00135 private->last_task_id = 0;
00136
00137 private->dispose_has_run = FALSE;
00138 }
00139
00140 static void
00141 gtask_factory_dispose( GObject *obj ) {
00142 GTaskFactory *factory = GTASK_FACTORY( obj );
00143
00144 if( factory->private->dispose_has_run )
00145 return;
00146
00147 g_object_unref( factory->private->conn_group );
00148 factory->private->dispose_has_run = TRUE;
00149
00150 if( G_OBJECT_CLASS( parent_class )->dispose )
00151 G_OBJECT_CLASS( parent_class )->dispose( obj );
00152 }
00153
00154 static void
00155 gtask_factory_finalize( GObject *obj ) {
00156 GTaskFactory *factory = GTASK_FACTORY( obj );
00157
00158 g_string_free( factory->private->application, TRUE );
00159 g_string_free( factory->private->program, TRUE );
00160 g_free( factory->private );
00161
00162 if( G_OBJECT_CLASS( parent_class )->finalize )
00163 G_OBJECT_CLASS( parent_class )->finalize( obj );
00164 }
00165
00166 static void
00167 gtask_factory_set_property( GObject *object,
00168 guint param_id,
00169 const GValue *value,
00170 GParamSpec *pspec )
00171 {
00172 GTaskFactory *factory = GTASK_FACTORY( object );
00173
00174 switch( param_id ) {
00175 case PROP_APP:
00176 gtask_factory_set_application( factory, g_value_get_string( value ) );
00177 break;
00178 case PROP_PROGRAM:
00179 gtask_factory_set_program( factory, g_value_get_string( value ) );
00180 break;
00181 case PROP_CONN_GROUP:
00182 gtask_factory_set_connection_group( factory, g_value_get_object( value ) );
00183 break;
00184 default:
00185 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, param_id, pspec );
00186 }
00187 }
00188
00189 static void
00190 gtask_factory_get_property( GObject *object,
00191 guint param_id,
00192 GValue *value,
00193 GParamSpec *pspec )
00194 {
00195 GTaskFactory *factory = GTASK_FACTORY( object );
00196
00197 switch( param_id ) {
00198 case PROP_APP:
00199 g_value_set_string( value, factory->private->application->str );
00200 break;
00201 case PROP_PROGRAM:
00202 g_value_set_string( value, factory->private->program->str );
00203 break;
00204 case PROP_CONN_GROUP:
00205 g_value_set_object( value, factory->private->conn_group );
00206 break;
00207 default:
00208 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, param_id, pspec );
00209 }
00210 }
00211
00219 GTaskFactory *
00220 gtask_factory_new( ) {
00221 return g_object_new( GTASK_FACTORY_TYPE, NULL );
00222 }
00223
00224 static GTaskFactory *
00225 gtask_factory_default( void ) {
00226 if( !default_factory )
00227 default_factory = gtask_factory_new( );
00228
00229 return default_factory;
00230 }
00231
00253 GTaskFactory *
00254 gtask_factory_new_with_resources( GTaskConnectionGroup *conn_group,
00255 const gchar *program,
00256 const gchar *application )
00257 {
00258 GTaskFactory *factory = gtask_factory_new( );
00259
00260 gtask_factory_set_application( factory, application );
00261 gtask_factory_set_program( factory, program );
00262 gtask_factory_set_connection_group( factory, conn_group );
00263
00264 return factory;
00265 }
00266
00288 GTaskGenericTask *
00289 gtask_factory_create_task( GTaskFactory *factory, gchar *task_type ) {
00290 GTaskGenericTask *task;
00291 char *fact_task_id;
00292 char *task_id;
00293
00295 if( !factory )
00296 factory = gtask_factory_default( );
00297 else
00298 g_return_val_if_fail( GTASK_IS_FACTORY( factory ), NULL );
00299
00300 if( !task_type )
00301 task_type = "GTaskGenericTask";
00302
00303 task = gtask_generic_task_new_with_connection_group( factory->private->conn_group );
00304
00305 if( strcmp( task_type, "GTaskDownloadTask" ) == 0 ) {
00306 gtask_generic_task_set_category( task, "Downloads" );
00307 } else if( strcmp( task_type, "GTaskPrintTask" ) == 0 ) {
00308 gtask_generic_task_set_category( task, "Print Jobs" );
00309 } else {
00310 gtask_generic_task_set_category( task, "Unspecified" );
00311 }
00312
00313 fact_task_id = gtask_factory_next_task_id( factory );
00314
00317 task_id = g_strdup_printf( "%s-%s", gtask_factory_get_program( factory ),
00318 fact_task_id );
00319
00320 gtask_generic_task_set_id( task, task_id );
00321 gtask_generic_task_set_application( task, factory->private->application->str );
00322
00323 g_free( fact_task_id );
00324 g_free( task_id );
00325
00326 return task;
00327 }
00328
00338 static gchar *
00339 gtask_factory_next_task_id( GTaskFactory *factory ) {
00340 g_return_val_if_fail( GTASK_IS_FACTORY( factory ), "" );
00341
00342 return g_strdup_printf( "%d", factory->private->last_task_id++ );
00343 }
00344
00355 const gchar *
00356 gtask_factory_get_program( GTaskFactory *factory ) {
00357 if( !factory )
00358 factory = gtask_factory_default( );
00359 else
00360 g_return_val_if_fail( GTASK_IS_FACTORY( factory ), "" );
00361
00362 return factory->private->program->str;
00363 }
00364
00371 void
00372 gtask_factory_set_program( GTaskFactory *factory, const char *program ) {
00373 if( !factory )
00374 factory = gtask_factory_default( );
00375 else
00376 g_return_if_fail( GTASK_IS_FACTORY( factory ) );
00377
00378 g_return_if_fail( program != NULL );
00379
00380 g_string_assign( factory->private->program, program );
00381
00382 g_object_notify( (GObject *) factory, "program" );
00383 }
00384
00395 const gchar *
00396 gtask_factory_get_application( GTaskFactory *factory ) {
00397 if( !factory )
00398 factory = gtask_factory_default( );
00399 else
00400 g_return_val_if_fail( GTASK_IS_FACTORY( factory ), "" );
00401
00402 return factory->private->application->str;
00403 }
00404
00411 void
00412 gtask_factory_set_application( GTaskFactory *factory,
00413 const char *application )
00414 {
00415 if( !factory )
00416 factory = gtask_factory_default( );
00417 else
00418 g_return_if_fail( GTASK_IS_FACTORY( factory ) );
00419
00420 g_return_if_fail( application != NULL );
00421
00422 g_string_assign( factory->private->application, application );
00423
00424 g_object_notify( (GObject *) factory, "application" );
00425 }
00426
00434 GTaskConnectionGroup *
00435 gtask_factory_get_connection_group( GTaskFactory *factory ) {
00436 if( !factory )
00437 factory = gtask_factory_default( );
00438 else
00439 g_return_val_if_fail( GTASK_IS_FACTORY( factory ), NULL );
00440
00441 g_return_val_if_fail( !factory->private->dispose_has_run, NULL );
00442
00443 return factory->private->conn_group;
00444 }
00445
00455 void
00456 gtask_factory_set_connection_group( GTaskFactory *factory,
00457 GTaskConnectionGroup *conn_group ) {
00458 g_return_if_fail( GTASK_IS_CONNECTION_GROUP( conn_group ) );
00459
00460 if( !factory )
00461 factory = gtask_factory_default( );
00462 else
00463 g_return_if_fail( GTASK_IS_FACTORY( factory ) );
00464
00465 g_return_if_fail( !factory->private->dispose_has_run );
00466
00467 if( factory->private->conn_group )
00468 g_object_unref( factory->private->conn_group );
00469
00470 g_object_ref( conn_group );
00471
00472 factory->private->conn_group = conn_group;
00473
00474 g_object_notify( (GObject *) factory, "connection-group" );
00475 }
00476
00477