[6af072]: src / ui / wxWidgets / safecombinationentry.cpp  Maximize  Restore  History

Download this file

501 lines (405 with data), 16.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Copyright (c) 2003-2013 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/** \file safecombinationentry.cpp
*
*/
// Generated by DialogBlocks, Sun 18 Jan 2009 09:22:13 PM IST
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
////@begin includes
#include "SafeCombinationCtrl.h"
////@end includes
#include <wx/timer.h>
#include "safecombinationentry.h"
#include "safecombinationsetup.h"
#include "version.h"
#include "core/core.h"
#include "core/PWSdirs.h"
#include "os/file.h"
////@begin XPM images
#include "graphics/Yubikey-button.xpm"
////@end XPM images
#include "./graphics/cpane.xpm"
#include "./graphics/psafetxt.xpm"
#include "pwsafeapp.h"
#include <wx/filename.h>
#ifdef __WXMSW__
#include <wx/msw/msvcrt.h>
#endif
#include <iostream> // for debugging
#include <iomanip>
/*!
* CSafeCombinationEntry type definition
*/
IMPLEMENT_CLASS( CSafeCombinationEntry, wxDialog )
/*!
* CSafeCombinationEntry event table definition
*/
BEGIN_EVENT_TABLE( CSafeCombinationEntry, wxDialog )
////@begin CSafeCombinationEntry event table entries
EVT_BUTTON( ID_ELLIPSIS, CSafeCombinationEntry::OnEllipsisClick )
EVT_BUTTON( ID_NEWDB, CSafeCombinationEntry::OnNewDbClick )
EVT_BUTTON( ID_YUBIBTN, CSafeCombinationEntry::OnYubibtnClick )
EVT_BUTTON( wxID_OK, CSafeCombinationEntry::OnOk )
EVT_BUTTON( wxID_CANCEL, CSafeCombinationEntry::OnCancel )
////@end CSafeCombinationEntry event table entries
EVT_TIMER(POLLING_TIMER_ID, CSafeCombinationEntry::OnPollingTimer)
END_EVENT_TABLE()
/*!
* CSafeCombinationEntry constructors
*/
CSafeCombinationEntry::CSafeCombinationEntry(PWScore &core)
: m_core(core), m_tries(0)
{
Init();
}
CSafeCombinationEntry::CSafeCombinationEntry(wxWindow* parent, PWScore &core,
wxWindowID id,
const wxString& caption,
const wxPoint& pos,
const wxSize& size, long style)
: m_core(core), m_tries(0)
{
Init();
Create(parent, id, caption, pos, size, style);
}
/*!
* CSafeCombinationEntry creator
*/
bool CSafeCombinationEntry::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin CSafeCombinationEntry creation
SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
wxDialog::Create( parent, id, caption, pos, size, style );
CreateControls();
if (GetSizer())
{
GetSizer()->SetSizeHints(this);
}
Centre();
////@end CSafeCombinationEntry creation
SetupMixin(FindWindow(ID_YUBIBTN), FindWindow(ID_YUBISTATUS));
m_pollingTimer = new wxTimer(this, POLLING_TIMER_ID);
m_pollingTimer->Start(250); // check for Yubikey every 250ms.
return true;
}
/*!
* CSafeCombinationEntry destructor
*/
CSafeCombinationEntry::~CSafeCombinationEntry()
{
////@begin CSafeCombinationEntry destruction
////@end CSafeCombinationEntry destruction
delete m_pollingTimer;
}
/*!
* Member initialisation
*/
void CSafeCombinationEntry::Init()
{
m_readOnly = m_core.IsReadOnly();
m_filename = m_core.GetCurFile().c_str();
////@begin CSafeCombinationEntry member initialisation
m_version = NULL;
m_filenameCB = NULL;
m_combinationEntry = NULL;
m_YubiBtn = NULL;
m_yubiStatusCtrl = NULL;
////@end CSafeCombinationEntry member initialisation
}
/*!
* Control creation for CSafeCombinationEntry
*/
void CSafeCombinationEntry::CreateControls()
{
////@begin CSafeCombinationEntry content construction
CSafeCombinationEntry* itemDialog1 = this;
wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
itemDialog1->SetSizer(itemBoxSizer2);
wxStaticBitmap* itemStaticBitmap3 = new wxStaticBitmap( itemDialog1, wxID_STATIC, itemDialog1->GetBitmapResource(wxT("../graphics/wxWidgets/cpane.xpm")), wxDefaultPosition, itemDialog1->ConvertDialogToPixels(wxSize(49, 46)), 0 );
itemBoxSizer2->Add(itemStaticBitmap3, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxVERTICAL);
itemBoxSizer2->Add(itemBoxSizer4, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
itemBoxSizer4->Add(itemBoxSizer5, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
wxStaticBitmap* itemStaticBitmap6 = new wxStaticBitmap( itemDialog1, wxID_STATIC, itemDialog1->GetBitmapResource(wxT("../graphics/wxWidgets/psafetxt.xpm")), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer5->Add(itemStaticBitmap6, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
m_version = new wxStaticText( itemDialog1, wxID_STATIC, _("VX.YY"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer5->Add(m_version, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Open Password Database:"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer4->Add(itemStaticText8, 0, wxALIGN_LEFT|wxALL, 3);
wxBoxSizer* itemBoxSizer9 = new wxBoxSizer(wxHORIZONTAL);
itemBoxSizer4->Add(itemBoxSizer9, 50, wxGROW|wxALL, 5);
wxArrayString m_filenameCBStrings;
m_filenameCB = new wxComboBox( itemDialog1, ID_DBASECOMBOBOX, wxEmptyString, wxDefaultPosition, wxSize(itemDialog1->ConvertDialogToPixels(wxSize(140, -1)).x, -1), m_filenameCBStrings, wxCB_DROPDOWN );
itemBoxSizer9->Add(m_filenameCB, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM, 0);
wxButton* itemButton11 = new wxButton( itemDialog1, ID_ELLIPSIS, _("..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
itemBoxSizer9->Add(itemButton11, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxStaticText* itemStaticText12 = new wxStaticText( itemDialog1, wxID_STATIC, _("Safe Combination:"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer4->Add(itemStaticText12, 0, wxALIGN_LEFT|wxALL, 3);
m_combinationEntry = new CSafeCombinationCtrl( itemDialog1, ID_COMBINATION, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer4->Add(m_combinationEntry, 0, wxGROW|wxRIGHT|wxTOP|wxBOTTOM, 5);
wxBoxSizer* itemBoxSizer14 = new wxBoxSizer(wxHORIZONTAL);
itemBoxSizer4->Add(itemBoxSizer14, 0, wxGROW|wxALL, 5);
wxCheckBox* itemCheckBox15 = new wxCheckBox( itemDialog1, ID_READONLY, _("Open as read-only"), wxDefaultPosition, wxDefaultSize, 0 );
itemCheckBox15->SetValue(false);
itemBoxSizer14->Add(itemCheckBox15, 0, wxALIGN_CENTER_VERTICAL|wxALL, 0);
itemBoxSizer14->Add(120, 10, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxButton* itemButton17 = new wxButton( itemDialog1, ID_NEWDB, _("New..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
itemBoxSizer14->Add(itemButton17, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxTOP|wxBOTTOM, 5);
wxBoxSizer* itemBoxSizer18 = new wxBoxSizer(wxHORIZONTAL);
itemBoxSizer4->Add(itemBoxSizer18, 0, wxGROW|wxALL, 5);
m_YubiBtn = new wxBitmapButton( itemDialog1, ID_YUBIBTN, itemDialog1->GetBitmapResource(wxT("graphics/Yubikey-button.xpm")), wxDefaultPosition, itemDialog1->ConvertDialogToPixels(wxSize(40, 15)), wxBU_AUTODRAW );
itemBoxSizer18->Add(m_YubiBtn, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM|wxSHAPED, 5);
m_yubiStatusCtrl = new wxStaticText( itemDialog1, ID_YUBISTATUS, _("Please insert your YubiKey"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer18->Add(m_yubiStatusCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxStdDialogButtonSizer* itemStdDialogButtonSizer21 = new wxStdDialogButtonSizer;
itemBoxSizer4->Add(itemStdDialogButtonSizer21, 0, wxGROW|wxALL, 0);
wxButton* itemButton22 = new wxButton( itemDialog1, wxID_OK, _("OK"), wxDefaultPosition, wxDefaultSize, 0 );
itemButton22->SetDefault();
itemStdDialogButtonSizer21->AddButton(itemButton22);
wxButton* itemButton23 = new wxButton( itemDialog1, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
itemStdDialogButtonSizer21->AddButton(itemButton23);
wxButton* itemButton24 = new wxButton( itemDialog1, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, 0 );
itemStdDialogButtonSizer21->AddButton(itemButton24);
itemStdDialogButtonSizer21->Realize();
// Set validators
m_filenameCB->SetValidator( wxGenericValidator(& m_filename) );
itemCheckBox15->SetValidator( wxGenericValidator(& m_readOnly) );
////@end CSafeCombinationEntry content construction
m_combinationEntry->SetValidatorTarget(& m_password);
#if (REVISION == 0)
m_version->SetLabel(wxString::Format(_("V%d.%d %s"),
MAJORVERSION, MINORVERSION, SPECIALBUILD));
#else
m_version->SetLabel(wxString::Format(_("V%d.%d.%d %s"),
MAJORVERSION, MINORVERSION,
REVISION, SPECIALBUILD));
#endif
wxArrayString recentFiles;
wxGetApp().recentDatabases().GetAll(recentFiles);
m_filenameCB->Append(recentFiles);
// if m_readOnly, then don't allow user to change it
itemCheckBox15->Enable(!m_readOnly);
// if filename field not empty, set focus to password:
if (!m_filename.empty()) {
FindWindow(ID_COMBINATION)->SetFocus();
}
SetIcons(wxGetApp().GetAppIcons());
}
/*!
* Should we show tooltips?
*/
bool CSafeCombinationEntry::ShowToolTips()
{
return true;
}
/*!
* Get bitmap resources
*/
wxBitmap CSafeCombinationEntry::GetBitmapResource( const wxString& name )
{
// Bitmap retrieval
////@begin CSafeCombinationEntry bitmap retrieval
wxUnusedVar(name);
if (name == _T("graphics/cpane.xpm"))
{
wxBitmap bitmap(cpane_xpm);
return bitmap;
}
else if (name == _T("graphics/psafetxt.xpm"))
{
wxBitmap bitmap(psafetxt_xpm);
return bitmap;
}
else if (name == _T("graphics/Yubikey-button.xpm"))
{
wxBitmap bitmap(Yubikey_button_xpm);
return bitmap;
}
return wxNullBitmap;
////@end CSafeCombinationEntry bitmap retrieval
}
/*!
* Get icon resources
*/
wxIcon CSafeCombinationEntry::GetIconResource( const wxString& name )
{
// Icon retrieval
////@begin CSafeCombinationEntry icon retrieval
wxUnusedVar(name);
return wxNullIcon;
////@end CSafeCombinationEntry icon retrieval
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
*/
void CSafeCombinationEntry::OnOk( wxCommandEvent& )
{
if (Validate() && TransferDataFromWindow()) {
if (m_password.empty()) {
wxMessageDialog err(this, _("The combination cannot be blank."),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
FindWindow(ID_COMBINATION)->SetFocus();
return;
}
if (!pws_os::FileExists(tostdstring(m_filename))) {
wxMessageDialog err(this, _("File or path not found."),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
m_filenameCB->SetFocus();
return;
}
ProcessPhrase();
} // Validate && TransferDataFromWindow
}
void CSafeCombinationEntry::ProcessPhrase()
{
int status = m_core.CheckPasskey(tostringx(m_filename), m_password);
wxString errmess;
switch (status) {
case PWScore::SUCCESS:
m_core.SetReadOnly(m_readOnly);
m_core.SetCurFile(tostringx(m_filename));
wxGetApp().recentDatabases().AddFileToHistory(m_filename);
EndModal(wxID_OK);
return;
case PWScore::CANT_OPEN_FILE:
{ stringT str;
LoadAString(str, IDSC_FILE_UNREADABLE);
errmess = str.c_str();
}
break;
case PWScore::WRONG_PASSWORD:
default:
if (m_tries >= 2) {
errmess = _("Three strikes - yer out!");
} else {
m_tries++;
errmess = _("Incorrect passkey, not a PasswordSafe database, or a corrupt database. (Backup database has same name as original, ending with '~')");
}
break;
} // switch (status)
// here iff CheckPasskey failed.
wxMessageDialog err(this, errmess,
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
wxTextCtrl *txt = (wxTextCtrl *)FindWindow(ID_COMBINATION);
txt->SetSelection(-1,-1);
txt->SetFocus();
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
*/
void CSafeCombinationEntry::OnCancel( wxCommandEvent& event )
{
////@begin wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL in CSafeCombinationEntry.
// Before editing this code, remove the block markers.
event.Skip();
////@end wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL in CSafeCombinationEntry.
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_ELLIPSIS
*/
void CSafeCombinationEntry::OnEllipsisClick( wxCommandEvent& /* evt */ )
{
wxFileDialog fd(this, _("Please Choose a Database to Open:"),
PWSdirs::GetSafeDir().c_str(), wxT(""),
_("Password Safe Databases (*.psafe3; *.dat)|*.psafe3;*.dat| All files (*.*; *)|*.*;*"),
(wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR));
if (fd.ShowModal() == wxID_OK) {
m_filename = fd.GetPath();
wxComboBox *cb = dynamic_cast<wxComboBox *>(FindWindow(ID_DBASECOMBOBOX));
cb->SetValue(m_filename);
}
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_NEWDB
*/
void CSafeCombinationEntry::OnNewDbClick( wxCommandEvent& /* evt */ )
{
// 1. Get a filename from a file dialog box
// 2. Get a password
// 3. Set m_filespec && m_passkey to returned value!
wxString newfile;
wxString cs_msg, cs_title, cs_temp;
wxString cf(_("pwsafe")); // reasonable default for first time user
stringT v3FileName = PWSUtil::GetNewFileName(tostdstring(cf), wxT("psafe3"));
stringT dir = PWSdirs::GetSafeDir();
while (1) {
wxFileDialog fd(this, _("Please choose a name for the new database"),
dir.c_str(), v3FileName.c_str(),
_("Password Safe Databases (*.psafe3; *.dat)|*.psafe3;*.dat| All files (*.*; *)|*.*;*"),
(wxFD_SAVE | wxFD_OVERWRITE_PROMPT| wxFD_CHANGE_DIR));
int rc = fd.ShowModal();
if (rc == wxID_OK) {
wxFileName wxfn(fd.GetPath());
if (wxfn.GetExt().empty()) {
wxfn.SetExt(DEFAULT_SUFFIX);
}
newfile = wxfn.GetFullPath();
break;
} else
return;
}
// 2. Get a password
CSafeCombinationSetup pksetup(this);
int rc = pksetup.ShowModal();
if (rc != wxID_OK)
return; //User cancelled password entry
// 3. Set m_filespec && m_passkey to returned value!
m_core.SetCurFile(tostringx(newfile));
// Now lock the new file
std::wstring locker(L""); // null init is important here
m_core.LockFile(tostdstring(newfile), locker);
m_core.SetReadOnly(false); // new file can't be read-only...
m_core.NewFile(tostringx(pksetup.GetPassword()));
if ((rc = m_core.WriteCurFile()) == PWSfile::SUCCESS) {
wxGetApp().recentDatabases().AddFileToHistory(newfile);
EndModal(wxID_OK);
}
else {
wxMessageBox(wxString()<< newfile << wxT("\n\n") << _("Could not open file for writing!"),
_("Write Error"), wxOK | wxICON_ERROR, this);
}
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_YUBIBTN
*/
void CSafeCombinationEntry::OnYubibtnClick( wxCommandEvent& /* event */ )
{
m_combinationEntry->AllowEmptyCombinationOnce(); // Allow blank password when Yubi's used
if (Validate() && TransferDataFromWindow()) {
if (!pws_os::FileExists(tostdstring(m_filename))) {
wxMessageDialog err(this, _("File or path not found."),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
m_filenameCB->SetFocus();
return;
}
StringX response;
if (PerformChallengeResponse(m_password, response)) {
m_password = response;
ProcessPhrase();
UpdateStatus();
}
}
}
void CSafeCombinationEntry::OnPollingTimer(wxTimerEvent &evt)
{
if (evt.GetId() == POLLING_TIMER_ID) {
HandlePollingTimer(); // in CYubiMixin
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks