]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-linking-dialog.c
factor out start_gnome_contacts()
[empathy.git] / libempathy-gtk / empathy-linking-dialog.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Philip Withnall <philip.withnall@collabora.co.uk>
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <gtk/gtk.h>
27 #include <glib/gi18n-lib.h>
28
29 #include <folks/folks.h>
30 #include <folks/folks-telepathy.h>
31
32 #include <libempathy/empathy-individual-manager.h>
33 #include <libempathy/empathy-utils.h>
34
35 #include "empathy-linking-dialog.h"
36 #include "empathy-individual-linker.h"
37 #include "empathy-ui-utils.h"
38
39 /**
40  * SECTION:empathy-individual-widget
41  * @title:EmpathyLinkingDialog
42  * @short_description: A dialog used to link individuals together
43  * @include: libempathy-empathy-linking-dialog.h
44  *
45  * #EmpathyLinkingDialog is a dialog which allows selection of individuals to
46  * link together, and preview of the newly linked individual. When submitted, it
47  * pushes the new links to backing storage.
48  */
49
50 /**
51  * EmpathyLinkingDialog:
52  * @parent: parent object
53  *
54  * Widget which displays appropriate widgets with details about an individual,
55  * also allowing changing these details, if desired.
56  *
57  * Currently, it's just a thin wrapper around #EmpathyContactWidget, and
58  * displays the details of the first eligible persona found in the individual.
59  */
60
61 static GtkWidget *linking_dialog = NULL;
62
63 /* Fairly arbitrary response ID for the "Unlink" button */
64 #define RESPONSE_UNLINK 5
65
66 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLinkingDialog)
67
68 typedef struct {
69   EmpathyIndividualLinker *linker; /* child widget */
70   GtkWidget *link_button; /* child widget */
71 } EmpathyLinkingDialogPriv;
72
73 G_DEFINE_TYPE (EmpathyLinkingDialog, empathy_linking_dialog,
74     GTK_TYPE_DIALOG);
75
76 static void
77 linker_notify_has_changed_cb (EmpathyIndividualLinker *linker,
78     GParamSpec *pspec,
79     EmpathyLinkingDialog *self)
80 {
81   EmpathyLinkingDialogPriv *priv = GET_PRIV (self);
82
83   /* Only make the "Link" button sensitive if the linked Individual has been
84    * changed. */
85   gtk_widget_set_sensitive (priv->link_button,
86       empathy_individual_linker_get_has_changed (linker));
87 }
88
89 static void
90 empathy_linking_dialog_class_init (EmpathyLinkingDialogClass *klass)
91 {
92   GObjectClass *object_class = G_OBJECT_CLASS (klass);
93   g_type_class_add_private (object_class, sizeof (EmpathyLinkingDialogPriv));
94 }
95
96 static void
97 empathy_linking_dialog_init (EmpathyLinkingDialog *self)
98 {
99   EmpathyLinkingDialogPriv *priv;
100   GtkDialog *dialog;
101   GtkWidget *button;
102   GtkBox *content_area;
103
104   priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
105       EMPATHY_TYPE_LINKING_DIALOG, EmpathyLinkingDialogPriv);
106   self->priv = priv;
107
108   dialog = GTK_DIALOG (self);
109
110   /* Set up dialog */
111   gtk_window_set_resizable (GTK_WINDOW (self), TRUE);
112   /* Translators: this is the title of the linking dialogue (reached by
113    * right-clicking on a contact and selecting "Link…"). "Link" in this title
114    * is a verb. */
115   gtk_window_set_title (GTK_WINDOW (self), _("Link Contacts"));
116   gtk_widget_set_size_request (GTK_WIDGET (self), 600, 500);
117
118   /* Unlink button */
119   button = gtk_button_new_with_mnemonic (
120       C_("Unlink individual (button)", "_Unlink…"));
121   gtk_widget_set_tooltip_text (button, _("Completely split the displayed "
122       "linked contacts into the separate contacts."));
123   gtk_dialog_add_action_widget (dialog, button, RESPONSE_UNLINK);
124   gtk_widget_show (button);
125
126   /* Cancel button */
127   button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
128   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
129   gtk_dialog_add_action_widget (dialog, button, GTK_RESPONSE_CANCEL);
130   gtk_widget_show (button);
131
132   /* Add button */
133   /* Translators: this is an action button in the linking dialogue. "Link" is
134    * used here as a verb meaning "to connect two contacts to form a
135    * meta-contact". */
136   priv->link_button = gtk_button_new_with_mnemonic (_("_Link"));
137   gtk_dialog_add_action_widget (dialog, priv->link_button, GTK_RESPONSE_OK);
138   gtk_widget_show (priv->link_button);
139
140   /* Linker widget */
141   priv->linker =
142       EMPATHY_INDIVIDUAL_LINKER (empathy_individual_linker_new (NULL));
143   g_signal_connect (priv->linker, "notify::has-changed",
144       (GCallback) linker_notify_has_changed_cb, self);
145
146   gtk_container_set_border_width (GTK_CONTAINER (priv->linker), 8);
147   content_area = GTK_BOX (gtk_dialog_get_content_area (dialog));
148   gtk_box_pack_start (content_area, GTK_WIDGET (priv->linker), TRUE, TRUE, 0);
149   gtk_widget_show (GTK_WIDGET (priv->linker));
150 }
151
152 static void
153 linking_response_cb (EmpathyLinkingDialog *self,
154     gint response,
155     gpointer user_data)
156 {
157   EmpathyLinkingDialogPriv *priv = GET_PRIV (self);
158
159   if (response == GTK_RESPONSE_OK)
160     {
161       EmpathyIndividualManager *manager;
162       GeeSet *personas;
163
164       manager = empathy_individual_manager_dup_singleton ();
165
166       personas = empathy_individual_linker_get_linked_personas (priv->linker);
167       empathy_individual_manager_link_personas (manager, personas);
168
169       g_object_unref (manager);
170     }
171   else if (response == RESPONSE_UNLINK)
172     {
173       EmpathyIndividualManager *manager;
174       FolksIndividual *individual;
175       GtkWidget *dialog;
176
177       individual =
178           empathy_individual_linker_get_start_individual (priv->linker);
179
180       /* Show a confirmation dialogue first */
181       dialog = gtk_message_dialog_new (GTK_WINDOW (self), GTK_DIALOG_MODAL,
182           GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
183           _("Unlink linked contacts '%s'?"),
184           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
185       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
186           _("Are you sure you want to unlink these linked contacts? This will "
187             "completely split the linked contacts into separate contacts."));
188       gtk_dialog_add_buttons (GTK_DIALOG (dialog),
189           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
190           C_("Unlink individual (button)", "_Unlink"), GTK_RESPONSE_OK,
191           NULL);
192
193       if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK)
194         {
195           gtk_widget_destroy (dialog);
196           return;
197         }
198
199       gtk_widget_destroy (dialog);
200
201       manager = empathy_individual_manager_dup_singleton ();
202       empathy_individual_manager_unlink_individual (manager, individual);
203       g_object_unref (manager);
204     }
205
206   linking_dialog = NULL;
207   gtk_widget_destroy (GTK_WIDGET (self));
208 }
209
210 /**
211  * empathy_linking_dialog_show:
212  * @individual: the #FolksIndividual to start linking against
213  * @parent: a parent window for the dialogue, or %NULL
214  *
215  * Create and show the linking dialogue, with @individual selected as the
216  * individual to link to. If the dialogue is already being shown, raise it and
217  * reset it so the start individual is @individual.
218  *
219  * Return value: the linking dialog
220  */
221 GtkWidget *
222 empathy_linking_dialog_show (FolksIndividual *individual,
223     GtkWindow *parent)
224 {
225   EmpathyLinkingDialogPriv *priv;
226   GeeSet *personas;
227   GeeIterator *iter;
228   guint num_personas = 0;
229
230   /* Create the dialogue if it doesn't exist */
231   if (linking_dialog == NULL)
232     {
233       linking_dialog = GTK_WIDGET (g_object_new (EMPATHY_TYPE_LINKING_DIALOG,
234           NULL));
235
236       g_signal_connect (linking_dialog, "response",
237           (GCallback) linking_response_cb, NULL);
238     }
239
240   priv = GET_PRIV (linking_dialog);
241
242   if (parent != NULL)
243     gtk_window_set_transient_for (GTK_WINDOW (linking_dialog), parent);
244
245   empathy_individual_linker_set_start_individual (priv->linker, individual);
246
247   /* Count how many Telepathy personas we have, to see whether we can
248    * unlink */
249   personas = folks_individual_get_personas (individual);
250   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
251   while (gee_iterator_next (iter))
252     {
253       FolksPersona *persona = gee_iterator_get (iter);
254       if (empathy_folks_persona_is_interesting (persona))
255         num_personas++;
256
257       g_clear_object (&persona);
258     }
259   g_clear_object (&iter);
260
261   /* Only make the "Unlink" button sensitive if we have enough personas */
262   gtk_dialog_set_response_sensitive (GTK_DIALOG (linking_dialog),
263       RESPONSE_UNLINK, (num_personas > 1) ? TRUE : FALSE);
264
265   gtk_window_present (GTK_WINDOW (linking_dialog));
266
267   return linking_dialog;
268 }
269
270 EmpathyIndividualLinker *
271 empathy_linking_dialog_get_individual_linker (EmpathyLinkingDialog *self)
272 {
273   g_return_val_if_fail (EMPATHY_IS_LINKING_DIALOG (self), NULL);
274
275   return GET_PRIV (self)->linker;
276 }