]> Pileus Git - ~andy/gtk/blob - tests/testmountoperation.c
e5a41bbe7bdac8439467d6d157cd3b3d07166c92
[~andy/gtk] / tests / testmountoperation.c
1 /* testmultidisplay.c
2  * Copyright (C) 2008 Christian Kellner
3  * Author: Christian Kellner <gicmo@gnome.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * 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
21 #include "config.h"
22 #include <gtk/gtk.h>
23
24 static gboolean ask_question = FALSE;
25 static gboolean anonymous = FALSE;
26 static gboolean dont_ask_username = FALSE;
27 static gboolean dont_ask_domain = FALSE;
28 static gboolean dont_ask_password = FALSE;
29 static gboolean dont_save_password = FALSE;
30
31
32 static void
33 got_reply (GMountOperation       *op,
34            GMountOperationResult  result,
35            gpointer               user_data)
36 {
37   if (result == G_MOUNT_OPERATION_HANDLED)
38     {
39
40       if (ask_question)
41         {
42           gint choice = g_mount_operation_get_choice (op);
43           g_print ("User chose: %d\n", choice);
44         }
45       else
46         {
47           if (anonymous)
48             g_print ("Anymous: %s\n",
49                      g_mount_operation_get_anonymous (op) ? "true" : "false");
50
51           if (!dont_ask_username)
52             g_print ("Username: %s\n", g_mount_operation_get_username (op));
53
54           if (!dont_ask_domain)
55             g_print ("Domain: %s\n", g_mount_operation_get_domain (op));
56
57           if (!dont_ask_password)
58             g_print ("Password: %s\n", g_mount_operation_get_password (op));
59
60           if (!dont_save_password)
61             {
62               GPasswordSave pw_save;
63
64               pw_save = g_mount_operation_get_password_save (op);
65               g_print ("Save password: ");
66               switch (pw_save)
67                 {
68                case G_PASSWORD_SAVE_NEVER:
69                  g_print ("never");
70                  break;
71
72                case G_PASSWORD_SAVE_FOR_SESSION:
73                  g_print ("session");
74                  break;
75
76                case G_PASSWORD_SAVE_PERMANENTLY:
77                  g_print ("forever");
78                  break;
79
80                default:
81                  g_assert_not_reached ();
82                 }
83               g_print ("\n");
84             }
85         }
86     }
87   else if (result == G_MOUNT_OPERATION_ABORTED)
88     g_print ("Operation aborted.\n");
89   else if (G_MOUNT_OPERATION_UNHANDLED)
90     g_assert_not_reached ();
91
92   gtk_main_quit ();
93 }
94
95 int
96 main (int argc, char *argv[])
97 {
98   GMountOperation *op;
99   gboolean force_rtl = FALSE;
100   GError *error = NULL;
101   GOptionEntry options[] = {
102     { "ask-question", 'q', 0, G_OPTION_ARG_NONE, &ask_question, "Ask a question not a password.", NULL },
103     { "right-to-left", 'r', 0, G_OPTION_ARG_NONE, &force_rtl, "Force right-to-left layout.", NULL },
104     { "anonymous", 'a', 0, G_OPTION_ARG_NONE, &anonymous, "Anonymous login allowed.", NULL },
105     { "no-username", 'u', 0, G_OPTION_ARG_NONE, &dont_ask_username, "Don't ask for the username.", NULL },
106     { "no-password", 'p', 0, G_OPTION_ARG_NONE, &dont_ask_password, "Don't ask for the password.", NULL },
107     { "no-domain", 'd', 0, G_OPTION_ARG_NONE, &dont_ask_domain, "Don't ask for the domain.", NULL },
108     { "no-pw-save", 's', 0, G_OPTION_ARG_NONE, &dont_save_password, "Don't show password save options.", NULL },
109     { NULL }
110   };
111
112   if (!gtk_init_with_args (&argc, &argv, "", options, NULL, &error))
113     {
114       g_print ("Failed to parse args: %s\n", error->message);
115       g_error_free (error);
116       return 1;
117     }
118
119   if (force_rtl)
120     gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
121
122   op = gtk_mount_operation_new (NULL);
123
124   g_signal_connect (op, "reply", G_CALLBACK (got_reply), NULL);
125
126   if (ask_question)
127     {
128       static const char *choices[] = {
129         "Yes", "No", "Sauerkraut", NULL
130       };
131
132       g_signal_emit_by_name (op, "ask_question", "Foo\nbar", choices);
133     }
134   else
135     {
136       GAskPasswordFlags flags;
137
138       flags = 0;
139
140       if (!dont_ask_password)
141           flags |= G_ASK_PASSWORD_NEED_PASSWORD;
142
143       if (!dont_ask_username)
144           flags |= G_ASK_PASSWORD_NEED_USERNAME;
145
146       if (!dont_ask_domain)
147           flags |= G_ASK_PASSWORD_NEED_DOMAIN;
148
149       if (anonymous)
150           flags |= G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;
151
152       if (!dont_save_password)
153           flags |= G_ASK_PASSWORD_SAVING_SUPPORTED;
154
155       g_signal_emit_by_name (op, "ask_password",
156                              argc > 1 ? argv[1] : "Credentials needed",
157                              argc > 2 ? argv[2] : "default user",
158                              argc > 3 ? argv[3] : "default domain",
159                              flags);
160     }
161
162   gtk_main ();
163   return 0;
164 }