Fix some naughty variable name reuse (hopefully correctly)
[Rockbox.git] / rbutil / rbutilApp.cpp
blob812142185fd0c8ed259ddb69eb131a815ef87b21
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: rbutilApp.cpp
11 * Copyright (C) 2005 Christi Alice Scarborough
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include "rbutilApp.h"
22 #include "bootloaders.h"
24 GlobalVars* gv = new GlobalVars();
26 IMPLEMENT_APP(rbutilFrmApp)
28 bool rbutilFrmApp::OnInit()
30 wxString buf = wxT("");
32 wxLogVerbose(wxT("=== begin rbutilFrmApp::Oninit()"));
35 gv->stdpaths = new wxStandardPaths();
37 // Get application directory
38 // DANGER! GetDataDir() doesn't portably return the application directory
39 // We want to use the form below instead, but not until wxWidgets 2.8 is
40 // released. *Datadir gives the wrong dir for this on Linux/Mac even on Wx2.8 *
41 gv->AppDir = gv->stdpaths->GetExecutablePath().BeforeLast(PATH_SEP_CHR);
42 // buf = gv->stdpaths->GetDataDir(); buf.Append(PATH_SEP);
43 // gv->AppDir = buf.BeforeLast(PATH_SEP_CHR).c_str();
45 buf = gv->stdpaths->GetUserDataDir();
46 if (! wxDirExists(buf) )
48 wxLogNull lognull;
49 if (! wxMkdir(buf, 0777))
51 wxLogFatalError(wxT("Can't create data directory %s"),
52 buf.c_str());
56 buf += PATH_SEP wxT("rbutil.log");
57 gv->logfile = new wxFFile(buf, wxT("w"));
58 if (! gv->logfile->IsOpened() )
59 wxLogFatalError(wxT("Unable to open log file"));
61 gv->loggui = new wxLogGui();
62 gv->loggui->SetActiveTarget(gv->loggui);
63 gv->loggui->SetLogLevel(wxLOG_Message);
64 gv->logchain = new wxLogChain(
65 gv->logstderr = new wxLogStderr(gv->logfile->fp() ) );
67 buf = buf.Left(buf.Len() - 10);
68 buf.Append(wxT("download"));
69 if (! wxDirExists(buf) ) wxMkdir(buf, 0777);
71 wxFileSystem::AddHandler(new wxInternetFSHandler);
72 wxFileSystem::AddHandler(new wxZipFSHandler);
74 if (!ReadGlobalConfig(NULL))
76 ERR_DIALOG(gv->ErrStr->GetData(), wxT("Rockbox Utility"));
77 return FALSE;
79 ReadUserConfig();
81 rbutilFrm *myFrame = new rbutilFrm(NULL);
82 SetTopWindow(myFrame);
83 myFrame->Show(TRUE);
85 initIpodpatcher(); // reserve mem for ipodpatcher
86 wxInitAllImageHandlers(); //init Image handlers
88 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnInit()"));
89 return TRUE;
92 int rbutilFrmApp::OnExit()
94 wxLogVerbose(wxT("=== begin rbUtilFrmApp::OnExit()"));
96 WriteUserConfig();
98 gv->logfile->Close();
99 /* Enabling this code causes the program to crash. I
100 * have no idea why. (possibly because deleting non existing objects ? :-) )
101 wxLog::DontCreateOnDemand();
102 // Free a bunch of structures.
103 delete gv->GlobalConfig;
104 delete gv->ErrStr;
105 delete gv->stdpaths;
106 delete gv->platform;
108 delete gv->logstderr;
109 delete gv->logchain;
110 delete gv->logfile;
111 delete gv->loggui;
113 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnExit()"));
114 return 0;
117 bool rbutilFrmApp::ReadGlobalConfig(rbutilFrm* myFrame)
119 wxString buf, tmpstr, stack;
120 wxLogVerbose(wxT("=== begin rbutilFrmApp::ReadGlobalConfig(%p)"),
121 (void*) myFrame);
123 // Cross-platform compatibility: look for rbutil.ini in the same dir as the
124 // executable before trying the standard data directory. On Windows these
125 // are of course the same directory.
126 buf.Printf(wxT("%s" PATH_SEP "rbutil.ini"), gv->AppDir.c_str() );
128 // if (! wxFileExists(buf) )
129 // {
130 // gv->ResourceDir = gv->stdpaths->GetResourcesDir();
131 // buf.Printf(wxT("%s" PATH_SEP "rbutil.ini"),
132 // gv->ResourceDir.c_str() );
133 // } else
134 // {
135 // gv->ResourceDir = gv->AppDir;
136 // }
138 wxFileInputStream* cfgis = new wxFileInputStream(buf);
140 if (!cfgis->CanRead()) {
141 gv->ErrStr = new wxString(wxT("Unable to open configuration file"));
142 return false;
145 gv->GlobalConfig = new wxFileConfig(*cfgis);
146 gv->GlobalConfigFile = buf;
148 unsigned int i = 0;
150 stack = gv->GlobalConfig->GetPath();
151 gv->GlobalConfig->SetPath(wxT("/platforms"));
152 while(gv->GlobalConfig->Read(buf.Format(wxT("platform%d"), i + 1),
153 &tmpstr)) {
154 wxString cur = tmpstr;
155 //gv->plat_id.Add(tmpstr);
156 gv->GlobalConfig->Read(buf.Format(wxT("/%s/name"),
157 cur.c_str()), &tmpstr);
158 gv->plat_name.Add(tmpstr);
159 gv->GlobalConfig->Read(buf.Format(wxT("/%s/platform"),
160 cur.c_str()), &tmpstr);
161 gv->plat_id.Add(tmpstr);
162 gv->GlobalConfig->Read(buf.Format(wxT("/%s/released"),
163 cur.c_str()), &tmpstr);
164 gv->plat_released.Add( (tmpstr == wxT("yes")) ? true : false ) ;
165 gv->GlobalConfig->Read(buf.Format(wxT("/%s/needsbootloader"),
166 cur.c_str()), &tmpstr);
167 gv->plat_needsbootloader.Add( (tmpstr == wxT("yes")) ? true : false ) ;
168 gv->GlobalConfig->Read(buf.Format(wxT("/%s/bootloadermethod"),
169 cur.c_str()), &tmpstr);
170 gv->plat_bootloadermethod.Add(tmpstr);
171 gv->GlobalConfig->Read(buf.Format(wxT("/%s/bootloadername"),
172 cur.c_str()), &tmpstr);
173 gv->plat_bootloadername.Add(tmpstr);
174 gv->GlobalConfig->Read(buf.Format(wxT("/%s/autodetect"),
175 cur.c_str()), &tmpstr);
176 gv->plat_autodetect.Add( (tmpstr == wxT("yes")) ? true : false ) ;
177 gv->GlobalConfig->Read(buf.Format(wxT("/%s/combinedname"),
178 cur.c_str()), &tmpstr);
179 gv->plat_combinedname.Add(tmpstr);
180 gv->GlobalConfig->Read(buf.Format(wxT("/%s/resolution"),
181 cur.c_str()), &tmpstr);
182 gv->plat_resolution.Add(tmpstr);
184 i++;
187 gv->GlobalConfig->SetPath(wxT("/general"));
188 gv->GlobalConfig->Read(wxT("default_platform"), &tmpstr, wxT("cthulhu"));
190 for (i=0; i< gv->plat_id.GetCount(); i++) {
191 if (gv->plat_id[i] == tmpstr) gv->curplatnum = i;
194 gv->GlobalConfig->Read(wxT("last_release"), &tmpstr);
195 gv->last_release = tmpstr;
197 gv->GlobalConfig->Read(wxT("download_url"), &tmpstr);
198 gv->download_url = tmpstr;
200 gv->GlobalConfig->Read(wxT("daily_url"), &tmpstr);
201 gv->daily_url = tmpstr;
203 gv->GlobalConfig->Read(wxT("bleeding_url"), &tmpstr);
204 gv->bleeding_url = tmpstr;
206 gv->GlobalConfig->Read(wxT("server_conf_url"), &tmpstr);
207 gv->server_conf_url = tmpstr;
209 gv->GlobalConfig->Read(wxT("font_url"), &tmpstr);
210 gv->font_url = tmpstr;
212 gv->GlobalConfig->Read(wxT("prog_name"), &tmpstr);
213 gv->prog_name = tmpstr;
215 gv->GlobalConfig->Read(wxT("bootloader_url"), &tmpstr);
216 gv->bootloader_url = tmpstr;
218 gv->GlobalConfig->Read(wxT("themes_url"), &tmpstr);
219 gv->themes_url = tmpstr;
221 #ifdef __WXMSW__
222 gv->curdestdir = wxT("D:\\");
223 #else
224 gv->curdestdir = wxT("/mnt");
225 #endif
226 gv->GlobalConfig->SetPath(stack);
228 wxLogVerbose(wxT("=== end rbutilFrmApp::ReadGlobalConfig()"));
229 return true;
232 void rbutilFrmApp::ReadUserConfig()
234 wxString buf, str, stack;
236 buf.Printf(wxT("%s" PATH_SEP "RockboxUtility.cfg"),
237 gv->AppDir.c_str());
239 if (wxFileExists(buf) )
241 gv->portable = true;
243 else
245 gv->portable = false;
246 buf.Printf(wxT("%s" PATH_SEP "%s"),
247 gv->stdpaths->GetUserDataDir().c_str(), wxT("RockboxUtility.cfg"));
250 gv->UserConfig = new wxFileConfig(wxEmptyString, wxEmptyString, buf);
251 gv->UserConfigFile = buf;
252 gv->UserConfig->Set(gv->UserConfig); // Store wxWidgets internal settings
253 stack = gv->UserConfig->GetPath();
255 gv->UserConfig->SetPath(wxT("/defaults"));
256 if (gv->UserConfig->Read(wxT("curdestdir"), &str) ) gv->curdestdir = str;
257 if (gv->UserConfig->Read(wxT("curplatform"), &str) ) gv->curplat = str;
258 gv->UserConfig->SetPath(stack);
261 void rbutilFrmApp::WriteUserConfig()
263 gv->UserConfig->SetPath(wxT("/defaults"));
264 gv->UserConfig->Write(wxT("curdestdir"), gv->curdestdir);
265 gv->UserConfig->Write(wxT("curplatform"), gv->curplat);
267 delete gv->UserConfig;