File indexing completed on 2024-05-05 16:27:23

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2005-2010 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer 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, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 #include "kgraphviewer.h"
0020 #include "kgraphviewer_debug.h"
0021 
0022 #include "config-kgraphviewer.h"
0023 #include "kgraphvieweradaptor.h"
0024 #include <KAboutData>
0025 #include <QApplication>
0026 #include <QByteArray>
0027 #include <QCommandLineParser>
0028 #include <QDBusConnection>
0029 #include <QDBusConnectionInterface>
0030 #include <QDBusInterface>
0031 #include <QDBusReply>
0032 #include <QDebug>
0033 #include <QDir>
0034 #include <QMessageBox>
0035 #include <iostream>
0036 #include <kaboutdata.h>
0037 #include <klocalizedstring.h>
0038 
0039 int main(int argc, char **argv)
0040 {
0041     QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
0042     QApplication app(argc, argv);
0043 
0044     KLocalizedString::setApplicationDomain("kgraphviewer");
0045 
0046     KAboutData about(QStringLiteral("kgraphviewer"),
0047                      i18n("KGraphViewer"),
0048                      KGRAPHVIEWER_VERSION_STRING,
0049                      i18n("A Graphviz DOT graph viewer"),
0050                      KAboutLicense::GPL,
0051                      i18n("(C) 2005-2010 Gaël de Chalendar"),
0052                      QString(),
0053                      QStringLiteral("https://www.kde.org/applications/graphics/kgraphviewer"));
0054     about.addAuthor(i18n("Gaël de Chalendar"), i18n("Original Author and current maintainer"), "kleag@free.fr");
0055     about.addAuthor(i18n("Reimar Döffinger"), i18n("Contributor"), "kde@reimardoeffinger.de");
0056     about.addAuthor(i18n("Matthias Peinhardt"), i18n("Contributor"), "matthias.peinhardt@googlemail.com");
0057     about.addAuthor(i18n("Sandro Andrade"), i18n("Contributor"), "sandro.andrade@gmail.com");
0058     about.addAuthor(i18n("Milian Wolff"), i18n("Contributor"), "mail@milianw.de");
0059     about.addAuthor(i18n("Martin Sandsmark"), i18n("Port to KF5"), "martin.sandsmark@kde.org");
0060 
0061     app.setOrganizationDomain(QStringLiteral("kde.org"));
0062     app.setOrganizationName(QStringLiteral("KDE"));
0063 
0064     KAboutData::setApplicationData(about);
0065 
0066     app.setWindowIcon(QIcon::fromTheme("kgraphviewer", app.windowIcon()));
0067 
0068     QCommandLineParser options;
0069     options.addPositionalArgument(QStringLiteral("url"), i18n("Path or URL to scan"), i18n("[url]"));
0070     about.setupCommandLine(&options);
0071     options.process(app);
0072     about.processCommandLine(&options);
0073 
0074     // see if we are starting with session management
0075     if (app.isSessionRestored()) {
0076         RESTORE(KGraphViewerWindow);
0077     } else {
0078         // no session.. just start up normally
0079         QStringList args = options.positionalArguments();
0080 
0081         KGraphViewerWindow *widget = nullptr;
0082         if (args.count() == 0) {
0083             widget = new KGraphViewerWindow;
0084             new KgraphviewerAdaptor(widget);
0085             QDBusConnection::sessionBus().registerObject("/KGraphViewer", widget);
0086             widget->show();
0087         } else {
0088             QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kgraphviewer");
0089 
0090             bool instanceExists = reply.value();
0091 
0092             for (int i = 0; i < args.count(); i++) {
0093                 if (instanceExists &&
0094                     (QMessageBox::question(nullptr, i18n("Opening in new window confirmation"), i18n("A KGraphViewer window is already open, where do you want to open this file in the existing window?")) == QMessageBox::Yes)) {
0095                     QByteArray tosenddata;
0096                     QDataStream arg(&tosenddata, QIODevice::WriteOnly);
0097                     QString strarg = args[i];
0098                     QUrl url;
0099                     if (strarg.left(1) == "/")
0100                         url = QUrl::fromUserInput(strarg);
0101                     else
0102                         url = QUrl::fromLocalFile(QDir::currentPath() + '/' + strarg);
0103                     arg << url;
0104                     QDBusInterface iface("org.kde.kgraphviewer", "/KGraphViewer", "", QDBusConnection::sessionBus());
0105                     if (iface.isValid()) {
0106                         QDBusReply<void> reply = iface.call("openUrl", url.url(QUrl::PreferLocalFile));
0107                         if (reply.isValid()) {
0108                             qCWarning(KGRAPHVIEWER_LOG) << "Reply was valid";
0109                             return 0;
0110                         }
0111 
0112                         qCWarning(KGRAPHVIEWER_LOG) << "Call failed: " << reply.error().message();
0113                         return 1;
0114                     }
0115                     qCWarning(KGRAPHVIEWER_LOG) << "Invalid interface";
0116                     exit(0);
0117                 } else {
0118                     widget = new KGraphViewerWindow;
0119                     new KgraphviewerAdaptor(widget);
0120                     QDBusConnection::sessionBus().registerObject("/KGraphViewer", widget);
0121                     widget->show();
0122                     widget->openUrl(QUrl::fromUserInput(args[i]));
0123                 }
0124             }
0125         }
0126         if (widget) {
0127             widget->reloadPreviousFiles();
0128         }
0129     }
0130     return app.exec();
0131 }