File indexing completed on 2024-05-19 04:48:57

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Aaron Seigo <aseigo@kde.org>                                      *
0003  * Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>                                    *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #include "../AmarokContextPackageStructure.h"
0019 
0020 #include <iostream>
0021 
0022 #include <QApplication>
0023 #include <QAction>
0024 #include <QCommandLineParser>
0025 #include <QDBusInterface>
0026 #include <QDir>
0027 #include <QLocale>
0028 #include <QStandardPaths>
0029 #include <QTextStream>
0030 
0031 #include <KAboutData>
0032 #include <KLocalizedString>
0033 #include <KShell>
0034 #include <KProcess>
0035 #include <KConfigGroup>
0036 #include <KPackage/PackageLoader>
0037 #include <KPackage/Package>
0038 
0039 
0040 static const char description[] = "Install, list, remove Amarok applets";
0041 static const char version[] = "0.2";
0042 
0043 
0044 void output(const QString &msg)
0045 {
0046     std::cout << msg.toLocal8Bit().constData() << std::endl;
0047 }
0048 
0049 void runKbuildsycoca()
0050 {
0051     QDBusInterface dbus(QStringLiteral("org.kde.kded"), QStringLiteral("/kbuildsycoca"), QStringLiteral("org.kde.kbuildsycoca"));
0052     dbus.call(QDBus::NoBlock, QStringLiteral("recreate"));
0053 }
0054 
0055 QStringList packages()
0056 {
0057     auto loader = KPackage::PackageLoader::self();
0058     auto structure = new AmarokContextPackageStructure;
0059     loader->addKnownPackageStructure(QStringLiteral("Amarok/ContextApplet"), structure);
0060     auto applets = loader->findPackages(QStringLiteral("Amarok/ContextApplet"),
0061                                         QString(),
0062                                         [] (const KPluginMetaData &data)
0063                                         { return data.serviceTypes().contains(QStringLiteral("Amarok/ContextApplet")); });
0064 
0065     QStringList result;
0066 
0067     for (const auto &applet : applets)
0068         result << applet.pluginId();
0069 
0070     return result;
0071 }
0072 
0073 void listPackages()
0074 {
0075     QStringList list = packages();
0076     list.sort();
0077     foreach(const QString& package, list) {
0078         output(package);
0079     }
0080 }
0081 
0082 int main(int argc, char **argv)
0083 {
0084     KAboutData aboutData(QStringLiteral("amarokpkg"), i18n("Amarok Applet Manager"),
0085                          version, i18n(description), KAboutLicense::GPL,
0086                          i18n("(C) 2008, Aaron Seigo, (C) 2009, Leo Franchi"));
0087     aboutData.addAuthor( i18n("Aaron Seigo"),
0088                          i18n("Original author"),
0089                         QStringLiteral("aseigo@kde.org") );
0090     aboutData.addAuthor( i18n( "Leo Franchi" ),
0091                          i18n( "Developer" ) ,
0092                          QStringLiteral("lfranchi@kde.org")  );
0093 
0094     QApplication app(argc, argv);
0095     app.setApplicationName(QStringLiteral("amarokpkg"));
0096     app.setOrganizationDomain(QStringLiteral("kde.org"));
0097     app.setApplicationDisplayName(i18n("Amarok Applet Manager"));
0098     app.setApplicationVersion(version);
0099 
0100     /**
0101      * TODO: DO WE NEED THIS ?
0102      */
0103     KAboutData::setApplicationData(aboutData);
0104 
0105     QCommandLineParser parser;
0106 
0107     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("g") << QStringLiteral("global"),
0108                                         i18n("For install or remove, operates on applets installed for all users.")));
0109     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("s") << QStringLiteral("i") << QStringLiteral("install <path>"),
0110                                         i18nc("Do not translate <path>", "Install the applet at <path>")));
0111     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("u") << QStringLiteral("upgrade <path>"),
0112                                         i18nc("Do not translate <path>", "Upgrade the applet at <path>")));
0113     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("l") << QStringLiteral("list"),
0114                                         i18n("Most installed applets")));
0115     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("r") << QStringLiteral("remove <name>"),
0116                                         i18nc("Do not translate <name>", "Remove the applet named <name>")));
0117     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("p") << QStringLiteral("packageroot <path>"),
0118                                         i18n("Absolute path to the package root. If not supplied, then the standard data directories for this KDE session will be searched instead.")));
0119 
0120     parser.addVersionOption();
0121     parser.addHelpOption();
0122     aboutData.setupCommandLine(&parser);
0123     parser.process(app);
0124     aboutData.processCommandLine(&parser);
0125 
0126     QString packageRoot = QStringLiteral("kpackage/amarok");
0127     KPackage::Package *installer = nullptr;
0128 
0129     if (parser.isSet(QStringLiteral("list"))) {
0130         listPackages();
0131     } else {
0132         // install, remove or upgrade
0133         if (!installer)
0134             installer = new KPackage::Package(new AmarokContextPackageStructure);
0135 
0136         if (parser.isSet(QStringLiteral("packageroot"))) {
0137             packageRoot = parser.value(QStringLiteral("packageroot"));
0138         } else if (parser.isSet(QStringLiteral("global"))) {
0139             packageRoot = QStandardPaths::locate(QStandardPaths::GenericDataLocation, packageRoot);
0140         } else {
0141             //@FIXME Maybe we need to check whether the folders exist or not.
0142             packageRoot = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + packageRoot;
0143         }
0144 
0145         QString package;
0146         QString packageFile;
0147         if (parser.isSet(QStringLiteral("remove"))) {
0148             package = parser.value(QStringLiteral("remove"));
0149         } else if (parser.isSet(QStringLiteral("upgrade"))) {
0150             package = parser.value(QStringLiteral("upgrade"));
0151         } else if (parser.isSet(QStringLiteral("install"))) {
0152             package = parser.value(QStringLiteral("install"));
0153         }
0154         if (!QDir::isAbsolutePath(package)) {
0155             packageFile = QDir(QDir::currentPath() + QLatin1Char('/') + package).absolutePath();
0156         } else {
0157             packageFile = package;
0158         }
0159 
0160         if (parser.isSet(QStringLiteral("remove")) || parser.isSet(QStringLiteral("upgrade"))) {
0161             installer->setPath(packageFile);
0162             KPluginMetaData metadata = installer->metadata();
0163 
0164             QString pluginId;
0165             if (metadata.name().isEmpty()) {
0166                 // plugin name given in command line
0167                 pluginId = package;
0168             } else {
0169                 // Parameter was a plasma package, get plugin name from the package
0170                 pluginId = metadata.pluginId();
0171             }
0172 
0173             QStringList installed = packages();
0174             if (installed.contains(pluginId)) {
0175                 if (installer->uninstall(pluginId, packageRoot)) {
0176                     output(i18n("Successfully removed %1", pluginId));
0177                 } else if (!parser.isSet(QStringLiteral("upgrade"))) {
0178                     output(i18n("Removal of %1 failed.", pluginId));
0179                     delete installer;
0180                     return 1;
0181                 }
0182             } else {
0183                 output(i18n("Plugin %1 is not installed.", pluginId));
0184             }
0185         }
0186         if (parser.isSet(QStringLiteral("install")) || parser.isSet(QStringLiteral("upgrade"))) {
0187             if (installer->install(packageFile, packageRoot)) {
0188                 output(i18n("Successfully installed %1", packageFile));
0189                 runKbuildsycoca();
0190             } else {
0191                 output(i18n("Installation of %1 failed.", packageFile));
0192                 delete installer;
0193                 return 1;
0194             }
0195         }
0196 
0197         if (package.isEmpty()) {
0198             QTextStream out(stdout);
0199             out << i18nc("No option was given, this is the error message telling the user he needs at least one, do not translate install, remove, upgrade nor list", "One of install, remove, upgrade or list is required.");
0200         } else {
0201             runKbuildsycoca();
0202         }
0203     }
0204     delete installer;
0205     return 0;
0206 }
0207 
0208 
0209