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