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

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 "kgrapheditor.h"
0020 #include "kgrapheditor_debug.h"
0021 
0022 #include <QApplication>
0023 #include <QByteArray>
0024 #include <QCommandLineParser>
0025 #include <QDBusConnection>
0026 #include <QDBusConnectionInterface>
0027 #include <QDBusInterface>
0028 #include <QDBusReply>
0029 #include <QDebug>
0030 #include <QDir>
0031 #include <QMessageBox>
0032 #include <iostream>
0033 #include <kaboutdata.h>
0034 
0035 #include "config-kgraphviewer.h"
0036 #include "kgrapheditoradaptor.h"
0037 #include <KAboutData>
0038 #include <klocalizedstring.h>
0039 
0040 int main(int argc, char **argv)
0041 {
0042     QApplication app(argc, argv);
0043 
0044     KLocalizedString::setApplicationDomain("kgraphviewer");
0045 
0046     KAboutData about(QStringLiteral("kgrapheditor"), i18n("KGraphEditor"), KGRAPHVIEWER_VERSION_STRING, i18n("A Graphviz DOT graph editor by KDE"), KAboutLicense::GPL, i18n("(C) 2005-2010 Gaƫl de Chalendar"));
0047 
0048     app.setOrganizationDomain(QStringLiteral("kde.org"));
0049     app.setOrganizationName(QStringLiteral("KDE"));
0050 
0051     KAboutData::setApplicationData(about);
0052 
0053     app.setWindowIcon(QIcon::fromTheme("kgraphviewer", app.windowIcon()));
0054 
0055     QCommandLineParser options;
0056     options.addHelpOption();
0057     options.addVersionOption();
0058     options.addPositionalArgument(QStringLiteral("url"), i18n("Path or URL to scan"), i18n("[url]"));
0059     about.setupCommandLine(&options);
0060     options.process(app);
0061     about.processCommandLine(&options);
0062 
0063     // see if we are starting with session management
0064     if (app.isSessionRestored()) {
0065         RESTORE(KGraphEditor);
0066     } else {
0067         // no session.. just start up normally
0068         QStringList args = options.positionalArguments();
0069 
0070         KGraphEditor *widget = nullptr;
0071         if (args.count() == 0) {
0072             widget = new KGraphEditor;
0073             new KgrapheditorAdaptor(widget);
0074             QDBusConnection::sessionBus().registerObject("/KGraphEditor", widget);
0075             widget->show();
0076         } else {
0077             QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kgrapheditor");
0078 
0079             bool instanceExists = reply.value();
0080 
0081             for (int i = 0; i < args.count(); i++) {
0082                 if (instanceExists &&
0083                     (QMessageBox::question(nullptr, i18n("A KGraphEditor window is already open, do you want to open the file in it?"), i18n("Opening in new window confirmation"), "openInNewWindowMode") == QMessageBox::Yes)) {
0084                     QByteArray tosenddata;
0085                     QDataStream arg(&tosenddata, QIODevice::WriteOnly);
0086                     QString strarg = args[i];
0087                     QUrl url = QUrl::fromUserInput(strarg, QDir::currentPath(), QUrl::AssumeLocalFile);
0088                     arg << url;
0089                     QDBusInterface iface("org.kde.kgrapheditor", "/KGraphEditor", "", QDBusConnection::sessionBus());
0090                     if (iface.isValid()) {
0091                         QDBusReply<void> reply = iface.call("openUrl", url.url(QUrl::PreferLocalFile));
0092                         if (reply.isValid()) {
0093                             qCDebug(KGRAPHEDITOR_LOG) << "Reply was valid" << Qt::endl;
0094                             return 0;
0095                         }
0096 
0097                         qCWarning(KGRAPHEDITOR_LOG) << "Call failed: " << reply.error().message() << Qt::endl;
0098                         return 1;
0099                     }
0100                     qCWarning(KGRAPHEDITOR_LOG) << "Invalid interface" << Qt::endl;
0101                     exit(0);
0102                 } else {
0103                     widget = new KGraphEditor;
0104                     new KgrapheditorAdaptor(widget);
0105                     QDBusConnection::sessionBus().registerObject("/KGraphEditor", widget);
0106                     widget->show();
0107                     widget->openUrl(QUrl::fromUserInput(args[0], QDir::currentPath(), QUrl::AssumeLocalFile));
0108                 }
0109             }
0110         }
0111         if (widget) {
0112             widget->reloadPreviousFiles();
0113         }
0114     }
0115     return app.exec();
0116 }