Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

dlgNewFile.cpp

Go to the documentation of this file.
00001 /* 00002 * wxChecksums 00003 * Copyright (C) 2003-2004 Julien Couot 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /** 00021 * \file dlgNewFile.cpp 00022 * New file dialog. 00023 */ 00024 00025 00026 //--------------------------------------------------------------------------- 00027 // For compilers that support precompilation, includes "wx.h". 00028 #include <wx/wxprec.h> 00029 00030 #ifdef __BORLANDC__ 00031 #pragma hdrstop 00032 #endif 00033 00034 #ifndef WX_PRECOMP 00035 // Include your minimal set of headers here, or wx.h 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 /// The C++ standard namespace. 00053 using namespace std; 00054 00055 00056 //########################################################################### 00057 // A validator for the file name 00058 //########################################################################### 00059 00060 00061 /** 00062 * A validator for the file name. 00063 * 00064 * Checks that the given file name has an absolute path and that the path 00065 * exists. 00066 * 00067 * If the file already exists, ask for overwriting. 00068 */ 00069 class dlgNewFile::FileNameValidator : public wxValidator 00070 { 00071 protected: 00072 wxString* value; ///< Adress of the string where the value will be transfered. 00073 00074 // Clones the source instance in this instance. 00075 void clone(const FileNameValidator& source); 00076 00077 // Returns the wxTextCtrl associated with the validator. 00078 wxTextCtrl* GetTextCtrl() const; 00079 00080 public: 00081 // Constructor. 00082 FileNameValidator(wxString* pValue); 00083 00084 // Copy constructor. 00085 FileNameValidator(const FileNameValidator& source); 00086 00087 // Assignment operator. 00088 FileNameValidator& operator=(const FileNameValidator& source); 00089 00090 // Returns a copy of the instance of the validator. 00091 virtual wxObject* Clone() const; 00092 00093 // Validates the value in the associated window. 00094 virtual bool Validate(wxWindow* parent); 00095 00096 // Transfers the value in the window to the validator. 00097 virtual bool TransferFromWindow(); 00098 00099 // Transfers the value associated with the validator to the window. 00100 virtual bool TransferToWindow(); 00101 }; 00102 //--------------------------------------------------------------------------- 00103 00104 00105 /** 00106 * Constructor. 00107 * 00108 * @param pValue Pointer on a string where to value will be transfered. 00109 */ 00110 dlgNewFile::FileNameValidator::FileNameValidator(wxString* pValue) 00111 { 00112 value = pValue; 00113 } 00114 //--------------------------------------------------------------------------- 00115 00116 00117 /** 00118 * Clones the source instance in this instance. 00119 * 00120 * @param source Source instance. 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 * Copy constructor. 00134 * 00135 * @param source Source instance. 00136 */ 00137 dlgNewFile::FileNameValidator::FileNameValidator(const FileNameValidator& source) 00138 { 00139 clone(source); 00140 } 00141 //--------------------------------------------------------------------------- 00142 00143 00144 /** 00145 * Assignment operator. 00146 * 00147 * @param source Source instance. 00148 * @return A reference on the instance. 00149 */ 00150 dlgNewFile::FileNameValidator& dlgNewFile::FileNameValidator::operator=(const FileNameValidator& source) 00151 { 00152 clone(source); 00153 return *this; 00154 } 00155 //--------------------------------------------------------------------------- 00156 00157 00158 /** 00159 * Returns a copy of the instance of the validator. 00160 * 00161 * @return A copy of the instance of the validator. 00162 */ 00163 wxObject* dlgNewFile::FileNameValidator::Clone() const 00164 { 00165 return new FileNameValidator(*this); 00166 } 00167 //--------------------------------------------------------------------------- 00168 00169 00170 /** 00171 * Returns the <CODE>wxTextCtrl</CODE> associated with the validator. 00172 * 00173 * @return The <CODE>wxTextCtrl</CODE> associated with the validator or 00174 * <CODE>NULL</CODE> if the <CODE>wxTextCtrl</CODE> associated with 00175 * the validator isn't a <CODE>wxTextCtrl</CODE>. 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 * Validates the value in the associated window. 00192 * 00193 * @param parent Parent window. 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 * Transfers the value in the window to the validator. 00255 * 00256 * @return <CODE>true</CODE> on success, <CODE>false</CODE> otherwise. 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 * Transfers the value associated with the validator to the window. 00274 * 00275 * return <CODE>true</CODE> on success, <CODE>false</CODE> otherwise. 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 // dlgNewFile methods 00294 //########################################################################### 00295 00296 IMPLEMENT_DYNAMIC_CLASS(dlgNewFile, wxDialog) 00297 00298 00299 /** 00300 * Creates a new dialog. 00301 */ 00302 dlgNewFile::dlgNewFile() : wxDialog() 00303 { 00304 createControls(); 00305 } 00306 //--------------------------------------------------------------------------- 00307 00308 00309 /** 00310 * Creates a new dialog. 00311 * 00312 * @param parent Parent of the dialog. 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 * Creates and initializes the controls of the dialog. 00330 */ 00331 void dlgNewFile::createControls() 00332 { 00333 // Creates the controls 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 // Initializes the controls 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 // Creates the dialog sizers 00366 00367 // Dialog sizer 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 // File type 00374 dlgNewFileSizer->Add(lblFileType); 00375 dlgNewFileSizer->Add(rbxSFV, 0, wxTOP, 0); 00376 dlgNewFileSizer->Add(rbxMD5, 0, wxTOP, CONTROL_SPACE / 2); 00377 00378 // File name 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 // Validation buttons sizer 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 // Set on the auto-layout feature 00392 this->SetAutoLayout(true); 00393 this->Layout(); 00394 } 00395 //--------------------------------------------------------------------------- 00396 00397 00398 /** 00399 * The class descructor. 00400 */ 00401 dlgNewFile::~dlgNewFile() 00402 { 00403 } 00404 //--------------------------------------------------------------------------- 00405 00406 00407 /** 00408 * Processes button Browse. 00409 * 00410 * @param event The event's parameters 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 // Gets the filter for knows types of checksums' files 00422 wxFileDialogFilterMaker fltMaker = ::getFilterForKnownTypesOfChecksumsFiles(); 00423 00424 // Look in getFilterForKnownTypesOfChecksumsFiles() in the fileutil.cpp file 00425 // which is the order of the filters. 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 // Shows the dialog of file's selection 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 * Gets the choosen file type. 00455 * 00456 * @return The choosen file type. 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 * Gets the name of the file. 00470 * 00471 * @return The name of the file. 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 //---------------------------------------------------------------------------

Generated on Sun May 30 13:37:44 2004 for wxChecksums by doxygen 1.3.7