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 cksums.cpp 00022 * Application definition body. 00023 */ 00024 00025 //--------------------------------------------------------------------------- 00026 // For compilers that support precompilation, includes "wx.h". 00027 #include <wx/wxprec.h> 00028 00029 #ifdef __BORLANDC__ 00030 #pragma hdrstop 00031 #endif 00032 00033 #ifndef WX_PRECOMP 00034 // Include your minimal set of headers here, or wx.h 00035 #include <wx/wx.h> 00036 #endif 00037 00038 #include <wx/cmdline.h> 00039 #include <wx/config.h> 00040 #include <wx/fileconf.h> 00041 00042 #include "cksums.hpp" 00043 #include "appprefs.hpp" 00044 #include "bytedisp.hpp" 00045 #include "checksumfactory.hpp" 00046 #include "cmdlnopt.hpp" 00047 #include "comdefs.hpp" 00048 #include "frmSums.hpp" 00049 #include "language.hpp" 00050 #include "osdep.hpp" 00051 #include "utils.hpp" 00052 00053 #include "compat.hpp" 00054 //--------------------------------------------------------------------------- 00055 00056 00057 /// The C++ standard namespace. 00058 using namespace std; 00059 00060 IMPLEMENT_APP(CkSumsApp) 00061 //--------------------------------------------------------------------------- 00062 00063 00064 /** 00065 * Application initialization. 00066 * 00067 * This method is called on application startup. 00068 * If <CODE>false</CODE> is return the application stops immediatly. 00069 * 00070 * @return <CODE>true</CODE> if initialization was successfully done. 00071 * <CODE>false</CODE> otherwise. 00072 */ 00073 bool CkSumsApp::OnInit() 00074 { 00075 // Sets the application name. 00076 SetAppName(wxT(APP_NAME)); 00077 00078 // Don't create a configuration object on demand (with wxConfigBase::Get method) 00079 wxConfig::DontCreateOnDemand(); 00080 00081 // Creates a new configuration file object. It will be deleted by wxWidgets. 00082 wxFileConfig* config = new wxFileConfig(wxT(APP_NAME), wxT(APP_AUTHOR), 00083 AppPrefs::getConfigPathName(), 00084 wxT(""), wxCONFIG_USE_LOCAL_FILE); 00085 wxConfigBase* oldCfg = wxConfig::Set(config); 00086 if (oldCfg != NULL) 00087 delete oldCfg; 00088 00089 // Sets a new instance of the application preferences. 00090 //AppPrefs::set(new AppPrefs()); // Automatic now 00091 00092 // Initializes the locale 00093 InitLocale(); 00094 00095 // Initializes the BytesDisplayer class 00096 BytesDisplayer::initStatic(); 00097 00098 // Initializes the SumFileFactory class 00099 SumFileFactory::initialize(); 00100 00101 // Call the OnInit() of the superclass to parse the command line. 00102 bool cont = wxApp::OnInit(); 00103 00104 if (cont) 00105 { 00106 bool error, warning, close; 00107 frmSums* frame = new frmSums(_("wxChecksums")); 00108 SetTopWindow(frame); 00109 frame->initializeFromCmdLine(error, warning); 00110 close = frame->closeAfterInitFromCmdLine(error, warning); 00111 frame->Show(!close); 00112 if (close) 00113 { 00114 wxCloseEvent e(wxEVT_CLOSE_WINDOW); 00115 e.SetCanVeto(false); 00116 frame->AddPendingEvent(e); 00117 } 00118 } 00119 00120 // We don't need the command line options, clean-up the memory. 00121 CmdLineOptions::cleanup(); 00122 00123 if (!cont) 00124 // Clear the resources created in OnInit(). 00125 CleanUpAppResources(); 00126 00127 return cont; 00128 } 00129 //--------------------------------------------------------------------------- 00130 00131 00132 /** 00133 * Application cleanup method. 00134 * 00135 * Provide this member function for any processing which needs to be done as 00136 * the application is about to exit. <CODE>OnExit</CODE> is called after 00137 * destroying all application windows and controls, but before wxWidgets 00138 * cleanup. 00139 * 00140 * @return the application return code. 00141 */ 00142 int CkSumsApp::OnExit() 00143 { 00144 CleanUpAppResources(); 00145 00146 return 0; 00147 } 00148 //--------------------------------------------------------------------------- 00149 00150 00151 /** 00152 * Cleans up the ressources taken by the instance. 00153 * 00154 * Call it before quitting the application. 00155 */ 00156 void CkSumsApp::CleanUpAppResources() 00157 { 00158 /* if (instanceChecker != NULL) 00159 delete instanceChecker; */ 00160 00161 // Clean-up the global preferences. 00162 AppPrefs::cleanupGlobal(); 00163 00164 // Deletes the configuration object. 00165 wxConfigBase* oldCfg = wxConfig::Set(NULL); 00166 if (oldCfg != NULL) 00167 delete oldCfg; 00168 } 00169 //--------------------------------------------------------------------------- 00170 00171 00172 /** 00173 * Adds all supported options to the given parser. 00174 * 00175 * Called from <CODE>wxApp::OnInit</CODE> and may be used to initialize the 00176 * parser with the command line options for this application. The base class 00177 * versions adds support for a few standard options only. 00178 * 00179 * @param parser The command line parser which be used to parse the command 00180 * line. 00181 */ 00182 void CkSumsApp::OnInitCmdLine(wxCmdLineParser& parser) 00183 { 00184 // Call the superclass OnInitCmdLine() to add "--help" and specific platform 00185 // options. 00186 wxApp::OnInitCmdLine(parser); 00187 00188 // Add the logo 00189 parser.SetLogo(::getAppName() + wxT("\n")); 00190 00191 // Add command line options 00192 parser.AddSwitch(wxT("V"), wxT("version"), _("show version information")); 00193 parser.AddSwitch(wxT("v"), wxT("verify"), _("verify sums against given list")); 00194 parser.AddOption(wxT("a"), wxT("append"), _("append files to a checksums' file"), wxCMD_LINE_VAL_STRING); 00195 parser.AddOption(wxT("c"), wxT("create"), _("create a checksums' file"), wxCMD_LINE_VAL_STRING); 00196 parser.AddOption(wxT("ct"), wxT("create-type"), _("type of the checksums' file to create"), wxCMD_LINE_VAL_STRING); 00197 parser.AddOption(wxT("fl"), wxT("files-list"), _("list(s) of files"), wxCMD_LINE_VAL_STRING); 00198 parser.AddSwitch(wxString(), wxT("delete-temp-list"), _("delete temporary lists of files")); 00199 parser.AddParam(_("file(s)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE); 00200 } 00201 //--------------------------------------------------------------------------- 00202 00203 00204 /** 00205 * Called after successfully parsing the command line. 00206 * 00207 * @param parser The command line parser which was used to parse the command 00208 * line. 00209 * @return <CODE>true</CODE> to continue normal execution or <CODE>false</CODE> 00210 * to return <CODE>false</CODE> from <CODE>wxApp::OnInit</CODE> thus 00211 * terminating the program. 00212 */ 00213 bool CkSumsApp::OnCmdLineParsed(wxCmdLineParser& parser) 00214 { 00215 // Process the standard command line options and ignore if it fails 00216 wxApp::OnCmdLineParsed(parser); 00217 00218 // Initializes the CmdLineOptions class 00219 return CmdLineOptions::init(parser); 00220 00221 // return true; // never abort the program. 00222 } 00223 //--------------------------------------------------------------------------- 00224 00225 00226 /** 00227 * Called if <CODE>-h</CODE> or <CODE>--help</CODE> option was specified. 00228 * 00229 * Called by <CODE>wxApp::OnInit</CODE> when the help option <CODE>-h</CODE> or 00230 * <CODE>--help</CODE> was specified on the command line. 00231 * The processing of <CODE>-h</CODE> and <CODE>--help</CODE> is made by 00232 * <CODE>wxApp::OnInitCmdLine</CODE> which is called by 00233 * <CODE>CkSumsApp::OnInitCmdLine</CODE>. 00234 * 00235 * @param parser The command line parser which was used to parse the command 00236 * line. 00237 * @return <CODE>true</CODE> to continue normal execution or <CODE>false</CODE> 00238 * to return <CODE>false</CODE> from <CODE>wxApp::OnInit</CODE> thus 00239 * terminating the program. 00240 */ 00241 bool CkSumsApp::OnCmdLineHelp(wxCmdLineParser& parser) 00242 { 00243 return wxApp::OnCmdLineHelp(parser); 00244 00245 /* #ifdef __WXMSW__ 00246 return true; // on windows never abort the program. 00247 #endif // __WXMSW__ 00248 return false; */ 00249 } 00250 //--------------------------------------------------------------------------- 00251 00252 00253 /** 00254 * Called when command line parsing fails. 00255 * 00256 * @param parser The command line parser which was used to parse the command 00257 * line. 00258 * @return <CODE>true</CODE> to continue normal execution or <CODE>false</CODE> 00259 * to return <CODE>false</CODE> from <CODE>wxApp::OnInit</CODE> thus 00260 * terminating the program. */ 00261 bool CkSumsApp::OnCmdLineError(wxCmdLineParser& parser) 00262 { 00263 return wxApp::OnCmdLineError(parser); // Don't print the usage 00264 00265 //return true; // never abort the program. 00266 } 00267 //--------------------------------------------------------------------------- 00268 00269 00270 /** 00271 * Initializes the locale 00272 */ 00273 void CkSumsApp::InitLocale() 00274 { 00275 wxString locName = AppPrefs::get()->readString(prLANGUAGE_NAME); 00276 bool canAddCatalog = true; // add the catalogs ? 00277 00278 #if defined(__WXMSW__) 00279 // Add the path of the application executable to the list of path to browse 00280 // to find the catalog files. 00281 appLocale.AddCatalogLookupPathPrefix(::getAppPath().GetPath(wxPATH_GET_VOLUME)); 00282 #endif // __WXMSW__ 00283 00284 appLocale.AddCatalogLookupPathPrefix(wxT(".")); 00285 00286 Languages langs; 00287 canAddCatalog = langs.setLocale(appLocale); 00288 00289 // Adds the language catalogs 00290 if (canAddCatalog) 00291 appLocale.AddCatalog(wxT("cksums")); 00292 } 00293 //---------------------------------------------------------------------------