]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-dialogs.c
We already know that this contact is a FolksIndividual if it isn't NULL, so simplify...
[empathy.git] / libempathy-gtk / empathy-individual-dialogs.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2010 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <telepathy-glib/util.h>
31 #include <folks/folks.h>
32
33 #include <libempathy/empathy-individual-manager.h>
34 #include <libempathy/empathy-utils.h>
35
36 #include "empathy-individual-dialogs.h"
37 #include "empathy-contact-widget.h"
38 #include "empathy-ui-utils.h"
39
40 static GtkWidget *new_individual_dialog = NULL;
41
42 /*
43  *  New contact dialog
44  */
45
46 static gboolean
47 can_add_contact_to_account (TpAccount *account,
48     gpointer user_data)
49 {
50   EmpathyIndividualManager *individual_manager;
51   TpConnection *connection;
52   gboolean result;
53
54   connection = tp_account_get_connection (account);
55   if (connection == NULL)
56     return FALSE;
57
58   individual_manager = empathy_individual_manager_dup_singleton ();
59   result = empathy_individual_manager_get_flags_for_connection (
60     individual_manager, connection) & EMPATHY_INDIVIDUAL_MANAGER_CAN_ADD;
61   g_object_unref (individual_manager);
62
63   return result;
64 }
65
66 static void
67 new_individual_response_cb (GtkDialog *dialog,
68     gint response,
69     GtkWidget *contact_widget)
70 {
71   EmpathyIndividualManager *individual_manager;
72   EmpathyContact *contact;
73
74   individual_manager = empathy_individual_manager_dup_singleton ();
75   contact = empathy_contact_widget_get_contact (contact_widget);
76
77   if (contact && response == GTK_RESPONSE_OK)
78     empathy_individual_manager_add_from_contact (individual_manager, contact);
79
80   new_individual_dialog = NULL;
81   gtk_widget_destroy (GTK_WIDGET (dialog));
82   g_object_unref (individual_manager);
83 }
84
85 void
86 empathy_new_individual_dialog_show (GtkWindow *parent)
87 {
88   empathy_new_individual_dialog_show_with_individual (parent, NULL);
89 }
90
91 void
92 empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
93     FolksIndividual *individual)
94 {
95   GtkWidget *dialog;
96   GtkWidget *button;
97   EmpathyContact *contact = NULL;
98   GtkWidget *contact_widget;
99
100   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
101
102   if (new_individual_dialog)
103     {
104       gtk_window_present (GTK_WINDOW (new_individual_dialog));
105       return;
106     }
107
108   /* Create dialog */
109   dialog = gtk_dialog_new ();
110   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
111   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
112   gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
113
114   /* Cancel button */
115   button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
116   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
117   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
118       GTK_RESPONSE_CANCEL);
119   gtk_widget_show (button);
120
121   /* Add button */
122   button = gtk_button_new_with_label (GTK_STOCK_ADD);
123   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
124   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
125   gtk_widget_show (button);
126
127   /* Contact info widget */
128   if (individual != NULL)
129     contact = empathy_contact_dup_from_folks_individual (individual);
130
131   contact_widget = empathy_contact_widget_new (contact,
132       EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
133       EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
134       EMPATHY_CONTACT_WIDGET_EDIT_ID |
135       EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
136   gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
137   gtk_box_pack_start (
138       GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
139       contact_widget, TRUE, TRUE, 0);
140   empathy_contact_widget_set_account_filter (contact_widget,
141       can_add_contact_to_account, NULL);
142   gtk_widget_show (contact_widget);
143
144   new_individual_dialog = dialog;
145
146   g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
147       contact_widget);
148
149   if (parent != NULL)
150     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
151
152   gtk_widget_show (dialog);
153
154   tp_clear_object (&contact);
155 }