purple: add "SIP ID" search field
[siplcs.git] / src / purple / purple-search.c
blob72bbac6692168866cae24557400eae710966cbcf
1 /**
2 * @file purple-search.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2014 SIPE Project <http://sipe.sourceforge.net/>
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.
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.
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
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <glib.h>
29 #include "notify.h"
30 #include "request.h"
32 #include "version.h"
33 #if PURPLE_VERSION_CHECK(3,0,0)
34 #include "conversations.h"
35 #endif
37 #include "sipe-common.h"
38 #include "sipe-backend.h"
39 #include "sipe-core.h"
40 #include "sipe-nls.h"
42 #include "purple-private.h"
44 void sipe_backend_search_failed(struct sipe_core_public *sipe_public,
45 SIPE_UNUSED_PARAMETER struct sipe_backend_search_token *token,
46 const gchar *msg)
48 sipe_backend_notify_error(sipe_public, msg, NULL);
51 struct sipe_backend_search_results *sipe_backend_search_results_start(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
52 SIPE_UNUSED_PARAMETER struct sipe_backend_search_token *token)
54 PurpleNotifySearchResults *results = purple_notify_searchresults_new();
56 if (results) {
57 PurpleNotifySearchColumn *column;
58 column = purple_notify_searchresults_column_new(_("User name"));
59 purple_notify_searchresults_column_add(results, column);
61 column = purple_notify_searchresults_column_new(_("Name"));
62 purple_notify_searchresults_column_add(results, column);
64 column = purple_notify_searchresults_column_new(_("Company"));
65 purple_notify_searchresults_column_add(results, column);
67 column = purple_notify_searchresults_column_new(_("Country"));
68 purple_notify_searchresults_column_add(results, column);
70 column = purple_notify_searchresults_column_new(_("Email"));
71 purple_notify_searchresults_column_add(results, column);
74 return((struct sipe_backend_search_results *)results);
77 void sipe_backend_search_results_add(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
78 struct sipe_backend_search_results *results,
79 const gchar *uri,
80 const gchar *name,
81 const gchar *company,
82 const gchar *country,
83 const gchar *email)
85 GList *row = NULL;
86 row = g_list_append(row, g_strdup(uri));
87 row = g_list_append(row, g_strdup(name));
88 row = g_list_append(row, g_strdup(company));
89 row = g_list_append(row, g_strdup(country));
90 row = g_list_append(row, g_strdup(email));
91 purple_notify_searchresults_row_add((PurpleNotifySearchResults *) results,
92 row);
95 static void searchresults_im_buddy(PurpleConnection *gc,
96 GList *row,
97 SIPE_UNUSED_PARAMETER void *user_data)
99 PurpleAccount *acct = purple_connection_get_account(gc);
100 gchar *id = sip_uri_from_name(g_list_nth_data(row, 0));
102 #if PURPLE_VERSION_CHECK(3,0,0)
103 PurpleIMConversation *conv = purple_conversations_find_im_with_account(id,
104 acct);
106 if (conv == NULL)
107 conv = purple_im_conversation_new(acct, id);
108 #else
109 PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
111 acct);
112 if (conv == NULL)
113 conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, acct, id);
114 #endif
116 g_free(id);
117 purple_conversation_present((PurpleConversation *) conv);
120 static void searchresults_add_buddy(PurpleConnection *gc,
121 GList *row,
122 SIPE_UNUSED_PARAMETER void *user_data)
124 purple_blist_request_add_buddy(purple_connection_get_account(gc),
125 g_list_nth_data(row, 0),
126 _("Other Contacts"),
127 g_list_nth_data(row, 1));
131 void sipe_backend_search_results_finalize(struct sipe_core_public *sipe_public,
132 struct sipe_backend_search_results *results,
133 const gchar *description,
134 SIPE_UNUSED_PARAMETER gboolean more)
136 struct sipe_backend_private *purple_private = sipe_public->backend_private;
137 PurpleNotifySearchResults *r = (PurpleNotifySearchResults *) results;
139 purple_notify_searchresults_button_add(r,
140 PURPLE_NOTIFY_BUTTON_IM,
141 searchresults_im_buddy);
142 purple_notify_searchresults_button_add(r,
143 PURPLE_NOTIFY_BUTTON_ADD,
144 searchresults_add_buddy);
145 purple_notify_searchresults(purple_private->gc,
146 NULL,
147 NULL,
148 description,
150 NULL,
151 NULL);
155 static void sipe_purple_find_contact_cb(PurpleConnection *gc,
156 PurpleRequestFields *fields)
158 GList *entries = purple_request_field_group_get_fields(purple_request_fields_get_groups(fields)->data);
159 const gchar *given_name = NULL;
160 const gchar *surname = NULL;
161 const gchar *email = NULL;
162 const gchar *company = NULL;
163 const gchar *country = NULL;
165 while (entries) {
166 PurpleRequestField *field = entries->data;
167 const char *id = purple_request_field_get_id(field);
168 const char *value = purple_request_field_string_get_value(field);
170 SIPE_DEBUG_INFO("sipe_purple_find_contact_cb: %s = '%s'", id, value ? value : "");
172 if (value && strlen(value)) {
173 if (strcmp(id, "given") == 0) {
174 given_name = value;
175 } else if (strcmp(id, "surname") == 0) {
176 surname = value;
177 } else if (strcmp(id, "email") == 0) {
178 email = value;
179 } else if (strcmp(id, "sipid") == 0) {
180 /* TBD */
181 } else if (strcmp(id, "company") == 0) {
182 company = value;
183 } else if (strcmp(id, "country") == 0) {
184 country = value;
188 entries = g_list_next(entries);
191 sipe_core_buddy_search(PURPLE_GC_TO_SIPE_CORE_PUBLIC,
192 NULL,
193 given_name,
194 surname,
195 email,
196 company,
197 country);
200 void sipe_purple_show_find_contact(PurplePluginAction *action)
202 PurpleConnection *gc = (PurpleConnection *) action->context;
203 PurpleRequestFields *fields;
204 PurpleRequestFieldGroup *group;
205 PurpleRequestField *field;
207 fields = purple_request_fields_new();
208 group = purple_request_field_group_new(NULL);
209 purple_request_fields_add_group(fields, group);
211 field = purple_request_field_string_new("given", _("First name"), NULL, FALSE);
212 purple_request_field_group_add_field(group, field);
213 field = purple_request_field_string_new("surname", _("Last name"), NULL, FALSE);
214 purple_request_field_group_add_field(group, field);
215 field = purple_request_field_string_new("email", _("Email"), NULL, FALSE);
216 purple_request_field_group_add_field(group, field);
217 field = purple_request_field_string_new("sipid", _("SIP ID"), NULL, FALSE);
218 purple_request_field_group_add_field(group, field);
219 field = purple_request_field_string_new("company", _("Company"), NULL, FALSE);
220 purple_request_field_group_add_field(group, field);
221 field = purple_request_field_string_new("country", _("Country"), NULL, FALSE);
222 purple_request_field_group_add_field(group, field);
224 purple_request_fields(gc,
225 _("Search"),
226 _("Search for a contact"),
227 _("Enter the information for the person you wish to find. Empty fields will be ignored."),
228 fields,
229 _("_Search"), G_CALLBACK(sipe_purple_find_contact_cb),
230 _("_Cancel"), NULL,
231 #if PURPLE_VERSION_CHECK(3,0,0)
232 purple_request_cpar_from_connection(gc),
233 #else
234 purple_connection_get_account(gc), NULL, NULL,
235 #endif
236 gc);
240 Local Variables:
241 mode: c
242 c-file-style: "bsd"
243 indent-tabs-mode: t
244 tab-width: 8
245 End: