]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-linking-dialog.c
Change the sensitivity of the Link button if changes have been made
[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 <libempathy/empathy-individual-manager.h>
30 #include <libempathy/empathy-utils.h>
31
32 #include "empathy-linking-dialog.h"
33 #include "empathy-individual-linker.h"
34
35 /**
36  * SECTION:empathy-individual-widget
37  * @title:EmpathyLinkingDialog
38  * @short_description: A dialog used to link individuals together
39  * @include: libempathy-empathy-linking-dialog.h
40  *
41  * #EmpathyLinkingDialog is a dialog which allows selection of individuals to
42  * link together, and preview of the newly linked individual. When submitted, it
43  * pushes the new links to backing storage.
44  */
45
46 /**
47  * EmpathyLinkingDialog:
48  * @parent: parent object
49  *
50  * Widget which displays appropriate widgets with details about an individual,
51  * also allowing changing these details, if desired.
52  *
53  * Currently, it's just a thin wrapper around #EmpathyContactWidget, and
54  * displays the details of the first eligible persona found in the individual.
55  */
56
57 static GtkWidget *linking_dialog = NULL;
58
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLinkingDialog)
60
61 typedef struct {
62   EmpathyIndividualLinker *linker; /* child widget */
63   GtkWidget *link_button; /* child widget */
64 } EmpathyLinkingDialogPriv;
65
66 G_DEFINE_TYPE (EmpathyLinkingDialog, empathy_linking_dialog,
67     GTK_TYPE_DIALOG);
68
69 static void
70 linker_notify_has_changed_cb (EmpathyIndividualLinker *linker,
71     GParamSpec *pspec,
72     EmpathyLinkingDialog *self)
73 {
74   EmpathyLinkingDialogPriv *priv = GET_PRIV (self);
75
76   /* Only make the "Link" button sensitive if the linked Individual has been
77    * changed. */
78   gtk_widget_set_sensitive (priv->link_button,
79       empathy_individual_linker_get_has_changed (linker));
80 }
81
82 static void
83 empathy_linking_dialog_class_init (EmpathyLinkingDialogClass *klass)
84 {
85   GObjectClass *object_class = G_OBJECT_CLASS (klass);
86   g_type_class_add_private (object_class, sizeof (EmpathyLinkingDialogPriv));
87 }
88
89 static void
90 empathy_linking_dialog_init (EmpathyLinkingDialog *self)
91 {
92   EmpathyLinkingDialogPriv *priv;
93   GtkDialog *dialog;
94   GtkWidget *button;
95   GtkBox *content_area;
96
97   priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
98       EMPATHY_TYPE_LINKING_DIALOG, EmpathyLinkingDialogPriv);
99   self->priv = priv;
100
101   dialog = GTK_DIALOG (self);
102
103   /* Set up dialog */
104   gtk_dialog_set_has_separator (dialog, FALSE);
105   gtk_window_set_resizable (GTK_WINDOW (self), TRUE);
106   /* Translators: this is the title of the linking dialogue (reached by
107    * right-clicking on a contact and selecting "Linkā€¦"). "Link" in this title
108    * is a verb. */
109   gtk_window_set_title (GTK_WINDOW (self), _("Link Contacts"));
110   gtk_widget_set_size_request (GTK_WIDGET (self), 600, 500);
111
112   /* Cancel button */
113   button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
114   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
115   gtk_dialog_add_action_widget (dialog, button, GTK_RESPONSE_CANCEL);
116   gtk_widget_show (button);
117
118   /* Add button */
119   /* Translators: this is an action button in the linking dialogue. "Link" is
120    * used here as a verb meaning "to connect two contacts to form a
121    * meta-contact". */
122   priv->link_button = gtk_button_new_with_mnemonic (_("_Link"));
123   gtk_dialog_add_action_widget (dialog, priv->link_button, GTK_RESPONSE_OK);
124   gtk_widget_show (priv->link_button);
125
126   /* Linker widget */
127   priv->linker =
128       EMPATHY_INDIVIDUAL_LINKER (empathy_individual_linker_new (NULL));
129   g_signal_connect (priv->linker, "notify::has-changed",
130       (GCallback) linker_notify_has_changed_cb, self);
131
132   gtk_container_set_border_width (GTK_CONTAINER (priv->linker), 8);
133   content_area = GTK_BOX (gtk_dialog_get_content_area (dialog));
134   gtk_box_pack_start (content_area, GTK_WIDGET (priv->linker), TRUE, TRUE, 0);
135   gtk_widget_show (GTK_WIDGET (priv->linker));
136 }
137
138 static void
139 linking_response_cb (EmpathyLinkingDialog *self,
140     gint response,
141     gpointer user_data)
142 {
143   EmpathyLinkingDialogPriv *priv = GET_PRIV (self);
144
145   if (response == GTK_RESPONSE_OK) {
146     EmpathyIndividualManager *manager;
147     GList *personas;
148
149     manager = empathy_individual_manager_dup_singleton ();
150
151     personas = empathy_individual_linker_get_linked_personas (priv->linker);
152     empathy_individual_manager_link_personas (manager, personas);
153
154     g_object_unref (manager);
155   }
156
157   linking_dialog = NULL;
158   gtk_widget_destroy (GTK_WIDGET (self));
159 }
160
161 /**
162  * empathy_linking_dialog_show:
163  * @individual: the #FolksIndividual to start linking against
164  * @parent: a parent window for the dialogue, or %NULL
165  *
166  * Create and show the linking dialogue, with @individual selected as the
167  * individual to link to. If the dialogue is already being shown, raise it and
168  * reset it so the start individual is @individual.
169  *
170  * Return value: the linking dialog
171  */
172 GtkWidget *
173 empathy_linking_dialog_show (FolksIndividual *individual,
174     GtkWindow *parent)
175 {
176   EmpathyLinkingDialogPriv *priv;
177
178   /* Create the dialogue if it doesn't exist */
179   if (linking_dialog == NULL)
180     {
181       linking_dialog = GTK_WIDGET (g_object_new (EMPATHY_TYPE_LINKING_DIALOG,
182           NULL));
183
184       g_signal_connect (linking_dialog, "response",
185           (GCallback) linking_response_cb, NULL);
186     }
187
188   priv = GET_PRIV (linking_dialog);
189
190   if (parent != NULL)
191     gtk_window_set_transient_for (GTK_WINDOW (linking_dialog), parent);
192
193   empathy_individual_linker_set_start_individual (priv->linker, individual);
194
195   gtk_window_present (GTK_WINDOW (linking_dialog));
196
197   return linking_dialog;
198 }