14404
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22
|
|
23 #include "blist.h"
|
|
24 #include "debug.h"
|
|
25
|
|
26 #include "buddy_opt.h"
|
|
27 #include "group_free.h"
|
|
28 #include "group_internal.h"
|
|
29 #include "group_misc.h"
|
|
30 #include "utils.h"
|
|
31
|
|
32 static gchar *_qq_group_set_my_status_desc(qq_group *group)
|
|
33 {
|
|
34 const char *status_desc;
|
|
35 g_return_val_if_fail(group != NULL, g_strdup(""));
|
|
36
|
|
37 switch (group->my_status) {
|
|
38 case QQ_GROUP_MEMBER_STATUS_NOT_MEMBER:
|
|
39 status_desc = _("I am not member");
|
|
40 break;
|
|
41 case QQ_GROUP_MEMBER_STATUS_IS_MEMBER:
|
|
42 status_desc = _("I am a member");
|
|
43 break;
|
|
44 case QQ_GROUP_MEMBER_STATUS_APPLYING:
|
|
45 status_desc = _("I am applying to join");
|
|
46 break;
|
|
47 case QQ_GROUP_MEMBER_STATUS_IS_ADMIN:
|
|
48 status_desc = _("I am the admin");
|
|
49 break;
|
|
50 default:
|
|
51 status_desc = _("Unknown status");
|
|
52 }
|
|
53
|
|
54 return g_strdup(status_desc);
|
|
55 }
|
|
56
|
|
57 static void _qq_group_add_to_blist(GaimConnection *gc, qq_group *group)
|
|
58 {
|
|
59 GHashTable *components;
|
|
60 GaimGroup *g;
|
|
61 GaimChat *chat;
|
|
62 components = qq_group_to_hashtable(group);
|
|
63 chat = gaim_chat_new(gaim_connection_get_account(gc), group->group_name_utf8, components);
|
|
64 g = qq_get_gaim_group(GAIM_GROUP_QQ_QUN);
|
|
65 gaim_blist_add_chat(chat, g, NULL);
|
|
66 gaim_debug(GAIM_DEBUG_INFO, "QQ", "You have added group \"%s\" to blist locally\n", group->group_name_utf8);
|
|
67 }
|
|
68
|
|
69 /* Create a dummy qq_group, which includes only internal_id, external_id,
|
|
70 * and potentially group_name_utf8, in case we need to call group_conv_show_window
|
|
71 * right after creation. All other attributes are set to empty.
|
|
72 * We need to send a get_group_info to the QQ server to update it right away */
|
|
73 qq_group *qq_group_create_internal_record(GaimConnection *gc,
|
|
74 guint32 internal_id, guint32 external_id, gchar *group_name_utf8)
|
|
75 {
|
|
76 qq_group *group;
|
|
77 qq_data *qd;
|
|
78
|
|
79 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, NULL);
|
|
80 g_return_val_if_fail(internal_id > 0, NULL);
|
|
81 qd = (qq_data *) gc->proto_data;
|
|
82
|
|
83 group = g_new0(qq_group, 1);
|
|
84 group->my_status = QQ_GROUP_MEMBER_STATUS_NOT_MEMBER;
|
|
85 group->my_status_desc = _qq_group_set_my_status_desc(group);
|
|
86 group->internal_group_id = internal_id;
|
|
87 group->external_group_id = external_id;
|
|
88 group->group_type = 0x01; /* assume permanent Qun */
|
|
89 group->creator_uid = 10000; /* assume by QQ admin */
|
|
90 group->group_category = 0x01;
|
|
91 group->auth_type = 0x02; /* assume need auth */
|
|
92 group->group_name_utf8 = g_strdup(group_name_utf8 == NULL ? "" : group_name_utf8);
|
|
93 group->group_desc_utf8 = g_strdup("");
|
|
94 group->notice_utf8 = g_strdup("");
|
|
95 group->members = NULL;
|
|
96
|
|
97 qd->groups = g_list_append(qd->groups, group);
|
|
98 _qq_group_add_to_blist(gc, group);
|
|
99
|
|
100 return group;
|
|
101 }
|
|
102
|
|
103 void qq_group_delete_internal_record(qq_data *qd, guint32 internal_group_id)
|
|
104 {
|
|
105 qq_group *group;
|
|
106 GList *list;
|
|
107 g_return_if_fail(qd != NULL);
|
|
108
|
|
109 list = qd->groups;
|
|
110 while (list != NULL) {
|
|
111 group = (qq_group *) qd->groups->data;
|
|
112 if (internal_group_id == group->internal_group_id) {
|
|
113 qd->groups = g_list_remove(qd->groups, group);
|
|
114 qq_group_free(group);
|
|
115 break;
|
|
116 } else {
|
|
117 list = list->next;
|
|
118 }
|
|
119 }
|
|
120 }
|
|
121
|
|
122 /* convert a qq_group to hash-table, which could be component of GaimChat */
|
|
123 GHashTable *qq_group_to_hashtable(qq_group *group)
|
|
124 {
|
|
125 GHashTable *components;
|
|
126 components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
127 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_MEMBER_STATUS), g_strdup_printf("%d", group->my_status));
|
|
128 group->my_status_desc = _qq_group_set_my_status_desc(group);
|
|
129
|
|
130 g_hash_table_insert(components,
|
|
131 g_strdup(QQ_GROUP_KEY_INTERNAL_ID), g_strdup_printf("%d", group->internal_group_id));
|
|
132 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_EXTERNAL_ID),
|
|
133 g_strdup_printf("%d", group->external_group_id));
|
|
134 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_GROUP_TYPE), g_strdup_printf("%d", group->group_type));
|
|
135 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_CREATOR_UID), g_strdup_printf("%d", group->creator_uid));
|
|
136 g_hash_table_insert(components,
|
|
137 g_strdup(QQ_GROUP_KEY_GROUP_CATEGORY), g_strdup_printf("%d", group->group_category));
|
|
138 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_AUTH_TYPE), g_strdup_printf("%d", group->auth_type));
|
|
139 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_MEMBER_STATUS_DESC), g_strdup(group->my_status_desc));
|
|
140 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_GROUP_NAME_UTF8), g_strdup(group->group_name_utf8));
|
|
141 g_hash_table_insert(components, g_strdup(QQ_GROUP_KEY_GROUP_DESC_UTF8), g_strdup(group->group_desc_utf8));
|
|
142 return components;
|
|
143 }
|
|
144
|
|
145 /* create a qq_group from hashtable */
|
|
146 qq_group *qq_group_from_hashtable(GaimConnection *gc, GHashTable *data)
|
|
147 {
|
|
148 qq_data *qd;
|
|
149 qq_group *group;
|
|
150
|
|
151 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, NULL);
|
|
152 g_return_val_if_fail(data != NULL, NULL);
|
|
153 qd = (qq_data *) gc->proto_data;
|
|
154
|
|
155 group = g_new0(qq_group, 1);
|
|
156 group->my_status =
|
|
157 qq_string_to_dec_value
|
|
158 (NULL ==
|
|
159 g_hash_table_lookup(data,
|
|
160 QQ_GROUP_KEY_MEMBER_STATUS) ?
|
|
161 g_strdup_printf("%d",
|
|
162 QQ_GROUP_MEMBER_STATUS_NOT_MEMBER) :
|
|
163 g_hash_table_lookup(data, QQ_GROUP_KEY_MEMBER_STATUS));
|
|
164 group->internal_group_id = qq_string_to_dec_value(g_hash_table_lookup(data, QQ_GROUP_KEY_INTERNAL_ID));
|
|
165 group->external_group_id = qq_string_to_dec_value(g_hash_table_lookup(data, QQ_GROUP_KEY_EXTERNAL_ID));
|
|
166 group->group_type = qq_string_to_dec_value(g_hash_table_lookup(data, QQ_GROUP_KEY_GROUP_TYPE));
|
|
167 group->creator_uid = qq_string_to_dec_value(g_hash_table_lookup(data, QQ_GROUP_KEY_CREATOR_UID));
|
|
168 group->group_category = qq_string_to_dec_value(g_hash_table_lookup(data, QQ_GROUP_KEY_GROUP_CATEGORY));
|
|
169 group->auth_type = qq_string_to_dec_value(g_hash_table_lookup(data, QQ_GROUP_KEY_AUTH_TYPE));
|
|
170 group->group_name_utf8 = g_strdup(g_hash_table_lookup(data, QQ_GROUP_KEY_GROUP_NAME_UTF8));
|
|
171 group->group_desc_utf8 = g_strdup(g_hash_table_lookup(data, QQ_GROUP_KEY_GROUP_DESC_UTF8));
|
|
172 group->my_status_desc = _qq_group_set_my_status_desc(group);
|
|
173
|
|
174 qd->groups = g_list_append(qd->groups, group);
|
|
175
|
|
176 return group;
|
|
177 }
|
|
178
|
|
179 /* refresh group local subscription */
|
|
180 void qq_group_refresh(GaimConnection *gc, qq_group *group)
|
|
181 {
|
|
182 GaimChat *chat;
|
|
183 gchar *external_group_id;
|
|
184 g_return_if_fail(gc != NULL && group != NULL);
|
|
185
|
|
186 external_group_id = g_strdup_printf("%d", group->external_group_id);
|
|
187 chat = gaim_blist_find_chat(gaim_connection_get_account(gc), external_group_id);
|
|
188 g_free(external_group_id);
|
|
189 if (chat == NULL && group->my_status != QQ_GROUP_MEMBER_STATUS_NOT_MEMBER) {
|
|
190 _qq_group_add_to_blist(gc, group);
|
|
191 } else if (chat != NULL) { /* we have a local record, update its info */
|
|
192 /* if there is group_name_utf8, we update the group name */
|
|
193 if (group->group_name_utf8 != NULL && strlen(group->group_name_utf8) > 0)
|
|
194 gaim_blist_alias_chat(chat, group->group_name_utf8);
|
|
195 g_hash_table_replace(chat->components,
|
|
196 g_strdup(QQ_GROUP_KEY_MEMBER_STATUS), g_strdup_printf("%d", group->my_status));
|
|
197 group->my_status_desc = _qq_group_set_my_status_desc(group);
|
|
198 g_hash_table_replace(chat->components,
|
|
199 g_strdup(QQ_GROUP_KEY_MEMBER_STATUS_DESC), g_strdup(group->my_status_desc));
|
|
200 g_hash_table_replace(chat->components,
|
|
201 g_strdup(QQ_GROUP_KEY_INTERNAL_ID),
|
|
202 g_strdup_printf("%d", group->internal_group_id));
|
|
203 g_hash_table_replace(chat->components,
|
|
204 g_strdup(QQ_GROUP_KEY_EXTERNAL_ID),
|
|
205 g_strdup_printf("%d", group->external_group_id));
|
|
206 g_hash_table_replace(chat->components,
|
|
207 g_strdup(QQ_GROUP_KEY_GROUP_TYPE), g_strdup_printf("%d", group->group_type));
|
|
208 g_hash_table_replace(chat->components,
|
|
209 g_strdup(QQ_GROUP_KEY_CREATOR_UID), g_strdup_printf("%d", group->creator_uid));
|
|
210 g_hash_table_replace(chat->components,
|
|
211 g_strdup(QQ_GROUP_KEY_GROUP_CATEGORY),
|
|
212 g_strdup_printf("%d", group->group_category));
|
|
213 g_hash_table_replace(chat->components,
|
|
214 g_strdup(QQ_GROUP_KEY_AUTH_TYPE), g_strdup_printf("%d", group->auth_type));
|
|
215 g_hash_table_replace(chat->components,
|
|
216 g_strdup(QQ_GROUP_KEY_GROUP_NAME_UTF8), g_strdup(group->group_name_utf8));
|
|
217 g_hash_table_replace(chat->components,
|
|
218 g_strdup(QQ_GROUP_KEY_GROUP_DESC_UTF8), g_strdup(group->group_desc_utf8));
|
|
219 }
|
|
220 }
|
|
221
|
|
222 /* NOTE: If we knew how to convert between an external and internal group id, as the official
|
|
223 * client seems to, the following would be unnecessary. That would be ideal. */
|
|
224
|
|
225 /* Use list to specify if id's alternate id is pending discovery. */
|
|
226 void qq_set_pending_id(GSList **list, guint32 id, gboolean pending)
|
|
227 {
|
|
228 if (pending)
|
|
229 *list = g_slist_prepend(*list, GINT_TO_POINTER(id));
|
|
230 else
|
|
231 *list = g_slist_remove(*list, GINT_TO_POINTER(id));
|
|
232 }
|
|
233
|
|
234 /* Return the location of id in list, or NULL if not found */
|
|
235 GSList *qq_get_pending_id(GSList *list, guint32 id)
|
|
236 {
|
|
237 return g_slist_find(list, GINT_TO_POINTER(id));
|
|
238 }
|