kexi

kexiscriptpart.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Lucijan Busch <lucijan@kde.org>
00003    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00004    Copyright (C) 2005 Sebastian Sauer <mail@dipe.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kexiscriptpart.h"
00023 #include "kexiscriptdesignview.h"
00024 
00025 #include "kexiviewbase.h"
00026 #include "keximainwindow.h"
00027 #include "kexiproject.h"
00028 
00029 #include <kross/main/manager.h>
00030 #include <kross/main/scriptaction.h>
00031 #include <kross/main/scriptguiclient.h>
00032 
00033 #include <kgenericfactory.h>
00034 #include <kexipartitem.h>
00035 #include <kxmlguiclient.h>
00036 #include <kexidialogbase.h>
00037 #include <kconfig.h>
00038 #include <kdebug.h>
00039 
00041 class KexiScriptPart::Private
00042 {
00043     public:
00044         Kross::Api::ScriptGUIClient* scriptguiclient;
00045 };
00046 
00047 KexiScriptPart::KexiScriptPart(QObject *parent, const char *name, const QStringList &l)
00048     : KexiPart::Part(parent, name, l)
00049     , d( new Private() )
00050 {
00051     d->scriptguiclient = 0;
00052 
00053     // REGISTERED ID:
00054     m_registeredPartID = (int)KexiPart::ScriptObjectType;
00055 
00056     m_names["instanceName"] 
00057         = i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
00058         "Use '_' character instead of spaces. First character should be a..z character. "
00059         "If you cannot use latin characters in your language, use english word.", 
00060         "script");
00061     m_names["instanceCaption"] = i18n("Script");
00062     m_supportedViewModes = Kexi::DesignViewMode;
00063 }
00064 
00065 KexiScriptPart::~KexiScriptPart()
00066 {
00067     delete d->scriptguiclient;
00068     delete d;
00069 }
00070 
00071 bool KexiScriptPart::execute(KexiPart::Item* item, QObject* sender)
00072 {
00073     Q_UNUSED(sender);
00074 
00075     if(! item) {
00076         kdWarning() << "KexiScriptPart::execute: Invalid item." << endl;
00077         return false;
00078     }
00079 
00080     KexiDialogBase* dialog = new KexiDialogBase(m_mainWin);
00081     dialog->setId( item->identifier() );
00082     KexiScriptDesignView* view = dynamic_cast<KexiScriptDesignView*>( createView(dialog, dialog, *item, Kexi::DesignViewMode) );
00083     if(! view) {
00084         kdWarning() << "KexiScriptPart::execute: Failed to create a view." << endl;
00085         return false;
00086     }
00087 
00088     Kross::Api::ScriptAction* scriptaction = view->scriptAction();
00089     if(scriptaction) {
00090 
00091         const QString dontAskAgainName = "askExecuteScript";
00092         KConfig* config = KGlobal::config();
00093         QString dontask = config->readEntry(dontAskAgainName).lower();
00094 
00095         bool exec = (dontask == "yes");
00096         if( !exec && dontask != "no" ) {
00097             exec = KMessageBox::warningContinueCancel(0,
00098                 i18n("Do you want to execute the script \"%1\"?\n\nScripts obtained from unknown sources can contain dangerous code.").arg(scriptaction->text()),
00099                 i18n("Execute Script?"), KGuiItem(i18n("Execute"), "exec"),
00100                 dontAskAgainName, KMessageBox::Notify | KMessageBox::Dangerous
00101             ) == KMessageBox::Continue;
00102         }
00103 
00104         if(exec) {
00105             //QTimer::singleShot(10, scriptaction, SLOT(activate()));
00106             d->scriptguiclient->executeScriptAction( scriptaction );
00107         }
00108     }
00109 
00110     view->deleteLater(); // not needed any longer.
00111     return true;
00112 }
00113 
00114 void KexiScriptPart::initPartActions()
00115 {
00116     if(m_mainWin) {
00117         // At this stage the KexiPart::Part::m_mainWin should be defined, so
00118         // that we are able to use it's KXMLGUIClient.
00119 
00120         // Initialize the ScriptGUIClient.
00121         d->scriptguiclient = new Kross::Api::ScriptGUIClient( m_mainWin );
00122 
00123         // Publish the KexiMainWindow singelton instance. At least the KexiApp 
00124         // scripting-plugin depends on this instance and loading the plugin will 
00125         // fail if it's not avaiable.
00126         if(! Kross::Api::Manager::scriptManager()->hasChild("KexiMainWindow")) {
00127             Kross::Api::Manager::scriptManager()->addQObject(m_mainWin, "KexiMainWindow");
00128 
00129             // Add the KAction's provided by the ScriptGUIClient to the
00130             // KexiMainWindow.
00131             //FIXME: fix+use createSharedPartAction() whyever it doesn't work as expected right now...
00132             QPopupMenu* popup = m_mainWin->findPopupMenu("tools");
00133             if(popup) {
00134                 KAction* execscriptaction = d->scriptguiclient->action("executescriptfile");
00135                 if(execscriptaction)
00136                     execscriptaction->plug( popup );
00137                 KAction* configscriptaction = d->scriptguiclient->action("configurescripts");
00138                 if(configscriptaction)
00139                     configscriptaction->plug( popup );
00140                 KAction* scriptmenuaction = d->scriptguiclient->action("installedscripts");
00141                 if(scriptmenuaction)
00142                     scriptmenuaction->plug( popup );
00143                 /*
00144                 KAction* execscriptmenuaction = d->scriptguiclient->action("executedscripts");
00145                 if(execscriptmenuaction)
00146                     execscriptmenuaction->plug( popup );
00147                 KAction* loadedscriptmenuaction = d->scriptguiclient->action("loadedscripts");
00148                 if(loadedscriptmenuaction)
00149                     loadedscriptmenuaction->plug( popup );
00150                 */
00151             }
00152         }
00153     }
00154 }
00155 
00156 void KexiScriptPart::initInstanceActions()
00157 {
00158     //createSharedAction(Kexi::DesignViewMode, i18n("Execute Script"), "player_play", 0, "data_execute");
00159     createSharedAction(Kexi::DesignViewMode, i18n("Configure Editor..."), "configure", 0, "script_config_editor");
00160 }
00161 
00162 KexiViewBase* KexiScriptPart::createView(QWidget *parent, KexiDialogBase* dialog, KexiPart::Item& item, int viewMode, QMap<QString,QString>*)
00163 {
00164     QString partname = item.name();
00165     if( ! partname.isNull() ) {
00166         KexiMainWindow *win = dialog->mainWin();
00167         if(!win || !win->project() || !win->project()->dbConnection())
00168             return 0;
00169 
00170         Kross::Api::ScriptActionCollection* collection = d->scriptguiclient->getActionCollection("projectscripts");
00171         if(! collection) {
00172             collection = new Kross::Api::ScriptActionCollection( i18n("Scripts"), d->scriptguiclient->actionCollection(), "projectscripts" );
00173             d->scriptguiclient->addActionCollection("projectscripts", collection);
00174         }
00175 
00176         const char* name = partname.latin1();
00177         Kross::Api::ScriptAction::Ptr scriptaction = collection->action(name);
00178         if(! scriptaction) {
00179             scriptaction = new Kross::Api::ScriptAction(partname);
00180             collection->attach(scriptaction); //TODO remove again on unload!
00181         }
00182 
00183         if(viewMode == Kexi::DesignViewMode) {
00184             return new KexiScriptDesignView(win, parent, scriptaction);
00185         }
00186     }
00187     return 0;
00188 }
00189 
00190 QString KexiScriptPart::i18nMessage(const QCString& englishMessage) const
00191 {
00192     if (englishMessage=="Design of object \"%1\" has been modified.")
00193         return i18n("Design of script \"%1\" has been modified.");
00194     if (englishMessage=="Object \"%1\" already exists.")
00195         return i18n("Script \"%1\" already exists.");
00196     return englishMessage;
00197 }
00198 
00199 K_EXPORT_COMPONENT_FACTORY( kexihandler_script, KGenericFactory<KexiScriptPart>("kexihandler_script") )
00200 
00201 #include "kexiscriptpart.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys