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