diff --git a/plasmoidviewer/main.cpp b/plasmoidviewer/main.cpp index cb34134..db4313b 100644 --- a/plasmoidviewer/main.cpp +++ b/plasmoidviewer/main.cpp @@ -1,142 +1,148 @@ /* * Copyright 2007 Frerich Raabe * Copyright 2007-2008 Aaron Seigo * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include "view.h" #include static const char version[] = PROJECT_VERSION; int main(int argc, char **argv) { QQmlDebuggingEnabler debugEnabler; QApplication app(argc, argv); KDBusService service(KDBusService::Multiple); app.setApplicationVersion(version); QCommandLineParser parser; parser.setApplicationDescription(i18n("Run Plasma widgets in their own window")); parser.addVersionOption(); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("c") << QStringLiteral("containment"), i18n("The name of the containment plugin"), QStringLiteral("containment"), QStringLiteral("org.kde.desktopcontainment"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("a") << QStringLiteral("applet"), i18n("The name of the applet plugin"), QStringLiteral("applet"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("f") << QStringLiteral("formfactor"), i18n("The formfactor to use (horizontal, vertical, mediacenter, planar or application)"), QStringLiteral("formfactor"), QStringLiteral("planar"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("l") << QStringLiteral("location"), i18n("The location constraint to start the Containment with (floating, desktop, fullscreen, topedge, bottomedge, leftedge, rightedge)"), QStringLiteral("location"), QStringLiteral("floating"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("x") << QStringLiteral("xPosition"), i18n("Set the x position of the plasmoidviewer on the plasma desktop"), QStringLiteral("xPosition"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("y") << QStringLiteral("yPosition"), i18n("Set the y position of the plasmoidviewer on the plasma desktop"), QStringLiteral("yPosition"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("s") << QStringLiteral("size"), i18n("Set the window size of the plasmoidview"), QStringLiteral("widthXheight"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("p") << QStringLiteral("pixmapcache"), i18n("The size in kB to set the pixmap cache to"), QStringLiteral("size"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("t") << QStringLiteral("theme"), i18n("The name of the theme which the shell will use"), QStringLiteral("themeName"))); + parser.addPositionalArgument(QStringLiteral("externalData"), i18n("Data that should be passed to the applet as 'externalData' event")); parser.addHelpOption(); parser.addVersionOption(); parser.process(app); const QString applet = parser.value("applet"); if (applet.isEmpty()) { qWarning() << "An applet name must be specified"; return 1; } Plasma::Theme theme; if (parser.isSet("theme")) { theme.setUseGlobalSettings(false); theme.setThemeName(parser.value("theme")); } View *v = new View(View::createCorona(), false); v->addContainment(parser.value("containment")); v->addFormFactor(parser.value("formfactor")); v->addApplet(applet); v->addLocation(parser.value("location")); if (parser.isSet("size")) { // The size could be 800X640 or 800x640, so always do toLower. const QStringList realSize = parser.value("size").toLower().split(QChar('x')); // check if the parameter is valid. if (!parser.value("size").toLower().contains(QChar('x'))) { qWarning() << "The size " + parser.value("size") + " is not valid, the size parameter will be ignored."; } else { const int realWidth = realSize.at(0).toInt(); const int realHeight = realSize.at(1).toInt(); if (realWidth != 0 && realHeight != 0) { v->setWidth(realWidth); v->setHeight(realHeight); } } } if (parser.isSet("xPosition")) { v->setX(parser.value("xPosition").toInt()); } if (parser.isSet("yPosition")) { v->setY(parser.value("yPosition").toInt()); } if (parser.isSet("pixmapcache")) { QPixmapCache::setCacheLimit(parser.value("pixmapcache").toInt()); } + // emit externalData event so we you can launch e.g. an icon applet already with a proper URL + if (parser.positionalArguments().count() == 1) { + v->emitExternalData(parser.positionalArguments().constFirst()); + } + v->show(); return app.exec(); } diff --git a/plasmoidviewer/view.cpp b/plasmoidviewer/view.cpp index 293a2e2..04fe65e 100644 --- a/plasmoidviewer/view.cpp +++ b/plasmoidviewer/view.cpp @@ -1,307 +1,324 @@ /* * Copyright 2013 Giorgos Tsiapaliokas * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include #include #include #include #include "view.h" class ViewerCorona : public Plasma::Corona { public: ViewerCorona() : Plasma::Corona(), m_view(0) {} void setView(View *view) { m_view = view; } QRect screenGeometry(int id) const { Q_UNUSED(id); if (m_view) { return m_view->geometry(); } else { return QRect(); } } private: View *m_view; }; View::View(ViewerCorona *cor, bool konsoleVisible, QWindow *parent) : PlasmaQuick::ContainmentView(cor, parent) { cor->setView(this); m_konsoleVisible = konsoleVisible; engine()->rootContext()->setContextProperty("desktop", this); setSource(QUrl::fromLocalFile(cor->package().filePath("views", "Desktop.qml"))); } View::~View() { } QString View::pluginFromPath(const QString &path) const { QDir dir(path); if (!dir.exists()) { return QString(); } QString metadataPath = dir.absolutePath(); if (!QFile(metadataPath).exists()) { return QString(); } else { return metadataPath; } } void View::addApplet(const QString &applet) { QString metadataPath = pluginFromPath(applet); Plasma::Containment *c = containment(); if (!c) { qCritical("Containment doesn't exist"); return; } Plasma::Applet *a = 0; if (metadataPath.isEmpty()) { a = containment()->createApplet(applet); } else { a = Plasma::Applet::loadPlasmoid(metadataPath); containment()->addApplet(a); } if (!a->pluginInfo().isValid()) { // xgettext:no-c-format qCritical() << i18n("Applet %1 does not exist.", applet); return; } m_lastAppletName = applet; } void View::addContainment(const QString &cont) { QString actualCont = pluginFromPath(cont); if (actualCont.isEmpty()) { actualCont = cont; } Plasma::Containment *c = corona()->createContainment(actualCont); if (!c->pluginInfo().isValid()) { // xgettext:no-c-format qCritical() << i18n("Containment %1 does not exist.", actualCont); return; } setContainment(c); connect(containment(), &Plasma::Containment::appletRemoved, [=](Plasma::Applet *applet) { if (applet && applet->pluginInfo().isValid()) { addApplet(applet->pluginInfo().pluginName()); } }); } void View::addFormFactor(const QString &formFactor) { Plasma::Types::FormFactor formFactorType = Plasma::Types::Planar; const QString ff = formFactor.toLower(); if (ff.isEmpty() || ff == QStringLiteral("planar")) { formFactorType = Plasma::Types::Planar; } else if (ff == QStringLiteral("vertical")) { formFactorType = Plasma::Types::Vertical; } else if (ff == QStringLiteral("horizontal")) { formFactorType = Plasma::Types::Horizontal; } else if (ff == QStringLiteral("mediacenter")) { formFactorType = Plasma::Types::MediaCenter; } else if (ff == QStringLiteral("application")) { formFactorType = Plasma::Types::Application; } else { qWarning() << "FormFactor " << ff << "doesn't exist. Planar formFactor has been used!!"; } Plasma::Containment *c = containment(); if (!c) { qCritical("Containment doesn't exist!"); return; } c->setFormFactor(formFactorType); } void View::changeFormFactor(int formFactor) { QString formFactorType = "planar"; switch (formFactor) { case Plasma::Types::Planar: formFactorType = "planar"; break; case Plasma::Types::Vertical: formFactorType = "vertical"; break; case Plasma::Types::Horizontal: formFactorType = "horizontal"; break; case Plasma::Types::MediaCenter: formFactorType = "mediacenter"; break; case Plasma::Types::Application: formFactorType = "application"; break; } addFormFactor(formFactorType); } void View::addLocation(const QString &location) { Plasma::Types::Location locationType = Plasma::Types::Floating; const QString l = location.toLower(); if (l.isEmpty() || l == QStringLiteral("floating")) { locationType = Plasma::Types::Floating; } else if (l == QStringLiteral("desktop")) { locationType = Plasma::Types::Desktop; } else if (l == QStringLiteral("fullscreen")) { locationType = Plasma::Types::FullScreen; } else if (l == QStringLiteral("topedge")) { locationType = Plasma::Types::TopEdge; } else if (l == QStringLiteral("bottomedge")) { locationType = Plasma::Types::BottomEdge; } else if (l == QStringLiteral("rightedge")) { locationType = Plasma::Types::RightEdge; } else if (l == QStringLiteral("leftedge")) { locationType = Plasma::Types::LeftEdge; } else { qWarning() << "Location " << l << "doesn't exist. Floating location has been used!!"; } Plasma::Containment *c = containment(); if (!c) { qCritical("Containment doesn't exist!"); return; } setLocation(locationType); } +void View::emitExternalData(const QString &data) +{ + if (data.isEmpty()) { + return; + } + + Plasma::Applet *applet = containment()->applets().constFirst(); + + QObject *graphicsObject = qobject_cast(applet->property("_plasma_graphicObject").value()); + if (!graphicsObject) { + return; + } + + QMetaObject::invokeMethod(graphicsObject, "externalData", Q_ARG(QString, QString()), + Q_ARG(QVariant, data)); +} + bool View::konsoleVisible() { return m_konsoleVisible; } void View::changeLocation(int location) { QString locationType = "floating"; switch (location) { case Plasma::Types::Floating: locationType = "floating"; break; case Plasma::Types::Desktop: locationType = "desktop"; break; case Plasma::Types::FullScreen: locationType = "fullscreen"; break; case Plasma::Types::TopEdge: locationType = "topedge"; break; case Plasma::Types::BottomEdge: locationType = "bottomedge"; break; case Plasma::Types::RightEdge: locationType = "rightedge"; break; case Plasma::Types::LeftEdge: locationType = "leftedge"; break; } addLocation(locationType); } ViewerCorona *View::createCorona() { Plasma::Package package = Plasma::PluginLoader::self()->loadPackage("Plasma/Shell"); package.setPath("org.kde.plasma.plasmoidviewershell"); ViewerCorona *cor = new ViewerCorona(); cor->setPackage(package); return cor; } void View::takeScreenShot() { QDBusInterface interface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QStringLiteral("org.kde.kwin.Screenshot")); QDBusPendingCall async = interface.asyncCall(QStringLiteral("screenshotArea"), x(), y(), width(), height()); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this); connect(watcher,&QDBusPendingCallWatcher::finished, [](QDBusPendingCallWatcher *call) { QDBusPendingReply reply = *call; call->deleteLater(); if (!reply.isValid()) { qDebug() << "The screenshot has failed, the reply is invalid with error" << reply.error().message(); return; } QString dest = QFileDialog::getSaveFileName(0, i18nc("@title:window", "Save Screenshot"), QDir::homePath(), QStringLiteral("Images (*.png)")); if (dest.isEmpty()) { return; } if (!dest.endsWith(QStringLiteral(".png"))) { dest.append(QStringLiteral(".png")); } QFile f(reply.value()); f.rename(dest); }); } #include "moc_view.cpp" diff --git a/plasmoidviewer/view.h b/plasmoidviewer/view.h index 16aa332..8a750ff 100644 --- a/plasmoidviewer/view.h +++ b/plasmoidviewer/view.h @@ -1,55 +1,56 @@ /* * Copyright 2013 Giorgos Tsiapaliokas * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef VIEW_H #define VIEW_H #include "privateheaders/containmentview.h" class ViewerCorona; class View : public PlasmaQuick::ContainmentView { Q_OBJECT Q_PROPERTY(bool konsoleVisible READ konsoleVisible CONSTANT); public: View(ViewerCorona *corona, bool konsoleVisible, QWindow *parent = 0); ~View(); void addApplet(const QString &applet); void addContainment(const QString &containment); void addFormFactor(const QString &formFactor = QStringLiteral("planar")); void addLocation(const QString &location = QStringLiteral("floating")); + void emitExternalData(const QString &data); bool konsoleVisible(); Q_INVOKABLE void changeFormFactor(int formFactor); Q_INVOKABLE void changeLocation(int location); Q_INVOKABLE void takeScreenShot(); static ViewerCorona *createCorona(); protected: QString pluginFromPath(const QString &path) const; private: QString m_lastAppletName; bool m_konsoleVisible; }; #endif // VIEW_H