testmountoperation.c 4.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* testmultidisplay.c
 * Copyright (C) 2008 Christian Kellner
 * Author: Christian Kellner <gicmo@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18
 */

19
#include "config.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#include <gtk/gtk.h>

static gboolean ask_question = FALSE;
static gboolean anonymous = FALSE;
static gboolean dont_ask_username = FALSE;
static gboolean dont_ask_domain = FALSE;
static gboolean dont_ask_password = FALSE;
static gboolean dont_save_password = FALSE;


static void
got_reply (GMountOperation       *op,
           GMountOperationResult  result,
           gpointer               user_data)
{
  if (result == G_MOUNT_OPERATION_HANDLED)
    {

      if (ask_question)
        {
          gint choice = g_mount_operation_get_choice (op);
          g_print ("User chose: %d\n", choice);
        }
      else
        {
          if (anonymous)
            g_print ("Anymous: %s\n",
                     g_mount_operation_get_anonymous (op) ? "true" : "false");

          if (!dont_ask_username)
            g_print ("Username: %s\n", g_mount_operation_get_username (op));

          if (!dont_ask_domain)
            g_print ("Domain: %s\n", g_mount_operation_get_domain (op));

          if (!dont_ask_password)
            g_print ("Password: %s\n", g_mount_operation_get_password (op));

          if (!dont_save_password)
            {
              GPasswordSave pw_save;

              pw_save = g_mount_operation_get_password_save (op);
              g_print ("Save password: ");
              switch (pw_save)
                {
               case G_PASSWORD_SAVE_NEVER:
                 g_print ("never");
                 break;

               case G_PASSWORD_SAVE_FOR_SESSION:
                 g_print ("session");
                 break;

               case G_PASSWORD_SAVE_PERMANENTLY:
                 g_print ("forever");
                 break;

               default:
                 g_assert_not_reached ();
                }
              g_print ("\n");
            }
        }
    }
  else if (result == G_MOUNT_OPERATION_ABORTED)
    g_print ("Operation aborted.\n");
  else if (G_MOUNT_OPERATION_UNHANDLED)
    g_assert_not_reached ();

  gtk_main_quit ();
}

int
main (int argc, char *argv[])
{
  GMountOperation *op;
  gboolean force_rtl = FALSE;
  GError *error = NULL;
  GOptionEntry options[] = {
    { "ask-question", 'q', 0, G_OPTION_ARG_NONE, &ask_question, "Ask a question not a password.", NULL },
    { "right-to-left", 'r', 0, G_OPTION_ARG_NONE, &force_rtl, "Force right-to-left layout.", NULL },
    { "anonymous", 'a', 0, G_OPTION_ARG_NONE, &anonymous, "Anonymous login allowed.", NULL },
    { "no-username", 'u', 0, G_OPTION_ARG_NONE, &dont_ask_username, "Don't ask for the username.", NULL },
    { "no-password", 'p', 0, G_OPTION_ARG_NONE, &dont_ask_password, "Don't ask for the password.", NULL },
    { "no-domain", 'd', 0, G_OPTION_ARG_NONE, &dont_ask_domain, "Don't ask for the domain.", NULL },
    { "no-pw-save", 's', 0, G_OPTION_ARG_NONE, &dont_save_password, "Don't show password save options.", NULL },
    { NULL }
  };

  if (!gtk_init_with_args (&argc, &argv, "", options, NULL, &error))
    {
      g_print ("Failed to parse args: %s\n", error->message);
      g_error_free (error);
      return 1;
    }

  if (force_rtl)
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

  op = gtk_mount_operation_new (NULL);

  g_signal_connect (op, "reply", G_CALLBACK (got_reply), NULL);

  if (ask_question)
    {
      static const char *choices[] = {
        "Yes", "No", "Sauerkraut", NULL
      };

      g_signal_emit_by_name (op, "ask_question", "Foo\nbar", choices);
    }
  else
    {
      GAskPasswordFlags flags;

      flags = 0;

      if (!dont_ask_password)
          flags |= G_ASK_PASSWORD_NEED_PASSWORD;

      if (!dont_ask_username)
          flags |= G_ASK_PASSWORD_NEED_USERNAME;

      if (!dont_ask_domain)
          flags |= G_ASK_PASSWORD_NEED_DOMAIN;

      if (anonymous)
          flags |= G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;

      if (!dont_save_password)
          flags |= G_ASK_PASSWORD_SAVING_SUPPORTED;

      g_signal_emit_by_name (op, "ask_password",
                             argc > 1 ? argv[1] : "Credentials needed",
                             argc > 2 ? argv[2] : "default user",
                             argc > 3 ? argv[3] : "default domain",
                             flags);
    }

  gtk_main ();
  return 0;
}