forked from MosheWagner/Orayta-QT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·321 lines (246 loc) · 8.11 KB
/
main.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
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Moshe Wagner. <[email protected]>
*/
#include <QApplication>
#ifdef MOBILE
#include "Mobile/mobileapp.h"
#else
#include "Desktop/desktopapp.h"
#endif
#include <QDir>
#include <QSettings>
#include "OraytaBase/functions.h"
#include "Desktop/about.h"
#include <QDebug>
#include <QTranslator>
#include <QSplashScreen>
#include <iostream>
#include <QFontDatabase>
#include <QDesktopWidget>
#include <QPainter>
#include <QRect>
#include <QFileInfo>
#include <QFileInfoList>
#include <QTime>
//For test only
//#define MOBILE
//Define location for program dirs
void initPaths()
{
QString defPath;
#ifdef Q_OS_ANDROID
QString sdcard= getenv("EXTERNAL_STORAGE");
defPath = sdcard+"/Orayta/Books/";
//set terget to assets folder
MAINPATH = "assets:/Orayta/";
//move the settings to where a user might be able to read them.
QSettings::setPath(QSettings::NativeFormat, QSettings::UserScope, defPath);
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, defPath);
#elif defined Q_OS_LINUX
defPath = "/usr/share/Orayta/Books/";
MAINPATH = "/usr/share/Orayta/";
#ifdef MOBILE_TEST
defPath = "Orayta_test_dir/";
//MAINPATH = defPath;
#endif
#elif defined Q_OS_WIN
defPath = QDir::rootPath() + "\\program files\\orayta\\books\\"; //TODO: ask yoch to correct this.
MAINPATH = QDir::rootPath() + "\\program files\\orayta\\";
#endif
QDir dir("Books/");
if (dir.exists()) BOOKPATH = dir.absolutePath() + "/" ;
else BOOKPATH = defPath;
// qDebug() << "bookpath:" << BOOKPATH ;
dir.setPath("UserData/");
if (dir.exists()) USERPATH = dir.absolutePath() + "/";
else
{
dir.mkdir(QDir::homePath() + "/.Orayta/");
USERPATH = QDir::homePath() + "/.Orayta/";
}
dir.setPath("Htmltmp/");
if (dir.exists()) TMPPATH = dir.absolutePath() + "/";
else
{
//Improved by Yoch. Thanks again.
dir.mkdir(QDir::tempPath() + "/Orayta/");
dir.mkdir(QDir::tempPath() + "/Orayta/Htmltmp/");
TMPPATH = QDir::tempPath() + "/Orayta/Htmltmp/";
}
dir.setPath("Pics/");
if ( !dir.exists() )
{
//UNIX ONLY:
#ifndef Q_WS_WIN //Yoch's idea. Thank you very much :-)
//Create symbolic link to the folder
//TODO: this should be solved in a different way. the html files should be corrected so the link is to the right place
system("ln -s -T /tmp/Orayta/Pics /usr/share/Orayta/Books/Pics 2> /dev/null");
#endif
}
}
//IZAR: adds a font to the application
void addFont(const QString &font)
{
int fontId = QFontDatabase::addApplicationFont(font);
if (fontId >= 0) {
// qDebug()<< "installed font successfuly: " << font;
return;
}
else
qDebug()<< "cant add font "<< font;
}
//IZAR: define location for fonts
void initFonts()
{
QList<QString> fonts;
// QString fontpath (":/fonts/");
QString fontpath (MAINPATH + "fonts/");
#ifdef Q_OS_ANDROID
fontpath = "/sdcard/Orayta/fonts/"; //assets not working well yet on alpha 3.4
#elif defined MOBILE_TEST
fontpath = "fonts/";
#endif
QDir fontDir(fontpath);
// qDebug() << "fontdir: " << fontDir;
QStringList filter("*.ttf");
QFileInfoList list = fontDir.entryInfoList(filter, QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name);
foreach(QFileInfo child, list){
addFont(child.filePath());
}
}
void initLang(QApplication *app)
{
//Read lang conf
QSettings settings("Orayta", "SingleUser");
settings.beginGroup("Confs");
bool systemlang = false;
#ifndef MOBILE
//system language disabled in mobile because it doesn't work.
systemlang = settings.value("systemLang",true).toBool();
#endif
if (systemlang) LANG = QLocale::languageToString(QLocale::system().language());
else (LANG = settings.value("lang","Hebrew").toString());
settings.endGroup();
extern QTranslator *translator;
translator = new QTranslator();
//Update path for translator installation
QString transPath (MAINPATH);
if (!translator->load(LANG + ".qm", ".")) translator->load(LANG + ".qm", transPath);
//Install as the app's translator
app->installTranslator(translator);
}
int main(int argc, char *argv[])
{
//Start the App
QApplication app(argc, argv);
//Deal with command line options
//TODO: add more if it's needed
QStringList args = app.arguments();
if (args.contains("-v") || args.contains("--version"))
{
cout << "Orayta, Hebrew books program, ";
cout << "Version " << VERSION << endl;
exit(0);
}
if (args.contains("-i") || args.contains("--import"))
{
}
//Build DB option chosen
if (args.contains("-D") || args.contains("--buildDB"))
{
int p = args.indexOf("-D");
if (p == -1) p = args.indexOf("--buildDB");
QString path = "";
if (args.size() > p + 1) path = args[p+1];
else
{
qDebug() << "Error! no path for DB bulding given.";
return 2;
}
qDebug()<<"creating DB for: "<<path;
QString lvlmapoutpath;
QString dboutpath;
QString pt = path;
if (path.endsWith(".txt"))
{
lvlmapoutpath = pt.replace(".txt", ".LMP");
dboutpath = pt.replace(".LMP", ".TDB");
}
if (path.endsWith(".TXT"))
{
lvlmapoutpath = pt.replace(".TXT", ".LMP");
dboutpath = pt.replace(".LMP", ".TDB");
}
GenerateSearchTextDB(path, dboutpath, lvlmapoutpath);
return 0;
}
//Define location for program dirs
initPaths();
//IZAR: add fonts to our app.
initFonts();
//Define the program's language
initLang(&app);
#ifndef MOBILE
//Show splash screen:
QPixmap pixmap(":/Images/Orayta.png");
QSplashScreen *splash = new QSplashScreen(pixmap);
splash->show();
QApplication::setOverrideCursor(Qt::WaitCursor);
app.processEvents();
QApplication::restoreOverrideCursor();
DesktopApp d;
d.show();
app.processEvents();
//Hide slpash screen
splash->finish(&d);
#endif
#ifdef MOBILE
//Show splash screen:
QPixmap basePixmap(":/Images/spalsh-portrait.png");
//get desktop size
QDesktopWidget* desktop = QApplication::desktop();
const QRect desktopRect = desktop->availableGeometry();
QSplashScreen *splash;
//Scale splash image to fit screen size:
//create a resized version of image
QPixmap resizedPixmap (basePixmap.scaledToWidth(desktopRect.width()));
//create a background empty rectangle
QPixmap splashPixmap(desktopRect.width(), desktopRect.height());
splashPixmap.fill(QColor("black"));
//put our image in the middle of background
QPainter p;
p.begin(&splashPixmap);
QRect targetRect((splashPixmap.width() - resizedPixmap.width())/2,
(splashPixmap.height() - resizedPixmap.height())/2,
resizedPixmap.width(), resizedPixmap.height());
p.drawPixmap(targetRect, resizedPixmap);
p.end();
splash = new QSplashScreen(splashPixmap);
splash->show();
//splash->showMessage("Loading Orayta...", Qt::AlignLeft, Qt::white);
// fix for certain devices which don't support hebrew chars well.
app.setFont(QFont("DejaVu Sans"));
MobileApp m;
#ifdef Q_OS_ANDROID
//Make sure app is maximized!
m.showMaximized();
#endif
m.show();
app.processEvents();
//Hide slpash screen
splash->finish(&m);
#endif
return app.exec();
}