not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / tools / plasmapkg / main.cpp
bloba6df6e337dc895be54a7d6a8c5b647bd8a6d7c85
1 /*
2 * Copyright 2008 Aaron Seigo <aseigo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <iostream>
22 #include <QDir>
23 #include <QDBusInterface>
25 #include <KApplication>
26 #include <KAboutData>
27 #include <KAction>
28 #include <KCmdLineArgs>
29 #include <KLocale>
30 #include <KService>
31 #include <KServiceTypeTrader>
32 #include <KShell>
33 #include <KStandardDirs>
34 #include <KProcess>
35 #include <KSycoca>
36 #include <KConfigGroup>
38 #include <Plasma/PackageStructure>
39 #include <Plasma/Package>
40 #include <Plasma/PackageMetadata>
42 static const char description[] = I18N_NOOP("Install, list, remove Plasma packages");
43 static const char version[] = "0.1";
45 void output(const QString &msg)
47 std::cout << msg.toLocal8Bit().constData() << std::endl;
50 void runKbuildsycoca()
52 QDBusInterface dbus("org.kde.kded", "/kbuildsycoca", "org.kde.kbuildsycoca");
53 dbus.call(QDBus::Block, "recreate");
56 QStringList packages(const QString& type)
58 QStringList result;
59 KService::List services = KServiceTypeTrader::self()->query("Plasma/" + type);
60 foreach(const KService::Ptr &service, services) {
61 result << service->property("X-KDE-PluginInfo-Name", QVariant::String).toString();
63 return result;
66 void listPackages(const QString& type)
68 QStringList list = packages(type);
69 list.sort();
70 foreach(const QString& package, list) {
71 output(package);
75 int main(int argc, char **argv)
77 KAboutData aboutData("plasmapkg", 0, ki18n("Plasma Package Manager"),
78 version, ki18n(description), KAboutData::License_GPL,
79 ki18n("(C) 2008, Aaron Seigo"));
80 aboutData.addAuthor( ki18n("Aaron Seigo"),
81 ki18n("Original author"),
82 "aseigo@kde.org" );
84 KComponentData componentData(aboutData);
86 KCmdLineArgs::init( argc, argv, &aboutData );
88 KCmdLineOptions options;
89 options.add("g");
90 options.add("global", ki18n("For install or remove, operates on packages installed for all users."));
91 options.add("t");
92 options.add("type <type>",
93 ki18nc("theme, wallpaper, etc. are keywords, but they may be translated, as both versions "
94 "are recognized by the application "
95 "(if translated, should be same as messages with 'package type' context below)",
96 "The type of package, e.g. theme, wallpaper, plasmoid, dataengine, runner, etc."),
97 "plasmoid");
98 options.add("s");
99 options.add("i");
100 options.add("install <path>", ki18nc("Do not translate <path>", "Install the package at <path>"));
101 options.add("u");
102 options.add("upgrade <path>", ki18nc("Do not translate <path>", "Upgrade the package at <path>"));
103 options.add("l");
104 options.add("list", ki18n("List installed packages"));
105 options.add("r");
106 options.add("remove <name>", ki18nc("Do not translate <name>", "Remove the package named <name>"));
107 options.add("p");
108 options.add("packageroot <path>", ki18n("Absolute path to the package root. If not supplied, then the standard data directories for this KDE session will be searched instead."));
109 KCmdLineArgs::addCmdLineOptions( options );
111 KApplication app;
113 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
114 const QString type = args->getOption("type").toLower();
115 QString packageRoot = type;
116 QString servicePrefix;
117 QString pluginType;
118 Plasma::PackageStructure *installer = 0;
120 if (type == i18nc("package type", "plasmoid") || type == "plasmoid") {
121 packageRoot = "plasma/plasmoids/";
122 servicePrefix = "plasma-applet-";
123 pluginType = "Applet";
124 } else if (type == i18nc("package type", "theme") || type == "theme") {
125 packageRoot = "desktoptheme/";
126 } else if (type == i18nc("package type", "wallpaper") || type == "wallpaper") {
127 packageRoot = "wallpapers/";
128 } else if (type == i18nc("package type", "dataengine") || type == "dataengine") {
129 packageRoot = "plasma/dataengines/";
130 servicePrefix = "plasma-dataengine-";
131 pluginType = "DataEngine";
132 } else if (type == i18nc("package type", "runner") || type == "runner") {
133 packageRoot = "plasma/runners/";
134 servicePrefix = "plasma-runner-";
135 pluginType = "Runner";
136 } else {
137 QString constraint = QString("'%1' == [X-KDE-PluginInfo-Name]").arg(packageRoot);
138 KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure", constraint);
139 if (offers.isEmpty()) {
140 output(i18n("Could not find a suitable installer for package of type %1", type));
141 return 1;
144 KService::Ptr offer = offers.first();
145 QString error;
146 installer = offer->createInstance<Plasma::PackageStructure>(0, QVariantList(), &error);
148 if (!installer) {
149 output(i18n("Could not load installer for package of type %1. Error reported was: %2",
150 type, error));
151 return 1;
153 packageRoot = installer->defaultPackageRoot();
154 pluginType = installer->type();
157 if (args->isSet("list")) {
158 listPackages(pluginType);
159 } else {
160 // install, remove or upgrade
161 if (!installer) {
162 installer = new Plasma::PackageStructure();
163 installer->setServicePrefix(servicePrefix);
166 if (args->isSet("packageroot")) {
167 packageRoot = args->getOption("packageroot");
168 } else if (args->isSet("global")) {
169 packageRoot = KStandardDirs::locate("data", packageRoot);
170 } else {
171 packageRoot = KStandardDirs::locateLocal("data", packageRoot);
174 QString package;
175 QString packageFile;
176 if (args->isSet("remove")) {
177 package = args->getOption("remove");
178 } else if (args->isSet("upgrade")) {
179 package = args->getOption("upgrade");
180 } else if (args->isSet("install")) {
181 package = args->getOption("install");
183 if (!QDir::isAbsolutePath(package)) {
184 packageFile = QDir(QDir::currentPath() + '/' + package).absolutePath();
185 } else {
186 packageFile = package;
189 if (args->isSet("remove") || args->isSet("upgrade")) {
190 installer->setPath(packageFile);
191 Plasma::PackageMetadata metadata = installer->metadata();
193 QString pluginName;
194 if (metadata.pluginName().isEmpty()) {
195 // plugin name given in command line
196 pluginName = package;
197 } else {
198 // Parameter was a plasma package, get plugin name from the package
199 pluginName = metadata.pluginName();
202 QStringList installed = packages(pluginType);
203 if (installed.contains(pluginName)) {
204 if (installer->uninstallPackage(pluginName, packageRoot)) {
205 output(i18n("Successfully removed %1", pluginName));
206 } else if (!args->isSet("upgrade")) {
207 output(i18n("Removal of %1 failed.", pluginName));
208 return 1;
210 } else {
211 output(i18n("Plugin %1 is not installed.", pluginName));
214 if (args->isSet("install") || args->isSet("upgrade")) {
215 if (installer->installPackage(packageFile, packageRoot)) {
216 output(i18n("Successfully installed %1", packageFile));
217 } else {
218 output(i18n("Installation of %1 failed.", packageFile));
219 return 1;
222 if (package.isEmpty()) {
223 KCmdLineArgs::usageError(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."));
224 } else {
225 runKbuildsycoca();
228 return 0;