2009-05-13 02:55:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Chani Armitage <chani@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, 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 Library 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 "appletinterface.h"
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QSignalMapper>
|
2009-11-18 23:57:09 +01:00
|
|
|
#include <QTimer>
|
2009-05-13 02:55:25 +02:00
|
|
|
|
2009-11-14 01:24:14 +01:00
|
|
|
#include <KIcon>
|
|
|
|
#include <KService>
|
|
|
|
#include <KServiceTypeTrader>
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
#include <Plasma/Plasma>
|
|
|
|
#include <Plasma/Applet>
|
|
|
|
#include <Plasma/Context>
|
|
|
|
#include <Plasma/DataEngine>
|
|
|
|
#include <Plasma/Package>
|
|
|
|
|
|
|
|
AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
m_appletScriptEngine(parent),
|
|
|
|
m_actionSignals(0)
|
|
|
|
{
|
|
|
|
connect(this, SIGNAL(releaseVisualFocus()), applet(), SIGNAL(releaseVisualFocus()));
|
|
|
|
connect(this, SIGNAL(configNeedsSaving()), applet(), SIGNAL(configNeedsSaving()));
|
|
|
|
}
|
|
|
|
|
|
|
|
AppletInterface::~AppletInterface()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::DataEngine* AppletInterface::dataEngine(const QString &name)
|
|
|
|
{
|
|
|
|
return applet()->dataEngine(name);
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:44:51 +01:00
|
|
|
AppletInterface::FormFactor AppletInterface::formFactor() const
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
|
|
|
return static_cast<FormFactor>(applet()->formFactor());
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:44:51 +01:00
|
|
|
AppletInterface::Location AppletInterface::location() const
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
|
|
|
return static_cast<Location>(applet()->location());
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:44:51 +01:00
|
|
|
QString AppletInterface::currentActivity() const
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
|
|
|
return applet()->context()->currentActivity();
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:44:51 +01:00
|
|
|
AppletInterface::AspectRatioMode AppletInterface::aspectRatioMode() const
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
|
|
|
return static_cast<AspectRatioMode>(applet()->aspectRatioMode());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setAspectRatioMode(AppletInterface::AspectRatioMode mode)
|
|
|
|
{
|
|
|
|
applet()->setAspectRatioMode(static_cast<Plasma::AspectRatioMode>(mode));
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:44:51 +01:00
|
|
|
bool AppletInterface::shouldConserveResources() const
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
|
|
|
return applet()->shouldConserveResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setFailedToLaunch(bool failed, const QString &reason)
|
|
|
|
{
|
|
|
|
m_appletScriptEngine->setFailedToLaunch(failed, reason);
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:44:51 +01:00
|
|
|
bool AppletInterface::isBusy() const
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
|
|
|
return applet()->isBusy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setBusy(bool busy)
|
|
|
|
{
|
|
|
|
applet()->setBusy(busy);
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:44:51 +01:00
|
|
|
AppletInterface::BackgroundHints AppletInterface::backgroundHints() const
|
|
|
|
{
|
|
|
|
return static_cast<BackgroundHints>(static_cast<int>(applet()->backgroundHints()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setBackgroundHints(BackgroundHints hint)
|
|
|
|
{
|
|
|
|
applet()->setBackgroundHints(Plasma::Applet::BackgroundHints(hint));
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
void AppletInterface::setConfigurationRequired(bool needsConfiguring, const QString &reason)
|
|
|
|
{
|
|
|
|
m_appletScriptEngine->setConfigurationRequired(needsConfiguring, reason);
|
|
|
|
}
|
|
|
|
|
2009-11-13 23:10:32 +01:00
|
|
|
void AppletInterface::update(const QRectF &rect)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-11-13 23:10:32 +01:00
|
|
|
applet()->update(rect);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString AppletInterface::activeConfig() const
|
|
|
|
{
|
|
|
|
return m_currentConfig.isEmpty() ? "main" : m_currentConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setActiveConfig(const QString &name)
|
|
|
|
{
|
|
|
|
if (name == "main") {
|
2009-05-26 20:03:15 +02:00
|
|
|
m_currentConfig.clear();
|
2009-05-13 02:55:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::ConfigLoader *loader = m_configs.value(name, 0);
|
|
|
|
|
|
|
|
if (!loader) {
|
|
|
|
QString path = applet()->package()->filePath("config", name + ".xml");
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile f(path);
|
|
|
|
KConfigGroup cg = applet()->config();
|
|
|
|
loader = new Plasma::ConfigLoader(&cg, &f, this);
|
|
|
|
m_configs.insert(name, loader);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_currentConfig = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::writeConfig(const QString &entry, const QVariant &value)
|
|
|
|
{
|
|
|
|
Plasma::ConfigLoader *config = 0;
|
|
|
|
if (m_currentConfig.isEmpty()) {
|
|
|
|
config = applet()->configScheme();
|
|
|
|
} else {
|
|
|
|
config = m_configs.value(m_currentConfig, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config) {
|
|
|
|
KConfigSkeletonItem *item = config->findItemByName(entry);
|
|
|
|
if (item) {
|
|
|
|
item->setProperty(value);
|
2009-05-26 20:03:15 +02:00
|
|
|
config->blockSignals(true);
|
2009-05-13 02:55:25 +02:00
|
|
|
config->writeConfig();
|
2009-05-26 20:03:15 +02:00
|
|
|
config->blockSignals(false);
|
2009-05-13 02:55:25 +02:00
|
|
|
m_appletScriptEngine->configNeedsSaving();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue AppletInterface::readConfig(const QString &entry) const
|
|
|
|
{
|
|
|
|
Plasma::ConfigLoader *config = 0;
|
|
|
|
QVariant result;
|
2009-05-26 20:03:15 +02:00
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
if (m_currentConfig.isEmpty()) {
|
|
|
|
config = applet()->configScheme();
|
|
|
|
} else {
|
|
|
|
config = m_configs.value(m_currentConfig, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config) {
|
|
|
|
result = config->property(entry);
|
|
|
|
}
|
2009-05-26 20:03:15 +02:00
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
return m_appletScriptEngine->variantToScriptValue(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AppletInterface::file(const QString &fileType)
|
|
|
|
{
|
|
|
|
return m_appletScriptEngine->package()->filePath(fileType.toLocal8Bit().constData());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AppletInterface::file(const QString &fileType, const QString &filePath)
|
|
|
|
{
|
|
|
|
return m_appletScriptEngine->package()->filePath(fileType.toLocal8Bit().constData(), filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Plasma::Package *AppletInterface::package() const
|
|
|
|
{
|
|
|
|
return m_appletScriptEngine->package();
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QAction*> AppletInterface::contextualActions() const
|
|
|
|
{
|
|
|
|
QList<QAction*> actions;
|
|
|
|
Plasma::Applet *a = applet();
|
|
|
|
|
|
|
|
foreach (const QString &name, m_actions) {
|
|
|
|
QAction *action = a->action(name);
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
actions << action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF AppletInterface::size() const
|
|
|
|
{
|
|
|
|
return applet()->size();
|
|
|
|
}
|
|
|
|
|
2009-10-21 01:30:36 +02:00
|
|
|
QRectF AppletInterface::rect() const
|
|
|
|
{
|
2009-10-21 01:49:48 +02:00
|
|
|
return applet()->contentsRect();
|
2009-10-21 01:30:36 +02:00
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
void AppletInterface::setAction(const QString &name, const QString &text, const QString &icon, const QString &shortcut)
|
|
|
|
{
|
|
|
|
Plasma::Applet *a = applet();
|
|
|
|
QAction *action = a->action(name);
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
action->setText(text);
|
|
|
|
} else {
|
|
|
|
action = new QAction(text, this);
|
|
|
|
a->addAction(name, action);
|
|
|
|
|
|
|
|
Q_ASSERT(!m_actions.contains(name));
|
|
|
|
m_actions.insert(name);
|
|
|
|
|
|
|
|
if (!m_actionSignals) {
|
|
|
|
m_actionSignals = new QSignalMapper(this);
|
|
|
|
connect(m_actionSignals, SIGNAL(mapped(QString)),
|
|
|
|
m_appletScriptEngine, SLOT(executeAction(QString)));
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(action, SIGNAL(triggered()), m_actionSignals, SLOT(map()));
|
|
|
|
m_actionSignals->setMapping(action, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
action->setIcon(icon.isEmpty() ? QIcon() : KIcon(icon));
|
|
|
|
action->setShortcut(shortcut);
|
|
|
|
action->setObjectName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::removeAction(const QString &name)
|
|
|
|
{
|
|
|
|
Plasma::Applet *a = applet();
|
|
|
|
QAction *action = a->action(name);
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
if (m_actionSignals) {
|
|
|
|
m_actionSignals->removeMappings(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete action;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_actions.remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::resize(qreal w, qreal h)
|
|
|
|
{
|
|
|
|
applet()->resize(w,h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setMinimumSize(qreal w, qreal h)
|
|
|
|
{
|
|
|
|
applet()->setMinimumSize(w,h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setPreferredSize(qreal w, qreal h)
|
|
|
|
{
|
|
|
|
applet()->setPreferredSize(w,h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::dataUpdated(QString source, Plasma::DataEngine::Data data)
|
|
|
|
{
|
|
|
|
m_appletScriptEngine->dataUpdated(source, data);
|
|
|
|
}
|
|
|
|
|
2009-11-13 23:04:16 +01:00
|
|
|
QGraphicsLayout *AppletInterface::layout() const
|
|
|
|
{
|
|
|
|
return applet()->layout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletInterface::setLayout(QGraphicsLayout *layout)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-11-13 23:04:16 +01:00
|
|
|
applet()->setLayout(layout);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-11-13 23:27:30 +01:00
|
|
|
bool AppletInterface::immutable() const
|
|
|
|
{
|
|
|
|
return applet()->immutability() != Plasma::Mutable;
|
|
|
|
}
|
|
|
|
|
2009-11-14 01:24:14 +01:00
|
|
|
int AppletInterface::apiVersion() const
|
|
|
|
{
|
|
|
|
const QString constraint("[X-Plasma-API] == 'javascript' and 'Applet' in [X-Plasma-ComponentTypes]");
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
|
|
|
|
if (offers.isEmpty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offers.first()->property("X-KDE-PluginInfo-Version", QVariant::Int).toInt();
|
|
|
|
}
|
|
|
|
|
2009-11-14 06:19:19 +01:00
|
|
|
bool AppletInterface::include(const QString &script)
|
|
|
|
{
|
|
|
|
const QString path = package()->filePath("scripts", script);
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_appletScriptEngine->include(path);
|
|
|
|
}
|
|
|
|
|
2009-11-14 08:33:42 +01:00
|
|
|
bool AppletInterface::hasExtension(const QString &extension) const
|
|
|
|
{
|
|
|
|
return m_appletScriptEngine->loadedExtensions().contains(extension.toLower());
|
|
|
|
}
|
|
|
|
|
2009-11-14 08:32:00 +01:00
|
|
|
void AppletInterface::debug(const QString &msg)
|
|
|
|
{
|
|
|
|
kDebug() << msg;
|
|
|
|
}
|
|
|
|
|
2009-11-18 23:40:38 +01:00
|
|
|
void AppletInterface::gc()
|
|
|
|
{
|
|
|
|
QTimer::singleShot(0, m_appletScriptEngine, SLOT(collectGarbage()));
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
#include "appletinterface.moc"
|