hindi update
[empathy-mirror.git] / src / empathy-invite-participant-dialog.c
blob9280dda3274125a9c85aaf4a51418e37e0a7393a
1 /*
2 * empathy-invite-participant-dialog.c
4 * EmpathyInviteParticipantDialog
6 * (c) 2009, Collabora Ltd.
8 * Authors:
9 * Danielle Madeley <danielle.madeley@collabora.co.uk>
12 #include <glib/gi18n.h>
13 #include <folks/folks-telepathy.h>
15 #include "empathy-invite-participant-dialog.h"
17 #include <libempathy/empathy-utils.h>
19 #include <libempathy-gtk/empathy-contact-chooser.h>
20 #include <libempathy-gtk/empathy-individual-view.h>
21 #include <libempathy-gtk/empathy-ui-utils.h>
23 G_DEFINE_TYPE (EmpathyInviteParticipantDialog,
24 empathy_invite_participant_dialog, GTK_TYPE_DIALOG);
26 enum
28 PROP_TP_CHAT = 1
31 struct _EmpathyInviteParticipantDialogPrivate
33 EmpathyTpChat *tp_chat;
35 GtkWidget *chooser;
36 GtkWidget *invite_button;
39 static void
40 invite_participant_dialog_get_property (GObject *object,
41 guint param_id,
42 GValue *value,
43 GParamSpec *pspec)
45 EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
46 object;
48 switch (param_id)
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;
59 static void
60 invite_participant_dialog_set_property (GObject *object,
61 guint param_id,
62 const GValue *value,
63 GParamSpec *pspec)
65 EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
66 object;
68 switch (param_id)
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;
80 static void
81 invite_participant_dialog_dispose (GObject *object)
83 EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
84 object;
86 tp_clear_object (&self->priv->tp_chat);
88 G_OBJECT_CLASS (empathy_invite_participant_dialog_parent_class)->dispose (
89 object);
92 static void
93 selection_changed_cb (GtkWidget *treeview,
94 FolksIndividual *selected,
95 EmpathyInviteParticipantDialog *self)
97 gtk_widget_set_sensitive (self->priv->invite_button, selected != NULL);
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)
106 TpConnection *chat_conn;
108 chat_conn = tp_channel_borrow_connection (TP_CHANNEL (self->priv->tp_chat));
110 return empathy_get_tp_contact_for_individual (individual, chat_conn);
113 static gboolean
114 filter_individual (EmpathyContactChooser *chooser,
115 FolksIndividual *individual,
116 gboolean is_online,
117 gboolean searching,
118 gpointer user_data)
120 EmpathyInviteParticipantDialog *self = user_data;
121 GList *members, *l;
122 TpContact *contact;
123 gboolean display = TRUE;
125 /* Filter out offline contacts if we are not searching */
126 if (!searching && !is_online)
127 return FALSE;
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);
133 if (contact == NULL)
134 return FALSE;
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));
140 for (l = members; l != NULL; l = g_list_next (l))
142 EmpathyContact *member = l->data;
143 TpHandle handle;
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);
152 if (handle == tp_contact_get_handle (contact))
154 display = FALSE;
155 break;
159 g_list_free_full (members, g_object_unref);
161 return display;
164 static void
165 invite_participant_dialog_constructed (GObject *object)
167 EmpathyInviteParticipantDialog *self =
168 (EmpathyInviteParticipantDialog *) object;
169 GtkDialog *dialog = GTK_DIALOG (self);
170 GtkWidget *label;
171 char *str;
172 GtkWidget *content;
174 content = gtk_dialog_get_content_area (dialog);
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);
184 gtk_box_pack_start (GTK_BOX (content), label, FALSE, TRUE, 6);
185 gtk_widget_show (label);
187 gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
189 /* contact chooser */
190 self->priv->chooser = empathy_contact_chooser_new ();
192 empathy_contact_chooser_set_filter_func (
193 EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
195 gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
196 gtk_widget_show (self->priv->chooser);
198 g_signal_connect (self->priv->chooser, "selection-changed",
199 G_CALLBACK (selection_changed_cb), self);
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);
205 gtk_window_set_title (GTK_WINDOW (self), _("Invite Participant"));
206 gtk_window_set_role (GTK_WINDOW (self), "invite_participant");
208 /* Set a default height so a few contacts are displayed */
209 gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
212 static void
213 empathy_invite_participant_dialog_class_init (
214 EmpathyInviteParticipantDialogClass *klass)
216 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
223 g_type_class_add_private (object_class,
224 sizeof (EmpathyInviteParticipantDialogPrivate));
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));
233 static void
234 empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
236 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
237 self, EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
238 EmpathyInviteParticipantDialogPrivate);
241 GtkWidget *
242 empathy_invite_participant_dialog_new (GtkWindow *parent,
243 EmpathyTpChat *tp_chat)
245 GtkWidget *self = g_object_new (EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
246 "tp-chat", tp_chat,
247 NULL);
249 if (parent != NULL)
251 gtk_window_set_transient_for (GTK_WINDOW (self), parent);
254 return self;
257 TpContact *
258 empathy_invite_participant_dialog_get_selected (
259 EmpathyInviteParticipantDialog *self)
261 FolksIndividual *individual;
262 TpContact *contact;
264 individual = empathy_contact_chooser_dup_selected (
265 EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
266 if (individual == NULL)
267 return NULL;
269 contact = get_tp_contact_for_chat (self, individual);
271 g_object_unref (individual);
272 return contact;