1 | /* |
---|
2 | |
---|
3 | Copyright 2012-2013 Adam Reichold |
---|
4 | Copyright 2014 Dorian Scholz |
---|
5 | Copyright 2012 Michał Trybus |
---|
6 | Copyright 2013 Chris Young |
---|
7 | |
---|
8 | This file is part of qpdfview. |
---|
9 | |
---|
10 | qpdfview is free software: you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation, either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | qpdfview is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | |
---|
23 | */ |
---|
24 | |
---|
25 | #include <iostream> |
---|
26 | |
---|
27 | #include <QApplication> |
---|
28 | #include <QDebug> |
---|
29 | #include <QDir> |
---|
30 | #include <QInputDialog> |
---|
31 | #include <QLibraryInfo> |
---|
32 | #include <QMessageBox> |
---|
33 | #include <QScopedPointer> |
---|
34 | #include <QTranslator> |
---|
35 | |
---|
36 | #ifdef WITH_DBUS |
---|
37 | |
---|
38 | #include <QDBusInterface> |
---|
39 | #include <QDBusReply> |
---|
40 | |
---|
41 | #endif // WITH_DBUS |
---|
42 | |
---|
43 | #ifdef WITH_SYNCTEX |
---|
44 | |
---|
45 | #include <synctex_parser.h> |
---|
46 | |
---|
47 | #endif // WITH_SYNCTEX |
---|
48 | |
---|
49 | #include "renderparam.h" |
---|
50 | #include "documentview.h" |
---|
51 | #include "database.h" |
---|
52 | #include "mainwindow.h" |
---|
53 | |
---|
54 | #ifdef WITH_SIGNALS |
---|
55 | |
---|
56 | #include "signalhandler.h" |
---|
57 | |
---|
58 | #endif // WITH_SIGNALS |
---|
59 | |
---|
60 | #ifdef __amigaos4__ |
---|
61 | |
---|
62 | #include <proto/dos.h> |
---|
63 | #include <workbench/startup.h> |
---|
64 | |
---|
65 | const char* __attribute__((used)) stack_cookie = "\0$STACK:500000\0"; |
---|
66 | |
---|
67 | #endif // __amigaos4__ |
---|
68 | |
---|
69 | namespace |
---|
70 | { |
---|
71 | |
---|
72 | using namespace qpdfview; |
---|
73 | |
---|
74 | struct File |
---|
75 | { |
---|
76 | QString filePath; |
---|
77 | int page; |
---|
78 | |
---|
79 | QString sourceName; |
---|
80 | int sourceLine; |
---|
81 | int sourceColumn; |
---|
82 | QRectF enclosingBox; |
---|
83 | |
---|
84 | File() : filePath(), page(-1), sourceName(), sourceLine(-1), sourceColumn(-1), enclosingBox() {} |
---|
85 | |
---|
86 | }; |
---|
87 | |
---|
88 | enum ExitStatus |
---|
89 | { |
---|
90 | ExitOk = 0, |
---|
91 | ExitUnknownArgument = 1, |
---|
92 | ExitIllegalArgument = 2, |
---|
93 | ExitInconsistentArguments = 3, |
---|
94 | ExitDBusError = 4 |
---|
95 | }; |
---|
96 | |
---|
97 | bool unique = false; |
---|
98 | bool quiet = false; |
---|
99 | |
---|
100 | QString instanceName; |
---|
101 | QString searchText; |
---|
102 | |
---|
103 | QList< File > files; |
---|
104 | |
---|
105 | MainWindow* mainWindow = 0; |
---|
106 | |
---|
107 | void loadTranslators() |
---|
108 | { |
---|
109 | QTranslator* toolkitTranslator = new QTranslator(qApp); |
---|
110 | QTranslator* applicationTranslator = new QTranslator(qApp); |
---|
111 | |
---|
112 | #if QT_VERSION >= QT_VERSION_CHECK(4,8,0) |
---|
113 | |
---|
114 | if(toolkitTranslator->load(QLocale::system(), "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { qApp->installTranslator(toolkitTranslator); } |
---|
115 | |
---|
116 | // if(applicationTranslator->load(QLocale::system(), "qpdfview", "_", QDir(QApplication::applicationDirPath()).filePath("data"))) { qApp->installTranslator(applicationTranslator); } |
---|
117 | if(applicationTranslator->load(QLocale::system(), "qpdfview", "_", "/usr/share/terepaima/")) { qApp->installTranslator(applicationTranslator); } |
---|
118 | else if(applicationTranslator->load(QLocale::system(), "qpdfview", "_", DATA_INSTALL_PATH)) { qApp->installTranslator(applicationTranslator); } |
---|
119 | //if(applicationTranslator->load(QLocale::system(), "TibisayPDFView", "_", QDir(QApplication::applicationDirPath()).filePath("data"))) { qApp->installTranslator(applicationTranslator); } |
---|
120 | //else if(applicationTranslator->load(QLocale::system(), "TibisayPDFView", "_", DATA_INSTALL_PATH)) { qApp->installTranslator(applicationTranslator); } |
---|
121 | |
---|
122 | #else |
---|
123 | |
---|
124 | if(toolkitTranslator->load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { qApp->installTranslator(toolkitTranslator); } |
---|
125 | |
---|
126 | if(applicationTranslator->load("terepaima" + QLocale::system().name(), QDir(QApplication::applicationDirPath()).filePath("data"))) { qApp->installTranslator(applicationTranslator); } |
---|
127 | else if(applicationTranslator->load("terepaima" + QLocale::system().name(), DATA_INSTALL_PATH)) { qApp->installTranslator(applicationTranslator); } |
---|
128 | // if(applicationTranslator->load("TibisayPDFView_" + QLocale::system().name(), QDir(QApplication::applicationDirPath()).filePath("data"))) { qApp->installTranslator(applicationTranslator); } |
---|
129 | // else if(applicationTranslator->load("TibisayPDFView_" + QLocale::system().name(), DATA_INSTALL_PATH)) { qApp->installTranslator(applicationTranslator); } |
---|
130 | #endif // QT_VERSION |
---|
131 | } |
---|
132 | |
---|
133 | void parseCommandLineArguments() |
---|
134 | { |
---|
135 | bool instanceNameIsNext = false; |
---|
136 | bool searchTextIsNext = false; |
---|
137 | bool noMoreOptions = false; |
---|
138 | |
---|
139 | QRegExp fileAndPageRegExp("(.+)#(\\d+)"); |
---|
140 | QRegExp fileAndSourceRegExp("(.+)#src:(.+):(\\d+):(\\d+)"); |
---|
141 | QRegExp instanceNameRegExp("[A-Za-z_]+[A-Za-z0-9_]*"); |
---|
142 | |
---|
143 | QStringList arguments = QApplication::arguments(); |
---|
144 | |
---|
145 | if(!arguments.isEmpty()) |
---|
146 | { |
---|
147 | arguments.removeFirst(); |
---|
148 | } |
---|
149 | |
---|
150 | foreach(const QString& argument, arguments) |
---|
151 | { |
---|
152 | if(instanceNameIsNext) |
---|
153 | { |
---|
154 | if(argument.isEmpty()) |
---|
155 | { |
---|
156 | qCritical() << QObject::tr("An empty instance name is not allowed."); |
---|
157 | exit(ExitIllegalArgument); |
---|
158 | } |
---|
159 | |
---|
160 | instanceNameIsNext = false; |
---|
161 | instanceName = argument; |
---|
162 | } |
---|
163 | else if(searchTextIsNext) |
---|
164 | { |
---|
165 | if(argument.isEmpty()) |
---|
166 | { |
---|
167 | qCritical() << QObject::tr("An empty search text is not allowed."); |
---|
168 | exit(ExitIllegalArgument); |
---|
169 | } |
---|
170 | |
---|
171 | searchTextIsNext = false; |
---|
172 | searchText = argument; |
---|
173 | } |
---|
174 | else if(!noMoreOptions && argument.startsWith("--")) |
---|
175 | { |
---|
176 | if(argument == QLatin1String("--unique")) |
---|
177 | { |
---|
178 | unique = true; |
---|
179 | } |
---|
180 | else if(argument == QLatin1String("--quiet")) |
---|
181 | { |
---|
182 | quiet = true; |
---|
183 | } |
---|
184 | else if(argument == QLatin1String("--instance")) |
---|
185 | { |
---|
186 | instanceNameIsNext = true; |
---|
187 | } |
---|
188 | else if(argument == QLatin1String("--search")) |
---|
189 | { |
---|
190 | searchTextIsNext = true; |
---|
191 | } |
---|
192 | else if(argument == QLatin1String("--choose-instance")) |
---|
193 | { |
---|
194 | bool ok = false; |
---|
195 | const QString chosenInstanceName = QInputDialog::getItem(0, QObject::tr("Choose instance"), QObject::tr("Instance:"), Database::instance()->loadInstanceNames(), 0, true, &ok); |
---|
196 | |
---|
197 | if(ok) |
---|
198 | { |
---|
199 | instanceName = chosenInstanceName; |
---|
200 | } |
---|
201 | } |
---|
202 | else if(argument == QLatin1String("--help")) |
---|
203 | { |
---|
204 | std::cout << "Usage: qpdfview [options] [--] [file[#page]] [file[#src:name:line:column]] ..." << std::endl |
---|
205 | << std::endl |
---|
206 | << "Available options:" << std::endl |
---|
207 | << " --help Show this information" << std::endl |
---|
208 | << " --quiet Suppress warning messages when opening files" << std::endl |
---|
209 | << " --search text Search for text in the current tab" << std::endl |
---|
210 | << " --unique Open files as tabs in unique window" << std::endl |
---|
211 | << " --unique --instance name Open files as tabs in named instance" << std::endl |
---|
212 | << " --unique --choose-instance Open files as tabs after choosing an instance name" << std::endl |
---|
213 | << std::endl |
---|
214 | << "Please report bugs at \"https://launchpad.net/qpdfview\"." << std::endl; |
---|
215 | |
---|
216 | exit(ExitOk); |
---|
217 | } |
---|
218 | else if(argument == QLatin1String("--")) |
---|
219 | { |
---|
220 | noMoreOptions = true; |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | qCritical() << QObject::tr("Unknown command-line option '%1'.").arg(argument); |
---|
225 | exit(ExitUnknownArgument); |
---|
226 | } |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | File file; |
---|
231 | |
---|
232 | if(fileAndPageRegExp.exactMatch(argument)) |
---|
233 | { |
---|
234 | file.filePath = fileAndPageRegExp.cap(1); |
---|
235 | file.page = fileAndPageRegExp.cap(2).toInt(); |
---|
236 | } |
---|
237 | else if(fileAndSourceRegExp.exactMatch(argument)) |
---|
238 | { |
---|
239 | file.filePath = fileAndSourceRegExp.cap(1); |
---|
240 | file.sourceName = fileAndSourceRegExp.cap(2); |
---|
241 | file.sourceLine = fileAndSourceRegExp.cap(3).toInt(); |
---|
242 | file.sourceColumn = fileAndSourceRegExp.cap(4).toInt(); |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | file.filePath = argument; |
---|
247 | } |
---|
248 | |
---|
249 | files.append(file); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | if(instanceNameIsNext) |
---|
254 | { |
---|
255 | qCritical() << QObject::tr("Using '--instance' requires an instance name."); |
---|
256 | exit(ExitInconsistentArguments); |
---|
257 | } |
---|
258 | |
---|
259 | if(!unique && !instanceName.isEmpty()) |
---|
260 | { |
---|
261 | qCritical() << QObject::tr("Using '--instance' is not allowed without using '--unique'."); |
---|
262 | exit(ExitInconsistentArguments); |
---|
263 | } |
---|
264 | |
---|
265 | if(!instanceName.isEmpty() && !instanceNameRegExp.exactMatch(instanceName)) |
---|
266 | { |
---|
267 | qCritical() << QObject::tr("An instance name must only contain the characters \"[A-Z][a-z][0-9]_\" and must not begin with a digit."); |
---|
268 | exit(ExitIllegalArgument); |
---|
269 | } |
---|
270 | |
---|
271 | if(searchTextIsNext) |
---|
272 | { |
---|
273 | qCritical() << QObject::tr("Using '--search' requires a search text."); |
---|
274 | exit(ExitInconsistentArguments); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | void parseWorkbenchExtendedSelection(int argc, char** argv) |
---|
279 | { |
---|
280 | #ifdef __amigaos4__ |
---|
281 | |
---|
282 | if(argc == 0) |
---|
283 | { |
---|
284 | const int pathLength = 1024; |
---|
285 | const QScopedArrayPointer< char > filePath(new char[pathLength]); |
---|
286 | |
---|
287 | const struct WBStartup* wbStartup = reinterpret_cast< struct WBStartup* >(argv); |
---|
288 | |
---|
289 | for(int index = 1; index < wbStartup->sm_NumArgs; ++index) |
---|
290 | { |
---|
291 | const struct WBArg* wbArg = wbStartup->sm_ArgList + index; |
---|
292 | |
---|
293 | if((wbArg->wa_Lock) && (*wbArg->wa_Name)) |
---|
294 | { |
---|
295 | IDOS->DevNameFromLock(wbArg->wa_Lock, filePath.data(), pathLength, DN_FULLPATH); |
---|
296 | IDOS->AddPart(filePath.data(), wbArg->wa_Name, pathLength); |
---|
297 | |
---|
298 | File file; |
---|
299 | file.filePath = filePath.data(); |
---|
300 | |
---|
301 | files.append(file); |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | #else |
---|
307 | |
---|
308 | Q_UNUSED(argc); |
---|
309 | Q_UNUSED(argv); |
---|
310 | |
---|
311 | #endif // __amigaos4__ |
---|
312 | } |
---|
313 | |
---|
314 | void resolveSourceReferences() |
---|
315 | { |
---|
316 | #ifdef WITH_SYNCTEX |
---|
317 | |
---|
318 | for(int index = 0; index < files.count(); ++index) |
---|
319 | { |
---|
320 | File& file = files[index]; |
---|
321 | |
---|
322 | if(!file.sourceName.isNull()) |
---|
323 | { |
---|
324 | synctex_scanner_t scanner = synctex_scanner_new_with_output_file(file.filePath.toLocal8Bit(), 0, 1); |
---|
325 | |
---|
326 | if(scanner != 0) |
---|
327 | { |
---|
328 | if(synctex_display_query(scanner, file.sourceName.toLocal8Bit(), file.sourceLine, file.sourceColumn) > 0) |
---|
329 | { |
---|
330 | for(synctex_node_t node = synctex_next_result(scanner); node != 0; node = synctex_next_result(scanner)) |
---|
331 | { |
---|
332 | int page = synctex_node_page(node); |
---|
333 | QRectF enclosingBox(synctex_node_box_visible_h(node), synctex_node_box_visible_v(node), synctex_node_box_visible_width(node), synctex_node_box_visible_height(node)); |
---|
334 | |
---|
335 | if(file.page != page) |
---|
336 | { |
---|
337 | file.page = page; |
---|
338 | file.enclosingBox = enclosingBox; |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | file.enclosingBox = file.enclosingBox.united(enclosingBox); |
---|
343 | } |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | synctex_scanner_free(scanner); |
---|
348 | } |
---|
349 | else |
---|
350 | { |
---|
351 | qWarning() << QObject::tr("SyncTeX data for '%1' could not be found.").arg(file.filePath); |
---|
352 | } |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | #endif // WITH_SYNCTEX |
---|
357 | } |
---|
358 | |
---|
359 | void activateUniqueInstance() |
---|
360 | { |
---|
361 | qApp->setObjectName(instanceName); |
---|
362 | |
---|
363 | #ifdef WITH_DBUS |
---|
364 | |
---|
365 | if(unique) |
---|
366 | { |
---|
367 | QString serviceName = QApplication::organizationDomain(); |
---|
368 | |
---|
369 | if(!instanceName.isEmpty()) |
---|
370 | { |
---|
371 | serviceName.append('.'); |
---|
372 | serviceName.append(instanceName); |
---|
373 | } |
---|
374 | |
---|
375 | QScopedPointer< QDBusInterface > interface(new QDBusInterface(serviceName, "/MainWindow", "local.qpdfview.MainWindow", QDBusConnection::sessionBus())); |
---|
376 | |
---|
377 | if(interface->isValid()) |
---|
378 | { |
---|
379 | interface->call("raiseAndActivate"); |
---|
380 | |
---|
381 | foreach(const File& file, files) |
---|
382 | { |
---|
383 | QDBusReply< bool > reply = interface->call("jumpToPageOrOpenInNewTab", QFileInfo(file.filePath).absoluteFilePath(), file.page, true, file.enclosingBox, quiet); |
---|
384 | |
---|
385 | if(!reply.isValid()) |
---|
386 | { |
---|
387 | qCritical() << QDBusConnection::sessionBus().lastError().message(); |
---|
388 | |
---|
389 | exit(ExitDBusError); |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | if(!searchText.isEmpty()) |
---|
394 | { |
---|
395 | interface->call("startSearch", searchText); |
---|
396 | } |
---|
397 | |
---|
398 | exit(ExitOk); |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | mainWindow = new MainWindow(); |
---|
403 | |
---|
404 | new MainWindowAdaptor(mainWindow); |
---|
405 | |
---|
406 | if(!QDBusConnection::sessionBus().registerService(serviceName)) |
---|
407 | { |
---|
408 | qCritical() << QDBusConnection::sessionBus().lastError().message(); |
---|
409 | |
---|
410 | delete mainWindow; |
---|
411 | exit(ExitDBusError); |
---|
412 | } |
---|
413 | |
---|
414 | if(!QDBusConnection::sessionBus().registerObject("/MainWindow", mainWindow)) |
---|
415 | { |
---|
416 | qCritical() << QDBusConnection::sessionBus().lastError().message(); |
---|
417 | |
---|
418 | delete mainWindow; |
---|
419 | exit(ExitDBusError); |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | return; |
---|
424 | } |
---|
425 | else |
---|
426 | { |
---|
427 | mainWindow = new MainWindow(); |
---|
428 | } |
---|
429 | |
---|
430 | #else |
---|
431 | |
---|
432 | mainWindow = new MainWindow(); |
---|
433 | |
---|
434 | #endif // WITH_DBUS |
---|
435 | } |
---|
436 | |
---|
437 | void prepareSignalHandler() |
---|
438 | { |
---|
439 | #ifdef WITH_SIGNALS |
---|
440 | |
---|
441 | if(SignalHandler::prepareSignals()) |
---|
442 | { |
---|
443 | SignalHandler* signalHandler = new SignalHandler(mainWindow); |
---|
444 | |
---|
445 | QObject::connect(signalHandler, SIGNAL(sigIntReceived()), mainWindow, SLOT(close())); |
---|
446 | QObject::connect(signalHandler, SIGNAL(sigTermReceived()), mainWindow, SLOT(close())); |
---|
447 | } |
---|
448 | else |
---|
449 | { |
---|
450 | qWarning() << QObject::tr("Could not prepare signal handler."); |
---|
451 | } |
---|
452 | |
---|
453 | #endif // WITH_SIGNALS |
---|
454 | } |
---|
455 | |
---|
456 | } // anonymous |
---|
457 | |
---|
458 | int main(int argc, char** argv) |
---|
459 | { |
---|
460 | qRegisterMetaType< QList< QRectF > >("QList<QRectF>"); |
---|
461 | qRegisterMetaType< Rotation >("Rotation"); |
---|
462 | qRegisterMetaType< RenderParam >("RenderParam"); |
---|
463 | |
---|
464 | parseWorkbenchExtendedSelection(argc, argv); |
---|
465 | |
---|
466 | QApplication application(argc, argv); |
---|
467 | QApplication::setOrganizationDomain("local.terepaima"); |
---|
468 | // QApplication::setOrganizationDomain("local.qpdfview"); |
---|
469 | // QApplication::setOrganizationName("qpdfview"); |
---|
470 | // QApplication::setApplicationName("qpdfview"); |
---|
471 | QApplication::setOrganizationName("terepaima"); |
---|
472 | QApplication::setApplicationName("terepaima"); |
---|
473 | |
---|
474 | QApplication::setApplicationVersion(APPLICATION_VERSION); |
---|
475 | |
---|
476 | //QApplication::setWindowIcon(QIcon(":icons/qpdfview.svg")); |
---|
477 | QApplication::setWindowIcon(QIcon(":icons/terepaima_logoCircular.svg")); |
---|
478 | |
---|
479 | loadTranslators(); |
---|
480 | |
---|
481 | parseCommandLineArguments(); |
---|
482 | |
---|
483 | resolveSourceReferences(); |
---|
484 | |
---|
485 | activateUniqueInstance(); |
---|
486 | |
---|
487 | prepareSignalHandler(); |
---|
488 | |
---|
489 | mainWindow->show(); |
---|
490 | mainWindow->setAttribute(Qt::WA_DeleteOnClose); |
---|
491 | |
---|
492 | foreach(const File& file, files) |
---|
493 | { |
---|
494 | mainWindow->jumpToPageOrOpenInNewTab(file.filePath, file.page, true, file.enclosingBox, quiet); |
---|
495 | } |
---|
496 | |
---|
497 | if(!searchText.isEmpty()) |
---|
498 | { |
---|
499 | mainWindow->startSearch(searchText); |
---|
500 | } |
---|
501 | |
---|
502 | return application.exec(); |
---|
503 | } |
---|