~x2go/x2go/pinentry-x2go_build-main

« back to all changes in this revision

Viewing changes to qt/main.cpp

  • Committer: Mike Gabriel
  • Date: 2012-06-13 12:55:37 UTC
  • Revision ID: git-v1:d2060291d5cc7beb92f78168e48ececfe765d552
Strip code project down to its essentials, remove a lot of unneeded cruft. / Make code tree fully build with autotools, see README file for further info.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* main.cpp - Secure KDE dialog for PIN entry.
2
 
   Copyright (C) 2002 Klar�lvdalens Datakonsult AB
3
 
   Copyright (C) 2003 g10 Code GmbH
4
 
   Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>.
5
 
   Modified by Marcus Brinkmann <marcus@g10code.de>.
6
 
   
7
 
   This program is free software; you can redistribute it and/or
8
 
   modify it under the terms of the GNU General Public License as
9
 
   published by the Free Software Foundation; either version 2 of the
10
 
   License, or (at your option) any later version.
11
 
 
12
 
   This program is distributed in the hope that it will be useful, but
13
 
   WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
   General Public License for more details.
16
 
 
17
 
   You should have received a copy of the GNU General Public License
18
 
   along with this program; if not, write to the Free Software
19
 
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20
 
   02111-1307, USA  */
21
 
 
22
 
#ifdef HAVE_CONFIG_H
23
 
#include "config.h"
24
 
#endif
25
 
 
26
 
#include <stdlib.h>
27
 
#include <errno.h>
28
 
 
29
 
#include <qapplication.h>
30
 
#include <qwidget.h>
31
 
#include <qmessagebox.h>
32
 
#include "secqstring.h"
33
 
 
34
 
#include "pinentrydialog.h"
35
 
 
36
 
#include "pinentry.h"
37
 
 
38
 
#ifdef FALLBACK_CURSES
39
 
#include <pinentry-curses.h>
40
 
#endif
41
 
 
42
 
/* Hack for creating a QWidget with a "foreign" window ID */
43
 
class ForeignWidget : public QWidget
44
 
{
45
 
public:
46
 
  ForeignWidget( WId wid ) : QWidget( 0 )
47
 
  {
48
 
    QWidget::destroy();
49
 
    create( wid, false, false );
50
 
  }
51
 
 
52
 
  ~ForeignWidget()
53
 
  {
54
 
    destroy( false, false );
55
 
  }
56
 
};
57
 
 
58
 
static int
59
 
qt_cmd_handler (pinentry_t pe)
60
 
{
61
 
  QWidget *parent = 0;
62
 
 
63
 
  int want_pass = !!pe->pin;
64
 
 
65
 
  if (want_pass)
66
 
    {
67
 
      /* FIXME: Add parent window ID to pinentry and GTK.  */
68
 
      if (pe->parent_wid)
69
 
        parent = new ForeignWidget (pe->parent_wid);
70
 
 
71
 
      PinEntryDialog pinentry (parent, NULL, true, !!pe->quality_bar);
72
 
 
73
 
      pinentry.setPinentryInfo (pe);
74
 
      pinentry.setPrompt (QString::fromUtf8 (pe->prompt));
75
 
      pinentry.setDescription (QString::fromUtf8 (pe->description));
76
 
      /* If we reuse the same dialog window.  */
77
 
#if 0
78
 
      pinentry.setText (SecQString::null);
79
 
#endif
80
 
 
81
 
      if (pe->ok)
82
 
        pinentry.setOkText (QString::fromUtf8 (pe->ok));
83
 
      if (pe->cancel)
84
 
        pinentry.setCancelText (QString::fromUtf8 (pe->cancel));
85
 
      if (pe->error)
86
 
        pinentry.setError (QString::fromUtf8 (pe->error));
87
 
      if (pe->quality_bar)
88
 
        pinentry.setQualityBar (QString::fromUtf8 (pe->quality_bar));
89
 
      if (pe->quality_bar_tt)
90
 
        pinentry.setQualityBarTT (QString::fromUtf8 (pe->quality_bar_tt));
91
 
 
92
 
      bool ret = pinentry.exec ();
93
 
      if (!ret)
94
 
        return -1;
95
 
 
96
 
      char *pin = (char *) pinentry.text().utf8();
97
 
      if (!pin)
98
 
        return -1;
99
 
 
100
 
      int len = strlen (pin);
101
 
      if (len >= 0)
102
 
        {
103
 
          pinentry_setbufferlen (pe, len + 1);
104
 
          if (pe->pin)
105
 
            {
106
 
              strcpy (pe->pin, pin);
107
 
              ::secmem_free (pin);
108
 
              return len;
109
 
            }
110
 
        }
111
 
      ::secmem_free (pin);
112
 
      return -1;
113
 
    }
114
 
  else
115
 
    {
116
 
      bool ret = QMessageBox::information (parent, "", pe->description,
117
 
                                           pe->ok ? pe->ok : "OK",
118
 
                                           pe->cancel ? pe->cancel : "Cancel");
119
 
      return !ret;
120
 
    }
121
 
}
122
 
 
123
 
pinentry_cmd_handler_t pinentry_cmd_handler = qt_cmd_handler;
124
 
 
125
 
int 
126
 
main (int argc, char *argv[])
127
 
{
128
 
  pinentry_init ("pinentry-qt");
129
 
 
130
 
#ifdef FALLBACK_CURSES
131
 
  if (!pinentry_have_display (argc, argv))
132
 
    pinentry_cmd_handler = curses_cmd_handler;
133
 
  else
134
 
#endif
135
 
    {
136
 
      /* Qt does only understand -display but not --display; thus we
137
 
         are fixing that here.  The code is pretty simply and may get
138
 
         confused if an argument is called "--display". */
139
 
      char **new_argv, *p;
140
 
      size_t n;
141
 
      int i, done;
142
 
 
143
 
      for (n=0,i=0; i < argc; i++)
144
 
        n += strlen (argv[i])+1;
145
 
      n++;
146
 
      new_argv = (char**)calloc (argc+1, sizeof *new_argv);
147
 
      if (new_argv)
148
 
        *new_argv = (char*)malloc (n);
149
 
      if (!new_argv || !*new_argv)
150
 
        {
151
 
          fprintf (stderr, "pinentry-qt: can't fixup argument list: %s\n",
152
 
                   strerror (errno));
153
 
          exit (EXIT_FAILURE);
154
 
          
155
 
        }
156
 
      for (done=0,p=*new_argv,i=0; i < argc; i++)
157
 
        if (!done && !strcmp (argv[i], "--display"))
158
 
          {
159
 
            new_argv[i] = "-display";
160
 
            done = 1;
161
 
          }
162
 
        else
163
 
          {
164
 
            new_argv[i] = strcpy (p, argv[i]);
165
 
            p += strlen (argv[i]) + 1;
166
 
          }
167
 
 
168
 
      /* We use a modal dialog window, so we don't need the application
169
 
         window anymore.  */
170
 
      i = argc;
171
 
      new QApplication (i, new_argv);
172
 
    }
173
 
  
174
 
 
175
 
  /* Consumes all arguments.  */
176
 
  if (pinentry_parse_opts (argc, argv))
177
 
    {
178
 
      printf ("pinentry-qt (pinentry) " VERSION "\n");
179
 
      exit (EXIT_SUCCESS);
180
 
    }
181
 
 
182
 
  if (pinentry_loop ())
183
 
    return 1;
184
 
 
185
 
  return 0;
186
 
}