1 /*
2     dspdfviewer - Dual Screen PDF Viewer for LaTeX-Beamer
3     Copyright (C) 2012  Danny Edel <mail@danny-edel.de>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 
21 #include <QApplication>
22 #include "debug.h"
23 #include "dspdfviewer.h"
24 #include "runtimeconfiguration.h"
25 #include <stdexcept>
26 #include <iostream>
27 #include <QMessageBox>
28 #include <QFileDialog>
29 #include <QTranslator>
30 #include <QLibraryInfo>
31 
32 #if defined ( _WIN32 )
33 	#pragma comment(linker, "/ENTRY:mainCRTStartup")
34 
35 	#if defined( NDEBUG )
36 		#pragma comment(linker, "/SUBSYSTEM:windows")
37 	#endif
38 
39 #endif
40 
main(int argc,char ** argv)41 int main(int argc, char** argv)
42 {
43 	QApplication app(argc, argv);
44 
45 	app.setApplicationName( QString::fromUtf8("dspdfviewer") );
46 	app.setApplicationVersion( QString::fromUtf8( DSPDFVIEWER_VERSION ) );
47 
48 	const auto locale = QLocale::system();
49 	const auto localeName = locale.name();
50 	const auto systemTranslationsPath =
51 		QLibraryInfo::location(QLibraryInfo::TranslationsPath);
52 
53 	QTranslator qtTranslator;
54 	DEBUGOUT << "Loading qt system translation for" << localeName
55 		<< "from" << systemTranslationsPath;
56 	if ( !qtTranslator.load(
57 			QString::fromUtf8( "qt_" ) + localeName, systemTranslationsPath )
58 			) {
59 		WARNINGOUT << "Failed to load qt translations for locale" << localeName;
60 	} else {
61 		app.installTranslator(&qtTranslator);
62 		DEBUGOUT << "Qt system translation loaded for" << localeName;
63 	}
64 
65 	QTranslator appTranslator;
66 	DEBUGOUT << "Loading dspdfviewer translation for current locale:" << localeName;
67 	if ( ! appTranslator.load(QString::fromUtf8(":/translations/dspdfviewer") ) ) {
68 		WARNINGOUT << "Failed to load dspdfviewer translation for current locale" << localeName;
69 	} else {
70 		app.installTranslator(&appTranslator);
71 		DEBUGOUT << "dspdfviewer translation loaded for" << localeName;
72 	}
73 
74 	/* If anything goes wrong, try to display the exception to the user.
75 	 * Its the least i can do.
76 	 */
77 	try {
78 		/* Register the meta-type so that rendered pages can be passed around
79 		* using Qt's event/callback system
80 		*/
81 		qRegisterMetaType< QSharedPointer<RenderedPage> >("QSharedPointer<RenderedPage>");
82 		RuntimeConfiguration rc(argc, argv);
83 
84 		if ( ! rc.filePathDefined() ) {
85 			rc.filePath( qPrintable( QFileDialog::getOpenFileName(
86 				nullptr, QFileDialog::tr("Load PDF from disk"), QString(), QFileDialog::tr("PDF (*.pdf)") ) ) );
87 		}
88 
89 		DSPDFViewer foo( rc );
90 		return app.exec();
91 	} catch ( std::exception& e ) {
92 		std::cerr << "----- FATAL ERROR -----" << std::endl
93 			<< qPrintable( QCoreApplication::translate("DSPDFViewer", "Dual-Screen PDF Viewer has encountered an error and cannot continue") ) << std::endl
94 			<< e.what() << std::endl;
95 
96 		QMessageBox errorMsg;
97 		errorMsg.setText( QCoreApplication::translate("DSPDFViewer", "Dual-Screen PDF Viewer has encountered an error and cannot continue") );
98 		errorMsg.setInformativeText( QString::fromLocal8Bit( e.what() ) );
99 		errorMsg.setDefaultButton(QMessageBox::Discard);
100 		errorMsg.setIcon( QMessageBox::Critical );
101 		errorMsg.exec();
102 		return 1;
103 	}
104 }
105