]> git.0d.be Git - empathy.git/blob - src/empathy-invite-participant-dialog.c
Merge remote-tracking branch 'pochu/error-dialog'
[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 /* Return the TpContact of @individual which is on the same connection as the
101  * EmpathyTpChat */
102 static TpContact *
103 get_tp_contact_for_chat (EmpathyInviteParticipantDialog *self,
104     FolksIndividual *individual)
105 {
106   TpConnection *chat_conn;
107
108   chat_conn = tp_channel_borrow_connection (TP_CHANNEL (self->priv->tp_chat));
109
110   return empathy_get_tp_contact_for_individual (individual, chat_conn);
111 }
112
113 static gboolean
114 filter_individual (EmpathyContactChooser *chooser,
115     FolksIndividual *individual,
116     gboolean is_online,
117     gboolean searching,
118     gpointer user_data)
119 {
120   EmpathyInviteParticipantDialog *self = user_data;
121   GList *members, *l;
122   TpContact *contact;
123   gboolean display = TRUE;
124
125   /* Filter out offline contacts if we are not searching */
126   if (!searching && !is_online)
127     return FALSE;
128
129   /* Filter out individuals not having a persona on the same connection as the
130    * EmpathyTpChat. */
131   contact = get_tp_contact_for_chat (self, individual);
132
133   if (contact == NULL)
134     return FALSE;
135
136   /* Filter out contacts which are already in the chat */
137   members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (
138         self->priv->tp_chat));
139
140   for (l = members; l != NULL; l = g_list_next (l))
141     {
142       EmpathyContact *member = l->data;
143       TpHandle handle;
144
145       /* Try to get the non-channel specific handle. */
146       handle = tp_channel_group_get_handle_owner (
147           TP_CHANNEL (self->priv->tp_chat),
148           empathy_contact_get_handle (member));
149       if (handle == 0)
150         handle = empathy_contact_get_handle (member);
151
152       if (handle == tp_contact_get_handle (contact))
153         {
154           display = FALSE;
155           break;
156         }
157     }
158
159   g_list_free_full (members, g_object_unref);
160
161   return display;
162 }
163
164 static void
165 invite_participant_dialog_constructed (GObject *object)
166 {
167   EmpathyInviteParticipantDialog *self =
168     (EmpathyInviteParticipantDialog *) object;
169   GtkDialog *dialog = GTK_DIALOG (self);
170   GtkWidget *label;
171   char *str;
172   GtkWidget *content;
173
174   content = gtk_dialog_get_content_area (dialog);
175
176   label = gtk_label_new (NULL);
177   str = g_strdup_printf (
178       "<span size=\"x-large\" weight=\"bold\">%s</span>\n\n%s",
179       _("Invite Participant"),
180       _("Choose a contact to invite into the conversation:"));
181   gtk_label_set_markup (GTK_LABEL (label), str);
182   g_free (str);
183
184   gtk_box_pack_start (GTK_BOX (content), label, FALSE, TRUE, 6);
185   gtk_widget_show (label);
186
187   gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
188
189   /* contact chooser */
190   self->priv->chooser = empathy_contact_chooser_new ();
191
192   empathy_contact_chooser_set_filter_func (
193       EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
194
195   gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
196   gtk_widget_show (self->priv->chooser);
197
198   g_signal_connect (self->priv->chooser, "selection-changed",
199       G_CALLBACK (selection_changed_cb), self);
200
201   self->priv->invite_button = gtk_dialog_add_button (dialog, _("Invite"),
202       GTK_RESPONSE_ACCEPT);
203   gtk_widget_set_sensitive (self->priv->invite_button, FALSE);
204
205   gtk_window_set_title (GTK_WINDOW (self), _("Invite Participant"));
206   gtk_window_set_role (GTK_WINDOW (self), "invite_participant");
207
208   /* Set a default height so a few contacts are displayed */
209   gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
210 }
211
212 static void
213 empathy_invite_participant_dialog_class_init (
214     EmpathyInviteParticipantDialogClass *klass)
215 {
216   GObjectClass *object_class = G_OBJECT_CLASS (klass);
217
218   object_class->get_property = invite_participant_dialog_get_property;
219   object_class->set_property = invite_participant_dialog_set_property;
220   object_class->constructed = invite_participant_dialog_constructed;
221   object_class->dispose = invite_participant_dialog_dispose;
222
223   g_type_class_add_private (object_class,
224       sizeof (EmpathyInviteParticipantDialogPrivate));
225
226   g_object_class_install_property (object_class,
227       PROP_TP_CHAT,
228       g_param_spec_object ("tp-chat", "EmpathyTpChat", "EmpathyTpChat",
229           EMPATHY_TYPE_TP_CHAT,
230           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
231 }
232
233 static void
234 empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
235 {
236   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
237       self, EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
238       EmpathyInviteParticipantDialogPrivate);
239 }
240
241 GtkWidget *
242 empathy_invite_participant_dialog_new (GtkWindow *parent,
243     EmpathyTpChat *tp_chat)
244 {
245   GtkWidget *self = g_object_new (EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
246       "tp-chat", tp_chat,
247       NULL);
248
249   if (parent != NULL)
250     {
251       gtk_window_set_transient_for (GTK_WINDOW (self), parent);
252     }
253
254   return self;
255 }
256
257 TpContact *
258 empathy_invite_participant_dialog_get_selected (
259     EmpathyInviteParticipantDialog *self)
260 {
261   FolksIndividual *individual;
262   TpContact *contact;
263
264   individual = empathy_contact_chooser_dup_selected (
265       EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
266   if (individual == NULL)
267     return NULL;
268
269   contact = get_tp_contact_for_chat (self, individual);
270
271   g_object_unref (individual);
272   return contact;
273 }