comparison libpurple/protocols/jabber/buddy.c @ 29054:f2949cd7db0a

jabber: Unify the JabberBuddyState functions (and briefly document them)
author Paul Aurich <paul@darkrain42.org>
date Mon, 15 Feb 2010 05:31:34 +0000
parents 4e7fd81a39c6
children 73e4c14921e4
comparison
equal deleted inserted replaced
29047:031fd1cd88df 29054:f2949cd7db0a
1906 } else { 1906 } else {
1907 return NULL; 1907 return NULL;
1908 } 1908 }
1909 } 1909 }
1910 1910
1911 static const struct {
1912 const char *status_id; /* link to core */
1913 const char *show; /* The show child's cdata in a presence stanza */
1914 const char *readable; /* readable representation */
1915 JabberBuddyState state;
1916 } jabber_statuses[] = {
1917 { "offline", NULL, N_("Offline"), JABBER_BUDDY_STATE_UNAVAILABLE },
1918 { "available", NULL, N_("Available"), JABBER_BUDDY_STATE_ONLINE},
1919 { "freeforchat", "chat", N_("Chatty"), JABBER_BUDDY_STATE_CHAT },
1920 { "away", "away", N_("Away"), JABBER_BUDDY_STATE_AWAY },
1921 { "extended_away", "xa", N_("Extended Away"), JABBER_BUDDY_STATE_XA },
1922 { "dnd", "dnd", N_("Do Not Disturb"), JABBER_BUDDY_STATE_DND },
1923 { "error", NULL, N_("Error"), JABBER_BUDDY_STATE_ERROR }
1924 };
1911 1925
1912 const char * 1926 const char *
1913 jabber_buddy_state_get_name(JabberBuddyState state) 1927 jabber_buddy_state_get_name(const JabberBuddyState state)
1914 { 1928 {
1915 switch(state) { 1929 int i;
1916 case JABBER_BUDDY_STATE_UNKNOWN: 1930 for (i = 0; i < G_N_ELEMENTS(jabber_statuses); ++i)
1917 return _("Unknown"); 1931 if (jabber_statuses[i].state == state)
1918 case JABBER_BUDDY_STATE_ERROR: 1932 return _(jabber_statuses[i].readable);
1919 return _("Error");
1920 case JABBER_BUDDY_STATE_UNAVAILABLE:
1921 return _("Offline");
1922 case JABBER_BUDDY_STATE_ONLINE:
1923 return _("Available");
1924 case JABBER_BUDDY_STATE_CHAT:
1925 return _("Chatty");
1926 case JABBER_BUDDY_STATE_AWAY:
1927 return _("Away");
1928 case JABBER_BUDDY_STATE_XA:
1929 return _("Extended Away");
1930 case JABBER_BUDDY_STATE_DND:
1931 return _("Do Not Disturb");
1932 }
1933 1933
1934 return _("Unknown"); 1934 return _("Unknown");
1935 } 1935 }
1936 1936
1937 JabberBuddyState jabber_buddy_status_id_get_state(const char *id) { 1937 JabberBuddyState
1938 if(!id) 1938 jabber_buddy_status_id_get_state(const char *id)
1939 {
1940 int i;
1941 if (!id)
1939 return JABBER_BUDDY_STATE_UNKNOWN; 1942 return JABBER_BUDDY_STATE_UNKNOWN;
1940 if(!strcmp(id, "available")) 1943
1941 return JABBER_BUDDY_STATE_ONLINE; 1944 for (i = 0; i < G_N_ELEMENTS(jabber_statuses); ++i)
1942 if(!strcmp(id, "freeforchat")) 1945 if (g_str_equal(id, jabber_statuses[i].status_id))
1943 return JABBER_BUDDY_STATE_CHAT; 1946 return jabber_statuses[i].state;
1944 if(!strcmp(id, "away"))
1945 return JABBER_BUDDY_STATE_AWAY;
1946 if(!strcmp(id, "extended_away"))
1947 return JABBER_BUDDY_STATE_XA;
1948 if(!strcmp(id, "dnd"))
1949 return JABBER_BUDDY_STATE_DND;
1950 if(!strcmp(id, "offline"))
1951 return JABBER_BUDDY_STATE_UNAVAILABLE;
1952 if(!strcmp(id, "error"))
1953 return JABBER_BUDDY_STATE_ERROR;
1954 1947
1955 return JABBER_BUDDY_STATE_UNKNOWN; 1948 return JABBER_BUDDY_STATE_UNKNOWN;
1956 } 1949 }
1957 1950
1958 const struct {
1959 const char *name;
1960 JabberBuddyState state;
1961 } show_state_pairs[] = {
1962 { "available", JABBER_BUDDY_STATE_ONLINE },
1963 { "chat", JABBER_BUDDY_STATE_CHAT },
1964 { "away", JABBER_BUDDY_STATE_AWAY },
1965 { "xa", JABBER_BUDDY_STATE_XA },
1966 { "dnd", JABBER_BUDDY_STATE_DND },
1967 { "offline", JABBER_BUDDY_STATE_UNAVAILABLE },
1968 { "error", JABBER_BUDDY_STATE_ERROR },
1969 { NULL, JABBER_BUDDY_STATE_UNKNOWN }
1970 };
1971
1972 JabberBuddyState jabber_buddy_show_get_state(const char *id) 1951 JabberBuddyState jabber_buddy_show_get_state(const char *id)
1973 { 1952 {
1974 int i; 1953 int i;
1975 1954
1976 g_return_val_if_fail(id != NULL, JABBER_BUDDY_STATE_UNKNOWN); 1955 g_return_val_if_fail(id != NULL, JABBER_BUDDY_STATE_UNKNOWN);
1977 1956
1978 for (i = 0; show_state_pairs[i].name; ++i) 1957 for (i = 0; i < G_N_ELEMENTS(jabber_statuses); ++i)
1979 if (g_str_equal(id, show_state_pairs[i].name)) 1958 if (g_str_equal(id, jabber_statuses[i].show))
1980 return show_state_pairs[i].state; 1959 return jabber_statuses[i].state;
1981 1960
1982 purple_debug_warning("jabber", "Invalid value of presence <show/> " 1961 purple_debug_warning("jabber", "Invalid value of presence <show/> "
1983 "attribute: %s\n", id); 1962 "attribute: %s\n", id);
1984 return JABBER_BUDDY_STATE_UNKNOWN; 1963 return JABBER_BUDDY_STATE_UNKNOWN;
1985 } 1964 }
1986 1965
1987 const char * 1966 const char *
1988 jabber_buddy_state_get_show(JabberBuddyState state) 1967 jabber_buddy_state_get_show(JabberBuddyState state)
1989 { 1968 {
1990 int i; 1969 int i;
1991 1970 for (i = 0; i < G_N_ELEMENTS(jabber_statuses); ++i)
1992 for (i = 0; show_state_pairs[i].name; ++i) 1971 if (state == jabber_statuses[i].state)
1993 if (state == show_state_pairs[i].state) 1972 return jabber_statuses[i].show;
1994 return show_state_pairs[i].name; 1973
1995
1996 /* purple_debug_warning("jabber", "Unknown buddy state: %d\n", state); */
1997 return NULL; 1974 return NULL;
1998 } 1975 }
1999 1976
2000 const char *jabber_buddy_state_get_status_id(JabberBuddyState state) { 1977 const char *
2001 switch(state) { 1978 jabber_buddy_state_get_status_id(JabberBuddyState state)
2002 case JABBER_BUDDY_STATE_CHAT: 1979 {
2003 return "freeforchat"; 1980 int i;
2004 case JABBER_BUDDY_STATE_AWAY: 1981 for (i = 0; i < G_N_ELEMENTS(jabber_statuses); ++i)
2005 return "away"; 1982 if (state == jabber_statuses[i].state)
2006 case JABBER_BUDDY_STATE_XA: 1983 return jabber_statuses[i].status_id;
2007 return "extended_away"; 1984
2008 case JABBER_BUDDY_STATE_DND:
2009 return "dnd";
2010 case JABBER_BUDDY_STATE_ONLINE:
2011 return "available";
2012 case JABBER_BUDDY_STATE_UNKNOWN:
2013 return "available";
2014 case JABBER_BUDDY_STATE_ERROR:
2015 return "error";
2016 case JABBER_BUDDY_STATE_UNAVAILABLE:
2017 return "offline";
2018 }
2019 return NULL; 1985 return NULL;
2020 } 1986 }
2021 1987
2022 static void user_search_result_add_buddy_cb(PurpleConnection *gc, GList *row, void *user_data) 1988 static void user_search_result_add_buddy_cb(PurpleConnection *gc, GList *row, void *user_data)
2023 { 1989 {