File indexing completed on 2022-11-29 20:24:10

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 <kwidgetsaddons_version.h>
0046 #include <toplevel_interface.h>
0047 
0048 // TODO - make this register() or something like that and move dialog into main
0049 static bool askUser(const QString &filename, bool &readonly)
0050 {
0051     QString interfaceName = QStringLiteral("org.kde.keditbookmarks");
0052     QString appId = interfaceName + QLatin1Char('-') + QString().setNum(QApplication::applicationPid());
0053 
0054     QDBusConnection dbus = QDBusConnection::sessionBus();
0055     QDBusReply<QStringList> reply = dbus.interface()->registeredServiceNames();
0056     if (!reply.isValid())
0057         return true;
0058     const QStringList allServices = reply;
0059     for (QStringList::const_iterator it = allServices.begin(), end = allServices.end(); it != end; ++it) {
0060         const QString service = *it;
0061         if (service.startsWith(interfaceName) && service != appId) {
0062             org::kde::keditbookmarks keditbookmarks(service, QStringLiteral("/keditbookmarks"), dbus);
0063             QDBusReply<QString> bookmarks = keditbookmarks.bookmarkFilename();
0064             QString name;
0065             if (bookmarks.isValid())
0066                 name = bookmarks;
0067             if (name == filename) {
0068 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0069                 int ret = KMessageBox::warningTwoActions(nullptr,
0070 #else
0071                 int ret = KMessageBox::warningYesNo(nullptr,
0072 #endif
0073                                                          i18n("Another instance of %1 is already running. Do you really "
0074                                                               "want to open another instance or continue work in the same instance?\n"
0075                                                               "Please note that, unfortunately, duplicate views are read-only.",
0076                                                               QGuiApplication::applicationDisplayName()),
0077                                                          i18nc("@title:window", "Warning"),
0078                                                          KGuiItem(i18n("Run Another")), /* yes */
0079                                                          KGuiItem(i18n("Continue in Same")) /*  no */);
0080 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0081                 if (ret == KMessageBox::ButtonCode::SecondaryAction) {
0082 #else
0083                 if (ret == KMessageBox::No) {
0084 #endif
0085                     QDBusInterface keditinterface(service, QStringLiteral("/keditbookmarks/MainWindow_1"));
0086                     // TODO fix me
0087                     QDBusReply<qlonglong> value = keditinterface.call(QDBus::NoBlock, QStringLiteral("winId"));
0088                     qlonglong id = 0;
0089                     if (value.isValid())
0090                         id = value;
0091                     ////qCDebug(KEDITBOOKMARKS_LOG)<<" id !!!!!!!!!!!!!!!!!!! :"<<id;
0092                     KWindowSystem::activateWindow((WId)id);
0093                     return false;
0094 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0095                 } else if (ret == KMessageBox::ButtonCode::PrimaryAction) {
0096 #else
0097                 } else if (ret == KMessageBox::Yes) {
0098 #endif
0099                     readonly = true;
0100                 }
0101             }
0102         }
0103     }
0104     return true;
0105 }
0106 
0107 int main(int argc, char **argv)
0108 {
0109 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0110     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
0111 #endif
0112     QApplication app(argc, argv);
0113 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0114     // enable high dpi support
0115     app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0116     Kdelibs4ConfigMigrator migrate(QStringLiteral("keditbookmarks"));
0117     migrate.setConfigFiles(QStringList() << QStringLiteral("keditbookmarksrc"));
0118     migrate.setUiFiles(QStringList() << QStringLiteral("keditbookmarksuirc"));
0119     migrate.migrate();
0120 #endif
0121 
0122     KLocalizedString::setApplicationDomain("keditbookmarks");
0123 
0124     KAboutData aboutData(QStringLiteral("keditbookmarks"),
0125                          i18n("Bookmark Editor"),
0126                          QStringLiteral(KEDITBOOKMARKS_VERSION_STRING),
0127                          i18n("Bookmark Organizer and Editor"),
0128                          KAboutLicense::GPL,
0129                          i18n("Copyright 2000-2017, KDE developers"));
0130     aboutData.addAuthor(i18n("David Faure"), i18n("Initial author"), QStringLiteral("faure@kde.org"));
0131     aboutData.addAuthor(i18n("Alexander Kellett"), i18n("Author"), QStringLiteral("lypanov@kde.org"));
0132 
0133     aboutData.setDesktopFileName(QStringLiteral("org.kde.keditbookmarks"));
0134     KAboutData::setApplicationData(aboutData);
0135 
0136     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("bookmarks-organize")));
0137 
0138     QCommandLineParser parser;
0139     parser.setApplicationDescription(aboutData.shortDescription());
0140     // clang-format off
0141     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importmoz"), i18n("Import bookmarks from a file in Mozilla format"), QStringLiteral("filename")));
0142     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importns"), i18n("Import bookmarks from a file in Netscape (4.x and earlier) format"), QStringLiteral("filename")));
0143     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importie"), i18n("Import bookmarks from a file in Internet Explorer's Favorites format"), QStringLiteral("filename")));
0144     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importopera"), i18n("Import bookmarks from a file in Opera format"), QStringLiteral("filename")));
0145     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importkde3"), i18n("Import bookmarks from a file in KDE2 format"), QStringLiteral("filename")));
0146     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("importgaleon"), i18n("Import bookmarks from a file in Galeon format"), QStringLiteral("filename")));
0147     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportmoz"), i18n("Export bookmarks to a file in Mozilla format"), QStringLiteral("filename")));
0148     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportns"), i18n("Export bookmarks to a file in Netscape (4.x and earlier) format"), QStringLiteral("filename")));
0149     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exporthtml"), i18n("Export bookmarks to a file in a printable HTML format"), QStringLiteral("filename")));
0150     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportie"), i18n("Export bookmarks to a file in Internet Explorer's Favorites format"), QStringLiteral("filename")));
0151     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("exportopera"), i18n("Export bookmarks to a file in Opera format"), QStringLiteral("filename")));
0152     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("address"), i18n("Open at the given position in the bookmarks file"), QStringLiteral("address")));
0153     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("customcaption"), i18n("Set the user-readable caption, for example \"Konsole\""), QStringLiteral("caption")));
0154     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("nobrowser"), i18n("Hide all browser related functions")));
0155     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("dbusObjectName"), i18n("A unique name that represents this bookmark collection, usually the kinstance name.\n"
0156                                  "This should be \"konqueror\" for the Konqueror bookmarks, \"kfile\" for KFileDialog bookmarks, etc.\n"
0157                                  "The final D-Bus object path is /KBookmarkManager/<dbusObjectName>"), QStringLiteral("name")));
0158     // clang-format on
0159     parser.addPositionalArgument(QStringLiteral("[file]"), i18n("File to edit"));
0160 
0161     aboutData.setupCommandLine(&parser);
0162     parser.process(app);
0163     aboutData.processCommandLine(&parser);
0164 
0165     const bool isGui = !(parser.isSet(QStringLiteral("exportmoz")) || parser.isSet(QStringLiteral("exportns")) || parser.isSet(QStringLiteral("exporthtml")) //
0166                          || parser.isSet(QStringLiteral("exportie")) || parser.isSet(QStringLiteral("exportopera")) //
0167                          || parser.isSet(QStringLiteral("importmoz")) || parser.isSet(QStringLiteral("importns")) //
0168                          || parser.isSet(QStringLiteral("importie")) || parser.isSet(QStringLiteral("importopera")) //
0169                          || parser.isSet(QStringLiteral("importkde3")) || parser.isSet(QStringLiteral("importgaleon")));
0170 
0171     const bool browser = !parser.isSet(QStringLiteral("nobrowser"));
0172     const bool gotFilenameArg = (parser.positionalArguments().count() == 1);
0173 
0174     QString filename = gotFilenameArg ? parser.positionalArguments().at(0)
0175                                       : QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/bookmarks.xml");
0176 
0177     if (!isGui) {
0178         GlobalBookmarkManager::self()->createManager(filename, QString(), new CommandHistory());
0179         GlobalBookmarkManager::ExportType exportType = GlobalBookmarkManager::MozillaExport; // uumm.. can i just set it to -1 ?
0180         int got = 0;
0181         QString arg;
0182         QString arg2;
0183         QString importType;
0184         if (arg = QStringLiteral("exportmoz"), parser.isSet(arg)) {
0185             exportType = GlobalBookmarkManager::MozillaExport;
0186             arg2 = arg;
0187             got++;
0188         }
0189         if (arg = QStringLiteral("exportns"), parser.isSet(arg)) {
0190             exportType = GlobalBookmarkManager::NetscapeExport;
0191             arg2 = arg;
0192             got++;
0193         }
0194         if (arg = QStringLiteral("exporthtml"), parser.isSet(arg)) {
0195             exportType = GlobalBookmarkManager::HTMLExport;
0196             arg2 = arg;
0197             got++;
0198         }
0199         if (arg = QStringLiteral("exportie"), parser.isSet(arg)) {
0200             exportType = GlobalBookmarkManager::IEExport;
0201             arg2 = arg;
0202             got++;
0203         }
0204         if (arg = QStringLiteral("exportopera"), parser.isSet(arg)) {
0205             exportType = GlobalBookmarkManager::OperaExport;
0206             arg2 = arg;
0207             got++;
0208         }
0209         if (arg = QStringLiteral("importmoz"), parser.isSet(arg)) {
0210             importType = QStringLiteral("Moz");
0211             arg2 = arg;
0212             got++;
0213         }
0214         if (arg = QStringLiteral("importns"), parser.isSet(arg)) {
0215             importType = QStringLiteral("NS");
0216             arg2 = arg;
0217             got++;
0218         }
0219         if (arg = QStringLiteral("importie"), parser.isSet(arg)) {
0220             importType = QStringLiteral("IE");
0221             arg2 = arg;
0222             got++;
0223         }
0224         if (arg = QStringLiteral("importopera"), parser.isSet(arg)) {
0225             importType = QStringLiteral("Opera");
0226             arg2 = arg;
0227             got++;
0228         }
0229         if (arg = QStringLiteral("importgaleon"), parser.isSet(arg)) {
0230             importType = QStringLiteral("Galeon");
0231             arg2 = arg;
0232             got++;
0233         }
0234         if (arg = QStringLiteral("importkde3"), parser.isSet(arg)) {
0235             importType = QStringLiteral("KDE2");
0236             arg2 = arg;
0237             got++;
0238         }
0239         if (importType.isEmpty() && !arg2.isEmpty()) {
0240             // TODO - maybe an xbel export???
0241             if (got > 1) { // got == 0 isn't possible as !isGui is dependent on "export.*"
0242                 qCWarning(KEDITBOOKMARKS_LOG) << i18n("You may only specify a single --export option.");
0243                 return 1;
0244             }
0245             QString path = parser.value(arg2);
0246             GlobalBookmarkManager::self()->doExport(exportType, path);
0247         } else if (!importType.isEmpty()) {
0248             if (got > 1) { // got == 0 isn't possible as !isGui is dependent on "import.*"
0249                 qCWarning(KEDITBOOKMARKS_LOG) << i18n("You may only specify a single --import option.");
0250                 return 1;
0251             }
0252             QString path = parser.value(arg2);
0253             KBookmarkModel *model = GlobalBookmarkManager::self()->model();
0254             ImportCommand *importer = ImportCommand::importerFactory(model, importType);
0255             importer->import(path, true);
0256             importer->redo();
0257             GlobalBookmarkManager::self()->managerSave();
0258             GlobalBookmarkManager::self()->notifyManagers();
0259         }
0260         return 0; // error flag on exit?, 1?
0261     }
0262 
0263     QString address = parser.isSet(QStringLiteral("address")) ? parser.value(QStringLiteral("address")) : QStringLiteral("/0");
0264 
0265     QString caption = parser.isSet(QStringLiteral("customcaption")) ? parser.value(QStringLiteral("customcaption")) : QString();
0266 
0267     QString dbusObjectName;
0268     if (parser.isSet(QStringLiteral("dbusObjectName"))) {
0269         dbusObjectName = parser.value(QStringLiteral("dbusObjectName"));
0270     } else {
0271         if (gotFilenameArg)
0272             dbusObjectName = QString();
0273         else
0274             dbusObjectName = QStringLiteral("konqueror");
0275     }
0276 
0277     bool readonly = false; // passed by ref
0278 
0279     if (askUser((gotFilenameArg ? filename : QString()), readonly)) {
0280         KEBApp *toplevel = new KEBApp(filename, readonly, address, browser, caption, dbusObjectName);
0281         toplevel->setAttribute(Qt::WA_DeleteOnClose);
0282         toplevel->show();
0283         return app.exec();
0284     }
0285 
0286     return 0;
0287 }