File indexing completed on 2024-04-28 16:53:01

0001 /*
0002  * SPDX-FileCopyrightText: 2007 Frerich Raabe <raabe@kde.org>
0003  * SPDX-FileCopyrightText: 2007-2008 Aaron Seigo <aseigo@kde.org>
0004  *
0005  * SPDX-License-Identifier: BSD-2-Clause
0006  */
0007 
0008 #include <QApplication>
0009 #include <QQmlDebuggingEnabler>
0010 
0011 #include <KAboutData>
0012 #include <KDBusService>
0013 #include <KLocalizedString>
0014 #include <QDebug>
0015 #include <QPixmapCache>
0016 #include <QSurfaceFormat>
0017 
0018 #include <qcommandlineoption.h>
0019 #include <qcommandlineparser.h>
0020 
0021 #include "view.h"
0022 
0023 #include <Plasma/Theme>
0024 
0025 int main(int argc, char **argv)
0026 {
0027     QQmlDebuggingEnabler debugEnabler;
0028 
0029     auto format = QSurfaceFormat::defaultFormat();
0030     format.setOption(QSurfaceFormat::ResetNotification);
0031     QSurfaceFormat::setDefaultFormat(format);
0032 
0033     QApplication app(argc, argv);
0034     KLocalizedString::setApplicationDomain("plasmoidviewer");
0035 
0036     app.setApplicationName(QStringLiteral("plasmoidviewer"));
0037     app.setApplicationDisplayName(i18n("Plasmoidviewer"));
0038     app.setOrganizationDomain(QStringLiteral("kde.org"));
0039     app.setApplicationVersion(PROJECT_VERSION);
0040     app.setDesktopFileName(QStringLiteral("org.kde.plasmoidviewer"));
0041     QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("plasma"), app.windowIcon()));
0042 
0043     QCommandLineParser parser;
0044     parser.setApplicationDescription(i18n("Run Plasma widgets in their own window"));
0045 
0046     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("c") << QStringLiteral("containment"),
0047                                         i18n("The name of the containment plugin"),
0048                                         QStringLiteral("containment"),
0049                                         QStringLiteral("org.kde.desktopcontainment")));
0050     parser.addOption(
0051         QCommandLineOption(QStringList() << QStringLiteral("a") << QStringLiteral("applet"), i18n("The name of the applet plugin"), QStringLiteral("applet")));
0052     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("f") << QStringLiteral("formfactor"),
0053                                         i18n("The formfactor to use (horizontal, vertical, mediacenter, planar or application)"),
0054                                         QStringLiteral("formfactor"),
0055                                         QStringLiteral("planar")));
0056     parser.addOption(QCommandLineOption(
0057         QStringList() << QStringLiteral("l") << QStringLiteral("location"),
0058         i18n("The location constraint to start the Containment with (floating, desktop, fullscreen, topedge, bottomedge, leftedge, rightedge)"),
0059         QStringLiteral("location"),
0060         QStringLiteral("floating")));
0061 
0062     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("x") << QStringLiteral("xPosition"),
0063                                         i18n("Set the x position of the plasmoidviewer on the Plasma desktop"),
0064                                         QStringLiteral("xPosition")));
0065     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("y") << QStringLiteral("yPosition"),
0066                                         i18n("Set the y position of the plasmoidviewer on the Plasma desktop"),
0067                                         QStringLiteral("yPosition")));
0068     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("s") << QStringLiteral("size"),
0069                                         i18n("Set the window size of the plasmoidview"),
0070                                         QStringLiteral("widthXheight")));
0071     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("p") << QStringLiteral("pixmapcache"),
0072                                         i18n("The size in kB to set the pixmap cache to"),
0073                                         QStringLiteral("size")));
0074     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("t") << QStringLiteral("theme"),
0075                                         i18n("The name of the theme which the shell will use"),
0076                                         QStringLiteral("themeName")));
0077 
0078     parser.addPositionalArgument(QStringLiteral("externalData"), i18n("Data that should be passed to the applet as 'externalData' event"));
0079 
0080     parser.addHelpOption();
0081     parser.addVersionOption();
0082     parser.process(app);
0083 
0084     KDBusService service(KDBusService::Multiple);
0085 
0086     const QString applet = parser.value("applet");
0087     if (applet.isEmpty()) {
0088         qWarning() << "An applet name must be specified";
0089         return 1;
0090     }
0091 
0092     Plasma::Theme theme;
0093     if (parser.isSet("theme")) {
0094         theme.setUseGlobalSettings(false);
0095         theme.setThemeName(parser.value("theme"));
0096     }
0097 
0098     View *v = new View(View::createCorona(), false);
0099 
0100     v->addContainment(parser.value("containment"));
0101     v->addFormFactor(parser.value("formfactor"));
0102     v->addApplet(applet);
0103     v->addLocation(parser.value("location"));
0104 
0105     if (parser.isSet("size")) {
0106         // The size could be 800X640 or 800x640, so always do toLower.
0107         const QStringList realSize = parser.value("size").toLower().split(QChar('x'));
0108 
0109         // check if the parameter is valid.
0110         if (!parser.value("size").toLower().contains(QChar('x'))) {
0111             qWarning() << "The size " + parser.value("size") + " is not valid, the size parameter will be ignored.";
0112         } else {
0113             const int realWidth = realSize.at(0).toInt();
0114             const int realHeight = realSize.at(1).toInt();
0115             if (realWidth != 0 && realHeight != 0) {
0116                 v->setWidth(realWidth);
0117                 v->setHeight(realHeight);
0118             }
0119         }
0120     }
0121 
0122     if (parser.isSet("xPosition")) {
0123         v->setX(parser.value("xPosition").toInt());
0124     }
0125 
0126     if (parser.isSet("yPosition")) {
0127         v->setY(parser.value("yPosition").toInt());
0128     }
0129 
0130     if (parser.isSet("pixmapcache")) {
0131         QPixmapCache::setCacheLimit(parser.value("pixmapcache").toInt());
0132     }
0133 
0134     // emit externalData event so we you can launch e.g. an icon applet already with a proper URL
0135     if (parser.positionalArguments().count() == 1) {
0136         v->emitExternalData(parser.positionalArguments().constFirst());
0137     }
0138 
0139     v->show();
0140 
0141     return app.exec();
0142 }