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