Plasma
appletscript.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2007 by Aaron Seigo <aseigo@kde.org> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "scripting/appletscript.h" 00021 00022 #include "kconfig.h" 00023 #include "kconfigdialog.h" 00024 00025 #include "animations/animationscriptengine_p.h" 00026 #include "animator.h" 00027 #include "applet.h" 00028 #include "package.h" 00029 #include "private/applet_p.h" 00030 00031 namespace Plasma 00032 { 00033 00034 class AppletScriptPrivate 00035 { 00036 public: 00037 Applet *applet; 00038 }; 00039 00040 AppletScript::AppletScript(QObject *parent) 00041 : ScriptEngine(parent), 00042 d(new AppletScriptPrivate) 00043 { 00044 d->applet = 0; 00045 } 00046 00047 AppletScript::~AppletScript() 00048 { 00049 delete d; 00050 } 00051 00052 void AppletScript::setApplet(Plasma::Applet *applet) 00053 { 00054 d->applet = applet; 00055 } 00056 00057 Applet *AppletScript::applet() const 00058 { 00059 Q_ASSERT(d->applet); 00060 return d->applet; 00061 } 00062 00063 void AppletScript::paintInterface(QPainter *painter, 00064 const QStyleOptionGraphicsItem *option, 00065 const QRect &contentsRect) 00066 { 00067 Q_UNUSED(painter); 00068 Q_UNUSED(option); 00069 Q_UNUSED(contentsRect); 00070 } 00071 00072 QSizeF AppletScript::size() const 00073 { 00074 if (applet()) { 00075 return applet()->size(); 00076 } 00077 00078 return QSizeF(); 00079 } 00080 00081 void AppletScript::constraintsEvent(Plasma::Constraints constraints) 00082 { 00083 Q_UNUSED(constraints); 00084 } 00085 00086 QList<QAction*> AppletScript::contextualActions() 00087 { 00088 return QList<QAction*>(); 00089 } 00090 00091 QPainterPath AppletScript::shape() const 00092 { 00093 if (applet()) { 00094 QPainterPath path; 00095 path.addRect(applet()->boundingRect()); 00096 return path; 00097 } 00098 00099 return QPainterPath(); 00100 } 00101 00102 void AppletScript::setHasConfigurationInterface(bool hasInterface) 00103 { 00104 if (applet()) { 00105 applet()->setHasConfigurationInterface(hasInterface); 00106 } 00107 } 00108 00109 void AppletScript::setConfigurationRequired(bool req, const QString &reason) 00110 { 00111 if (applet()) { 00112 applet()->setConfigurationRequired(req, reason); 00113 } 00114 } 00115 00116 void AppletScript::setFailedToLaunch(bool failed, const QString &reason) 00117 { 00118 if (applet()) { 00119 applet()->setFailedToLaunch(failed, reason); 00120 } 00121 } 00122 00123 void AppletScript::configNeedsSaving() const 00124 { 00125 if (applet()) { 00126 emit applet()->configNeedsSaving(); 00127 } 00128 } 00129 00130 void AppletScript::showConfigurationInterface() 00131 { 00132 if (applet()) { 00133 KConfigDialog *dialog = applet()->d->generateGenericConfigDialog(); 00134 applet()->d->addStandardConfigurationPages(dialog); 00135 dialog->show(); 00136 } 00137 } 00138 00139 KConfigDialog *AppletScript::standardConfigurationDialog() 00140 { 00141 if (applet()) { 00142 return applet()->d->generateGenericConfigDialog(); 00143 } 00144 00145 return 0; 00146 } 00147 00148 void AppletScript::addStandardConfigurationPages(KConfigDialog *dialog) 00149 { 00150 if (applet()) { 00151 applet()->d->addStandardConfigurationPages(dialog); 00152 } 00153 } 00154 00155 void AppletScript::showMessage(const QIcon &icon, const QString &message, const MessageButtons buttons) 00156 { 00157 if (applet()) { 00158 applet()->showMessage(icon, message, buttons); 00159 } 00160 } 00161 00162 void AppletScript::registerAsDragHandle(QGraphicsItem *item) 00163 { 00164 if (applet()) { 00165 applet()->registerAsDragHandle(item); 00166 } 00167 } 00168 00169 void AppletScript::unregisterAsDragHandle(QGraphicsItem *item) 00170 { 00171 if (applet()) { 00172 applet()->unregisterAsDragHandle(item); 00173 } 00174 } 00175 00176 bool AppletScript::isRegisteredAsDragHandle(QGraphicsItem *item) 00177 { 00178 if (applet()) { 00179 return applet()->isRegisteredAsDragHandle(item); 00180 } 00181 return false; 00182 } 00183 00184 Animation *AppletScript::loadAnimationFromPackage(const QString &name, QObject *parent) 00185 { 00186 if (applet()) { 00187 const QString scopedName = applet()->pluginName() + ":" + name; 00188 if (!AnimationScriptEngine::isAnimationRegistered(scopedName)) { 00189 KConfig conf(applet()->package()->path() + "/metadata.desktop", KConfig::SimpleConfig); 00190 KConfigGroup animConf(&conf, "Animations"); 00191 QString file; 00192 foreach (const QString &possibleFile, animConf.keyList()) { 00193 const QStringList anims = animConf.readEntry(possibleFile, QStringList()); 00194 if (anims.contains(name)) { 00195 file = possibleFile; 00196 break; 00197 } 00198 } 00199 00200 if (file.isEmpty()) { 00201 return 0; 00202 } 00203 00204 const QString path = applet()->package()->filePath("animations", file); 00205 if (path.isEmpty()) { 00206 kDebug() << "file path was empty for" << file; 00207 return 0; 00208 } 00209 00210 if (!AnimationScriptEngine::loadScript(path, applet()->pluginName() + ':') || 00211 !AnimationScriptEngine::isAnimationRegistered(scopedName)) { 00212 kDebug() << "script engine loading failed for" << path; 00213 return 0; 00214 } 00215 } 00216 00217 Animation *anim = Animator::create(scopedName, parent ? parent : this); 00218 return anim; 00219 } 00220 00221 return 0; 00222 } 00223 00224 void AppletScript::configChanged() 00225 { 00226 } 00227 00228 DataEngine *AppletScript::dataEngine(const QString &engine) const 00229 { 00230 Q_ASSERT(d->applet); 00231 return d->applet->dataEngine(engine); 00232 } 00233 00234 QString AppletScript::mainScript() const 00235 { 00236 Q_ASSERT(d->applet); 00237 return d->applet->package()->filePath("mainscript"); 00238 } 00239 00240 const Package *AppletScript::package() const 00241 { 00242 Q_ASSERT(d->applet); 00243 return d->applet->package(); 00244 } 00245 00246 KPluginInfo AppletScript::description() const 00247 { 00248 Q_ASSERT(d->applet); 00249 return d->applet->d->appletDescription; 00250 } 00251 00252 Extender *AppletScript::extender() const 00253 { 00254 Q_ASSERT(d->applet); 00255 return d->applet->extender(); 00256 } 00257 00258 } // Plasma namespace 00259 00260 #include "appletscript.moc"
KDE 4.6 API Reference