"Fossies" - the Fresh Open Source Software Archive

Member "texstudio-3.1.1/src/main.cpp" (21 Feb 2021, 8049 Bytes) of package /linux/misc/texstudio-3.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_3.1.1.

    1 /***************************************************************************
    2  *   copyright       : (C) 2003-2007 by Pascal Brachet                     *
    3  *   addons by Frederic Devernay <frederic.devernay@m4x.org>               *
    4  *   addons by Luis Silvestre                                              *
    5  *   http://www.xm1math.net/texmaker/                                      *
    6  *                                                                         *
    7  *   This program 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  *                                                                         *
   12  ***************************************************************************/
   13 
   14 #include "mostQtHeaders.h"
   15 /*! \mainpage TexStudio
   16  *
   17  * \see Texstudio
   18  * \see PDFDocument
   19  */
   20 
   21 #include "texstudio.h"
   22 #include "smallUsefulFunctions.h"
   23 #include "debughelper.h"
   24 #include "debuglogger.h"
   25 #include "utilsVersion.h"
   26 #include <qtsingleapplication.h>
   27 #include <QSplashScreen>
   28 
   29 #ifdef Q_OS_WIN32
   30 #include "windows.h"
   31 typedef BOOL (WINAPI *AllowSetForegroundWindowFunc)(DWORD);
   32 #endif
   33 
   34 class TexstudioApp : public QtSingleApplication
   35 {
   36 public:
   37     bool initialized;
   38     QString delayedFileLoad;
   39     Texstudio *mw;  // Moved from private:
   40     TexstudioApp(int &argc, char **argv);
   41     TexstudioApp(QString &id, int &argc, char **argv);
   42     ~TexstudioApp();
   43     void init(QStringList &cmdLine);   // This function does all the initialization instead of the constructor.
   44     /*bool notify(QObject* obj, QEvent* event){
   45         //really slow global event logging:
   46         //qWarning(qPrintable(QString("%1 obj %2 named %3 typed %4 child of %5 received %6").arg(QTime::currentTime().toString("HH:mm:ss:zzz")).arg((long)obj,8,16).arg(obj->objectName()).arg(obj->metaObject()->className()).arg(obj->parent()?obj->parent()->metaObject()->className():"").arg(event->type())));
   47         try {
   48             return QApplication::notify(obj,event);
   49         } catch (const std::exception& e){
   50             qDebug() << "Catched exception: " << e.what();
   51             return false;
   52         }
   53     }*/
   54 
   55 
   56 protected:
   57     bool event(QEvent *event);
   58 };
   59 
   60 TexstudioApp::TexstudioApp(int &argc, char **argv) : QtSingleApplication(argc, argv)
   61 {
   62     mw = nullptr;
   63     initialized = false;
   64 }
   65 
   66 TexstudioApp::TexstudioApp(QString &id, int &argc, char **argv) : QtSingleApplication(id, argc, argv)
   67 {
   68     mw = nullptr;
   69     initialized = false;
   70 }
   71 
   72 void TexstudioApp::init(QStringList &cmdLine)
   73 {
   74     QPixmap pixmap(":/images/splash.png");
   75     QSplashScreen *splash = new QSplashScreen(pixmap);
   76     splash->show();
   77     processEvents();
   78 
   79     mw = new Texstudio(nullptr, Qt::WindowFlags(), splash);
   80     connect(this, SIGNAL(lastWindowClosed()), this, SLOT(quit()));
   81     splash->finish(mw);
   82     delete splash;
   83 
   84     initialized = true;
   85 
   86     if (!delayedFileLoad.isEmpty()) cmdLine << delayedFileLoad;
   87     mw->executeCommandLine(cmdLine, true);
   88     if(!cmdLine.contains("--auto-tests")){
   89         mw->startupCompleted();
   90     }
   91 }
   92 
   93 TexstudioApp::~TexstudioApp()
   94 {
   95     delete mw;
   96 }
   97 
   98 bool TexstudioApp::event(QEvent *event)
   99 {
  100     if (event->type() == QEvent::FileOpen) {
  101         QFileOpenEvent *oe = static_cast<QFileOpenEvent *>(event);
  102         if (initialized) mw->load(oe->file());
  103         else delayedFileLoad = oe->file();
  104         event->accept();
  105         return true;
  106     }
  107     return QApplication::event(event);
  108 }
  109 
  110 QString generateAppId()
  111 {
  112     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
  113     QString user = env.value("USER");
  114     if (user.isEmpty()) {
  115         user = env.value("USERNAME");
  116     }
  117     return QString("%1_%2").arg(TEXSTUDIO).arg(user);
  118 }
  119 
  120 QStringList parseArguments(const QStringList &args, bool &outStartAlways)
  121 {
  122     QStringList cmdLine;
  123     for (int i = 1; i < args.count(); ++i) {
  124         QString cmdArgument =  args[i];
  125 
  126         if (cmdArgument.startsWith('-')) {
  127             // various commands
  128             if (cmdArgument == "--start-always")
  129                 outStartAlways = true;
  130             else if (cmdArgument == "--no-session")
  131                 ConfigManager::dontRestoreSession = true;
  132             else if ((cmdArgument == "-line" || cmdArgument == "--line") && (++i < args.count()))
  133                 cmdLine << "--line" << args[i];
  134             else if ((cmdArgument == "-page" || cmdArgument == "--page") && (++i < args.count()))
  135                 cmdLine << "--page" << args[i];
  136             else if ((cmdArgument == "-insert-cite" || cmdArgument == "--insert-cite") && (++i < args.count()))
  137                 cmdLine << "--insert-cite" << args[i];
  138             else if (cmdArgument == "--ini-file" && (++i < args.count())) {
  139                 // deprecated: use --config instead
  140                 ConfigManager::configDirOverride = QFileInfo(args[i]).absolutePath();
  141             }
  142             else if (cmdArgument == "--config" && (++i < args.count()))
  143                 ConfigManager::configDirOverride = args[i];
  144 #ifdef DEBUG_LOGGER
  145             else if ((cmdArgument == "--debug-logfile") && (++i < args.count()))
  146                 debugLoggerStart(args[i]);
  147 #endif
  148             else
  149                 cmdLine << cmdArgument;
  150         } else
  151             cmdLine << QFileInfo(cmdArgument).absoluteFilePath();
  152     }
  153     return cmdLine;
  154 }
  155 
  156 bool handleCommandLineOnly(const QStringList &cmdLine) {
  157     // note: stdout is not supported for Win GUI applications. Will simply not output anything there.
  158     if (cmdLine.contains("--help")) {
  159         QTextStream(stdout) << "Usage: texstudio [options] [file]\n"
  160                             << "\n"
  161                             << "Options:\n"
  162                             << "  --config DIR              use the specified settings directory\n"
  163                             << "  --master                  define the document as explicit root document\n"
  164                             << "  --line LINE[:COL]         position the cursor at line LINE and column COL\n"
  165                             << "  --insert-cite CITATION    inserts the given citation\n"
  166                             << "  --start-always            start a new instance, even if TXS is already running\n"
  167                             << "  --pdf-viewer-only         run as a standalone pdf viewer without an editor\n"
  168                             << "  --page PAGENUM            display a certain page in the pdf viewer\n"
  169                             << "  --no-session              do not load/save the session at startup/close\n"
  170                             << "  --texpath PATH            force resetting command defaults with PATH as first search path"
  171                             << "  --version                 show version number\n"
  172 #ifdef DEBUG_LOGGER
  173                             << "  --debug-logfile pathname  write debug messages to pathname\n"
  174 #endif
  175                             ;
  176         return true;
  177     }
  178 
  179     if (cmdLine.contains("--version")) {
  180         QTextStream(stdout) << "TeXstudio " << TXSVERSION << " (" << TEXSTUDIO_GIT_REVISION << ")\n";
  181         return true;
  182     }
  183 
  184     return false;
  185 }
  186 
  187 int main(int argc, char **argv)
  188 {
  189     QString appId = generateAppId();
  190 #if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
  191     if(qEnvironmentVariableIntValue("TEXSTUDIO_HIDPI_SCALE")>0){
  192         QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  193     } else {
  194         QApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
  195     }
  196 #endif
  197     // This is a dummy constructor so that the programs loads fast.
  198     TexstudioApp a(appId, argc, argv);
  199     bool startAlways = false;
  200     QStringList cmdLine = parseArguments(QCoreApplication::arguments(), startAlways);
  201 
  202     if (handleCommandLineOnly(cmdLine)) {
  203         return 0;
  204     }
  205 
  206     if (!startAlways) {
  207         if (a.isRunning()) {
  208 #ifdef Q_OS_WIN32
  209             AllowSetForegroundWindowFunc asfw = (AllowSetForegroundWindowFunc) GetProcAddress(GetModuleHandleA("user32.dll"), "AllowSetForegroundWindow");
  210             if (asfw) asfw(/*ASFW_ANY*/(DWORD)(-1));
  211 #endif
  212             a.sendMessage(cmdLine.join("#!#"));
  213             return 0;
  214         }
  215     }
  216 
  217     a.setApplicationName( TEXSTUDIO );
  218 #if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)) && defined(Q_OS_LINUX)
  219     a.setDesktopFileName("texstudio");
  220 #endif
  221     a.init(cmdLine); // Initialization takes place only if there is no other instance running.
  222 
  223     QObject::connect(&a, SIGNAL(messageReceived(const QString &)),
  224                      a.mw, SLOT(onOtherInstanceMessage(const QString &)));
  225 
  226     try {
  227         int execResult = a.exec();
  228 #ifdef DEBUG_LOGGER
  229         if (debugLoggerIsLogging()) {
  230             debugLoggerStop();
  231         }
  232 #endif
  233         return execResult;
  234     } catch (...) {
  235 #ifndef NO_CRASH_HANDLER
  236         catchUnhandledException();
  237 #endif
  238         throw;
  239     }
  240 }