blob: c30e5b4aaa7f9f4267cd91f089c399911576ad84 [file] [log] [blame]
Christi Scarborough3b0b27e2006-12-13 21:11:24 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: rbutilApp.cpp
10 *
11 * Copyright (C) 2005 Christi Alice Scarborough
12 *
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.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
Christi Scarborough0957acd2006-12-11 21:25:37 +000020
21#include "rbutilApp.h"
Dominik Wengere863d592007-02-21 20:27:14 +000022#include "bootloaders.h"
Christi Scarborough0957acd2006-12-11 21:25:37 +000023
24GlobalVars* gv = new GlobalVars();
25
26IMPLEMENT_APP(rbutilFrmApp)
27
28bool rbutilFrmApp::OnInit()
29{
30 wxString buf = "";
31
32 wxLogVerbose(wxT("=== begin rbutilFrmApp::Oninit()"));
33
Dominik Wengere863d592007-02-21 20:27:14 +000034
Christi Scarborough0957acd2006-12-11 21:25:37 +000035 gv->stdpaths = new wxStandardPaths();
Christi Scarborough3b0b27e2006-12-13 21:11:24 +000036
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
Dominik Wengere863d592007-02-21 20:27:14 +000040 // released. *Datadir gives the wrong dir for this on Linux/Mac even on Wx2.8 *
Christi Scarboroughf938d8c2006-12-14 06:11:29 +000041 gv->AppDir = gv->stdpaths->GetExecutablePath().BeforeLast(PATH_SEP_CHR);
Dominik Wengere863d592007-02-21 20:27:14 +000042 // buf = gv->stdpaths->GetDataDir(); buf.Append(PATH_SEP);
43 // gv->AppDir = buf.BeforeLast(PATH_SEP_CHR).c_str();
Christi Scarborough3b0b27e2006-12-13 21:11:24 +000044
Christi Scarborough0957acd2006-12-11 21:25:37 +000045 buf = gv->stdpaths->GetUserDataDir();
46 if (! wxDirExists(buf) )
47 {
48 wxLogNull lognull;
Dave Chapman27b2e752006-12-12 16:01:27 +000049 if (! wxMkdir(buf, 0777))
Christi Scarborough0957acd2006-12-11 21:25:37 +000050 {
51 wxLogFatalError(_("Can't create data directory %s"),
52 buf.c_str());
53 }
54 }
55
56 buf += wxT(PATH_SEP "rbutil.log");
57 gv->logfile = new wxFFile(buf, "w");
58 if (! gv->logfile->IsOpened() )
59 wxLogFatalError(_("Unable to open log file"));
60
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() ) );
66
67 buf = buf.Left(buf.Len() - 10);
68 buf.Append(wxT("download"));
Dave Chapman27b2e752006-12-12 16:01:27 +000069 if (! wxDirExists(buf) ) wxMkdir(buf, 0777);
Christi Scarborough0957acd2006-12-11 21:25:37 +000070
71 wxFileSystem::AddHandler(new wxInternetFSHandler);
72 wxFileSystem::AddHandler(new wxZipFSHandler);
73
Christi Scarborough3b0b27e2006-12-13 21:11:24 +000074 if (!ReadGlobalConfig(NULL))
Christi Scarborough0957acd2006-12-11 21:25:37 +000075 {
76 ERR_DIALOG(gv->ErrStr->GetData(), _("Rockbox Utility"));
77 return FALSE;
78 }
Christi Scarborough0957acd2006-12-11 21:25:37 +000079 ReadUserConfig();
80
Christi Scarborough3b0b27e2006-12-13 21:11:24 +000081 rbutilFrm *myFrame = new rbutilFrm(NULL);
82 SetTopWindow(myFrame);
Christi Scarborough0957acd2006-12-11 21:25:37 +000083 myFrame->Show(TRUE);
84
Dominik Wengere863d592007-02-21 20:27:14 +000085 initIpodpatcher(); // reserve mem for ipodpatcher
Christi Scarborough0957acd2006-12-11 21:25:37 +000086 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnInit()"));
87 return TRUE;
88}
89
90int rbutilFrmApp::OnExit()
91{
92 wxLogVerbose(wxT("=== begin rbUtilFrmApp::OnExit()"));
93
94 WriteUserConfig();
95
96 gv->logfile->Close();
97 /* Enabling this code causes the program to crash. I
Dominik Wengere863d592007-02-21 20:27:14 +000098 * have no idea why. (possibly because deleting non existing objects ? :-) )
Christi Scarborough0957acd2006-12-11 21:25:37 +000099 wxLog::DontCreateOnDemand();
100 // Free a bunch of structures.
101 delete gv->GlobalConfig;
102 delete gv->ErrStr;
103 delete gv->stdpaths;
104 delete gv->platform;
105
106 delete gv->logstderr;
107 delete gv->logchain;
108 delete gv->logfile;
109 delete gv->loggui;
110*/
111 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnExit()"));
112 return 0;
113}
114
115bool rbutilFrmApp::ReadGlobalConfig(rbutilFrm* myFrame)
116{
117 wxString buf, tmpstr, stack;
118 wxLogVerbose(wxT("=== begin rbutilFrmApp::ReadGlobalConfig(%p)"),
119 (void*) myFrame);
120
Christi Scarborough3b0b27e2006-12-13 21:11:24 +0000121 // Cross-platform compatibility: look for rbutil.ini in the same dir as the
122 // executable before trying the standard data directory. On Windows these
123 // are of course the same directory.
124 buf.Printf(wxT("%s" PATH_SEP "rbutil.ini"), gv->AppDir.c_str() );
125
Dominik Wengere863d592007-02-21 20:27:14 +0000126// if (! wxFileExists(buf) )
127// {
128// gv->ResourceDir = gv->stdpaths->GetResourcesDir();
129// buf.Printf(wxT("%s" PATH_SEP "rbutil.ini"),
130// gv->ResourceDir.c_str() );
131// } else
132// {
133// gv->ResourceDir = gv->AppDir;
134// }
Christi Scarborough3b0b27e2006-12-13 21:11:24 +0000135
Christi Scarborough0957acd2006-12-11 21:25:37 +0000136 wxFileInputStream* cfgis = new wxFileInputStream(buf);
137
138 if (!cfgis->CanRead()) {
139 gv->ErrStr = new wxString(_("Unable to open configuration file"));
140 return false;
141 }
142
143 gv->GlobalConfig = new wxFileConfig(*cfgis);
Christi Scarborough3b0b27e2006-12-13 21:11:24 +0000144 gv->GlobalConfigFile = buf;
Christi Scarborough0957acd2006-12-11 21:25:37 +0000145
146 unsigned int i = 0;
147
148 stack = gv->GlobalConfig->GetPath();
149 gv->GlobalConfig->SetPath(wxT("/platforms"));
150 while(gv->GlobalConfig->Read(buf.Format(wxT("platform%d"), i + 1),
151 &tmpstr)) {
Dominik Wengere863d592007-02-21 20:27:14 +0000152 wxString cur = tmpstr;
153 //gv->plat_id.Add(tmpstr);
Christi Scarborough0957acd2006-12-11 21:25:37 +0000154 gv->GlobalConfig->Read(buf.Format(wxT("/%s/name"),
Dominik Wengere863d592007-02-21 20:27:14 +0000155 cur.c_str()), &tmpstr);
Christi Scarborough0957acd2006-12-11 21:25:37 +0000156 gv->plat_name.Add(tmpstr);
Dominik Wengere863d592007-02-21 20:27:14 +0000157 gv->GlobalConfig->Read(buf.Format(wxT("/%s/platform"),
158 cur.c_str()), &tmpstr);
159 gv->plat_id.Add(tmpstr);
Christi Scarborough0957acd2006-12-11 21:25:37 +0000160 gv->GlobalConfig->Read(buf.Format(wxT("/%s/released"),
Dominik Wengere863d592007-02-21 20:27:14 +0000161 cur.c_str()), &tmpstr);
Christi Scarborough0957acd2006-12-11 21:25:37 +0000162 gv->plat_released.Add( (tmpstr == wxT("yes")) ? true : false ) ;
Dominik Wengere863d592007-02-21 20:27:14 +0000163 gv->GlobalConfig->Read(buf.Format(wxT("/%s/needsbootloader"),
164 cur.c_str()), &tmpstr);
165 gv->plat_needsbootloader.Add( (tmpstr == wxT("yes")) ? true : false ) ;
166 gv->GlobalConfig->Read(buf.Format(wxT("/%s/bootloadermethod"),
167 cur.c_str()), &tmpstr);
168 gv->plat_bootloadermethod.Add(tmpstr);
169 gv->GlobalConfig->Read(buf.Format(wxT("/%s/bootloadername"),
170 cur.c_str()), &tmpstr);
171 gv->plat_bootloadername.Add(tmpstr);
172 gv->GlobalConfig->Read(buf.Format(wxT("/%s/autodetect"),
173 cur.c_str()), &tmpstr);
174 gv->plat_autodetect.Add( (tmpstr == wxT("yes")) ? true : false ) ;
175 gv->GlobalConfig->Read(buf.Format(wxT("/%s/combinedname"),
176 cur.c_str()), &tmpstr);
177 gv->plat_combinedname.Add(tmpstr);
178
Christi Scarborough0957acd2006-12-11 21:25:37 +0000179 i++;
180 }
181
182 gv->GlobalConfig->SetPath(wxT("/general"));
183 gv->GlobalConfig->Read(wxT("default_platform"), &tmpstr, wxT("cthulhu"));
184
185 for (i=0; i< gv->plat_id.GetCount(); i++) {
186 if (gv->plat_id[i] == tmpstr) gv->curplatnum = i;
187 }
188
189 gv->GlobalConfig->Read(wxT("last_release"), &tmpstr);
190 gv->last_release = tmpstr;
191
192 gv->GlobalConfig->Read(wxT("download_url"), &tmpstr);
193 gv->download_url = tmpstr;
194
195 gv->GlobalConfig->Read(wxT("daily_url"), &tmpstr);
196 gv->daily_url = tmpstr;
197
198 gv->GlobalConfig->Read(wxT("bleeding_url"), &tmpstr);
199 gv->bleeding_url = tmpstr;
200
201 gv->GlobalConfig->Read(wxT("server_conf_url"), &tmpstr);
202 gv->server_conf_url = tmpstr;
203
204 gv->GlobalConfig->Read(wxT("font_url"), &tmpstr);
205 gv->font_url = tmpstr;
206
207 gv->GlobalConfig->Read(wxT("prog_name"), &tmpstr);
208 gv->prog_name = tmpstr;
209
Dominik Wengere863d592007-02-21 20:27:14 +0000210 gv->GlobalConfig->Read(wxT("bootloader_url"), &tmpstr);
211 gv->bootloader_url = tmpstr;
212
Christi Scarborough0957acd2006-12-11 21:25:37 +0000213#ifdef __WXMSW__
214 gv->curdestdir = wxT("D:\\");
215#else
216 gv->curdestdir = wxT("/mnt");
217#endif
218 gv->GlobalConfig->SetPath(stack);
219
220 delete buf; delete tmpstr;
221 wxLogVerbose(wxT("=== end rbutilFrmApp::ReadGlobalConfig()"));
222 return true;
223}
224
225void rbutilFrmApp::ReadUserConfig()
226{
227 wxString buf, str, stack;
228
Christi Scarborough3b0b27e2006-12-13 21:11:24 +0000229 buf.Printf(wxT("%s" PATH_SEP "RockboxUtility.cfg"),
230 gv->AppDir.c_str());
231
232 if (wxFileExists(buf) )
233 {
234 gv->portable = true;
235 }
236 else
237 {
238 gv->portable = false;
239 buf.Printf(wxT("%s" PATH_SEP "%s"),
240 gv->stdpaths->GetUserDataDir().c_str(), wxT("RockboxUtility.cfg"));
241 }
242
Christi Scarborough0957acd2006-12-11 21:25:37 +0000243 gv->UserConfig = new wxFileConfig(wxEmptyString, wxEmptyString, buf);
Christi Scarborough3b0b27e2006-12-13 21:11:24 +0000244 gv->UserConfigFile = buf;
Christi Scarborough50036692006-12-14 03:55:15 +0000245 gv->UserConfig->Set(gv->UserConfig); // Store wxWidgets internal settings
Christi Scarborough0957acd2006-12-11 21:25:37 +0000246 stack = gv->UserConfig->GetPath();
247
248 gv->UserConfig->SetPath(wxT("/defaults"));
249 if (gv->UserConfig->Read(wxT("curdestdir"), &str) ) gv->curdestdir = str;
250 if (gv->UserConfig->Read(wxT("curplatform"), &str) ) gv->curplat = str;
251 gv->UserConfig->SetPath(stack);
252}
253
254void rbutilFrmApp::WriteUserConfig()
255{
256 gv->UserConfig->SetPath(wxT("/defaults"));
257 gv->UserConfig->Write(wxT("curdestdir"), gv->curdestdir);
258 gv->UserConfig->Write(wxT("curplatform"), gv->curplat);
259
260 delete gv->UserConfig;
261
262}
263