00001 #include <gtask/gtask-error-packet.h>
00002
00005
00006
00007 static gpointer parent_class;
00008
00009 static void
00010 gtask_error_packet_class_init( GTaskErrorPacketClass *klass );
00011
00012 static void
00013 gtask_error_packet_init( GTaskErrorPacket *packet, gpointer g_class );
00014
00015 static void
00016 gtask_error_packet_finalize( GObject *obj );
00017
00018 static gboolean
00019 gtask_error_packet_serialize_to( GTaskPacket *packet,
00020 GTaskConnection *conn );
00021
00022 GType gtask_error_packet_get_type( void ) {
00023 static GType type = 0;
00024
00025 if( type == 0 ) {
00026 static const GTypeInfo info = {
00027 sizeof( GTaskErrorPacketClass ),
00028 NULL,
00029 NULL,
00030 (GClassInitFunc) gtask_error_packet_class_init,
00031 NULL,
00032 NULL,
00033 sizeof( GTaskErrorPacket ),
00034 0,
00035 (GInstanceInitFunc) gtask_error_packet_init
00036 };
00037
00038 type = g_type_register_static( GTASK_PACKET_TYPE,
00039 "GTaskErrorPacket",
00040 &info,
00041 0 );
00042 }
00043
00044 return type;
00045 }
00046
00047 static void
00048 gtask_error_packet_class_init( GTaskErrorPacketClass *klass ) {
00049 GObjectClass *gobject_class = G_OBJECT_CLASS( klass );
00050 GTaskPacketClass *packet_class = GTASK_PACKET_CLASS( klass );
00051
00052 parent_class = g_type_class_peek_parent( klass );
00053
00054 gobject_class->finalize = gtask_error_packet_finalize;
00055
00056 packet_class->serialize_packet = gtask_error_packet_serialize_to;
00057 }
00058
00059 static void
00060 gtask_error_packet_init( GTaskErrorPacket *packet, gpointer g_class ) {
00061 GTaskErrorPacketPrivate *private = g_new0( GTaskErrorPacketPrivate, 1 );
00062
00063 packet->private = private;
00064
00065 private->error = GTASK_ERROR_NONE;
00066 private->error_msg = g_string_new( NULL );
00067 }
00068
00069 static void
00070 gtask_error_packet_finalize( GObject *obj ) {
00071 GTaskErrorPacket *packet = GTASK_ERROR_PACKET( obj );
00072 GTaskErrorPacketPrivate *private = packet->private;
00073
00074 g_string_free( private->error_msg, TRUE );
00075
00076 g_free( private );
00077
00078 if( G_OBJECT_CLASS( parent_class )->finalize )
00079 G_OBJECT_CLASS( parent_class )->finalize( obj );
00080 }
00081
00096 static gboolean
00097 gtask_error_packet_serialize_to( GTaskPacket *packet, GTaskConnection *conn ) {
00098 GTaskErrorPacket *e_packet;
00099 xmlOutputBufferPtr xml_output;
00100 xmlDocPtr doc;
00101 xmlNodePtr error_node;
00102 xmlNodePtr error_msg;
00103
00104 g_return_val_if_fail( GTASK_IS_ERROR_PACKET( packet ), FALSE );
00105 g_return_val_if_fail( GTASK_IS_CONNECTION( conn ), FALSE );
00106 g_return_val_if_fail( gtask_connection_is_connected( conn ), FALSE );
00107
00108 e_packet = (GTaskErrorPacket *) packet;
00109
00110 doc = xmlNewDoc( "1.0" );
00111
00112 g_return_val_if_fail( doc != NULL, FALSE );
00113
00114 error_node = xmlNewNode( NULL, "error" );
00115
00116 if( !error_node ) {
00117 xmlFreeDoc( doc );
00118
00119 return FALSE;
00120 }
00121
00122 xmlDocSetRootElement( doc, error_node );
00123 xmlNewProp( error_node, "type", "0" );
00124
00125 error_msg = xmlNewText( e_packet->private->error_msg->str );
00126
00127 if( !error_msg ) {
00128 xmlFreeDoc( doc );
00129
00130 return FALSE;
00131 }
00132
00133 xmlAddChild( error_node, error_msg );
00134
00135 xml_output = gtask_connection_get_xml_output_buffer( conn );
00136 xmlNodeDumpOutput( xml_output, doc, error_node, 1, 0, NULL );
00137 xmlOutputBufferFlush( xml_output );
00138
00139 xmlFreeDoc( doc );
00140
00141 return TRUE;
00142 }
00143
00151 GTaskErrorPacket *
00152 gtask_error_packet_new( ) {
00153 return g_object_new( GTASK_ERROR_PACKET_TYPE, NULL );
00154 }
00155
00165 GTaskErrorType
00166 gtask_error_packet_get_error_type( GTaskErrorPacket *packet ) {
00167 g_return_val_if_fail( GTASK_IS_ERROR_PACKET( packet ), GTASK_ERROR_NONE );
00168
00169 return packet->private->error;
00170 }
00171
00180 void
00181 gtask_error_packet_set_error_type( GTaskErrorPacket *packet,
00182 GTaskErrorType error )
00183 {
00184 g_return_if_fail( GTASK_IS_ERROR_PACKET( packet ) );
00185
00186 packet->private->error = error;
00187 }
00188
00198 const char *
00199 gtask_error_packet_get_error_message( GTaskErrorPacket *packet ) {
00200 g_return_val_if_fail( GTASK_IS_ERROR_PACKET( packet ), "" );
00201
00202 return packet->private->error_msg->str;
00203 }
00204
00213 void
00214 gtask_error_packet_set_error_message( GTaskErrorPacket *packet,
00215 const char *error_msg )
00216 {
00217 GTaskErrorPacketPrivate *private;
00218
00219 g_return_if_fail( GTASK_IS_ERROR_PACKET( packet ) );
00220
00221 private = packet->private;
00222
00227 g_string_assign( private->error_msg, error_msg );
00228 }
00229
00230