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