1 /*
2  * prompt-user-gtk.c
3  *
4  * This library is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-data-server-config.h"
19 
20 #include <glib/gi18n.h>
21 #include <gtk/gtk.h>
22 
23 #include <libebackend/libebackend.h>
24 
25 #include "prompt-user.h"
26 
27 #define E_USER_PROMPTER_ID_KEY "e-user-prompter-id"
28 
29 void
prompt_user_init(gint * argc,gchar *** argv)30 prompt_user_init (gint *argc,
31                   gchar ***argv)
32 {
33 	gtk_init (argc, argv);
34 }
35 
36 static void
message_response_cb(GtkWidget * dialog,gint button,EUserPrompterServer * server)37 message_response_cb (GtkWidget *dialog,
38                      gint button,
39                      EUserPrompterServer *server)
40 {
41 	gint prompt_id;
42 
43 	prompt_id = GPOINTER_TO_INT (g_object_get_data (
44 		G_OBJECT (dialog), E_USER_PROMPTER_ID_KEY));
45 
46 	gtk_widget_destroy (dialog);
47 
48 	g_return_if_fail (E_IS_USER_PROMPTER_SERVER (server));
49 
50 	e_user_prompter_server_response (server, prompt_id, button, NULL);
51 }
52 
53 void
prompt_user_show(EUserPrompterServer * server,gint id,const gchar * type,const gchar * title,const gchar * primary_text,const gchar * secondary_text,gboolean use_markup,const GSList * button_captions)54 prompt_user_show (EUserPrompterServer *server,
55                   gint id,
56                   const gchar *type,
57                   const gchar *title,
58                   const gchar *primary_text,
59                   const gchar *secondary_text,
60                   gboolean use_markup,
61                   const GSList *button_captions)
62 {
63 	GtkMessageType ntype = GTK_MESSAGE_OTHER;
64 	GtkWidget *message;
65 	gint index = 0;
66 	const GSList *iter;
67 
68 	g_return_if_fail (E_IS_USER_PROMPTER_SERVER (server));
69 
70 	if (primary_text == NULL)
71 		primary_text = "";
72 
73 	if (type) {
74 		if (g_ascii_strcasecmp (type, "info") == 0)
75 			ntype = GTK_MESSAGE_INFO;
76 		else if (g_ascii_strcasecmp (type, "warning") == 0)
77 			ntype = GTK_MESSAGE_WARNING;
78 		else if (g_ascii_strcasecmp (type, "question") == 0)
79 			ntype = GTK_MESSAGE_QUESTION;
80 		else if (g_ascii_strcasecmp (type, "error") == 0)
81 			ntype = GTK_MESSAGE_ERROR;
82 	}
83 
84 	if (use_markup) {
85 		message = gtk_message_dialog_new_with_markup (
86 			NULL, 0, ntype, GTK_BUTTONS_NONE, "%s", "");
87 		gtk_message_dialog_set_markup (
88 			GTK_MESSAGE_DIALOG (message), primary_text);
89 	} else {
90 		message = gtk_message_dialog_new (
91 			NULL, 0, ntype, GTK_BUTTONS_NONE, "%s", primary_text);
92 	}
93 
94 	/* To show dialog on a taskbar */
95 	gtk_window_set_skip_taskbar_hint (GTK_WINDOW (message), FALSE);
96 	gtk_window_set_title (GTK_WINDOW (message), title ? title : "");
97 	gtk_window_set_icon_name (GTK_WINDOW (message), "evolution");
98 
99 	if (secondary_text && *secondary_text) {
100 		if (use_markup)
101 			gtk_message_dialog_format_secondary_markup (
102 				GTK_MESSAGE_DIALOG (message),
103 				"%s", secondary_text);
104 		else
105 			gtk_message_dialog_format_secondary_text (
106 				GTK_MESSAGE_DIALOG (message),
107 				"%s", secondary_text);
108 	}
109 
110 	g_object_set (message, "resizable", TRUE, NULL);
111 
112 	for (iter = button_captions; iter != NULL; iter = iter->next) {
113 		gtk_dialog_add_button (
114 			GTK_DIALOG (message), iter->data, index++);
115 	}
116 
117 	if (index == 0)
118 		gtk_dialog_add_button (
119 			GTK_DIALOG (message), _("_Dismiss"), index);
120 
121 	g_object_set_data (
122 		G_OBJECT (message),
123 		E_USER_PROMPTER_ID_KEY,
124 		GINT_TO_POINTER (id));
125 
126 	g_signal_connect (
127 		message, "response",
128 		G_CALLBACK (message_response_cb), server);
129 
130 	gtk_widget_show (message);
131 }
132