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 KAction* KexiScriptPart::action(const QString& scripturi, QObject*)
00072 {
00073     if(! m_mainWin) {
00074         kdWarning() << "KexiScriptPart::action(KURL,QObject*) KexiMainWindow undefined." << endl;
00075         return 0;
00076     }
00077 
00078     KexiProject* project = m_mainWin->project();
00079     if(! project) {
00080         kdWarning() << "KexiScriptPart::action(KURL,QObject*) No project loaded." << endl;
00081         return 0;
00082     }
00083 
00084     KexiPart::ItemDict* itemdict = project->items( info() );
00085     if(! itemdict) {
00086         kdWarning() << "KexiScriptPart::action(KURL,QObject*) Project has no Scripts-items." << endl;
00087         return 0;
00088     }
00089 
00090     if(scripturi.isEmpty()) {
00091         kdWarning() << "KexiScriptPart::action(KURL,QObject*) Filename is empty." << endl;
00092         return 0;
00093     }
00094 
00095     KexiPart::Item* item = 0;
00096     for(KexiPart::ItemDictIterator it(*itemdict); it.current(); ++it) {
00097         if(it.current()->name() == scripturi) {
00098             item = it.current();
00099             break;
00100         }
00101     }
00102 
00103     if(! item) {
00104         kdWarning() << QString("KexiScriptPart::action(KURL,QObject*) No such item: \"%1\"").arg(scripturi) << endl;
00105         return 0;
00106     }
00107 
00108     //m_mainWin->openObject(item, Kexi::DesignViewMode, &map);
00109 
00110     KexiDialogBase* dialog = new KexiDialogBase(m_mainWin);
00111     dialog->setId( item->identifier() );
00112     KexiScriptDesignView* view = dynamic_cast<KexiScriptDesignView*>( createView(dialog, dialog, *item, Kexi::DesignViewMode) );
00113     if(! view) {
00114         kdWarning() << "KexiScriptPart::action(KURL,QObject*) Failed to create a view." << endl;
00115         return 0;
00116     }
00117 
00118     Kross::Api::ScriptAction* scriptaction = view->scriptAction();
00119     if(scriptaction) {
00120 
00121         const QString dontAskAgainName = "askExecuteScript";
00122         KConfig* config = KGlobal::config();
00123         QString dontask = config->readEntry(dontAskAgainName).lower();
00124 
00125         bool exec = (dontask == "yes");
00126         if( !exec && dontask != "no" ) {
00127             exec = KMessageBox::warningContinueCancel(0,
00128                 i18n("Do you want to execute the script \"%1\"?\n\nScripts obtained from unknown sources can contain dangerous code.").arg(scriptaction->text()),
00129                 i18n("Execute Script?"), KGuiItem(i18n("Execute"), "exec"),
00130                 dontAskAgainName, KMessageBox::Notify | KMessageBox::Dangerous
00131             ) == KMessageBox::Continue;
00132         }
00133 
00134         if(exec) {
00135             //QTimer::singleShot(10, scriptaction, SLOT(activate()));
00136             d->scriptguiclient->executeScriptAction( scriptaction );
00137         }
00138     }
00139 
00140     view->deleteLater(); // not needed any longer.
00141     return 0;
00142 }
00143 
00144 void KexiScriptPart::initPartActions()
00145 {
00146     if(m_mainWin) {
00147         // At this stage the KexiPart::Part::m_mainWin should be defined, so
00148         // that we are able to use it's KXMLGUIClient.
00149 
00150         // Initialize the ScriptGUIClient.
00151         d->scriptguiclient = new Kross::Api::ScriptGUIClient( m_mainWin );
00152 
00153         // Publish the KexiMainWindow singelton instance. At least the KexiApp 
00154         // scripting-plugin depends on this instance and loading the plugin will 
00155         // fail if it's not avaiable.
00156         if(! Kross::Api::Manager::scriptManager()->hasChild("KexiMainWindow")) {
00157             Kross::Api::Manager::scriptManager()->addQObject(m_mainWin, "KexiMainWindow");
00158 
00159             // Add the KAction's provided by the ScriptGUIClient to the
00160             // KexiMainWindow.
00161             //FIXME: fix+use createSharedPartAction() whyever it doesn't work as expected right now...
00162             QPopupMenu* popup = m_mainWin->findPopupMenu("tools");
00163             if(popup) {
00164                 KAction* execscriptaction = d->scriptguiclient->action("executescriptfile");
00165                 if(execscriptaction)
00166                     execscriptaction->plug( popup );
00167                 KAction* configscriptaction = d->scriptguiclient->action("configurescripts");
00168                 if(configscriptaction)
00169                     configscriptaction->plug( popup );
00170                 KAction* scriptmenuaction = d->scriptguiclient->action("installedscripts");
00171                 if(scriptmenuaction)
00172                     scriptmenuaction->plug( popup );
00173                 /*
00174                 KAction* execscriptmenuaction = d->scriptguiclient->action("executedscripts");
00175                 if(execscriptmenuaction)
00176                     execscriptmenuaction->plug( popup );
00177                 KAction* loadedscriptmenuaction = d->scriptguiclient->action("loadedscripts");
00178                 if(loadedscriptmenuaction)
00179                     loadedscriptmenuaction->plug( popup );
00180                 */
00181             }
00182         }
00183     }
00184 }
00185 
00186 void KexiScriptPart::initInstanceActions()
00187 {
00188     createSharedAction(Kexi::DesignViewMode, i18n("Execute Script"), "exec", 0, "script_execute");
00189     createSharedAction(Kexi::DesignViewMode, i18n("Configure Editor..."), "configure", 0, "script_config_editor");
00190 }
00191 
00192 KexiViewBase* KexiScriptPart::createView(QWidget *parent, KexiDialogBase* dialog, KexiPart::Item& item, int viewMode, QMap<QString,QString>*)
00193 {
00194     QString partname = item.name();
00195     if( ! partname.isNull() ) {
00196         KexiMainWindow *win = dialog->mainWin();
00197         if(!win || !win->project() || !win->project()->dbConnection())
00198             return 0;
00199 
00200         Kross::Api::ScriptActionCollection* collection = d->scriptguiclient->getActionCollection("projectscripts");
00201         if(! collection) {
00202             collection = new Kross::Api::ScriptActionCollection( i18n("Scripts"), d->scriptguiclient->actionCollection(), "projectscripts" );
00203             d->scriptguiclient->addActionCollection("projectscripts", collection);
00204         }
00205 
00206         const char* name = partname.latin1();
00207         Kross::Api::ScriptAction::Ptr scriptaction = collection->action(name);
00208         if(! scriptaction) {
00209             scriptaction = new Kross::Api::ScriptAction(partname);
00210             collection->attach(scriptaction); //TODO remove again on unload!
00211         }
00212 
00213         if(viewMode == Kexi::DesignViewMode) {
00214             return new KexiScriptDesignView(win, parent, scriptaction);
00215         }
00216     }
00217     return 0;
00218 }
00219 
00220 QString KexiScriptPart::i18nMessage(const QCString& englishMessage) const
00221 {
00222     if (englishMessage=="Design of object \"%1\" has been modified.")
00223         return i18n("Design of script \"%1\" has been modified.");
00224     if (englishMessage=="Object \"%1\" already exists.")
00225         return i18n("Script \"%1\" already exists.");
00226     return englishMessage;
00227 }
00228 
00229 K_EXPORT_COMPONENT_FACTORY( kexihandler_script, KGenericFactory<KexiScriptPart>("kexihandler_script") )
00230 
00231 #include "kexiscriptpart.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys