Warning, file /utilities/keditbookmarks/src/main.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // -*- indent-tabs-mode:nil -*-
0002 // vim: set ts=4 sts=4 sw=4 et:
0003 /* This file is part of the KDE project
0004    Copyright (C) 2000 David Faure <faure@kde.org>
0005    Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public
0009    License version 2 or at your option version 3 as published by
0010    the Free Software Foundation.
0011 
0012    This program is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    General Public License for more details.
0016 
0017    You should have received a copy of the GNU General Public License
0018    along with this program; see the file COPYING.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020    Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "globalbookmarkmanager.h"
0024 #include "importers.h"
0025 #include "kbookmarkmodel/commandhistory.h"
0026 #include "keditbookmarks_version.h"
0027 #include "toplevel.h"
0028 
0029 #include "keditbookmarks_debug.h"
0030 #include <QApplication>
0031 #include <QCommandLineOption>
0032 #include <QCommandLineParser>
0033 
0034 #include <KAboutData>
0035 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0036 #include <Kdelibs4ConfigMigrator>
0037 #endif
0038 
0039 #include <KMessageBox>
0040 #include <KWindowSystem>
0041 
0042 #include <KBookmarkManager>
0043 #include <QStandardPaths>
0044 #include <kbookmarkexporter.h>
0045 #include <toplevel_interface.h>
0046 
0047 // TODO - make this register() or something like that and move dialog into main
0048 static bool askUser(const QString &filename, bool &readonly)
0049 {
0050     QString interfaceName = QStringLiteral("org.kde.keditbookmarks");
0051     QString appId = interfaceName + QLatin1Char('-') + QString().setNum(QApplication::applicationPid());
0052 
0053     QDBusConnection dbus = QDBusConnection::sessionBus();
0054     QDBusReply<QStringList> reply = dbus.interface()->registeredServiceNames();
0055     if (!reply.isValid())
0056         return true;
0057     const QStringList allServices = reply;
0058     for (QStringList::const_iterator it = allServices.begin(), end = allServices.end(); it != end; ++it) {
0059         const QString service = *it;
0060         if (service.startsWith(interfaceName) && service != appId) {
0061             org::kde::keditbookmarks keditbookmarks(service, QStringLiteral("/keditbookmarks"), dbus);
0062             QDBusReply<QString> bookmarks = keditbookmarks.bookmarkFilename();
0063             QString name;
0064             if (bookmarks.isValid())
0065                 name = bookmarks;
0066             if (name == filename) {
0067                 int ret = KMessageBox::warningYesNo(nullptr,
0068                                                     i18n("Another instance of %1 is already running. Do you really "
0069                                                          "want to open another instance or continue work in the same instance?\n"
0070                                                          "Please note that, unfortunately, duplicate views are read-only.",
0071                                                          QGuiApplication::applicationDisplayName()),
0072                                                     i18nc("@title:window", "Warning"),
0073                                                     KGuiItem(i18n("Run Another")), /* yes */
0074                                                     KGuiItem(i18n("Continue in Same")) /*  no */);
0075                 if (ret == KMessageBox::No) {
0076                     QDBusInterface keditinterface(service, QStringLiteral("/keditbookmarks/MainWindow_1"));
0077                     // TODO fix me
0078                     QDBusReply<qlonglong> value = keditinterface.call(QDBus::NoBlock, QStringLiteral("winId"));
0079                     qlonglong id = 0;
0080                     if (value.isValid())
0081                         id = value;
0082                     ////qCDebug(KEDITBOOKMARKS_LOG)<<" id !!!!!!!!!!!!!!!!!!! :"<<id;
0083                     KWindowSystem::activateWindow((WId)id);
0084                     return false;
0085                 } else if (ret == KMessageBox::Yes) {
0086                     readonly = true;
0087                 }
0088             }
0089         }
0090     }
0091     return true;
0092 }
0093 
0094 int main(int argc, char **argv)
0095 {
0096 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0097     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
0098 #endif
0099     QApplication app(argc, argv);
0100 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0101     // enable high dpi support
0102     app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0103     Kdelibs4ConfigMigrator migrate(QStringLiteral("keditbookmarks"));
0104     migrate.setConfigFiles(QStringList() << QStringLiteral("keditbookmarksrc"));
0105     migrate.setUiFiles(QStringList() << QStringLiteral("keditbookmarksuirc"));
0106     migrate.migrate();
0107 #endif
0108 
0109     KLocalizedString::setApplicationDomain("keditbookmarks");
0110 
0111     KAboutData aboutData(QStringLiteral("keditbookmarks"),
0112                          i18n("Bookmark Editor"),
0113                          QStringLiteral(KEDITBOOKMARKS_VERSION_STRING),
0114                          i18n("Bookmark Organizer and Editor"),
0115                          KAboutLicense::GPL,
0116                          i18n("Copyright 2000-2017, KDE developers"));
0117     aboutData.addAuthor(i18n("David Faure"), i18n("Initial author"), QStringLiteral("faure@kde.org"));
0118     aboutData.addAuthor(i18n("Alexander Kellett"), i18n("Author"), QStringLiteral("lypanov@kde.org"));
0119 
0120     aboutData.setDesktopFileName(QStringLiteral("org.kde.keditbookmarks"));
0121     KAboutData::setApplicationData(aboutData);
0122 
0123     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("bookmarks-organize")));
0124 
0125     QCommandLineParser parser;
0126     parser.setApplicationDescription(aboutData.shortDescription());
0127     // clang-format off
0128     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importmoz"), i18n("Import bookmarks from a file in Mozilla format"), QStringLiteral("filename")));
0129     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importns"), i18n("Import bookmarks from a file in Netscape (4.x and earlier) format"), QStringLiteral("filename")));
0130     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importie"), i18n("Import bookmarks from a file in Internet Explorer's Favorites format"), QStringLiteral("filename")));
0131     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importopera"), i18n("Import bookmarks from a file in Opera format"), QStringLiteral("filename")));
0132     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importkde3"), i18n("Import bookmarks from a file in KDE2 format"), QStringLiteral("filename")));
0133     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importgaleon"), i18n("Import bookmarks from a file in Galeon format"), QStringLiteral("filename")));
0134     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportmoz"), i18n("Export bookmarks to a file in Mozilla format"), QStringLiteral("filename")));
0135     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportns"), i18n("Export bookmarks to a file in Netscape (4.x and earlier) format"), QStringLiteral("filename")));
0136     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exporthtml"), i18n("Export bookmarks to a file in a printable HTML format"), QStringLiteral("filename")));
0137     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportie"), i18n("Export bookmarks to a file in Internet Explorer's Favorites format"), QStringLiteral("filename")));
0138     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportopera"), i18n("Export bookmarks to a file in Opera format"), QStringLiteral("filename")));
0139     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("address"), i18n("Open at the given position in the bookmarks file"), QStringLiteral("address")));
0140     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("customcaption"), i18n("Set the user-readable caption, for example \"Konsole\""), QStringLiteral("caption")));
0141     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("nobrowser"), i18n("Hide all browser related functions")));
0142     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("dbusObjectName"), i18n("A unique name that represents this bookmark collection, usually the kinstance name.\n"
0143                                  "This should be \"konqueror\" for the Konqueror bookmarks, \"kfile\" for KFileDialog bookmarks, etc.\n"
0144                                  "The final D-Bus object path is /KBookmarkManager/<dbusObjectName>"), QStringLiteral("name")));
0145     // clang-format on
0146     parser.addPositionalArgument(QStringLiteral("[file]"), i18n("File to edit"));
0147 
0148     aboutData.setupCommandLine(&parser);
0149     parser.process(app);
0150     aboutData.processCommandLine(&parser);
0151 
0152     const bool isGui = !(parser.isSet(QStringLiteral("exportmoz")) || parser.isSet(QStringLiteral("exportns")) || parser.isSet(QStringLiteral("exporthtml")) //
0153                          || parser.isSet(QStringLiteral("exportie")) || parser.isSet(QStringLiteral("exportopera")) //
0154                          || parser.isSet(QStringLiteral("importmoz")) || parser.isSet(QStringLiteral("importns")) //
0155                          || parser.isSet(QStringLiteral("importie")) || parser.isSet(QStringLiteral("importopera")) //
0156                          || parser.isSet(QStringLiteral("importkde3")) || parser.isSet(QStringLiteral("importgaleon")));
0157 
0158     const bool browser = !parser.isSet(QStringLiteral("nobrowser"));
0159     const bool gotFilenameArg = (parser.positionalArguments().count() == 1);
0160 
0161     QString filename = gotFilenameArg ? parser.positionalArguments().at(0)
0162                                       : QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/bookmarks.xml");
0163 
0164     if (!isGui) {
0165         GlobalBookmarkManager::self()->createManager(filename, QString(), new CommandHistory());
0166         GlobalBookmarkManager::ExportType exportType = GlobalBookmarkManager::MozillaExport; // uumm.. can i just set it to -1 ?
0167         int got = 0;
0168         QString arg;
0169         QString arg2;
0170         QString importType;
0171         if (arg = QStringLiteral("exportmoz"), parser.isSet(arg)) {
0172             exportType = GlobalBookmarkManager::MozillaExport;
0173             arg2 = arg;
0174             got++;
0175         }
0176         if (arg = QStringLiteral("exportns"), parser.isSet(arg)) {
0177             exportType = GlobalBookmarkManager::NetscapeExport;
0178             arg2 = arg;
0179             got++;
0180         }
0181         if (arg = QStringLiteral("exporthtml"), parser.isSet(arg)) {
0182             exportType = GlobalBookmarkManager::HTMLExport;
0183             arg2 = arg;
0184             got++;
0185         }
0186         if (arg = QStringLiteral("exportie"), parser.isSet(arg)) {
0187             exportType = GlobalBookmarkManager::IEExport;
0188             arg2 = arg;
0189             got++;
0190         }
0191         if (arg = QStringLiteral("exportopera"), parser.isSet(arg)) {
0192             exportType = GlobalBookmarkManager::OperaExport;
0193             arg2 = arg;
0194             got++;
0195         }
0196         if (arg = QStringLiteral("importmoz"), parser.isSet(arg)) {
0197             importType = QStringLiteral("Moz");
0198             arg2 = arg;
0199             got++;
0200         }
0201         if (arg = QStringLiteral("importns"), parser.isSet(arg)) {
0202             importType = QStringLiteral("NS");
0203             arg2 = arg;
0204             got++;
0205         }
0206         if (arg = QStringLiteral("importie"), parser.isSet(arg)) {
0207             importType = QStringLiteral("IE");
0208             arg2 = arg;
0209             got++;
0210         }
0211         if (arg = QStringLiteral("importopera"), parser.isSet(arg)) {
0212             importType = QStringLiteral("Opera");
0213             arg2 = arg;
0214             got++;
0215         }
0216         if (arg = QStringLiteral("importgaleon"), parser.isSet(arg)) {
0217             importType = QStringLiteral("Galeon");
0218             arg2 = arg;
0219             got++;
0220         }
0221         if (arg = QStringLiteral("importkde3"), parser.isSet(arg)) {
0222             importType = QStringLiteral("KDE2");
0223             arg2 = arg;
0224             got++;
0225         }
0226         if (importType.isEmpty() && !arg2.isEmpty()) {
0227             // TODO - maybe an xbel export???
0228             if (got > 1) { // got == 0 isn't possible as !isGui is dependent on "export.*"
0229                 qCWarning(KEDITBOOKMARKS_LOG) << i18n("You may only specify a single --export option.");
0230                 return 1;
0231             }
0232             QString path = parser.value(arg2);
0233             GlobalBookmarkManager::self()->doExport(exportType, path);
0234         } else if (!importType.isEmpty()) {
0235             if (got > 1) { // got == 0 isn't possible as !isGui is dependent on "import.*"
0236                 qCWarning(KEDITBOOKMARKS_LOG) << i18n("You may only specify a single --import option.");
0237                 return 1;
0238             }
0239             QString path = parser.value(arg2);
0240             KBookmarkModel *model = GlobalBookmarkManager::self()->model();
0241             ImportCommand *importer = ImportCommand::importerFactory(model, importType);
0242             importer->import(path, true);
0243             importer->redo();
0244             GlobalBookmarkManager::self()->managerSave();
0245             GlobalBookmarkManager::self()->notifyManagers();
0246         }
0247         return 0; // error flag on exit?, 1?
0248     }
0249 
0250     QString address = parser.isSet(QStringLiteral("address")) ? parser.value(QStringLiteral("address")) : QStringLiteral("/0");
0251 
0252     QString caption = parser.isSet(QStringLiteral("customcaption")) ? parser.value(QStringLiteral("customcaption")) : QString();
0253 
0254     QString dbusObjectName;
0255     if (parser.isSet(QStringLiteral("dbusObjectName"))) {
0256         dbusObjectName = parser.value(QStringLiteral("dbusObjectName"));
0257     } else {
0258         if (gotFilenameArg)
0259             dbusObjectName = QString();
0260         else
0261             dbusObjectName = QStringLiteral("konqueror");
0262     }
0263 
0264     bool readonly = false; // passed by ref
0265 
0266     if (askUser((gotFilenameArg ? filename : QString()), readonly)) {
0267         KEBApp *toplevel = new KEBApp(filename, readonly, address, browser, caption, dbusObjectName);
0268         toplevel->setAttribute(Qt::WA_DeleteOnClose);
0269         toplevel->show();
0270         return app.exec();
0271     }
0272 
0273     return 0;
0274 }