]> git.0d.be Git - empathy.git/blob - src/empathy-invite-participant-dialog.c
EmpathyContactChooser: remove EmpathyTpChat property
[empathy.git] / src / empathy-invite-participant-dialog.c
1 /*
2  * empathy-invite-participant-dialog.c
3  *
4  * EmpathyInviteParticipantDialog
5  *
6  * (c) 2009, Collabora Ltd.
7  *
8  * Authors:
9  *    Danielle Madeley <danielle.madeley@collabora.co.uk>
10  */
11
12 #include <glib/gi18n.h>
13 #include <folks/folks-telepathy.h>
14
15 #include "empathy-invite-participant-dialog.h"
16
17 #include <libempathy-gtk/empathy-contact-chooser.h>
18 #include <libempathy-gtk/empathy-individual-view.h>
19 #include <libempathy-gtk/empathy-ui-utils.h>
20
21 G_DEFINE_TYPE (EmpathyInviteParticipantDialog,
22     empathy_invite_participant_dialog, GTK_TYPE_DIALOG);
23
24 enum
25 {
26   PROP_TP_CHAT = 1
27 };
28
29 struct _EmpathyInviteParticipantDialogPrivate
30 {
31   EmpathyTpChat *tp_chat;
32
33   GtkWidget *chooser;
34   GtkWidget *invite_button;
35 };
36
37 static void
38 invite_participant_dialog_get_property (GObject *object,
39     guint param_id,
40     GValue *value,
41     GParamSpec *pspec)
42 {
43   EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
44     object;
45
46   switch (param_id)
47     {
48     case PROP_TP_CHAT:
49       g_value_set_object (value, self->priv->tp_chat);
50       break;
51     default:
52       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
53       break;
54     };
55 }
56
57 static void
58 invite_participant_dialog_set_property (GObject *object,
59     guint param_id,
60     const GValue *value,
61     GParamSpec *pspec)
62 {
63   EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
64     object;
65
66   switch (param_id)
67     {
68     case PROP_TP_CHAT:
69       g_assert (self->priv->tp_chat == NULL); /* construct-only */
70       self->priv->tp_chat = g_value_dup_object (value);
71       break;
72     default:
73       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
74       break;
75     };
76 }
77
78 static void
79 invite_participant_dialog_dispose (GObject *object)
80 {
81   EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
82     object;
83
84   tp_clear_object (&self->priv->tp_chat);
85
86   G_OBJECT_CLASS (empathy_invite_participant_dialog_parent_class)->dispose (
87       object);
88 }
89
90 static void
91 selection_changed_cb (GtkWidget *treeview,
92     FolksIndividual *selected,
93     EmpathyInviteParticipantDialog *self)
94 {
95   gtk_widget_set_sensitive (self->priv->invite_button, selected != NULL);
96 }
97
98 /* Return the TpContact of @individual which is on the same connection as the
99  * EmpathyTpChat */
100 static TpContact *
101 get_tp_contact_for_chat (EmpathyInviteParticipantDialog *self,
102     FolksIndividual *individual)
103 {
104   TpContact *contact = NULL;
105   TpConnection *chat_conn;
106   GeeSet *personas;
107   GeeIterator *iter;
108
109   chat_conn = tp_channel_borrow_connection (TP_CHANNEL (self->priv->tp_chat));
110
111   personas = folks_individual_get_personas (individual);
112   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
113   while (contact == FALSE && gee_iterator_next (iter))
114     {
115       TpfPersona *persona = gee_iterator_get (iter);
116       TpConnection *contact_conn;
117       TpContact *contact_cur = NULL;
118
119       if (TPF_IS_PERSONA (persona))
120         {
121           contact_cur = tpf_persona_get_contact (persona);
122           if (contact_cur != NULL)
123             {
124               contact_conn = tp_contact_get_connection (contact_cur);
125
126               if (!tp_strdiff (tp_proxy_get_object_path (contact_conn),
127                     tp_proxy_get_object_path (chat_conn)))
128                 contact = contact_cur;
129             }
130         }
131
132       g_clear_object (&persona);
133     }
134   g_clear_object (&iter);
135
136   return contact;
137 }
138
139 static gboolean
140 filter_individual (EmpathyContactChooser *chooser,
141     FolksIndividual *individual,
142     gboolean is_online,
143     gboolean searching,
144     gpointer user_data)
145 {
146   EmpathyInviteParticipantDialog *self = user_data;
147   GList *members, *l;
148   TpContact *contact;
149   gboolean display = TRUE;
150
151   /* Filter out offline contacts if we are not searching */
152   if (!searching && !is_online)
153     return FALSE;
154
155   /* Filter out individuals not having a persona on the same connection as the
156    * EmpathyTpChat. */
157   contact = get_tp_contact_for_chat (self, individual);
158
159   if (contact == NULL)
160     return FALSE;
161
162   /* Filter out contacts which are already in the chat */
163   members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (
164         self->priv->tp_chat));
165
166   for (l = members; l != NULL; l = g_list_next (l))
167     {
168       EmpathyContact *member = l->data;
169       TpHandle handle;
170
171       /* Try to get the non-channel specific handle. */
172       handle = tp_channel_group_get_handle_owner (
173           TP_CHANNEL (self->priv->tp_chat),
174           empathy_contact_get_handle (member));
175       if (handle == 0)
176         handle = empathy_contact_get_handle (member);
177
178       if (handle == tp_contact_get_handle (contact))
179         {
180           display = FALSE;
181           break;
182         }
183     }
184
185   g_list_free_full (members, g_object_unref);
186
187   return display;
188 }
189
190 static void
191 invite_participant_dialog_constructed (GObject *object)
192 {
193   EmpathyInviteParticipantDialog *self =
194     (EmpathyInviteParticipantDialog *) object;
195   GtkDialog *dialog = GTK_DIALOG (self);
196   GtkWidget *label;
197   char *str;
198   GtkWidget *content;
199
200   content = gtk_dialog_get_content_area (dialog);
201
202   label = gtk_label_new (NULL);
203   str = g_strdup_printf (
204       "<span size=\"x-large\" weight=\"bold\">%s</span>\n\n%s",
205       _("Invite Participant"),
206       _("Choose a contact to invite into the conversation:"));
207   gtk_label_set_markup (GTK_LABEL (label), str);
208   g_free (str);
209
210   gtk_box_pack_start (GTK_BOX (content), label, FALSE, TRUE, 6);
211   gtk_widget_show (label);
212
213   gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
214
215   /* contact chooser */
216   self->priv->chooser = empathy_contact_chooser_new ();
217
218   empathy_contact_chooser_set_filter_func (
219       EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
220
221   gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
222   gtk_widget_show (self->priv->chooser);
223
224   g_signal_connect (self->priv->chooser, "selection-changed",
225       G_CALLBACK (selection_changed_cb), self);
226
227   self->priv->invite_button = gtk_dialog_add_button (dialog, _("Invite"),
228       GTK_RESPONSE_ACCEPT);
229   gtk_widget_set_sensitive (self->priv->invite_button, FALSE);
230
231   gtk_window_set_title (GTK_WINDOW (self), _("Invite Participant"));
232   gtk_window_set_role (GTK_WINDOW (self), "invite_participant");
233
234   /* Set a default height so a few contacts are displayed */
235   gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
236 }
237
238 static void
239 empathy_invite_participant_dialog_class_init (
240     EmpathyInviteParticipantDialogClass *klass)
241 {
242   GObjectClass *object_class = G_OBJECT_CLASS (klass);
243
244   object_class->get_property = invite_participant_dialog_get_property;
245   object_class->set_property = invite_participant_dialog_set_property;
246   object_class->constructed = invite_participant_dialog_constructed;
247   object_class->dispose = invite_participant_dialog_dispose;
248
249   g_type_class_add_private (object_class,
250       sizeof (EmpathyInviteParticipantDialogPrivate));
251
252   g_object_class_install_property (object_class,
253       PROP_TP_CHAT,
254       g_param_spec_object ("tp-chat", "EmpathyTpChat", "EmpathyTpChat",
255           EMPATHY_TYPE_TP_CHAT,
256           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
257 }
258
259 static void
260 empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
261 {
262   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
263       self, EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
264       EmpathyInviteParticipantDialogPrivate);
265 }
266
267 GtkWidget *
268 empathy_invite_participant_dialog_new (GtkWindow *parent,
269     EmpathyTpChat *tp_chat)
270 {
271   GtkWidget *self = g_object_new (EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
272       "tp-chat", tp_chat,
273       NULL);
274
275   if (parent != NULL)
276     {
277       gtk_window_set_transient_for (GTK_WINDOW (self), parent);
278     }
279
280   return self;
281 }
282
283 TpContact *
284 empathy_invite_participant_dialog_get_selected (
285     EmpathyInviteParticipantDialog *self)
286 {
287   FolksIndividual *individual;
288   TpContact *contact;
289
290   individual = empathy_contact_chooser_dup_selected (
291       EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
292   if (individual == NULL)
293     return NULL;
294
295   contact = get_tp_contact_for_chat (self, individual);
296
297   g_object_unref (individual);
298   return contact;
299 }