File indexing completed on 2025-01-12 05:01:59

0001 /*
0002     SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "Installer.h"
0007 #include "FontsPackage.h"
0008 #include "JobRunner.h"
0009 #include "Misc.h"
0010 #include "config-workspace.h"
0011 #include <KAboutData>
0012 #include <KIO/StatJob>
0013 #include <KMessageBox>
0014 #include <QApplication>
0015 #include <QCommandLineOption>
0016 #include <QCommandLineParser>
0017 #include <QFile>
0018 #include <QTemporaryDir>
0019 
0020 // This include must be at the end
0021 #include "CreateParent.h"
0022 
0023 namespace KFI
0024 {
0025 int CInstaller::install(const QSet<QUrl> &urls)
0026 {
0027     QSet<QUrl>::ConstIterator it(urls.begin()), end(urls.end());
0028     bool sysInstall(false);
0029     CJobRunner *jobRunner = new CJobRunner(m_parent);
0030 
0031     CJobRunner::startDbusService();
0032 
0033     if (!Misc::root()) {
0034         switch (KMessageBox::questionTwoActionsCancel(m_parent,
0035                                                       i18n("Do you wish to install the font(s) for personal use "
0036                                                            "(only available to you), or "
0037                                                            "system-wide (available to all users)?"),
0038                                                       i18n("Where to Install"),
0039                                                       KGuiItem(KFI_KIO_FONTS_USER.toString()),
0040                                                       KGuiItem(KFI_KIO_FONTS_SYS.toString()))) {
0041         case KMessageBox::SecondaryAction:
0042             sysInstall = true;
0043             break;
0044         case KMessageBox::Cancel:
0045             return -1;
0046         default:
0047             break;
0048         }
0049     }
0050 
0051     QSet<QUrl> instUrls;
0052 
0053     for (; it != end; ++it) {
0054         auto job = KIO::mostLocalUrl(*it);
0055         job->exec();
0056         QUrl local = job->mostLocalUrl();
0057         bool package(false);
0058 
0059         if (local.isLocalFile()) {
0060             QString localFile(local.toLocalFile());
0061 
0062             if (Misc::isPackage(localFile)) {
0063                 instUrls += FontsPackage::extract(localFile, &m_tempDir);
0064                 package = true;
0065             }
0066         }
0067         if (!package) {
0068             QList<QUrl> associatedUrls;
0069 
0070             CJobRunner::getAssociatedUrls(*it, associatedUrls, false, m_parent);
0071             instUrls.insert(*it);
0072 
0073             QList<QUrl>::Iterator aIt(associatedUrls.begin()), aEnd(associatedUrls.end());
0074 
0075             for (; aIt != aEnd; ++aIt) {
0076                 instUrls.insert(*aIt);
0077             }
0078         }
0079     }
0080 
0081     if (!instUrls.isEmpty()) {
0082         CJobRunner::ItemList list;
0083         QSet<QUrl>::ConstIterator it(instUrls.begin()), end(instUrls.end());
0084 
0085         for (; it != end; ++it) {
0086             list.append(*it);
0087         }
0088 
0089         return jobRunner->exec(CJobRunner::CMD_INSTALL, list, Misc::root() || sysInstall);
0090     } else {
0091         return -1;
0092     }
0093 }
0094 
0095 CInstaller::~CInstaller()
0096 {
0097     delete m_tempDir;
0098 }
0099 
0100 }
0101 
0102 int main(int argc, char **argv)
0103 {
0104     QApplication app(argc, argv);
0105 
0106     KLocalizedString::setApplicationDomain(KFI_CATALOGUE);
0107     KAboutData aboutData("kfontinst",
0108                          i18n("Font Installer"),
0109                          WORKSPACE_VERSION_STRING,
0110                          i18n("Simple font installer"),
0111                          KAboutLicense::GPL,
0112                          i18n("(C) Craig Drummond, 2007"));
0113     KAboutData::setApplicationData(aboutData);
0114 
0115     QGuiApplication::setWindowIcon(QIcon::fromTheme("preferences-desktop-font-installer"));
0116 
0117     QCommandLineParser parser;
0118     const QCommandLineOption embedOption(QLatin1String("embed"), i18n("Makes the dialog transient for an X app specified by winid"), QLatin1String("winid"));
0119     parser.addOption(embedOption);
0120     parser.addPositionalArgument(QLatin1String("[URL]"), i18n("URL to install"));
0121 
0122     aboutData.setupCommandLine(&parser);
0123     parser.process(app);
0124     aboutData.processCommandLine(&parser);
0125 
0126     QSet<QUrl> urls;
0127 
0128     foreach (const QString &arg, parser.positionalArguments())
0129         urls.insert(QUrl::fromUserInput(arg, QDir::currentPath()));
0130 
0131     if (!urls.isEmpty()) {
0132         QString opt(parser.value(embedOption));
0133         KFI::CInstaller inst(createParent(opt.size() ? opt.toInt(nullptr, 16) : 0));
0134 
0135         return inst.install(urls);
0136     }
0137 
0138     return -1;
0139 }