changeset 10419:c9b1f3fac753

[gaim-migrate @ 11670] Added some GaimStatusPrimitive<-->text mapping functions Renamed the GaimStatusSaved data types to GaimSavedStatus Modifying a status will now bring up an editor thing with the correct values set committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sun, 26 Dec 2004 02:03:14 +0000
parents bed2c96bc1fb
children 854c972ea863
files src/gtksavedstatuses.c src/savedstatuses.c src/savedstatuses.h src/status.c src/status.h
diffstat 5 files changed, 180 insertions(+), 125 deletions(-) [+]
line wrap: on
line diff
--- a/src/gtksavedstatuses.c	Sun Dec 26 00:46:26 2004 +0000
+++ b/src/gtksavedstatuses.c	Sun Dec 26 02:03:14 2004 +0000
@@ -98,10 +98,10 @@
 	/* TODO: Save the status */
 }
 
-/* TODO: Can this menu be created more automatically? */
 static GtkWidget *
-create_status_type_menu()
+create_status_type_menu(GaimStatusPrimitive type)
 {
+	int i;
 	GtkWidget *dropdown;
 	GtkWidget *menu;
 	GtkWidget *item;
@@ -109,31 +109,14 @@
 	dropdown = gtk_option_menu_new();
 	menu = gtk_menu_new();
 
-	/* GAIM_STATUS_AVAILABLE */
-	item = gtk_menu_item_new_with_label(_("Available"));
-	gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-	gtk_widget_show(item);
-
-	/* GAIM_STATUS_UNAVAILABLE */
-	item = gtk_menu_item_new_with_label(_("Unavailable"));
-	gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-	gtk_widget_show(item);
+	for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++)
+	{
+		item = gtk_menu_item_new_with_label(gaim_primitive_get_name_from_type(i));
+		gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+		gtk_widget_show(item);
+	}
 
-	/* GAIM_STATUS_HIDDEN */
-	item = gtk_menu_item_new_with_label(_("Hidden"));
-	gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-	gtk_widget_show(item);
-
-	/* GAIM_STATUS_AWAY */
-	item = gtk_menu_item_new_with_label(_("Away"));
-	gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-	gtk_widget_show(item);
-
-	/* GAIM_STATUS_EXTENDED_AWAY */
-	item = gtk_menu_item_new_with_label(_("Extended Away"));
-	gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-	gtk_widget_show(item);
-
+	gtk_menu_set_active(GTK_MENU(menu), type);
 	gtk_option_menu_set_menu(GTK_OPTION_MENU(dropdown), menu);
 	gtk_widget_show(menu);
 
@@ -234,7 +217,7 @@
 }
 
 void
-gaim_gtk_status_editor_show(void)
+gaim_gtk_status_editor_show(GaimSavedStatus *status)
 {
 	StatusEditor *dialog;
 	GtkSizeGroup *sg;
@@ -284,7 +267,10 @@
 	gtk_size_group_add_widget(sg, label);
 
 	entry = gtk_entry_new();
-	gtk_entry_set_text(GTK_ENTRY(entry), _("Out of the office"));
+	if (status != NULL)
+		gtk_entry_set_text(GTK_ENTRY(entry), gaim_savedstatus_get_title(status));
+	else
+		gtk_entry_set_text(GTK_ENTRY(entry), _("Out of the office"));
 	gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
 	gtk_widget_show(entry);
 
@@ -299,7 +285,10 @@
 	gtk_widget_show(label);
 	gtk_size_group_add_widget(sg, label);
 
-	dropdown = create_status_type_menu();
+	if (status != NULL)
+		dropdown = create_status_type_menu(gaim_savedstatus_get_type(status));
+	else
+		dropdown = create_status_type_menu(GAIM_STATUS_AWAY);
 	gtk_box_pack_start(GTK_BOX(hbox), dropdown, TRUE, TRUE, 0);
 	gtk_widget_show(dropdown);
 
@@ -318,6 +307,10 @@
 	gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 0);
 	gtk_widget_show(frame);
 
+	if (status != NULL)
+		gtk_imhtml_append_text(GTK_IMHTML(text),
+							   gaim_savedstatus_get_message(status), 0);
+
 	/* Custom status message disclosure */
 	disclosure = gaim_disclosure_new(_("Use a different status for some accounts"),
 									 _("Use a different status for some accounts"));
@@ -426,7 +419,7 @@
 static void
 status_window_add_cb(GtkButton *button, gpointer user_data)
 {
-	gaim_gtk_status_editor_show();
+	gaim_gtk_status_editor_show(NULL);
 }
 
 static void
@@ -434,12 +427,12 @@
 							 GtkTreeIter *iter, gpointer user_data)
 {
 	const char *title;
-	GaimStatusSaved *status;
+	GaimSavedStatus *status;
 
 	gtk_tree_model_get(model, iter, STATUS_WINDOW_COLUMN_TITLE, &title, -1);
 
-	status = gaim_savedstatuses_find(title);
-	/* TODO: Need to show a status editor pre-populated with this status */
+	status = gaim_savedstatus_find(title);
+	gaim_gtk_status_editor_show(status);
 }
 
 static void
@@ -461,7 +454,7 @@
 	if (status_window_find_savedstatus(&iter, title))
 		gtk_list_store_remove(status_window->model, &iter);
 
-	gaim_savedstatuses_delete(title);
+	gaim_savedstatus_delete(title);
 
 	g_free(title);
 }
@@ -520,7 +513,7 @@
 {
 	GtkTreeIter iter;
 	const GList *saved_statuses;
-	GaimStatusSaved *saved_status;
+	GaimSavedStatus *saved_status;
 	char *message;
 
 	gtk_list_store_clear(dialog->model);
@@ -528,14 +521,14 @@
 	for (saved_statuses = gaim_savedstatuses_get_all(); saved_statuses != NULL;
 			saved_statuses = g_list_next(saved_statuses))
 	{
-		saved_status = (GaimStatusSaved *)saved_statuses->data;
-		message = gaim_markup_strip_html(gaim_savedstatuses_get_message(saved_status));
+		saved_status = (GaimSavedStatus *)saved_statuses->data;
+		message = gaim_markup_strip_html(gaim_savedstatus_get_message(saved_status));
 		if (strlen(message) > 70)
 			strcpy(&message[68], "...");
 
 		gtk_list_store_append(dialog->model, &iter);
 		gtk_list_store_set(dialog->model, &iter,
-						   STATUS_WINDOW_COLUMN_TITLE, gaim_savedstatuses_get_title(saved_status),
+						   STATUS_WINDOW_COLUMN_TITLE, gaim_savedstatus_get_title(saved_status),
 						   STATUS_WINDOW_COLUMN_MESSAGE, message,
 						   -1);
 		free(message);
--- a/src/savedstatuses.c	Sun Dec 26 00:46:26 2004 +0000
+++ b/src/savedstatuses.c	Sun Dec 26 02:03:14 2004 +0000
@@ -42,22 +42,22 @@
  * are fully backward compatible.  The new status API just
  * adds the optional sub-statuses to the XML file.
  */
-struct _GaimStatusSaved
+struct _GaimSavedStatus
 {
 	char *title;
 	GaimStatusPrimitive type;
 	char *message;
 
-	GList *substatuses;      /**< A list of GaimStatusSavedSub's. */
+	GList *substatuses;      /**< A list of GaimSavedStatusSub's. */
 };
 
 /*
  * TODO: If an account is deleted, need to also delete any associated
- *       GaimStatusSavedSub's.
+ *       GaimSavedStatusSub's.
  * TODO: If a GaimStatusType is deleted, need to also delete any
- *       associated GaimStatusSavedSub's?
+ *       associated GaimSavedStatusSub's?
  */
-struct _GaimStatusSavedSub
+struct _GaimSavedStatusSub
 {
 	GaimAccount *account;
 	const GaimStatusType *type;
@@ -72,39 +72,8 @@
 * Helper functions
 **************************************************************************/
 
-/**
- * Elements of this array correspond to the GaimStatusPrimitive
- * enumeration.
- */
-static const char *primitive_names[] =
-{
-	"unset",
-	"offline",
-	"available",
-	"unavailable",
-	"hidden",
-	"away",
-	"extended_away"
-};
-
-static GaimStatusPrimitive
-gaim_primitive_get_type(const char *name)
-{
-	int i;
-
-	g_return_val_if_fail(name != NULL, GAIM_STATUS_UNSET);
-
-	for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++)
-	{
-		if (!strcmp(name, primitive_names[i]))
-			return i;
-	}
-
-	return GAIM_STATUS_UNSET;
-}
-
 static void
-free_statussavedsub(GaimStatusSavedSub *substatus)
+free_statussavedsub(GaimSavedStatusSub *substatus)
 {
 	g_return_if_fail(substatus != NULL);
 
@@ -113,7 +82,7 @@
 }
 
 static void
-free_statussaved(GaimStatusSaved *status)
+free_statussaved(GaimSavedStatus *status)
 {
 	g_return_if_fail(status != NULL);
 
@@ -122,7 +91,7 @@
 
 	while (status->substatuses != NULL)
 	{
-		GaimStatusSavedSub *substatus = status->substatuses->data;
+		GaimSavedStatusSub *substatus = status->substatuses->data;
 		status->substatuses = g_list_remove(status->substatuses, substatus);
 		free_statussavedsub(substatus);
 	}
@@ -136,7 +105,7 @@
 **************************************************************************/
 
 static xmlnode *
-substatus_to_xmlnode(GaimStatusSavedSub *substatus)
+substatus_to_xmlnode(GaimSavedStatusSub *substatus)
 {
 	xmlnode *node, *child;
 
@@ -164,7 +133,7 @@
 }
 
 static xmlnode *
-status_to_xmlnode(GaimStatusSaved *status)
+status_to_xmlnode(GaimSavedStatus *status)
 {
 	xmlnode *node, *child;
 	GList *cur;
@@ -173,7 +142,7 @@
 	xmlnode_set_attrib(node, "name", status->title);
 
 	child = xmlnode_new("state");
-	xmlnode_insert_data(child, primitive_names[status->type], -1);
+	xmlnode_insert_data(child, strdup(gaim_primitive_get_id_from_type(status->type)), -1);
 	xmlnode_insert_child(node, child);
 
 	child = xmlnode_new("message");
@@ -246,14 +215,14 @@
 /**************************************************************************
 * Saved status reading from disk
 **************************************************************************/
-static GaimStatusSavedSub *
+static GaimSavedStatusSub *
 parse_substatus(xmlnode *substatus)
 {
-	GaimStatusSavedSub *ret;
+	GaimSavedStatusSub *ret;
 	xmlnode *node;
 	char *data = NULL;
 
-	ret = g_new0(GaimStatusSavedSub, 1);
+	ret = g_new0(GaimSavedStatusSub, 1);
 
 	/* Read the account */
 	node = xmlnode_get_child(substatus, "account");
@@ -319,16 +288,16 @@
  *
  * I know.  Moving, huh?
  */
-static GaimStatusSaved *
+static GaimSavedStatus *
 parse_status(xmlnode *status)
 {
-	GaimStatusSaved *ret;
+	GaimSavedStatus *ret;
 	xmlnode *node;
 	const char *attrib;
 	char *data = NULL;
 	int i;
 
-	ret = g_new0(GaimStatusSaved, 1);
+	ret = g_new0(GaimSavedStatus, 1);
 
 	/* Read the title */
 	attrib = xmlnode_get_attrib(status, "name");
@@ -337,7 +306,7 @@
 	/* Ensure the title is unique */
 	ret->title = g_strdup(attrib);
 	i = 2;
-	while (gaim_savedstatuses_find(ret->title) != NULL)
+	while (gaim_savedstatus_find(ret->title) != NULL)
 	{
 		g_free(ret->title);
 		ret->title = g_strdup_printf("%s %d", attrib, i);
@@ -349,7 +318,7 @@
 	if (node != NULL)
 		data = xmlnode_get_data(node);
 	if (data != NULL) {
-		ret->type = gaim_primitive_get_type(data);
+		ret->type = gaim_primitive_get_type_from_id(data);
 		g_free(data);
 		data = NULL;
 	}
@@ -365,7 +334,7 @@
 	for (node = xmlnode_get_child(status, "status"); node != NULL;
 			node = xmlnode_get_next_twin(node))
 	{
-		GaimStatusSavedSub *new;
+		GaimSavedStatusSub *new;
 		new = parse_substatus(node);
 		if (new != NULL)
 			ret->substatuses = g_list_append(ret->substatuses, new);
@@ -426,7 +395,7 @@
 	for (status = xmlnode_get_child(statuses, "status"); status != NULL;
 			status = xmlnode_get_next_twin(status))
 	{
-		GaimStatusSaved *new;
+		GaimSavedStatus *new;
 		new = parse_status(status);
 		saved_statuses = g_list_append(saved_statuses, new);
 	}
@@ -471,12 +440,12 @@
 /**************************************************************************
 * Saved status API
 **************************************************************************/
-GaimStatusSaved *
-gaim_savedstatuses_new(const char *title, GaimStatusPrimitive type)
+GaimSavedStatus *
+gaim_savedstatus_new(const char *title, GaimStatusPrimitive type)
 {
-	GaimStatusSaved *status;
+	GaimSavedStatus *status;
 
-	status = g_new0(GaimStatusSaved, 1);
+	status = g_new0(GaimSavedStatus, 1);
 	status->title = g_strdup(title);
 	status->type = type;
 
@@ -488,11 +457,11 @@
 }
 
 gboolean
-gaim_savedstatuses_delete(const char *title)
+gaim_savedstatus_delete(const char *title)
 {
-	GaimStatusSaved *status;
+	GaimSavedStatus *status;
 
-	status = gaim_savedstatuses_find(title);
+	status = gaim_savedstatus_find(title);
 
 	if (status == NULL)
 		return FALSE;
@@ -511,15 +480,15 @@
 	return saved_statuses;
 }
 
-GaimStatusSaved *
-gaim_savedstatuses_find(const char *title)
+GaimSavedStatus *
+gaim_savedstatus_find(const char *title)
 {
 	GList *l;
-	GaimStatusSaved *status;
+	GaimSavedStatus *status;
 
 	for (l = saved_statuses; l != NULL; l = g_list_next(l))
 	{
-		status = (GaimStatusSaved *)l->data;
+		status = (GaimSavedStatus *)l->data;
 		if (!strcmp(status->title, title))
 			return status;
 	}
@@ -528,19 +497,19 @@
 }
 
 const char *
-gaim_savedstatuses_get_title(const GaimStatusSaved *saved_status)
+gaim_savedstatus_get_title(const GaimSavedStatus *saved_status)
 {
 	return saved_status->title;
 }
 
 GaimStatusPrimitive
-gaim_savedstatuses_get_type(const GaimStatusSaved *saved_status)
+gaim_savedstatus_get_type(const GaimSavedStatus *saved_status)
 {
 	return saved_status->type;
 }
 
 const char *
-gaim_savedstatuses_get_message(const GaimStatusSaved *saved_status)
+gaim_savedstatus_get_message(const GaimSavedStatus *saved_status)
 {
 	return saved_status->message;
 }
@@ -562,7 +531,7 @@
 	}
 
 	while (saved_statuses != NULL) {
-		GaimStatusSaved *status = saved_statuses->data;
+		GaimSavedStatus *status = saved_statuses->data;
 		saved_statuses = g_list_remove(saved_statuses, status);
 		free_statussaved(status);
 	}
--- a/src/savedstatuses.h	Sun Dec 26 00:46:26 2004 +0000
+++ b/src/savedstatuses.h	Sun Dec 26 02:03:14 2004 +0000
@@ -32,8 +32,8 @@
  * to the settings of that state.
  */
 
-typedef struct _GaimStatusSaved     GaimStatusSaved;
-typedef struct _GaimStatusSavedSub  GaimStatusSavedSub;
+typedef struct _GaimSavedStatus     GaimSavedStatus;
+typedef struct _GaimSavedStatusSub  GaimSavedStatusSub;
 
 /**************************************************************************/
 /** @name Saved status subsystem                                          */
@@ -49,7 +49,8 @@
  *
  * @return The newly created saved status.
  */
-GaimStatusSaved *gaim_savedstatuses_new(const char *title, GaimStatusPrimitive type);
+GaimSavedStatus *gaim_savedstatus_new(const char *title,
+									  GaimStatusPrimitive type);
 
 /**
  * Delete a saved status.  This removes the saved status from the list
@@ -61,7 +62,7 @@
  *         status could not be deleted because no saved status exists
  *         with the given title.
  */
-gboolean gaim_savedstatuses_delete(const char *title);
+gboolean gaim_savedstatus_delete(const char *title);
 
 /**
  * Returns all saved statuses.
@@ -77,19 +78,7 @@
  *
  * @return The saved status if found, or NULL.
  */
-GaimStatusSaved *gaim_savedstatuses_find(const char *title);
-
-/**
- * Create a new saved status and add it to the list
- * of saved statuses.
- *
- * @param title The title for the new saved status.
- * @param type  The type of saved status.
- *
- * @return The newly created saved status.
- */
-GaimStatusSaved *gaim_savedstatuses_new(const char *title,
-									  GaimStatusPrimitive type);
+GaimSavedStatus *gaim_savedstatus_find(const char *title);
 
 /**
  * Return the name of a given saved status.
@@ -98,7 +87,7 @@
  *
  * @return The title.
  */
-const char *gaim_savedstatuses_get_title(const GaimStatusSaved *saved_status);
+const char *gaim_savedstatus_get_title(const GaimSavedStatus *saved_status);
 
 /**
  * Return the name of a given saved status.
@@ -107,7 +96,7 @@
  *
  * @return The name.
  */
-GaimStatusPrimitive gaim_savedstatuses_get_type(const GaimStatusSaved *saved_status);
+GaimStatusPrimitive gaim_savedstatus_get_type(const GaimSavedStatus *saved_status);
 
 /**
  * Return the name of a given saved status.
@@ -116,7 +105,7 @@
  *
  * @return The name.
  */
-const char *gaim_savedstatuses_get_message(const GaimStatusSaved *saved_status);
+const char *gaim_savedstatus_get_message(const GaimSavedStatus *saved_status);
 
 /**
  * Get the handle for the status subsystem.
--- a/src/status.c	Sun Dec 26 00:46:26 2004 +0000
+++ b/src/status.c	Sun Dec 26 02:03:14 2004 +0000
@@ -141,6 +141,72 @@
 #define SCORE_IDLE_TIME 6
 
 /**************************************************************************
+ * GaimStatusPrimitive API
+ **************************************************************************/
+static struct GaimStatusPrimitiveMap
+{
+	GaimStatusPrimitive type;
+	const char *id;
+	const char *name;
+
+} const status_primitive_map[] =
+{
+	{ GAIM_STATUS_UNSET,           "unset",           N_("Unset")           },
+	{ GAIM_STATUS_OFFLINE,         "offline",         N_("Offline")         },
+	{ GAIM_STATUS_ONLINE,          "online",          N_("Online")          },
+	{ GAIM_STATUS_AVAILABLE,       "available",       N_("Available")       },
+	{ GAIM_STATUS_UNAVAILABLE,     "unavailable",     N_("Unavailable")     },
+	{ GAIM_STATUS_HIDDEN,          "hidden",          N_("Hidden")          },
+	{ GAIM_STATUS_AWAY,            "away",            N_("Away")            },
+	{ GAIM_STATUS_EXTENDED_AWAY,   "extended_away",   N_("Extended Away")   }
+};
+
+const char *
+gaim_primitive_get_id_from_type(GaimStatusPrimitive type)
+{
+    int i;
+
+    for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++)
+    {
+		if (type == status_primitive_map[i].type)
+			return status_primitive_map[i].id;
+    }
+
+    return status_primitive_map[0].id;
+}
+
+const char *
+gaim_primitive_get_name_from_type(GaimStatusPrimitive type)
+{
+    int i;
+
+    for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++)
+    {
+		if (type == status_primitive_map[i].type)
+			return status_primitive_map[i].name;
+    }
+
+    return status_primitive_map[0].name;
+}
+
+GaimStatusPrimitive
+gaim_primitive_get_type_from_id(const char *id)
+{
+    int i;
+
+    g_return_val_if_fail(id != NULL, GAIM_STATUS_UNSET);
+
+    for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++)
+    {
+        if (!strcmp(id, status_primitive_map[i].id))
+            return status_primitive_map[i].type;
+    }
+
+    return status_primitive_map[0].type;
+}
+
+
+/**************************************************************************
  * GaimStatusType API
  **************************************************************************/
 GaimStatusType *
--- a/src/status.h	Sun Dec 26 00:46:26 2004 +0000
+++ b/src/status.h	Sun Dec 26 02:03:14 2004 +0000
@@ -67,6 +67,44 @@
 #include "value.h"
 
 /**************************************************************************/
+/** @name GaimStatusPrimtive API                                          */
+/**************************************************************************/
+/*@{*/
+
+/**
+ * Lookup the id of a primitive status type based on the type.  This
+ * ID is a unique plain-text name of the status, without spaces.
+ *
+ * @param type A primitive status type.
+ *
+ * @return The unique ID for this type.
+ */
+const char *gaim_primitive_get_id_from_type(GaimStatusPrimitive type);
+
+/**
+ * Lookup the name of a primitive status type based on the type.  This
+ * name is the plain-English name of the status type.  It is usually one
+ * or two works.
+ *
+ * @param type A primitive status type.
+ *
+ * @return The name of this type, suitable for users to see.
+ */
+const char *gaim_primitive_get_name_from_type(GaimStatusPrimitive type);
+
+/**
+ * Lookup the value of a primitive status type based on the id.  The
+ * ID is a unique plain-text name of the status, without spaces.
+ *
+ * @param type The unique ID of a primitive status type.
+ *
+ * @return The GaimStatusPrimitive value.
+ */
+GaimStatusPrimitive gaim_primitive_get_type_from_id(const char *id);
+
+/*@}*/
+
+/**************************************************************************/
 /** @name GaimStatusType API                                              */
 /**************************************************************************/
 /*@{*/