- fix an endless autodetection loop when no user configuration is present
[Rockbox.git] / rbutil / rbutilApp.cpp
blob1901a62df10f4031f14fbd094d4f242dcc80295f
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 wxInitAllImageHandlers(); //init Image handlers
82 initIpodpatcher(); // reserve mem for ipodpatcher
83 initSansaPatcher(); // reserve mem for sansapatcher
85 rbutilFrm *myFrame = new rbutilFrm(NULL);
86 SetTopWindow(myFrame);
88 myFrame->Show(TRUE);
90 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnInit()"));
91 return TRUE;
94 int rbutilFrmApp::OnExit()
96 wxLogVerbose(wxT("=== begin rbUtilFrmApp::OnExit()"));
98 WriteUserConfig();
100 gv->logfile->Close();
101 /* Enabling this code causes the program to crash. I
102 * have no idea why. (possibly because deleting non existing objects ? :-) )
103 wxLog::DontCreateOnDemand();
104 // Free a bunch of structures.
105 delete gv->GlobalConfig;
106 delete gv->ErrStr;
107 delete gv->stdpaths;
108 delete gv->platform;
110 delete gv->logstderr;
111 delete gv->logchain;
112 delete gv->logfile;
113 delete gv->loggui;
115 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnExit()"));
116 return 0;
119 bool rbutilFrmApp::ReadGlobalConfig(rbutilFrm* myFrame)
121 wxString buf, tmpstr, stack;
122 wxLogVerbose(wxT("=== begin rbutilFrmApp::ReadGlobalConfig(%p)"),
123 (void*) myFrame);
125 // Cross-platform compatibility: look for rbutil.ini in then in the app dir
126 // then in the user config dir (linux ~/) and
127 // then config dir (linux /etc/ )
129 buf = gv->AppDir + wxT("" PATH_SEP "rbutil.ini");
130 if (! wxFileExists(buf) )
132 buf = gv->stdpaths->GetUserConfigDir()
133 + wxT("" PATH_SEP ".rbutil" PATH_SEP "rbutil.ini");
134 if (! wxFileExists(buf) )
136 buf = gv->stdpaths->GetConfigDir()
137 + wxT("" PATH_SEP "rbutil.ini");
141 if (! wxFileExists(buf) )
143 gv->ErrStr = new wxString(wxT("Configuration file doesnt exist!"));
144 return false;
148 wxFileInputStream* cfgis = new wxFileInputStream(buf);
150 if (!cfgis->CanRead()) {
151 gv->ErrStr = new wxString(wxT("Unable to open configuration file"));
152 return false;
155 gv->GlobalConfig = new wxFileConfig(*cfgis);
156 gv->GlobalConfigFile = buf;
158 unsigned int i = 0;
160 stack = gv->GlobalConfig->GetPath();
161 gv->GlobalConfig->SetPath(wxT("/platforms"));
162 while(gv->GlobalConfig->Read(buf.Format(wxT("platform%d"), i + 1),
163 &tmpstr)) {
164 wxString cur = tmpstr;
165 //gv->plat_id.Add(tmpstr);
166 gv->GlobalConfig->Read(buf.Format(wxT("/%s/name"),
167 cur.c_str()), &tmpstr);
168 gv->plat_name.Add(tmpstr);
169 gv->GlobalConfig->Read(buf.Format(wxT("/%s/platform"),
170 cur.c_str()), &tmpstr);
171 gv->plat_id.Add(tmpstr);
172 gv->GlobalConfig->Read(buf.Format(wxT("/%s/released"),
173 cur.c_str()), &tmpstr);
174 gv->plat_released.Add( (tmpstr == wxT("yes")) ? true : false ) ;
175 gv->GlobalConfig->Read(buf.Format(wxT("/%s/needsbootloader"),
176 cur.c_str()), &tmpstr);
177 gv->plat_needsbootloader.Add( (tmpstr == wxT("yes")) ? true : false ) ;
178 gv->GlobalConfig->Read(buf.Format(wxT("/%s/bootloadermethod"),
179 cur.c_str()), &tmpstr);
180 gv->plat_bootloadermethod.Add(tmpstr);
181 gv->GlobalConfig->Read(buf.Format(wxT("/%s/bootloadername"),
182 cur.c_str()), &tmpstr);
183 gv->plat_bootloadername.Add(tmpstr);
184 gv->GlobalConfig->Read(buf.Format(wxT("/%s/resolution"),
185 cur.c_str()), &tmpstr);
186 gv->plat_resolution.Add(tmpstr);
187 gv->GlobalConfig->Read(buf.Format(wxT("/%s/manualname"),
188 cur.c_str()), &tmpstr);
189 gv->plat_manualname.Add(tmpstr);
191 i++;
194 gv->GlobalConfig->SetPath(wxT("/general"));
195 gv->GlobalConfig->Read(wxT("default_platform"), &tmpstr, wxT("cthulhu"));
198 gv->GlobalConfig->Read(wxT("last_release"), &tmpstr);
199 gv->last_release = tmpstr;
201 gv->GlobalConfig->Read(wxT("download_url"), &tmpstr);
202 gv->download_url = tmpstr;
204 gv->GlobalConfig->Read(wxT("daily_url"), &tmpstr);
205 gv->daily_url = tmpstr;
207 gv->GlobalConfig->Read(wxT("bleeding_url"), &tmpstr);
208 gv->bleeding_url = tmpstr;
210 gv->GlobalConfig->Read(wxT("server_conf_url"), &tmpstr);
211 gv->server_conf_url = tmpstr;
213 gv->GlobalConfig->Read(wxT("font_url"), &tmpstr);
214 gv->font_url = tmpstr;
216 gv->GlobalConfig->Read(wxT("prog_name"), &tmpstr);
217 gv->prog_name = tmpstr;
219 gv->GlobalConfig->Read(wxT("bootloader_url"), &tmpstr);
220 gv->bootloader_url = tmpstr;
222 gv->GlobalConfig->Read(wxT("themes_url"), &tmpstr);
223 gv->themes_url = tmpstr;
225 gv->GlobalConfig->Read(wxT("manual_url"), &tmpstr);
226 gv->manual_url = tmpstr;
228 gv->GlobalConfig->Read(wxT("doom_url"), &tmpstr);
229 gv->doom_url = tmpstr;
231 #ifdef __WXMSW__
232 gv->curdestdir = wxT("D:\\");
233 #else
234 gv->curdestdir = wxT("/mnt");
235 #endif
236 gv->GlobalConfig->SetPath(stack);
238 wxLogVerbose(wxT("=== end rbutilFrmApp::ReadGlobalConfig()"));
239 return true;
242 void rbutilFrmApp::ReadUserConfig()
244 wxString buf, str, stack;
246 buf = gv->AppDir + wxT("" PATH_SEP "RockboxUtility.cfg");
248 if (wxFileExists(buf) )
250 gv->portable = true;
252 else
254 gv->portable = false;
255 buf = gv->stdpaths->GetUserDataDir()
256 + wxT("" PATH_SEP "RockboxUtility.cfg");
259 gv->UserConfig = new wxFileConfig(wxEmptyString, wxEmptyString, buf);
260 gv->UserConfigFile = buf;
261 gv->UserConfig->Set(gv->UserConfig); // Store wxWidgets internal settings
262 stack = gv->UserConfig->GetPath();
264 gv->UserConfig->SetPath(wxT("/defaults"));
265 if (gv->UserConfig->Read(wxT("curdestdir"), &str) ) gv->curdestdir = str;
266 if (gv->UserConfig->Read(wxT("curplatform"), &str) ) gv->curplat = str;
267 if (gv->UserConfig->Read(wxT("curfirmware"), &str) ) gv->curfirmware = str;
268 if (gv->UserConfig->Read(wxT("proxy_url"), &str) ) gv->proxy_url = str;
270 if (gv->UserConfig->Read(wxT("pathToTts"), &str) ) gv->pathToTts = str;
271 if (gv->UserConfig->Read(wxT("pathToEnc"), &str) ) gv->pathToEnc = str;
272 gv->UserConfig->SetPath(stack);
275 void rbutilFrmApp::WriteUserConfig()
277 gv->UserConfig->SetPath(wxT("/defaults"));
278 gv->UserConfig->Write(wxT("curdestdir"), gv->curdestdir);
279 gv->UserConfig->Write(wxT("curplatform"), gv->curplat);
280 gv->UserConfig->Write(wxT("curfirmware"), gv->curfirmware);
281 gv->UserConfig->Write(wxT("proxy_url"), gv->proxy_url);
282 gv->UserConfig->Write(wxT("pathToTts"), gv->pathToTts);
283 gv->UserConfig->Write(wxT("pathToEnc"), gv->pathToEnc);
285 delete gv->UserConfig;