forked from vslavik/poedit
/
edapp.cpp
386 lines (323 loc) · 10.8 KB
/
edapp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* This file is part of Poedit (http://www.poedit.net)
*
* Copyright (C) 1999-2010 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include <wx/wxprec.h>
#include <wx/wx.h>
#include <wx/config.h>
#include <wx/fs_zip.h>
#include <wx/image.h>
#include <wx/cmdline.h>
#include <wx/log.h>
#include <wx/xrc/xmlres.h>
#include <wx/xrc/xh_all.h>
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include <wx/sysopt.h>
#ifdef USE_SPARKLE
#include <Sparkle/SUCarbonAPI.h>
#include "osx/userdefaults.h"
#endif // USE_SPARKLE
#ifdef __WXMSW__
#include <winsparkle.h>
#endif
#if !wxUSE_UNICODE
#error "Unicode build of wxWidgets is required by Poedit"
#endif
#include "edapp.h"
#include "edframe.h"
#include "manager.h"
#include "prefsdlg.h"
#include "parser.h"
#include "chooselang.h"
#include "icons.h"
#include "version.h"
#include "transmem.h"
#include "utility.h"
IMPLEMENT_APP(PoeditApp);
wxString PoeditApp::GetAppPath() const
{
#if defined(__UNIX__)
wxString home;
if (!wxGetEnv(_T("POEDIT_PREFIX"), &home))
home = wxString::FromAscii(POEDIT_PREFIX);
return home;
#elif defined(__WXMSW__)
wxString exedir;
wxFileName::SplitPath(wxStandardPaths::Get().GetExecutablePath(),
&exedir, NULL, NULL);
wxFileName fn = wxFileName::DirName(exedir);
fn.RemoveLastDir();
return fn.GetFullPath();
#else
#error "Unsupported platform!"
#endif
}
wxString PoeditApp::GetAppVersion() const
{
return wxString::FromAscii(POEDIT_VERSION);
}
static wxArrayString gs_filesToOpen;
extern void InitXmlResource();
bool PoeditApp::OnInit()
{
if (!wxApp::OnInit())
return false;
#if defined(__WXMAC__) && wxCHECK_VERSION(2,8,5)
wxSystemOptions::SetOption(wxMAC_TEXTCONTROL_USE_SPELL_CHECKER, 1);
#endif
#ifdef __WXMAC__
SetExitOnFrameDelete(false);
#endif
#if defined(__UNIX__) && !defined(__WXMAC__)
wxString home = wxGetHomeDir() + _T("/");
// create Poedit cfg dir, move ~/.poedit to ~/.poedit/config
// (upgrade from older versions of Poedit which used ~/.poedit file)
if (!wxDirExists(home + _T(".poedit")))
{
if (wxFileExists(home + _T(".poedit")))
wxRenameFile(home + _T(".poedit"), home + _T(".poedit2"));
wxMkdir(home + _T(".poedit"));
if (wxFileExists(home + _T(".poedit2")))
wxRenameFile(home + _T(".poedit2"), home + _T(".poedit/config"));
}
#endif
SetVendorName(_T("Vaclav Slavik"));
SetAppName(_T("Poedit"));
#if defined(__WXMAC__)
#define CFG_FILE (wxStandardPaths::Get().GetUserConfigDir() + _T("/net.poedit.Poedit.cfg"))
#elif defined(__UNIX__)
#define CFG_FILE (home + _T(".poedit/config"))
#else
#define CFG_FILE wxEmptyString
#endif
#ifdef __WXMAC__
// upgrade from the old location of config file:
wxString oldcfgfile = wxStandardPaths::Get().GetUserConfigDir() + _T("/poedit.cfg");
if (wxFileExists(oldcfgfile) && !wxFileExists(CFG_FILE))
{
wxRenameFile(oldcfgfile, CFG_FILE);
}
#endif
wxConfigBase::Set(
new wxConfig(wxEmptyString, wxEmptyString, CFG_FILE, wxEmptyString,
wxCONFIG_USE_GLOBAL_FILE | wxCONFIG_USE_LOCAL_FILE));
wxConfigBase::Get()->SetExpandEnvVars(false);
wxImage::AddHandler(new wxGIFHandler);
wxImage::AddHandler(new wxPNGHandler);
wxXmlResource::Get()->InitAllHandlers();
InitXmlResource();
SetDefaultCfg(wxConfig::Get());
wxArtProvider::Insert(new PoeditArtProvider);
#ifdef __WXMAC__
wxLocale::AddCatalogLookupPathPrefix(
wxStandardPaths::Get().GetResourcesDir() + _T("/locale"));
#else
wxLocale::AddCatalogLookupPathPrefix(GetAppPath() + _T("/share/locale"));
#endif
m_locale.Init(GetUILanguage());
m_locale.AddCatalog(_T("poedit"));
#ifdef __WXMSW__
// Italian version of Windows uses just "?" for "Help" menu. This is
// correctly handled by wxWidgets catalogs, but Poedit catalog has "Help"
// entry too, so we add wxmsw.mo catalog again so that it takes
// precedence over poedit.mo:
m_locale.AddCatalog(_T("wxmsw"));
#endif
#ifdef __WXMAC__
// so that help menu is correctly merged with system-provided menu
// (see http://sourceforge.net/tracker/index.php?func=detail&aid=1600747&group_id=9863&atid=309863)
s_macHelpMenuTitleName = _("&Help");
#endif
#ifdef USE_TRANSMEM
// NB: It's important to do this before TM is used for the first time.
TranslationMemory::MoveLegacyDbIfNeeded();
#endif
// NB: opening files or creating empty window is handled differently on
// Macs, using MacOpenFile() and MacNewFile(), so don't create empty
// window if no files are given on command line; but still support
// passing files on command line
if (!gs_filesToOpen.empty())
{
for (size_t i = 0; i < gs_filesToOpen.GetCount(); i++)
OpenFile(gs_filesToOpen[i]);
gs_filesToOpen.clear();
}
#ifndef __WXMAC__
else
{
OpenNewFile();
}
#endif // !__WXMAC__
#ifdef USE_SPARKLE
// Remove config key for Sparkle < 1.5.
UserDefaults::RemoveValue("SUCheckAtStartup");
SUSparkleInitializeForCarbon();
#endif // USE_SPARKLE
#ifdef __WXMSW__
const char *appcast = "https://dl.updatica.com/poedit-win/appcast";
if ( GetAppVersion().Contains(_T("beta")) ||
GetAppVersion().Contains(_T("rc")) )
{
// Beta versions use unstable feed.
appcast = "https://dl.updatica.com/poedit-win/appcast/beta";
}
win_sparkle_set_appcast_url(appcast);
win_sparkle_init();
#endif
return true;
}
int PoeditApp::OnExit()
{
#ifdef __WXMSW__
win_sparkle_cleanup();
#endif
return wxApp::OnExit();
}
void PoeditApp::OpenNewFile()
{
if (wxConfig::Get()->Read(_T("manager_startup"), (long)false))
ManagerFrame::Create()->Show(true);
else
PoeditFrame::Create(wxEmptyString);
}
void PoeditApp::OpenFile(const wxString& name)
{
PoeditFrame::Create(name);
}
void PoeditApp::SetDefaultParsers(wxConfigBase *cfg)
{
ParsersDB pdb;
bool changed = false;
wxString defaultsVersion = cfg->Read(_T("Parsers/DefaultsVersion"),
_T("1.2.x"));
pdb.Read(cfg);
// Add parsers for languages supported by gettext itself (but only if the
// user didn't already add language with this name himself):
static struct
{
const wxChar *name;
const wxChar *exts;
} s_gettextLangs[] = {
{ _T("C/C++"), _T("*.c;*.cpp;*.h;*.hpp;*.cc;*.C;*.cxx;*.hxx") },
{ _T("C#"), _T("*.cs") },
{ _T("Java"), _T("*.java") },
{ _T("Perl"), _T("*.pl") },
{ _T("PHP"), _T("*.php") },
{ _T("Python"), _T("*.py") },
{ _T("TCL"), _T("*.tcl") },
{ NULL, NULL }
};
for (size_t i = 0; s_gettextLangs[i].name != NULL; i++)
{
// if this lang is already registered, don't overwrite it:
if (pdb.FindParser(s_gettextLangs[i].name) != -1)
continue;
wxString langflag;
if ( wxStrcmp(s_gettextLangs[i].name, _T("C/C++")) == 0 )
langflag = _T(" --language=C++");
else
langflag = wxString(_T(" --language=")) + s_gettextLangs[i].name;
// otherwise add new parser:
Parser p;
p.Name = s_gettextLangs[i].name;
p.Extensions = s_gettextLangs[i].exts;
p.Command = wxString(_T("xgettext")) + langflag + _T(" --force-po -o %o %C %K %F");
p.KeywordItem = _T("-k%k");
p.FileItem = _T("%f");
p.CharsetItem = _T("--from-code=%c");
pdb.Add(p);
changed = true;
}
// If upgrading Poedit to 1.2.4, add dxgettext parser for Delphi:
#ifdef __WINDOWS__
if (defaultsVersion == _T("1.2.x"))
{
Parser p;
p.Name = _T("Delphi (dxgettext)");
p.Extensions = _T("*.pas;*.dpr;*.xfm;*.dfm");
p.Command = _T("dxgettext --so %o %F");
p.KeywordItem = wxEmptyString;
p.FileItem = _T("%f");
pdb.Add(p);
changed = true;
}
#endif
// If upgrading Poedit to 1.2.5, update C++ parser to handle --from-code:
if (defaultsVersion == _T("1.2.x") || defaultsVersion == _T("1.2.4"))
{
int cpp = pdb.FindParser(_T("C/C++"));
if (cpp != -1)
{
if (pdb[cpp].Command == _T("xgettext --force-po -o %o %K %F"))
{
pdb[cpp].Command = _T("xgettext --force-po -o %o %C %K %F");
pdb[cpp].CharsetItem = _T("--from-code=%c");
changed = true;
}
}
}
if (changed)
{
pdb.Write(cfg);
cfg->Write(_T("Parsers/DefaultsVersion"), GetAppVersion());
}
}
void PoeditApp::SetDefaultCfg(wxConfigBase *cfg)
{
SetDefaultParsers(cfg);
if (cfg->Read(_T("version"), wxEmptyString) == GetAppVersion()) return;
if (cfg->Read(_T("TM/search_paths"), wxEmptyString).empty())
{
wxString paths;
#if defined(__UNIX__)
paths = wxGetHomeDir() + _T(":/usr/share/locale:/usr/local/share/locale");
#elif defined(__WXMSW__)
paths = _T("C:");
#endif
cfg->Write(_T("TM/search_paths"), paths);
}
cfg->Write(_T("version"), GetAppVersion());
}
namespace
{
const wxChar *CL_KEEP_TEMP_FILES = _T("keep-temp-files");
}
void PoeditApp::OnInitCmdLine(wxCmdLineParser& parser)
{
wxApp::OnInitCmdLine(parser);
parser.AddSwitch(_T(""), CL_KEEP_TEMP_FILES,
_("don't delete temporary files (for debugging)"));
parser.AddParam(_T("catalog.po"), wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE);
}
bool PoeditApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
if (!wxApp::OnCmdLineParsed(parser))
return false;
if ( parser.Found(CL_KEEP_TEMP_FILES) )
TempDirectory::KeepFiles();
for (size_t i = 0; i < parser.GetParamCount(); i++)
gs_filesToOpen.Add(parser.GetParam(i));
return true;
}