Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[be54f4]: src / gui / main.cc Maximize Restore History

Download this file

main.cc    325 lines (290 with data), 10.4 kB

  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
/*
* PDFedit - free program for PDF document manipulation.
* Copyright (C) 2006, 2007, 2008 PDFedit team: Michal Hocko,
* Miroslav Jahoda,
* Jozef Misutka,
* Martin Petricek
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* 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 (in doc/LICENSE.GPL); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Project is hosted on http://sourceforge.net/projects/pdfedit
*/
/** @file
Main function<br>
Handle commandline options and then open editor window(s)
- unless commandline parameters specify something different
@author Martin Petricek
*/
#include "qtcompat.h"
#include "args.h"
#include "config.h"
#include "consolewindow.h"
#include "optionwindow.h"
#include "pdfeditwindow.h"
#include "settings.h"
#include "util.h"
#include "version.h"
#include "kernel/cannotation.h"
#include "kernel/pdfedit-core-dev.h"
#include <iostream>
#include <qapplication.h>
#include <qdir.h>
#include <qtranslator.h>
#include <qstringlist.h>
#include <stdlib.h>
#include <time.h>
#include <utils/debug.h>
using namespace std;
using namespace gui;
using namespace pdfobjects;
using namespace pdfobjects::utils;
/** Path to directory in which the binary resides */
QString appPath;
/** run in commandline mode? */
bool consoleMode=false;
/** Types of scripts to run (commandline mode) */
QStringList runScript;
/** Script parameters (script code/filename) (commandline mode) */
QStringList runScriptParam;
/** delete settings object (and save settings)
This function is called at application exit
*/
void saveSettings(void) {
delete globalSettings;//this causes settings to be saved to disk
}
/**
handle --help parameter<br>
Print help to STDOUT and exit
*/
void handleHelp(){
printHeader();
cout << util::convertFromUnicode(QObject::tr("Usage:"),util::CON)
<< " " << util::convertFromUnicode(binName,util::CON)
<< " [" << util::convertFromUnicode(QObject::tr("option(s)"),util::CON)
<< "] [" << util::convertFromUnicode(QObject::tr("files(s)"),util::CON) << "]" << endl;
handleHelpOptions();
}
/**
handle --version parameter<br>
Print version to STDOUT and exit
*/
void handleVersion(){
cout << PDFEDIT_VERSION << endl;
exit(0);
}
/**
handle -d [n] parameter<br>
Change debugging level
@param param Parameter passed
*/
void handleDebug(const QString &param){
util::setDebugLevel(param);
}
/**
handle -script [file] parameter<br>
Run script file
@param param Parameter passed
*/
void handleScript(const QString &param){
runScript+="script";
runScriptParam+=param;
}
/**
handle -run [file] parameter<br>
Run script file
@param param Parameter passed
*/
void handleRun(const QString &param){
runScript+="run";
runScriptParam+=param;
}
/**
handle -eval [code] parameter<br>
Evaluate script code
@param param Parameter passed
*/
void handleEval(const QString &param){
runScript+="eval";
runScriptParam+=param;
}
/**
handle -console parameter<br>
Run in commandline mode
*/
void handleConsole(){
consoleMode=true;
}
/**
handle -s [file] parameter<br>
Run script file + set console mode
@param param Parameter passed
*/
void handleScriptShort(const QString &param){
handleConsole();
handleScript(param);
}
/** QApplication - main application */
QApplication *q_App;
/**
Run scripts/code specified on commandline in given editor window
@param w Editor window
*/
void runCmdScripts(PdfEditWindow *w) {
//Run scripts given on commandline in given PDFedit window
for (unsigned int i=0;i<runScript.count();i++) {
QString typ=runScript[i];
guiPrintDbg(debug::DBG_DBG, "Script cmdline " << Q_OUT(typ) << " " << Q_OUT(runScriptParam[i]));
if (typ=="run") w->runFile(runScriptParam[i]);
else if (typ=="script") w->run(runScriptParam[i]);
else if (typ=="eval") w->eval(runScriptParam[i]);
else {
assert(0);
}
}
}
/**
Main - load settings, load translation and launch
main window(s) according to settings and commandline parameters
@param argc Argument count
@param argv Commandline arguments
@return Executable return code
*/
int main(int argc, char *argv[]){
//We need this so we can use things like --version and --help even without X11 connection
#ifdef Q_WS_X11
bool useGUI=getenv("DISPLAY")!=0;
#else
bool useGUI=true;
#endif
QApplication app(argc,argv,useGUI);
q_App=&app;
//Get application path
appPath=app.applicationDirPath();
appPath=QDir(appPath).absPath();
//Seed random number generator
srand(time(NULL));
//Translation support
QTranslator translator;
//Try LC_ALL, LC_MESSAGES and LANG - first one that is set is used
const char *env_lang=getenv("LC_ALL");
if (!env_lang) env_lang=getenv("LC_MESSAGES");
if (!env_lang) env_lang=getenv("LANG");
if (env_lang) {//LC_ALL/LC_MESSAGES/LANG variable is present in environment -> attempt to load localization
guiPrintDbg(debug::DBG_WARN,"LANG= " << env_lang);
QString lang=QString("pdfedit_")+env_lang;
//look for translation file in DATA_PATH
if (!translator.load(lang,QString(DATA_PATH)+"/lang")) {
//Look for translation file in config directory in $HOME
if (!translator.load(lang,QDir::home().path()+"/"+CONFIG_DIR+"/lang")) {
//Look in binary path - testing compilations and (possibly) windows builds
if (!translator.load(lang,appPath+"/lang")) {
guiPrintDbg(debug::DBG_WARN,"Translation file " << Q_OUT(lang) << " not found");
}
}
}
}
app.installTranslator(&translator);
// initializes all global configuration stuff
if(pdfedit_core_dev_init(&argc, &argv))
{
guiPrintDbg(debug::DBG_PANIC, "Unable to initialize pdfedit core");
return 1;
}
//parse commandline parameters
/*
Whole name of one parameter should not be prefix of another parameter, as unpredictable behaviour can occur,
for example -d and -def, in this case there is no way to recognize between -def option and -d option
with 'ef' as parameter and it is undefined what case of these two will be recognized
*/
optionHandler("--help",handleHelp,QObject::tr("Print help and exit"));
optionHandler("--version",handleVersion,QObject::tr("Print version and exit"));
optionHandlerParam("-d","n",handleDebug,QObject::tr("Set debug messages verbosity")+" "+QObject::tr("(n = -1 .. 5)"));
optionHandler("-console",handleConsole,QObject::tr("Run in commandline mode"));
optionHandlerParam("-script",QObject::tr("file"),handleScript,QObject::tr("Run script from script path or current directory if not found"));
optionHandlerParam("-run",QObject::tr("file"),handleRun,QObject::tr("Run script from current directory"));
optionHandlerParam("-eval",QObject::tr("code"),handleEval,QObject::tr("Evaluate script code"));
optionHandlerParam("-s",QObject::tr("file"),handleScriptShort,QObject::tr("Shortcut for")+" '-console -script ["+QObject::tr("file")+"]'");
optionHandler("--",handleStopOpt,QObject::tr("Stop processing options"));
QStringList params=handleParams(app.argc(),app.argv());
guiPrintDbg(debug::DBG_DBG,"Commandline parameters processed");
guiPrintDbg(debug::DBG_DBG,"App path: " << Q_OUT(appPath));
//load settings
globalSettings=Settings::getInstance();
globalSettings->setName("settings");
atexit(saveSettings);
guiPrintDbg(debug::DBG_DBG,"Settings loaded");
///Register annotation factories
boost::shared_ptr<pdfobjects::utils::IAnnotInitializator> null;
boost::shared_ptr<IAnnotInitializator> ia=CAnnotation::setAnnotInitializator(null);
boost::shared_ptr<UniversalAnnotInitializer> ua=boost::dynamic_pointer_cast<UniversalAnnotInitializer>(ia);
string text_t="Text";
boost::shared_ptr<pdfobjects::utils::IAnnotInitializator> textA(new TextAnnotInitializer());
boost::shared_ptr<pdfobjects::utils::IAnnotInitializator> linkA(new LinkAnnotInitializer());
ua->registerInitializer("Text",textA);
ua->registerInitializer("Link",linkA);
//We need to set default charset on the beginning
util::setDefaultCharset(globalSettings->read("editor/charset"));
if (consoleMode) {
//Running in console mode
ConsoleWindow c(params);
c.init();
//Run scripts given on commandline
for (unsigned int i=0;i<runScript.count();i++) {
QString typ=runScript[i];
guiPrintDbg(debug::DBG_DBG, "Script cmdline " << Q_OUT(typ) << " " << Q_OUT(runScriptParam[i]));
if (typ=="run") c.runFile(runScriptParam[i]);
else if (typ=="script") c.run(runScriptParam[i]);
else if (typ=="eval") c.eval(runScriptParam[i]);
else {
assert(0);
}
}
//Call "default console start function"
c.call("onConsoleStart");
//Scripts complete ... exit
exit(0);
}
if (!useGUI) {
// Up until now DISPLAY was optional. For running GUI, it is mandatory
printHeaderErr();
util::fatalError(QObject::tr("Environment variable DISPLAY is not set - cannot run GUI of " APP_NAME));
}
//style
QString style=globalSettings->read("gui/style","");
if (style!="") {
if (!app.setStyle(style)) globalSettings->write("gui/style",""); //No such style -> reset
}
guiPrintDbg(debug::DBG_DBG,"Style loaded");
//font
applyLookAndFeel(false);
guiPrintDbg(debug::DBG_DBG,"Font and style applied");
//open editor windows(s)
int nFiles=params.size();
if (nFiles) { //open files from cmdline
guiPrintDbg(debug::DBG_DBG,"Opening files from commandline");
for (QStringList::Iterator it=params.begin();it!=params.end();++it) {
QString parameter=*it;
guiPrintDbg(debug::DBG_INFO,"Opening parameter: " << Q_OUT(parameter));
runCmdScripts(PdfEditWindow::create(parameter));
}
} else { //no parameters
guiPrintDbg(debug::DBG_DBG,"Opening empty editor window");
runCmdScripts(PdfEditWindow::create());
}
QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
int ret = app.exec();
GlobalParams::destroyGlobalParams();
return ret;
}