]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-dialogs.c
Merge remote branch 'vminko/fix-632024-v2'
[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_connection_can_add_personas (connection);
60   g_object_unref (individual_manager);
61
62   return result;
63 }
64
65 static void
66 new_individual_response_cb (GtkDialog *dialog,
67     gint response,
68     GtkWidget *contact_widget)
69 {
70   EmpathyIndividualManager *individual_manager;
71   EmpathyContact *contact;
72
73   individual_manager = empathy_individual_manager_dup_singleton ();
74   contact = empathy_contact_widget_get_contact (contact_widget);
75
76   if (contact && response == GTK_RESPONSE_OK)
77     empathy_individual_manager_add_from_contact (individual_manager, contact);
78
79   new_individual_dialog = NULL;
80   gtk_widget_destroy (GTK_WIDGET (dialog));
81   g_object_unref (individual_manager);
82 }
83
84 void
85 empathy_new_individual_dialog_show (GtkWindow *parent)
86 {
87   empathy_new_individual_dialog_show_with_individual (parent, NULL);
88 }
89
90 void
91 empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
92     FolksIndividual *individual)
93 {
94   GtkWidget *dialog;
95   GtkWidget *button;
96   EmpathyContact *contact = NULL;
97   GtkWidget *contact_widget;
98
99   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
100
101   if (new_individual_dialog)
102     {
103       gtk_window_present (GTK_WINDOW (new_individual_dialog));
104       return;
105     }
106
107   /* Create dialog */
108   dialog = gtk_dialog_new ();
109   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
110   gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
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 (GTK_DIALOG (dialog), button,
116       GTK_RESPONSE_CANCEL);
117   gtk_widget_show (button);
118
119   /* Add button */
120   button = gtk_button_new_with_label (GTK_STOCK_ADD);
121   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
122   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
123   gtk_widget_show (button);
124
125   /* Contact info widget */
126   if (individual != NULL)
127     contact = empathy_contact_dup_from_folks_individual (individual);
128
129   contact_widget = empathy_contact_widget_new (contact,
130       EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
131       EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
132       EMPATHY_CONTACT_WIDGET_EDIT_ID |
133       EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
134   gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
135   gtk_box_pack_start (
136       GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
137       contact_widget, TRUE, TRUE, 0);
138   empathy_contact_widget_set_account_filter (contact_widget,
139       can_add_contact_to_account, NULL);
140   gtk_widget_show (contact_widget);
141
142   new_individual_dialog = dialog;
143
144   g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
145       contact_widget);
146
147   if (parent != NULL)
148     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
149
150   gtk_widget_show (dialog);
151
152   tp_clear_object (&contact);
153 }