From 2932173873aa5e48bc383ef2c3f593eeac3ada00 Mon Sep 17 00:00:00 2001 From: Jesse van den Kieboom Date: Tue, 15 Nov 2005 12:00:18 +0000 Subject: [PATCH] Added auto login, added mcp reset on disconnect, added editor handling --- gnoemoe/gm-world.c | 129 +++++++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 44 deletions(-) diff --git a/gnoemoe/gm-world.c b/gnoemoe/gm-world.c index 2d0365f..0d83550 100644 --- a/gnoemoe/gm-world.c +++ b/gnoemoe/gm-world.c @@ -21,12 +21,15 @@ void gm_world_load_triggers(GmWorld *world); void on_gm_world_net_state_changing(GmNet *net, GmNetState state, GmWorld *world); +void on_gm_world_net_state_changed(GmNet *net, GmNetState state, + GmWorld *world); void on_gm_world_net_net_error(GmNet *net, gchar *error, gint code, GmWorld *world); void on_gm_world_net_bytes_recv(GmNet *net, gchar *text, gint len, GmWorld *world); void on_gm_world_options_option_changed(GmOptions *options, gchar *key, GmWorld *world); +void on_gm_world_editor_save(GmEditor *editor, GmWorld *world); struct _GmWorldPrivate { gchar *path; @@ -34,7 +37,6 @@ struct _GmWorldPrivate { gboolean active; guint activity; gchar *buffer; - gchar *status; GmOptions *options; GmTriggers *triggers; @@ -42,7 +44,6 @@ struct _GmWorldPrivate { GmMcpSession *mcp; GList *history; GList *editors; - GmWorldInfo info; GmEditingInfo editing_info; }; @@ -60,7 +61,6 @@ enum { NAME_CHANGED, ACTIVE_CHANGED, ACTIVITY_CHANGED, - STATUS_CHANGED, NUM_SIGNALS }; @@ -84,11 +84,10 @@ gm_world_finalize(GObject *object) { g_free(tmp_dir); } - g_list_free_simple(world->priv->history); + gm_g_list_free_simple(world->priv->history); g_free(world->priv->path); g_free(world->priv->buffer); - g_free(world->priv->status); g_free(world->priv->editing_info.name); g_free(world->priv->editing_info.upload); g_list_free(world->priv->editing_info.lines); @@ -226,17 +225,6 @@ gm_world_class_init(GmWorldClass *klass) { 1, G_TYPE_INT); - world_signals[STATUS_CHANGED] = - g_signal_new("status_changed", - G_OBJECT_CLASS_TYPE(object_class), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET(GmWorldClass, status_changed), - NULL, NULL, - g_cclosure_marshal_VOID__STRING, - G_TYPE_NONE, - 1, - G_TYPE_STRING); - g_type_class_add_private(object_class, sizeof(GmWorldPrivate)); } @@ -274,10 +262,11 @@ gm_world_init(GmWorld *world) { world->priv->mcp = gm_mcp_session_new(G_OBJECT(world)); world->priv->buffer = NULL; world->priv->editing_info.is_editing = FALSE; - world->priv->status = NULL; g_signal_connect(world->priv->net, "state_changing", G_CALLBACK(on_gm_world_net_state_changing), world); + g_signal_connect(world->priv->net, "state_changed", + G_CALLBACK(on_gm_world_net_state_changed), world); g_signal_connect(world->priv->net, "net_error", G_CALLBACK(on_gm_world_net_net_error), world); g_signal_connect(world->priv->net, "bytes_recv", @@ -430,11 +419,13 @@ gm_world_unload(GmWorld *world) { world->priv->loaded = FALSE; gm_world_disconnect(world); - //TODO: Destroy editors - //editors_close(wld); + while (world->priv->editors) { + gm_world_remove_editor(world, + GM_EDITOR(world->priv->editors->data)); + } - g_signal_emit(world, world_signals[UNLOAD], 0); - } + g_signal_emit(world, world_signals[UNLOAD], 0); + } } GmTriggers * @@ -462,11 +453,6 @@ gm_world_history(GmWorld *world) { return &(world->priv->history); } -GmWorldInfo -gm_world_info(GmWorld *world) { - return world->priv->info; -} - gboolean gm_world_loaded(GmWorld *world) { return world->priv->loaded; @@ -606,6 +592,9 @@ gm_world_parse_legacy_editing_start(GmWorld *world, gchar *line) { einfo->is_editing = TRUE; einfo->name = g_strndup(name_start + 6, (upload_start - name_start) - 6); einfo->upload = g_strndup(upload_start + 9, (upload_start - line) + 9); + + GM_DEBUG("Editing info: %s %s", einfo->name, einfo->upload); + einfo->lines = NULL; } @@ -613,6 +602,7 @@ void gm_world_process_line(GmWorld *world, gchar *line) { gchar *non_text_start = NULL; GmEditingInfo *einfo = &(world->priv->editing_info); + GmEditor *editor; if (strncmp(line, "\x1B[0m", 4) == 0) { non_text_start = g_strdup(line + 4); @@ -622,15 +612,21 @@ gm_world_process_line(GmWorld *world, gchar *line) { if (einfo->is_editing) { if (strcmp(non_text_start, ".") == 0) { - //TODO: implementation, create new editor object, invoke signal - gm_world_add_editor(world, gm_editor_new(einfo->name, einfo->upload, - einfo->lines)); - + editor = gm_editor_new(einfo->name, einfo->upload, + einfo->lines); + + g_signal_connect(editor, "save", + G_CALLBACK(on_gm_world_editor_save), world); + + gm_world_add_editor(world, editor); + einfo->is_editing = FALSE; g_free(einfo->name); einfo->name = NULL; + g_free(einfo->upload); einfo->upload = NULL; + g_list_free(einfo->lines); einfo->lines = NULL; } else { @@ -726,11 +722,13 @@ void gm_world_add_editor(GmWorld *world, GmEditor *editor) { world->priv->editors = g_list_append(world->priv->editors, editor); - g_signal_emit(world, world_signals[EDITOR_ADDED], 0, G_OBJECT(editor)); + g_signal_connect_swapped(editor, "close", + G_CALLBACK(gm_world_remove_editor), world); + g_signal_emit(world, world_signals[EDITOR_ADDED], 0, editor); } void -gm_world_sendln(GmWorld *world, gchar *text) { +gm_world_sendln_real(GmWorld *world, gchar *text, gboolean dolog) { gchar *normal; // Convert text from utf-8 to the correct locale @@ -738,15 +736,23 @@ gm_world_sendln(GmWorld *world, gchar *text) { gm_options_get(world->priv->options, "charset"), "?"); if (!normal) { - gm_debug_msg(DEBUG_DEFAULT, "GmWorld.Send: conversion failed!"); + gm_debug_msg(DEBUG_DEFAULT, "GmWorld.SendLnReal: conversion failed!"); normal = g_strdup(text); } - gm_world_log(world, LOG_OUT, text); + if (dolog) { + gm_world_log(world, LOG_OUT, text); + } + gm_net_send_line(world->priv->net, normal); g_free(normal); } +void +gm_world_sendln(GmWorld *world, gchar *text) { + gm_world_sendln_real(world, text, TRUE); +} + void gm_world_set_active(GmWorld *world, gboolean active) { world->priv->active = active; @@ -814,25 +820,60 @@ gm_world_name_changed(GmWorld *world) { gm_options_get(world->priv->options, "name")); } -void -gm_world_set_status(GmWorld *world, gchar const *status) { - g_free(world->priv->status); - world->priv->status = g_strdup(status); - - g_signal_emit(world, world_signals[STATUS_CHANGED], 0, - world->priv->status); +GmMcpSession * +gm_world_get_mcp_session(GmWorld *world) { + return world->priv->mcp; } -gchar const * -gm_world_get_status(GmWorld *world) { - return world->priv->status; +void +gm_world_auto_login(GmWorld *world) { + gchar const *player_name = gm_options_get(world->priv->options, + "player_name"); + gchar const *passwd = gm_options_get(world->priv->options, "password"); + gchar *sendln; + + if (player_name && *player_name != '\0') { + sendln = g_strconcat("connect ", player_name, " ", passwd, NULL); + gm_world_sendln_real(world, sendln, FALSE); + g_free(sendln); + } } /* Callbacks */ +void +on_gm_world_editor_save(GmEditor *editor, GmWorld *world) { + GList *line; + + if (gm_net_state(world->priv->net) == GM_NET_STATE_CONNECTED) { + gm_world_sendln(world, gm_editor_upload_cmd(editor)); + + for (line = gm_editor_lines(editor); line; line = line->next) { + gm_world_sendln(world, (gchar *)(line->data)); + } + + gm_world_sendln(world, "."); + gm_editor_saved(editor); + } else { + gm_world_writeln(world, _("# Could not save editor text, world is " + "not connected at the moment")); + } +} + void on_gm_world_net_state_changing(GmNet *net, GmNetState state, GmWorld *world) { g_signal_emit(world, world_signals[STATE_CHANGING], 0, state); + + if (state == GM_NET_STATE_DISCONNECTED) { + gm_mcp_session_reset(world->priv->mcp); + } +} + +void +on_gm_world_net_state_changed(GmNet *net, GmNetState state, GmWorld *world) { + if (state == GM_NET_STATE_CONNECTED) { + gm_world_auto_login(world); + } } void