00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
#include <wx/wxprec.h>
00029
00030
#ifdef __BORLANDC__
00031
#pragma hdrstop
00032
#endif
00033
00034
#ifndef WX_PRECOMP
00035
00036
#include <wx/wx.h>
00037
#endif
00038
00039
#include <wx/file.h>
00040
#include <wx/filename.h>
00041
00042
#include "dlgNewFile.hpp"
00043
#include "appprefs.hpp"
00044
#include "comdefs.hpp"
00045
#include "fdftlmk.hpp"
00046
#include "fileutil.hpp"
00047
00048
#include "compat.hpp"
00049
00050
00051
00052
00053
using namespace std;
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 class dlgNewFile::FileNameValidator :
public wxValidator
00070 {
00071
protected:
00072 wxString*
value;
00073
00074
00075
void clone(
const FileNameValidator& source);
00076
00077
00078 wxTextCtrl*
GetTextCtrl() const;
00079
00080 public:
00081
00082
FileNameValidator(wxString* pValue);
00083
00084
00085
FileNameValidator(const
FileNameValidator& source);
00086
00087
00088
FileNameValidator& operator=(const
FileNameValidator& source);
00089
00090
00091 virtual wxObject* Clone() const;
00092
00093
00094 virtual
bool Validate(wxWindow* parent);
00095
00096
00097 virtual
bool TransferFromWindow();
00098
00099
00100 virtual
bool TransferToWindow();
00101 };
00102
00103
00104
00105
00106
00107
00108
00109
00110 dlgNewFile::
FileNameValidator::
FileNameValidator(wxString* pValue)
00111 {
00112
value = pValue;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122 void dlgNewFile::FileNameValidator::clone(
const FileNameValidator& source)
00123 {
00124
if (
this != &source)
00125 {
00126 this->
value = source.
value;
00127 }
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 dlgNewFile::FileNameValidator::FileNameValidator(
const FileNameValidator& source)
00138 {
00139 clone(source);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 dlgNewFile::FileNameValidator&
dlgNewFile::FileNameValidator::operator=(
const FileNameValidator& source)
00151 {
00152 clone(source);
00153
return *
this;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163 wxObject*
dlgNewFile::FileNameValidator::Clone()
const
00164
{
00165
return new FileNameValidator(*
this);
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 wxTextCtrl*
dlgNewFile::FileNameValidator::GetTextCtrl()
const
00178
{
00179 wxWindow* win = GetWindow();
00180
if (win == NULL)
00181
return NULL;
00182
if (!win->IsKindOf(CLASSINFO(wxTextCtrl)))
00183
return NULL;
00184
00185
return wxDynamicCast(win, wxTextCtrl);
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195 bool dlgNewFile::FileNameValidator::Validate(wxWindow* parent)
00196 {
00197 wxTextCtrl* control =
GetTextCtrl();
00198
if (control == NULL)
00199
return false;
00200
00201 wxString msg;
00202 wxString val = control->GetValue();
00203 wxFileName fn(val);
00204
bool OK =
true;
00205
00206
00207
if (val.empty() || fn.GetFullName().empty())
00208 {
00209 msg = _(
"Please give a name for the new checksum file.");
00210 OK =
false;
00211 }
00212
else if (!fn.IsAbsolute())
00213 {
00214 msg = _(
"Please give an absolute path for the name of the new checksum file.");
00215 OK =
false;
00216 }
00217
else if (!::wxDirExists(fn.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR)))
00218 {
00219 msg.Printf(_(
"The directory '%s' doesn't exist."), fn.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR).c_str());
00220 OK =
false;
00221 }
00222
else if (::wxDirExists(fn.GetFullPath()))
00223 {
00224 msg.Printf(_(
"'%s' is a directory."), val.c_str());
00225 OK =
false;
00226 }
00227
else if (fn.FileExists())
00228 {
00229
if (wxFile::Access(val, wxFile::write))
00230 {
00231
if (::wxMessageBox(wxString::Format(_(
"The file '%s' already exists.\nDo you want to overwrite it?"), val.c_str()), _(
"Create a new file"), wxYES_NO | wxCANCEL | wxICON_EXCLAMATION) != wxYES)
00232 OK =
false;
00233 }
00234
else
00235 {
00236 msg.Printf(_(
"You don't have sufficient rights to write on '%s'."), val.c_str());
00237 OK =
false;
00238 }
00239 }
00240
00241
if (!OK)
00242 {
00243
if (!msg.empty())
00244 ::wxMessageBox(msg, _(
"Invalid file name"), wxOK | wxICON_EXCLAMATION);
00245 control->SetFocus();
00246 }
00247
00248
return OK;
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258 bool dlgNewFile::FileNameValidator::TransferFromWindow()
00259 {
00260 wxTextCtrl* control =
GetTextCtrl();
00261
if (control == NULL ||
value == NULL)
00262
return false;
00263
else
00264 {
00265 *
value = control->GetValue();
00266
return true;
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277 bool dlgNewFile::FileNameValidator::TransferToWindow()
00278 {
00279 wxTextCtrl* control =
GetTextCtrl();
00280
if (control == NULL ||
value == NULL)
00281
return false;
00282
else
00283 {
00284 control->SetValue(*
value);
00285
return true;
00286 }
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296 IMPLEMENT_DYNAMIC_CLASS(
dlgNewFile, wxDialog)
00297
00298
00299
00300
00301
00302 dlgNewFile::
dlgNewFile() : wxDialog()
00303 {
00304
createControls();
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314 dlgNewFile::dlgNewFile(wxWindow* parent) :
00315 wxDialog(parent, -1, _("Create a new checksums file"), wxDefaultPosition, wxDefaultSize,
00316 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
00317 {
00318
createControls();
00319
00320
int w, h;
00321 ::wxDisplaySize(&w, &h);
00322 Fit();
00323 Centre();
00324 }
00325
00326
00327
00328
00329
00330
00331 void dlgNewFile::createControls()
00332 {
00333
00334 wxStaticText* lblFileType =
new wxStaticText(
this, -1, _(
"Type of the new file:"));
00335
rbxSFV =
new wxRadioButton(
this, -1, _(
"&SFV file"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
00336
rbxMD5 =
new wxRadioButton(
this, -1, _(
"&MD5 file"));
00337
00338 wxStaticText* lblFileName =
new wxStaticText(
this, -1, _(
"&Name of the new file:"));
00339
txtFileName =
new wxTextCtrl(
this, -1, wxEmptyString, wxDefaultPosition,
00340 wxSize((this->GetParent()->GetSize().GetWidth() * 2) / 3, -1),
00341 0,
FileNameValidator(&(this->fileName)));
00342 wxButton* btnBrowse =
new wxButton(
this, BTN_BROWSE, _(
"&Browse..."));
00343
00344 wxButton* btnOK =
new wxButton(
this, wxID_OK, _(
"&OK"));
00345 btnOK->SetDefault();
00346 wxButton* btnCancel =
new wxButton(
this, wxID_CANCEL, _(
"&Cancel"));
00347
00348
00349
switch (static_cast<FileType>(
AppPrefs::get()->
readLong(prGUI_NEWFILE_LAST_FILETYPE)))
00350 {
00351
case ftMD5:
rbxMD5->SetValue(
true);
break;
00352
default :
rbxSFV->SetValue(
true);
break;
00353 }
00354
00355 this->
fileName =
AppPrefs::get()->
readString(prGUI_NEWFILE_LAST_DIRECTORY);
00356
if (this->
fileName.empty())
00357 {
00358 wxFileName curdir;
00359 curdir.AssignCwd();
00360 this->
fileName = curdir.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
00361 }
00362
txtFileName->SetFocus();
00363
00364
00365
00366
00367
00368 wxBoxSizer* dlgNewFileSizer2 =
new wxBoxSizer(wxVERTICAL);
00369 this->SetSizer(dlgNewFileSizer2);
00370 wxBoxSizer* dlgNewFileSizer =
new wxBoxSizer(wxVERTICAL);
00371 dlgNewFileSizer2->Add(dlgNewFileSizer, 1, wxALL | wxGROW,
CONTROL_SPACE);
00372
00373
00374 dlgNewFileSizer->Add(lblFileType);
00375 dlgNewFileSizer->Add(
rbxSFV, 0, wxTOP, 0);
00376 dlgNewFileSizer->Add(
rbxMD5, 0, wxTOP,
CONTROL_SPACE / 2);
00377
00378
00379 dlgNewFileSizer->Add(lblFileName, 0, wxTOP,
CONTROL_SPACE);
00380 wxBoxSizer* fileNameSizer =
new wxBoxSizer(wxHORIZONTAL);
00381 fileNameSizer->Add(
txtFileName, 1, wxALIGN_CENTER_VERTICAL);
00382 fileNameSizer->Add(btnBrowse, 0, wxLEFT | wxALIGN_CENTER_VERTICAL,
CONTROL_SPACE);
00383 dlgNewFileSizer->Add(fileNameSizer, 0, wxGROW);
00384
00385
00386 wxGridSizer* buttonsSizer =
new wxGridSizer(2, 0, 2 *
CONTROL_SPACE);
00387 buttonsSizer->Add(btnOK);
00388 buttonsSizer->Add(btnCancel);
00389 dlgNewFileSizer->Add(buttonsSizer, 0, wxTOP | wxALIGN_RIGHT, 2 *
CONTROL_SPACE);
00390
00391
00392 this->SetAutoLayout(
true);
00393 this->Layout();
00394 }
00395
00396
00397
00398
00399
00400
00401 dlgNewFile::~dlgNewFile()
00402 {
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412 void dlgNewFile::btnBrowseClick(wxCommandEvent& event)
00413 {
00414 wxString defDir = wxFileName(
txtFileName->GetValue()).GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
00415 wxString defName = _(
"new");
00416
int filterIdx;
00417
00418
if (!::wxDirExists(defDir))
00419 defDir.Clear();
00420
00421
00422
wxFileDialogFilterMaker fltMaker = ::getFilterForKnownTypesOfChecksumsFiles();
00423
00424
00425
00426
switch (this->
getFileType())
00427 {
00428
case ftSFV :
00429 defName += wxT(
".sfv");
00430 filterIdx = 1;
00431
break;
00432
case ftMD5 :
00433 defName += wxT(
".md5");
00434 filterIdx = 2;
00435
break;
00436 }
00437
00438
00439 wxFileDialog dlgNew(
this, _(
"Choose a name for the new checksums file"),
00440 defDir, defName, fltMaker.
GetFilters(),
00441 wxSAVE, wxDefaultPosition);
00442 dlgNew.SetFilterIndex(filterIdx);
00443
00444
if (dlgNew.ShowModal() == wxID_OK)
00445 {
00446
txtFileName->SetValue(dlgNew.GetPath());
00447
txtFileName->SetFocus();
00448 }
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458 dlgNewFile::FileType dlgNewFile::getFileType()
const
00459
{
00460
if (
rbxSFV->GetValue())
00461
return ftSFV;
00462
else
00463
return ftMD5;
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473 wxString
dlgNewFile::getFileName()
const
00474
{
00475
return this->
fileName;
00476 }
00477
00478
00479
00480 BEGIN_EVENT_TABLE(
dlgNewFile, wxDialog)
00481 EVT_BUTTON(BTN_BROWSE,
dlgNewFile::btnBrowseClick)
00482 END_EVENT_TABLE()
00483