File indexing completed on 2023-05-30 11:38:29

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2002 by Patrick Charbonnier <pch@freeshell.org>
0004    Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 
0012 #include <KAboutData>
0013 #include <KDBusService>
0014 #include <KLocalizedString>
0015 #include <KWindowSystem>
0016 #include <Kdelibs4ConfigMigrator>
0017 #include <Kdelibs4Migration>
0018 
0019 #include <QCommandLineOption>
0020 #include <QCommandLineParser>
0021 #include <QStandardPaths>
0022 
0023 #include "core/kget.h"
0024 #include "dbus/dbuskgetwrapper.h"
0025 #include "kget_version.h"
0026 #include "mainadaptor.h"
0027 #include "mainwindow.h"
0028 #include "settings.h"
0029 #include "ui/newtransferdialog.h"
0030 
0031 class KGetApp : public QObject
0032 {
0033 public:
0034     KGetApp(QCommandLineParser *p)
0035         : kget(nullptr)
0036         , parser(p)
0037     {
0038     }
0039 
0040     ~KGetApp() override
0041     {
0042         delete kget;
0043     }
0044 
0045     int newInstance()
0046     {
0047         if (!kget) {
0048 #ifdef DEBUG
0049             kget = new MainWindow(!parser->isSet("showDropTarget"), parser->isSet("startWithoutAnimation"), parser->isSet("test"));
0050 #else
0051             kget = new MainWindow(!parser->isSet("showDropTarget"), parser->isSet("startWithoutAnimation"), false);
0052 #endif
0053 
0054             auto *wrapper = new DBusKGetWrapper(kget);
0055             new MainAdaptor(wrapper);
0056             QDBusConnection::sessionBus().registerObject("/KGet", wrapper);
0057         } else {
0058             // activate window if it is already open
0059             kget->setAttribute(Qt::WA_NativeWindow, true);
0060             KWindowSystem::updateStartupId(kget->windowHandle());
0061             KWindowSystem::activateWindow(kget->windowHandle());
0062         }
0063 
0064         if (parser->isSet("showDropTarget"))
0065             Settings::setShowDropTarget(true);
0066 
0067         QList<QUrl> l;
0068         const QStringList args = parser->positionalArguments();
0069         for (int i = 0; i < args.count(); i++) {
0070             QString txt(args.at(i));
0071             if (txt.endsWith(QLatin1String(".kgt"), Qt::CaseInsensitive))
0072                 KGet::load(txt);
0073             else
0074                 l.push_back(QUrl::fromUserInput(args.at(i)));
0075         }
0076 
0077         if (!l.isEmpty())
0078             NewTransferDialogHandler::showNewTransferDialog(l);
0079 
0080         return 0;
0081     }
0082 
0083 public Q_SLOTS:
0084     void slotActivateRequested(QStringList args, const QString & /*workingDir*/)
0085     {
0086         parser->parse(args);
0087         newInstance();
0088     }
0089 
0090 private:
0091     MainWindow *kget;
0092     QCommandLineParser *parser;
0093 };
0094 
0095 int main(int argc, char *argv[])
0096 {
0097     QApplication app(argc, argv);
0098     QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
0099     KLocalizedString::setApplicationDomain("kget");
0100     KAboutData aboutData(QStringLiteral("kget"),
0101                          i18n("KGet"),
0102                          QStringLiteral(KGET_VERSION_STRING),
0103                          i18n("An advanced download manager by KDE"),
0104                          KAboutLicense::GPL,
0105                          i18n("(C) 2005 - 2014, The KGet developers\n"
0106                               "(C) 2001 - 2002, Patrick Charbonnier\n"
0107                               "(C) 2002, Carsten Pfeiffer\n"
0108                               "(C) 1998 - 2000, Matej Koss"),
0109                          i18n("<a href=\"mailto:kget@kde.org\">kget@kde.org</a>"));
0110 
0111     aboutData.addAuthor(i18n("Lukas Appelhans"), i18n("Maintainer, Core Developer, Torrent Plugin Author"), "l.appelhans@gmx.de");
0112     aboutData.addAuthor(i18n("Dario Massarin"), i18n("Core Developer"), "nekkar@libero.it");
0113     aboutData.addAuthor(i18n("Urs Wolfer"), i18n("Core Developer"), "uwolfer@kde.org");
0114     aboutData.addAuthor(i18n("Manolo Valdes"), i18n("Core Developer, Multithreaded Plugin Author"), "nolis71cu@gmail.com");
0115     aboutData.addAuthor(i18n("Matthias Fuchs"), i18n("Core Developer"), "mat69@gmx.net");
0116     aboutData.addAuthor(i18n("Javier Goday"), i18n("Developer"), "jgoday@gmail.com");
0117     aboutData.addAuthor(i18n("Aish Raj Dahal"), i18n("Google Summer of Code Student"));
0118     aboutData.addAuthor(i18n("Ernesto Rodriguez Ortiz"), i18n("Mms Plugin Author"), "eortiz@uci.cu");
0119     aboutData.addAuthor(i18n("Patrick Charbonnier"), i18n("Former Developer"), "pch@freeshell.org");
0120     aboutData.addAuthor(i18n("Carsten Pfeiffer"), i18n("Former Developer"), "pfeiffer@kde.org");
0121     aboutData.addAuthor(i18n("Matej Koss"), i18n("Former Developer"));
0122     aboutData.addCredit(i18n("Joris Guisson"), i18n("BTCore (KTorrent) Developer"), "joris.guisson@gmail.com");
0123     aboutData.addCredit(i18n("Mensur Zahirovic (Nookie)"), i18n("Design of Web Interface"), "linuxsajten@gmail.com");
0124     // necessary to make the "Translators" tab appear in the About dialog
0125     aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0126     KAboutData::setApplicationData(aboutData);
0127     QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("kget")));
0128 
0129     QCommandLineParser parser;
0130     aboutData.setupCommandLine(&parser);
0131 
0132     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("showDropTarget"), i18n("Start KGet with drop target")));
0133     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("hideMainWindow"), i18n("Start KGet with hidden main window")));
0134     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("startWithoutAnimation"), i18n("Start KGet without drop target animation")));
0135 #ifdef DEBUG
0136     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("test"), i18n("Execute Unit Testing")));
0137 #endif
0138     parser.addPositionalArgument(QLatin1String("[URL(s)]"), i18n("URL(s) to download"));
0139 
0140     parser.process(app);
0141     aboutData.processCommandLine(&parser);
0142 
0143     KDBusService dbusService(KDBusService::Unique);
0144 
0145     Kdelibs4ConfigMigrator migrate(QStringLiteral("kget"));
0146     migrate.setConfigFiles(QStringList() << QStringLiteral("kgetrc") << QStringLiteral("kget_bittorrentfactory.rc")
0147                                          << QStringLiteral("kget_checksumsearchfactory.rc") << QStringLiteral("kget_metalinkfactory.rc")
0148                                          << QStringLiteral("kget_mirrorsearchfactory.rc") << QStringLiteral("kget_mmsfactory.rc")
0149                                          << QStringLiteral("kget_multisegkiofactory.rc") << QStringLiteral("kget.notifyrc"));
0150     if (migrate.migrate()) {
0151         Kdelibs4Migration dataMigrator;
0152         const QString sourceBasePath = dataMigrator.saveLocation("data", QStringLiteral("kget"));
0153         const QString targetBasePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kget/");
0154         QString targetFilePath;
0155 
0156         QDir sourceDir(sourceBasePath);
0157         QDir targetDir(targetBasePath);
0158 
0159         if (sourceDir.exists()) {
0160             if (!targetDir.exists()) {
0161                 QDir().mkpath(targetBasePath);
0162             }
0163             QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
0164             foreach (const QString &fileName, fileNames) {
0165                 targetFilePath = targetBasePath + fileName;
0166                 if (!QFile::exists(targetFilePath)) {
0167                     QFile::copy(sourceBasePath + fileName, targetFilePath);
0168                 }
0169             }
0170         }
0171     }
0172 
0173     KGetApp kApp(&parser);
0174 
0175     QObject::connect(&dbusService, &KDBusService::activateRequested, &kApp, &KGetApp::slotActivateRequested);
0176 
0177     kApp.newInstance();
0178 
0179     return app.exec();
0180 }