File indexing completed on 2024-05-12 16:39:24

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2016 Dag Andersen <danders@get2net.dk>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 // clazy:excludeall=qstring-arg
0021 #include "commandlineparser.h"
0022 #include "part.h"
0023 #include "mainwindow.h"
0024 #include "aboutdata.h"
0025 
0026 #include <kiconloader.h>
0027 #include <KLocalizedString>
0028 #include <KAboutData>
0029 #include <KStartupInfo>
0030 #include <KWindowSystem>
0031 #include <KMessageBox>
0032 #include <kwindowsystem_version.h>
0033 
0034 #include <QApplication>
0035 #include <QDir>
0036 
0037 #include "debugarea.h"
0038 
0039 CommandLineParser::CommandLineParser()
0040     : QObject(),
0041     m_mainwindow(0)
0042 {
0043     KAboutData *aboutData = KPlatoWork::newAboutData();
0044     KAboutData::setApplicationData(*aboutData);
0045     qApp->setWindowIcon(QIcon::fromTheme(QStringLiteral("calligraplanwork"), qApp->windowIcon()));
0046 
0047     aboutData->setupCommandLine(&m_commandLineParser);
0048     m_commandLineParser.addHelpOption();
0049     m_commandLineParser.addVersionOption();
0050     m_commandLineParser.addPositionalArgument(QStringLiteral("[file]"), i18n("File to open"));
0051 
0052     m_commandLineParser.process(*qApp);
0053 
0054     aboutData->processCommandLine(&m_commandLineParser);
0055 
0056     delete aboutData;
0057 }
0058 
0059 CommandLineParser::~CommandLineParser()
0060 {
0061 }
0062 
0063 void CommandLineParser::handleActivateRequest(const QStringList &arguments, const QString &workingDirectory)
0064 {
0065     m_commandLineParser.parse(arguments);
0066 
0067     handleCommandLine(QDir(workingDirectory));
0068 
0069     // terminate startup notification and activate the mainwindow
0070 #if KWINDOWSYSTEM_VERSION >= QT_VERSION_CHECK(5,62,0)
0071     m_mainwindow->setAttribute(Qt::WA_NativeWindow, true);
0072     KStartupInfo::setNewStartupId(m_mainwindow->windowHandle(), KStartupInfo::startupId());
0073 #else
0074     KStartupInfo::setNewStartupId(m_mainwindow, KStartupInfo::startupId());
0075 #endif
0076     KWindowSystem::forceActiveWindow(m_mainwindow->winId());
0077 
0078 }
0079 
0080 void CommandLineParser::handleCommandLine(const QDir &workingDirectory)
0081 {
0082     QList<KMainWindow*> lst = KMainWindow::memberList();
0083     if (lst.count() > 1) {
0084         warnPlanWork<<"windows count > 1:"<<lst.count();
0085         return; // should never happen
0086     }
0087     if (lst.isEmpty()) {
0088         Q_ASSERT(m_mainwindow == 0);
0089     }
0090     if (m_mainwindow == 0) {
0091         m_mainwindow = new KPlatoWork_MainWindow();
0092         m_mainwindow->show();
0093     }    
0094     // Get the command line arguments which we have to parse
0095     const QStringList fileUrls = m_commandLineParser.positionalArguments();
0096     // TODO: remove once Qt has proper handling itself
0097     const QRegExp withProtocolChecker(QStringLiteral("^[a-zA-Z]+:"));
0098     foreach(const QString &fileUrl, fileUrls) {
0099         // convert to an url
0100         const bool startsWithProtocol = (withProtocolChecker.indexIn(fileUrl) == 0);
0101         const QUrl url = startsWithProtocol ?
0102             QUrl::fromUserInput(fileUrl) :
0103             QUrl::fromLocalFile(workingDirectory.absoluteFilePath(fileUrl));
0104 
0105         // For now create an empty document
0106         if (! m_mainwindow->openDocument(url)) {
0107             KMessageBox::error(0, i18n("Failed to open document"));
0108         }
0109     }
0110 }