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