32 #include <QApplication>
34 #include <QTranslator>
39 #include <QPluginLoader>
40 #include <QMessageBox>
41 #include <QLibraryInfo>
44 #include <QFontDatabase>
75 #ifndef KLF_RESOURCES_ENVNAM
76 #define KLF_RESOURCES_ENVNAM "KLF_RESOURCES"
81 #define EXIT_ERR_FILEINPUT 100
82 #define EXIT_ERR_FILESAVE 101
83 #define EXIT_ERR_OPT 102
216 {
"gs", 1, NULL,
OPT_GS },
235 ftty = fopen(
"/dev/tty",
"w");
241 fprintf(ftty,
"Interrupt\n");
242 if (ftty != stderr) fprintf(stderr,
"*** Interrupt\n");
244 static long last_sigint_time = 0;
247 bool isInsisted = (curtime - last_sigint_time <= 2);
248 if (!isInsisted && qApp != NULL) {
250 last_sigint_time = curtime;
252 fprintf(ftty,
"Exiting\n");
253 if (ftty != stderr) fprintf(stderr,
"*** Exiting\n");
257 if (sig == SIGSEGV) {
258 fprintf(ftty,
"Segmentation Fault :-(\n");
259 if (ftty != stderr) fprintf(stderr,
"** Segmentation Fault :-( **\n");
264 signal(SIGSEGV, SIG_DFL);
298 fprintf(fout,
"D: %s\n", msg);
303 fprintf(fout,
"Warning: %s\n", msg);
310 #if defined Q_WS_WIN && defined KLF_DEBUG
311 # define SAFECOUNTER_NUM 10
313 if (qApp != NULL && qApp->inherits(
"QApplication")) {
314 static int safecounter = SAFECOUNTER_NUM;
315 if (safecounter-- >= 0) {
318 QString(
"KLatexFormula System Warning:\n%1")
322 if (safecounter == -1) {
324 QString(
"Shown %1 system warnings. Will stop displaying them.").arg(SAFECOUNTER_NUM));
327 if (safecounter < -2) safecounter = -2;
332 fprintf(fout,
"Error: %s\n", msg);
335 if (qApp != NULL && qApp->inherits(
"QApplication")) {
338 "[[KLF's Qt Message Handler: dialog text]]")
344 fprintf(fout,
"Fatal: %s\n", msg);
347 if (qApp != NULL && qApp->inherits(
"QApplication")) {
349 "[[KLF's Qt Message Handler: dialog title]]"),
350 QObject::tr(
"KLatexFormula System FATAL ERROR:\n%1",
351 "[[KLF's Qt Message Handler: dialog text]]")
357 fprintf(fout,
"?????: %s\n", msg);
380 foreach(
QString s, qttempfiles) {
404 if (latexinput != NULL && strlen(latexinput) != 0) {
407 if (input != NULL && strlen(input) != 0) {
410 if ( fname ==
"-" ) {
411 if ( ! f.
open(stdin, QIODevice::ReadOnly) ) {
412 qCritical(
"%s", qPrintable(
QObject::tr(
"Can't read standard input (!)")));
417 if ( ! f.
open(QIODevice::ReadOnly) ) {
418 qCritical(
"%s", qPrintable(
QObject::tr(
"Can't read input file `%1'.").arg(fname)));
428 if (!qApp->inherits(
"QApplication")) {
430 qPrintable(
QObject::tr(
"--paste-{clipboard|selection} requires interactive mode. Ignoring option.")));
458 QString klf_resources = QString::null;
459 if (klf_resources_l.size() > 0) {
460 klf_resources = klf_resources_l[0].replace(rgx,
"");
463 bool klfsettings_can_import =
false;
467 #ifdef KLF_SHARE_RCCRESOURCES_DIR
474 if ( klf_resources.
isNull() ) {
477 rccfilepath = klf_resources;
482 for (QStringList::iterator it = rccfiles.begin(); it != rccfiles.end(); ++it) {
483 if ((*it).isEmpty()) {
485 it = rccfiles.erase(it, it+1);
486 for (j = 0; j < defaultrccpaths.size(); ++j) {
487 it = rccfiles.insert(it, defaultrccpaths[j]) + 1;
490 klfsettings_can_import =
true;
495 for (j = 0; j < rccfiles.size(); ++j) {
498 QDir dir(rccfiles[j]);
500 for (k = 0; k < files.size(); ++k) {
501 QString f = files[k].canonicalFilePath();
511 for (j = 0; j < rccfilesToLoad.size(); ++j) {
515 klfDbg(
"registered resource "<<addoninfo.
fpath()<<
".") ;
522 klfDbg(
"dump of :/ :" ) ;
529 char sindent[] =
" ";
530 uint nindent = indent*2;
531 if (nindent < strlen(sindent))
532 sindent[nindent] =
'\0';
537 for (k = 0; k < dchildren.size(); ++k) {
539 if (indent == 0 && dchildren[k] ==
"trolltech")
541 qDebug(
"%s%s/", sindent, qPrintable(dchildren[k]));
546 for (k = 0; k < fchildren.size(); ++k) {
547 qDebug(
"%s%s", sindent, qPrintable(fchildren[k]));
569 klfDbg(
"base plugins dirs are "<<baseplugindirs) ;
576 for (j = 0; j < pluginList.size(); ++j) {
578 klfDbg(
"Testing plugin psinfo="<<psinfo<<
"\n\tTo our system: qtver="<<qVersion()
589 qDebug(
"Comparing resource datetime (%s) with installed plugin datetime (%s)",
590 qPrintable(resourceplugin_dt.
toString()), qPrintable(installedplugin_dt.
toString()));
592 installedplugin_dt.
isNull() || resourceplugin_dt.
isNull() ||
593 ( resourceplugin_dt > installedplugin_dt ) ) {
601 klfDbg(
"\tcopy "<<resfn<<
" to "<<locfn ) ;
604 qWarning(
"Unable to copy plugin '%s' to local directory!", qPrintable(pluginList[j]));
607 QFile::ReadUser|QFile::WriteUser|QFile::ExeUser|
608 QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther);
609 qDebug(
"Copied plugin %s to local directory %s.", qPrintable(resfn), qPrintable(locfn));
619 for (n = 0; n < baseplugindirs.size(); ++n) {
620 QString baseplugindir = baseplugindirs[n];
621 klfDbg(
"exploring base plugin directory "<<baseplugindir) ;
627 QDir pdir(baseplugindir);
632 for (i = 0; i < pdirlist.size(); ++i) {
633 klfDbg(
"maybe adding plugin dir"<<pdirlist[i]<<
"; klfver="<<pdirlist[i].mid(3) ) ;
636 pluginsdirsbaserel << pdirlist[i]+
"/";
640 pluginsdirsbaserel <<
"" ;
642 klfDbg(
"pluginsdirs="<<pluginsdirs ) ;
644 for (i = 0; i < pluginsdirs.size(); ++i) {
648 QDir thisplugdir(pluginsdirs[i]);
651 for (j = 0; j < plugins.size(); ++j) {
652 QString pluginfname = plugins[j];
653 QString pluginfnamebaserel = pluginsdirsbaserel[i]+plugins[j];
654 bool plugin_already_loaded =
false;
658 klfDbg(
"Rejecting loading of plugin "<<pluginfname<<
" in dir "<<pluginsdirs[i]
659 <<
"; already loaded." ) ;
660 plugin_already_loaded =
true;
664 if (plugin_already_loaded)
668 bool loaded = pluginLoader.
load();
670 klfDbg(
"QPluginLoader failed to load plugin "<<pluginpath<<
". Skipping.");
674 if (pluginInstObject == NULL) {
675 klfDbg(
"QPluginLoader failed to load plugin "<<pluginpath<<
" (object is NULL). Skipping.");
679 if (pluginInstance == NULL) {
680 klfDbg(
"QPluginLoader failed to load plugin "<<pluginpath<<
" (instance is NULL). Skipping.");
686 <<
") from file "<<pluginfnamebaserel);
695 bool keepPlugin =
true;
698 bool pluginRejected =
false;
701 klfDbg(
"Rejecting loading of plugin "<<nm<<
" in "<<pluginfname<<
"; already loaded." ) ;
703 pluginRejected =
true;
711 pluginInfo.
name = nm;
715 pluginInfo.
fname = pluginfnamebaserel;
716 pluginInfo.
fpath = pluginpath;
721 klfDbg(
"got plugin info. keeping plugin? "<<keepPlugin);
725 klfDbg(
"prepared a configaccess "<<c);
727 pluginInfo.
instance = pluginInstance;
728 qDebug(
"\tPlugin %s loaded and initialized.", qPrintable(nm));
732 delete pluginInstance;
734 qDebug(
"\tPlugin %s NOT loaded.", qPrintable(nm));
750 a->setOrganizationDomain(
QLatin1String(
"klatexformula.org"));
753 #ifdef KLF_LIBKLFTOOLS_STATIC
754 Q_INIT_RESOURCE(klftoolsres) ;
756 #ifdef KLF_LIBKLFAPP_STATIC
757 Q_INIT_RESOURCE(klfres) ;
767 qRegisterMetaType< QImage >(
"QImage");
768 qRegisterMetaType< KLFStyle >();
769 qRegisterMetaTypeStreamOperators< KLFStyle >(
"KLFStyle");
770 qRegisterMetaType< KLFLibEntry >();
771 qRegisterMetaTypeStreamOperators< KLFLibEntry >(
"KLFLibEntry");
772 qRegisterMetaType< KLFLibResourceEngine::KLFLibEntryWithId >();
773 qRegisterMetaTypeStreamOperators< KLFLibResourceEngine::KLFLibEntryWithId >
774 (
"KLFLibResourceEngine::KLFLibEntryWithId");
777 qRegisterMetaType< QItemSelection >(
"QItemSelection");
778 qRegisterMetaType< QItemSelectionModel::SelectionFlags >(
"QItemSelectionModel::SelectionFlags");
784 int main(
int argc,
char **argv)
800 klfDbg(
"about to parse options") ;
805 klfDbg(
"options parsed.") ;
809 qCritical(
"Error while parsing command-line arguments.");
810 qCritical(
"Use --help to display command-line help.");
818 const char * SUFFIX =
".klfdebug";
820 if (strncmp(fname+(strlen(fname)-strlen(SUFFIX)), SUFFIX, strlen(SUFFIX)) != 0) {
822 strcat(fname, SUFFIX);
830 "-------------------------------------------------\n"
831 " KLATEXFORMULA DEBUG OUTPUT\n"
832 "-------------------------------------------------\n"
856 for (k = 0; k < appFontsInfoList.size(); ++k) {
873 args <<
"--latexinput="+latexinput;
896 const struct {
char c;
int optval; } borderoffsets[] =
900 for (k = 0; borderoffsets[k].c != 0; ++k)
901 if (borderoffsets[k].optval != -1)
914 for (k = 0; k < qtargvlist.size(); ++k)
915 args <<
"--qtoption="+qtargvlist[k];
917 for (k = 0;
klf_args[k] != NULL; ++k)
918 args << QString::fromLocal8Bit(
klf_args[k]);
920 klfDbg(
"Prepared deamonized process' command-line: progexe="<<progexe<<
"; args="<<args) ;
926 fprintf(stderr,
"%s",
927 qPrintable(
QObject::tr(
"KLatexFormula Daemon Process successfully launched with pid %1\n")
931 qWarning()<<qPrintable(
QObject::tr(
"Failed to launch daemon process. Not daemonizing."));
936 #if defined(KLF_USE_DBUS)
955 if ( ! latexinput.
isNull() )
983 for (
int k = 0;
klf_args[k] != NULL; ++k)
992 fprintf(stderr,
"KLatexFormula Version %s by Philippe Faist (c) 2005-2011\n"
993 "Licensed under the terms of the GNU Public License GPL\n\n",
996 klfDbgT(
"$$About to load config$$");
1003 klfDbgT(
"$$About to main_load_extra_resources$$");
1006 klfDbgT(
"$$About to main_reload_translations$$");
1014 klfDbgT(
"$$About to create lib factories$$");
1022 klfDbgT(
"$$START LOADING$$" ) ;
1038 klfDbgT(
"$$END LOADING$$" ) ;
1040 #if defined(KLF_USE_DBUS)
1046 dbusconn.
registerObject(
"/MainWindow/KLFMainWin", &mainWin, QDBusConnection::ExportAllContents
1047 | QDBusConnection::ExportChildObjects);
1054 qWarning(
"%s", qPrintable(
QObject::tr(
"Can't use --output without any input")));
1057 if ( ! latexinput.
isNull() )
1073 qDebug(
"opt_preamble != NULL, gui mode, preamble=%s",
opt_preamble);
1106 for (
int k = 0;
klf_args[k] != NULL; ++k)
1111 app.setQuitOnLastWindowClosed(
false);
1114 klfDbg(
"application has quit; we have cleaned up main(), ready to return. code="<<r) ;
1155 if (!cmdlHelpFile.
open(QIODevice::ReadOnly)) {
1156 qWarning()<<
KLF_FUNC_NAME<<
": Can't access command-line-help file :/data/cmdl-help.txt!";
1165 fprintf(stderr,
"KLatexFormula Version %s by Philippe Faist (c) 2005-2011\n"
1166 "Licensed under the terms of the GNU Public License GPL\n\n",
1167 KLF_VERSION_STRING);
1170 qWarning()<<qPrintable(
QObject::tr(
"The option --daemonize can only be used in interactive mode."));
1174 for (
int kl = 0;
klf_args[kl] != NULL; ++kl)
1175 qWarning()<<qPrintable(
QObject::tr(
"[Non-Interactive Mode] Ignoring additional command-line argument: %1")
1191 input.
latex = latexinput;
1218 bgcolor.
setRgb(255, 255, 255, 0);
1257 if (klfoutput.
status != 0) {
1283 if (arg[0] ==
'&') {
1284 int fd = atoi(&arg[1]);
1286 fp = fdopen(fd,
"a");
1287 if (fd <= 0 || fp == NULL) {
1288 qWarning(
"Failed to open file descriptor %d.", fd);
1293 if (!strcmp(arg,
"-")) {
1297 fp = fopen(arg,
"a");
1299 qWarning(
"Failed to open file `%s' to print help message.", arg);
1310 return defaultvalue;
1312 QRegExp booltruerx =
QRegExp(
"^\\s*on|y(es)?|1|t(rue)?\\s*", Qt::CaseInsensitive);
1313 QRegExp boolfalserx =
QRegExp(
"^\\s*off|n(o)?|0|f(alse)?\\s*", Qt::CaseInsensitive);
1324 return defaultvalue;
1339 char klfcmdl_optstring[1024];
1345 klfcmdl_optstring[j++] =
':';
1348 klfcmdl_optstring[j] =
'\0';
1353 c = getopt_long(argc, argv, klfcmdl_optstring,
klfcmdl_optlist, NULL);
1358 if (optarg != NULL) {
1369 arg = strdup(optarg);
1392 qWarning(
"%s", qPrintable(
QObject::tr(
"--paste-clipboard requires interactive mode. Switching.")));
1400 qWarning(
"%s", qPrintable(
QObject::tr(
"--paste-selection requires interactive mode. Switching.")));
1430 #if defined(Q_WS_MAC)
1434 if ( !strncmp(arg,
"sn_", 3) )
1491 char *colonptr = strchr(arg,
':');
1492 if (colonptr != NULL) {
1526 qWarning(
"%s", qPrintable(
QObject::tr(
"--noeval is relevant only in interactive mode.")));
1530 qWarning(
"%s", qPrintable(
QObject::tr(
"--noeval may not be used when --output is present.")));
1534 qWarning(
"%s", qPrintable(
QObject::tr(
"Ignoring --format without --output.")));