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