French translation update by Mustapha Senhaji.
[Rockbox.git] / rbutil / rbutilApp.cpp
blobc30e5b4aaa7f9f4267cd91f089c399911576ad84
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 = "";
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(_("Can't create data directory %s"),
52 buf.c_str());
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"));
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(), _("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 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnInit()"));
87 return TRUE;
90 int rbutilFrmApp::OnExit()
92 wxLogVerbose(wxT("=== begin rbUtilFrmApp::OnExit()"));
94 WriteUserConfig();
96 gv->logfile->Close();
97 /* Enabling this code causes the program to crash. I
98 * have no idea why. (possibly because deleting non existing objects ? :-) )
99 wxLog::DontCreateOnDemand();
100 // Free a bunch of structures.
101 delete gv->GlobalConfig;
102 delete gv->ErrStr;
103 delete gv->stdpaths;
104 delete gv->platform;
106 delete gv->logstderr;
107 delete gv->logchain;
108 delete gv->logfile;
109 delete gv->loggui;
111 wxLogVerbose(wxT("=== end rbUtilFrmApp::OnExit()"));
112 return 0;
115 bool rbutilFrmApp::ReadGlobalConfig(rbutilFrm* myFrame)
117 wxString buf, tmpstr, stack;
118 wxLogVerbose(wxT("=== begin rbutilFrmApp::ReadGlobalConfig(%p)"),
119 (void*) myFrame);
121 // 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() );
126 // 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 // }
136 wxFileInputStream* cfgis = new wxFileInputStream(buf);
138 if (!cfgis->CanRead()) {
139 gv->ErrStr = new wxString(_("Unable to open configuration file"));
140 return false;
143 gv->GlobalConfig = new wxFileConfig(*cfgis);
144 gv->GlobalConfigFile = buf;
146 unsigned int i = 0;
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)) {
152 wxString cur = tmpstr;
153 //gv->plat_id.Add(tmpstr);
154 gv->GlobalConfig->Read(buf.Format(wxT("/%s/name"),
155 cur.c_str()), &tmpstr);
156 gv->plat_name.Add(tmpstr);
157 gv->GlobalConfig->Read(buf.Format(wxT("/%s/platform"),
158 cur.c_str()), &tmpstr);
159 gv->plat_id.Add(tmpstr);
160 gv->GlobalConfig->Read(buf.Format(wxT("/%s/released"),
161 cur.c_str()), &tmpstr);
162 gv->plat_released.Add( (tmpstr == wxT("yes")) ? true : false ) ;
163 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);
179 i++;
182 gv->GlobalConfig->SetPath(wxT("/general"));
183 gv->GlobalConfig->Read(wxT("default_platform"), &tmpstr, wxT("cthulhu"));
185 for (i=0; i< gv->plat_id.GetCount(); i++) {
186 if (gv->plat_id[i] == tmpstr) gv->curplatnum = i;
189 gv->GlobalConfig->Read(wxT("last_release"), &tmpstr);
190 gv->last_release = tmpstr;
192 gv->GlobalConfig->Read(wxT("download_url"), &tmpstr);
193 gv->download_url = tmpstr;
195 gv->GlobalConfig->Read(wxT("daily_url"), &tmpstr);
196 gv->daily_url = tmpstr;
198 gv->GlobalConfig->Read(wxT("bleeding_url"), &tmpstr);
199 gv->bleeding_url = tmpstr;
201 gv->GlobalConfig->Read(wxT("server_conf_url"), &tmpstr);
202 gv->server_conf_url = tmpstr;
204 gv->GlobalConfig->Read(wxT("font_url"), &tmpstr);
205 gv->font_url = tmpstr;
207 gv->GlobalConfig->Read(wxT("prog_name"), &tmpstr);
208 gv->prog_name = tmpstr;
210 gv->GlobalConfig->Read(wxT("bootloader_url"), &tmpstr);
211 gv->bootloader_url = tmpstr;
213 #ifdef __WXMSW__
214 gv->curdestdir = wxT("D:\\");
215 #else
216 gv->curdestdir = wxT("/mnt");
217 #endif
218 gv->GlobalConfig->SetPath(stack);
220 delete buf; delete tmpstr;
221 wxLogVerbose(wxT("=== end rbutilFrmApp::ReadGlobalConfig()"));
222 return true;
225 void rbutilFrmApp::ReadUserConfig()
227 wxString buf, str, stack;
229 buf.Printf(wxT("%s" PATH_SEP "RockboxUtility.cfg"),
230 gv->AppDir.c_str());
232 if (wxFileExists(buf) )
234 gv->portable = true;
236 else
238 gv->portable = false;
239 buf.Printf(wxT("%s" PATH_SEP "%s"),
240 gv->stdpaths->GetUserDataDir().c_str(), wxT("RockboxUtility.cfg"));
243 gv->UserConfig = new wxFileConfig(wxEmptyString, wxEmptyString, buf);
244 gv->UserConfigFile = buf;
245 gv->UserConfig->Set(gv->UserConfig); // Store wxWidgets internal settings
246 stack = gv->UserConfig->GetPath();
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);
254 void rbutilFrmApp::WriteUserConfig()
256 gv->UserConfig->SetPath(wxT("/defaults"));
257 gv->UserConfig->Write(wxT("curdestdir"), gv->curdestdir);
258 gv->UserConfig->Write(wxT("curplatform"), gv->curplat);
260 delete gv->UserConfig;