diff --git a/libs/ksysguard/processui/scripting.cpp b/libs/ksysguard/processui/scripting.cpp index 32e5614ebd..6446c0ab7f 100644 --- a/libs/ksysguard/processui/scripting.cpp +++ b/libs/ksysguard/processui/scripting.cpp @@ -1,170 +1,192 @@ /* KSysGuard, the KDE System Guard Copyright (c) 2009 John Tapsell This library 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 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "scripting.h" #include #include #include #include #include #include #include #include #include #include #include #include "processes.h" #include "ksysguardprocesslist.h" #include #include #include #include #include +#include #include class ScriptingHtmlDialog : public KDialog { public: ScriptingHtmlDialog(QWidget *parent) : KDialog(parent) { setButtons( KDialog::Close ); setButtonGuiItem( KDialog::Close, KStandardGuiItem::close() ); setDefaultButton( KDialog::Close ); setEscapeButton( KDialog::Close ); showButtonSeparator( false ); setMainWidget(&m_webView); (void)minimumSizeHint(); //Force the dialog to be laid out now layout()->setContentsMargins(0,0,0,0); m_webView.settings()->setOfflineStoragePath(QString()); m_webView.settings()->setOfflineWebApplicationCachePath(QString()); m_webView.settings()->setObjectCacheCapacities(0,0,0); m_webView.settings()->setAttribute(QWebSettings::PluginsEnabled, false); m_webView.settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); m_webView.page()->setNetworkAccessManager(NULL); //Disable talking to remote servers m_webView.page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded); m_webView.page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); } QWebView *webView() { return &m_webView; } protected: QWebView m_webView; }; ProcessObject::ProcessObject(KSysGuard::Process *process) { setProperty("pid", (int)process->pid); setProperty("ppid", (int)process->parent_pid); setProperty("name", process->name.section(' ', 0,0)); setProperty("fullname", process->name); setProperty("command", process->command); } bool ProcessObject::fileExists(const QString &filename) { QFileInfo fileInfo(filename); return fileInfo.exists(); } QString ProcessObject::readFile(const QString &filename) { QFile file(filename); if(!file.open(QIODevice::ReadOnly)) return QString(); QTextStream stream(&file); QString contents = stream.readAll(); file.close(); return contents; } Scripting::Scripting(KSysGuardProcessList * parent) : QWidget(parent), mProcessList(parent) { mScriptingHtmlDialog = NULL; loadContextMenu(); } void Scripting::runScript(const QString &path, const QString &name) { //Record the script name and path for use in the script helper functions mScriptPath = path; mScriptName = name; QString fileName = path + "index.html"; if(!mScriptingHtmlDialog) { mScriptingHtmlDialog = new ScriptingHtmlDialog(this); connect(mScriptingHtmlDialog, SIGNAL(closeClicked()), SLOT(stopAllScripts())); + + KAction *refreshAction = new KAction("refresh", mScriptingHtmlDialog); + refreshAction->setShortcut(QKeySequence::Refresh); + connect(refreshAction, SIGNAL(triggered()), SLOT(refreshScript())); + mScriptingHtmlDialog->addAction(refreshAction); + + KAction *zoomInAction = KStandardAction::zoomIn(this, SLOT(zoomIn()), mScriptingHtmlDialog); + mScriptingHtmlDialog->addAction(zoomInAction); + + KAction *zoomOutAction = KStandardAction::zoomOut(this, SLOT(zoomOut()), mScriptingHtmlDialog); + mScriptingHtmlDialog->addAction(zoomOutAction); } //Make the process information available to the script mScriptingHtmlDialog->webView()->load(fileName); mScriptingHtmlDialog->show(); connect(mScriptingHtmlDialog->webView()->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), SLOT(setupJavascriptObjects())); setupJavascriptObjects(); // connect(mProcessList, SIGNAL(updated()), SLOT(refreshScript())); } +void Scripting::zoomIn() { + QWebView *webView = mScriptingHtmlDialog->webView(); + webView->setZoomFactor( webView->zoomFactor() * 1.1 ); +} +void Scripting::zoomOut() { + QWebView *webView = mScriptingHtmlDialog->webView(); + if(webView->zoomFactor() > 0.1) //Prevent it getting too small + webView->setZoomFactor( webView->zoomFactor() / 1.1 ); +} + void Scripting::refreshScript() { //Call any refresh function, if it exists if(mScriptingHtmlDialog && mScriptingHtmlDialog->webView() && mScriptingHtmlDialog->webView()->page() && mScriptingHtmlDialog->webView()->page()->mainFrame()) mScriptingHtmlDialog->webView()->page()->mainFrame()->evaluateJavaScript("refresh();"); } void Scripting::setupJavascriptObjects() { KSysGuard::Process *process = mProcessList->processModel()->getProcess(mPid); mScriptingHtmlDialog->webView()->page()->mainFrame()->addToJavaScriptWindowObject("process", new ProcessObject(process), QScriptEngine::ScriptOwnership); } void Scripting::stopAllScripts() { mScriptingHtmlDialog->deleteLater(); mScriptingHtmlDialog = NULL; mScriptPath.clear(); mScriptName.clear(); } void Scripting::loadContextMenu() { //Clear any existing actions qDeleteAll(mActions); mActions.clear(); QStringList scripts = KGlobal::dirs()->findAllResources("data", "ksysguard/scripts/*/*.desktop", KStandardDirs::NoDuplicates); foreach(const QString &script, scripts) { KDesktopFile desktopFile(script); if(!desktopFile.name().isEmpty() && !desktopFile.noDisplay()) { KAction *action = new KAction(desktopFile.readName(), this); action->setToolTip(desktopFile.readComment()); action->setIcon(QIcon(desktopFile.readIcon())); QString scriptPath = script; scriptPath.truncate(scriptPath.lastIndexOf('/')); action->setProperty("scriptPath", scriptPath + '/'); connect(action, SIGNAL(triggered(bool)), SLOT(runScriptSlot())); mProcessList->addAction(action); mActions << action; } } } void Scripting::runScriptSlot() { KAction *action = static_cast(sender()); //All the files for the script should be in the scriptPath QString path = action->property("scriptPath").toString(); QList selectedProcesses = mProcessList->selectedProcesses(); if(selectedProcesses.isEmpty()) return; mPid = selectedProcesses[0]->pid; runScript(path, action->text()); } diff --git a/libs/ksysguard/processui/scripting.h b/libs/ksysguard/processui/scripting.h index 04d76979ef..b7df171958 100644 --- a/libs/ksysguard/processui/scripting.h +++ b/libs/ksysguard/processui/scripting.h @@ -1,80 +1,82 @@ /* KSysGuard, the KDE System Guard Copyright (c) 2009 John Tapsell This library 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 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KSYSGUARDSCRIPTING_H #define KSYSGUARDSCRIPTING_H #include #include #include #include "processes.h" class QAction; class ScriptingHtmlDialog; //Defined in scripting.cpp file class KSysGuardProcessList; class Scripting : public QWidget { Q_OBJECT public: /** Create a scripting object */ Scripting(KSysGuardProcessList *parent); /** Run the script in the given path */ void runScript(const QString &path, const QString &name); /** Read all the script .desktop files and create an action for each one */ void loadContextMenu(); /** List of context menu actions that are created by loadContextMenu() */ QList actions() { return mActions; } /** Create a ScriptingHtmlDialog, if one does not already exist, and display the given html */ void displayHtml(const QString &html); public Q_SLOTS: /** Stop all scripts and delete the script engine */ void stopAllScripts(); private Q_SLOTS: /** Run the script associated with the QAction that called this slot */ void runScriptSlot(); void setupJavascriptObjects(); void refreshScript(); + void zoomIn(); + void zoomOut(); private: /** This is created on the fly as needed, and deleted when no longer used */ ScriptingHtmlDialog *mScriptingHtmlDialog; /** The parent process list to script for */ KSysGuardProcessList * const mProcessList; /** List of context menu actions that are created by loadContextMenu() */ QList mActions; QString mScriptPath; QString mScriptName; qlonglong mPid; }; class ProcessObject : public QObject { Q_OBJECT public: ProcessObject(KSysGuard::Process *process); public Q_SLOTS: bool fileExists(const QString &filename); QString readFile(const QString &filename); }; #endif