GTlsDatabase and related objects
[platform/upstream/glib.git] / gio / tests / gtlsconsoleinteraction.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2011 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 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
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Stef Walter <stefw@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "gtlsconsoleinteraction.h"
28
29 /*
30  * WARNING: This is not the example you're looking for [slow hand wave]. This
31  * is not industrial strength, it's just for testing. It uses embarassing
32  * functions like getpass() and does lazy things with threads.
33  */
34
35 G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INTERACTION);
36
37 static GTlsInteractionResult
38 g_tls_console_interaction_ask_password (GTlsInteraction    *interaction,
39                                         GTlsPassword       *password)
40 {
41   const gchar *value;
42   gchar *prompt;
43
44   prompt = g_strdup_printf ("Password \"%s\"': ", g_tls_password_get_description (password));
45   value = getpass (prompt);
46   g_free (prompt);
47
48   g_tls_password_set_value (password, (guchar *)value, -1);
49   return G_TLS_INTERACTION_HANDLED;
50 }
51
52 static void
53 ask_password_with_getpass (GSimpleAsyncResult    *res,
54                            GObject               *object,
55                            GCancellable          *cancellable)
56 {
57   GTlsPassword *password;
58
59   password = g_simple_async_result_get_op_res_gpointer (res);
60   g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password);
61 }
62
63 void
64 g_tls_console_interaction_ask_password_async (GTlsInteraction    *interaction,
65                                               GTlsPassword       *password,
66                                               GAsyncReadyCallback callback,
67                                               gpointer            user_data)
68 {
69   GSimpleAsyncResult *res;
70
71   res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
72                                    g_tls_console_interaction_ask_password);
73   g_simple_async_result_set_op_res_gpointer (res, g_object_ref (password), g_object_unref);
74   g_simple_async_result_run_in_thread (res, ask_password_with_getpass,
75                                        G_PRIORITY_DEFAULT, NULL);
76   g_object_unref (res);
77 }
78
79 GTlsInteractionResult
80 g_tls_console_interaction_ask_password_finish (GTlsInteraction    *interaction,
81                                                GAsyncResult       *result)
82 {
83   g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
84                         g_tls_console_interaction_ask_password), G_TLS_INTERACTION_ABORTED);
85   return G_TLS_INTERACTION_HANDLED;
86 }
87
88 static void
89 g_tls_console_interaction_init (GTlsConsoleInteraction *interaction)
90 {
91
92 }
93
94 static void
95 g_tls_console_interaction_class_init (GTlsConsoleInteractionClass *klass)
96 {
97   GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
98   interaction_class->ask_password = g_tls_console_interaction_ask_password;
99   interaction_class->ask_password_async = g_tls_console_interaction_ask_password_async;
100   interaction_class->ask_password_finish = g_tls_console_interaction_ask_password_finish;
101 }
102
103 GTlsInteraction *
104 g_tls_console_interaction_new (void)
105 {
106   return g_object_new (G_TYPE_TLS_CONSOLE_INTERACTION, NULL);
107 }