Back to index

scribus-ng  1.3.4.dfsg+svn20071115
scriptercore.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "scriptercore.h"
00008 
00009 #include <qglobal.h>
00010 #include <qwidget.h>
00011 #include <qstring.h>
00012 #include <qapplication.h>
00013 #include <qmessagebox.h>
00014 #include <qtextcodec.h>
00015 #include <qdom.h>
00016 #include <qtextstream.h>
00017 #include <cstdlib>
00018 #include <qtextedit.h>
00019 
00020 #include "runscriptdialog.h"
00021 #include "helpbrowser.h"
00022 #include "mpalette.h" //TODO Move the calls to this to a signal
00023 #include "seiten.h" //TODO Move the calls to this to a signal
00024 #include "layers.h" //TODO Move the calls to this to a signal
00025 #include "tree.h" //TODO Move the calls to this to a signal
00026 #include "menumanager.h"
00027 #include "pconsole.h"
00028 #include "scraction.h"
00029 #include "scribuscore.h"
00030 #include "scpaths.h"
00031 #include "selection.h"
00032 #include "prefsfile.h"
00033 #include "prefscontext.h"
00034 #include "prefstable.h"
00035 #include "prefsmanager.h"
00036 
00037 #include "scriptercore.moc"
00038 
00039 ScripterCore::ScripterCore(QWidget* parent)
00040 {
00041        pcon = new PythonConsole(parent);
00042        scrScripterActions.clear();
00043        scrRecentScriptActions.clear();
00044        returnString = "init";
00045 
00046        scrScripterActions.insert("scripterExecuteScript", new ScrAction(QObject::tr("&Execute Script..."), QKeySequence(), this, "scripterExecuteScript"));
00047        scrScripterActions.insert("scripterShowConsole", new ScrAction(QObject::tr("Show &Console"), QKeySequence(), this, "scripterShowConsole"));
00048        scrScripterActions.insert("scripterAboutScript", new ScrAction(QObject::tr("&About Script..."), QKeySequence(), this, "scripterAboutScript"));
00049 
00050        scrScripterActions["scripterShowConsole"]->setToggleAction(true);
00051 
00052        QObject::connect( scrScripterActions["scripterExecuteScript"], SIGNAL(activated()) , this, SLOT(runScriptDialog()) );
00053        QObject::connect( scrScripterActions["scripterShowConsole"], SIGNAL(toggled(bool)) , this, SLOT(slotInteractiveScript(bool)) );
00054        QObject::connect( scrScripterActions["scripterAboutScript"], SIGNAL(activated()) , this, SLOT(aboutScript()) );
00055 
00056        SavedRecentScripts.clear();
00057        ReadPlugPrefs();
00058 
00059        QObject::connect(pcon, SIGNAL(runCommand()), this, SLOT(slotExecute()));
00060        QObject::connect(pcon, SIGNAL(paletteShown(bool)), this, SLOT(slotInteractiveScript(bool)));
00061 }
00062 
00063 ScripterCore::~ScripterCore()
00064 {
00065        SavePlugPrefs();
00066 }
00067 
00068 void ScripterCore::addToMainWindowMenu(ScribusMainWindow *mw)
00069 {
00070        menuMgr = mw->scrMenuMgr;
00071        menuMgr->createMenu("Scripter", QObject::tr("&Script"));
00072        menuMgr->addMenuToMenuBarAfter("Scripter","Extras");
00073        menuMgr->createMenu("ScribusScripts", QObject::tr("&Scribus Scripts"), "Scripter");
00074        menuMgr->addMenuItem(scrScripterActions["scripterExecuteScript"], "Scripter");
00075        menuMgr->createMenu("RecentScripts", QObject::tr("&Recent Scripts"), "Scripter");
00076        menuMgr->addMenuSeparator("Scripter");
00077        menuMgr->addMenuItem(scrScripterActions["scripterShowConsole"], "Scripter");
00078        menuMgr->addMenuItem(scrScripterActions["scripterAboutScript"], "Scripter");
00079        buildScribusScriptsMenu();
00080        buildRecentScriptsMenu();
00081 }
00082 
00083 
00084 void ScripterCore::buildScribusScriptsMenu()
00085 {
00086        QString pfad = ScPaths::instance().scriptDir();
00087        QString pfad2;
00088        pfad2 = QDir::convertSeparators(pfad);
00089        QDir ds(pfad2, "*.py", QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::NoSymLinks);
00090        if ((ds.exists()) && (ds.count() != 0))
00091        {
00092               for (uint dc = 0; dc < ds.count(); ++dc)
00093               {
00094                      QFileInfo fs(ds[dc]);
00095                      QString strippedName=fs.baseName(false);
00096                      scrScripterActions.insert(strippedName, new ScrAction( ScrAction::RecentScript, QIconSet(), strippedName, QKeySequence(), this, strippedName));
00097                      connect( scrScripterActions[strippedName], SIGNAL(activatedData(QString)), this, SLOT(StdScript(QString)) );
00098                      menuMgr->addMenuItem(scrScripterActions[strippedName], "ScribusScripts");
00099               }
00100        }
00101 
00102 
00103 }
00104 
00105 void ScripterCore::rebuildRecentScriptsMenu()
00106 {
00107        for( QMap<QString, QGuardedPtr<ScrAction> >::Iterator it = scrRecentScriptActions.begin(); it!=scrRecentScriptActions.end(); ++it )
00108               menuMgr->removeMenuItem((*it), "RecentScripts");
00109 
00110        scrRecentScriptActions.clear();
00111        uint max = QMIN(PrefsManager::instance()->appPrefs.RecentDCount, RecentScripts.count());
00112        for (uint m = 0; m < max; ++m)
00113        {
00114               QString strippedName=RecentScripts[m];
00115               strippedName.remove(QDir::separator());
00116               scrRecentScriptActions.insert(strippedName, new ScrAction( ScrAction::RecentScript, QIconSet(), RecentScripts[m], QKeySequence(), this, strippedName));
00117               connect( scrRecentScriptActions[strippedName], SIGNAL(activatedData(QString)), this, SLOT(RecentScript(QString)) );
00118               menuMgr->addMenuItem(scrRecentScriptActions[strippedName], "RecentScripts");
00119        }
00120 }
00121 
00122 void ScripterCore::buildRecentScriptsMenu()
00123 {
00124        RecentScripts = SavedRecentScripts;
00125        scrRecentScriptActions.clear();
00126        if (SavedRecentScripts.count() != 0)
00127        {
00128               uint max = QMIN(PrefsManager::instance()->appPrefs.RecentDCount, SavedRecentScripts.count());
00129               for (uint m = 0; m < max; ++m)
00130               {
00131                      QFileInfo fd(SavedRecentScripts[m]);
00132                      if (fd.exists())
00133                      {
00134                             QString strippedName=SavedRecentScripts[m];
00135                             strippedName.remove(QDir::separator());
00136                             scrRecentScriptActions.insert(strippedName, new ScrAction( ScrAction::RecentScript, QIconSet(), SavedRecentScripts[m], QKeySequence(), this, strippedName));
00137                             connect( scrRecentScriptActions[strippedName], SIGNAL(activatedData(QString)), this, SLOT(RecentScript(QString)) );
00138                             menuMgr->addMenuItem(scrRecentScriptActions[strippedName], "RecentScripts");
00139                      }
00140               }
00141        }
00142 }
00143 
00144 void ScripterCore::FinishScriptRun()
00145 {
00146        ScribusMainWindow* ScMW=ScCore->primaryMainWindow();
00147        if (ScMW->HaveDoc)
00148        {
00149               ScMW->propertiesPalette->setDoc(ScMW->doc);
00150               ScMW->layerPalette->setDoc(ScMW->doc);
00151               ScMW->outlinePalette->setDoc(ScMW->doc);
00152               ScMW->outlinePalette->BuildTree();
00153               ScMW->pagePalette->setView(ScMW->view);
00154               ScMW->pagePalette->Rebuild();
00155               ScMW->doc->RePos = true;
00156               QPixmap pgPix(10, 10);
00157               QRect rd = QRect(0,0,9,9);
00158               ScPainter *painter = new ScPainter(&pgPix, pgPix.width(), pgPix.height());
00159               for (uint azz=0; azz<ScMW->doc->Items->count(); ++azz)
00160               {
00161                      PageItem *ite = ScMW->doc->Items->at(azz);
00162                      if (ite->Groups.count() != 0)
00163                             ScMW->doc->GroupOnPage(ite);
00164                      else
00165                             ite->OwnPage = ScMW->doc->OnPage(ite);
00166                      ite->setRedrawBounding();
00167                      if ((ite->itemType() == PageItem::TextFrame) || (ite->itemType() == PageItem::PathText)) // && (!ite->Redrawn))
00168                      {
00169                             if (ite->itemType() == PageItem::PathText)
00170                             {
00171                                    ite->Frame = false;
00172                                    ite->updatePolyClip();
00173                                    ite->DrawObj(painter, rd);
00174                             }
00175                             else
00176                             {
00177                                    if ((ite->prevInChain() != 0) || (ite->nextInChain() != 0))
00178                                    {
00179                                           PageItem *nextItem = ite;
00180                                           while (nextItem != 0)
00181                                           {
00182                                                  if (nextItem->prevInChain() != 0)
00183                                                         nextItem = nextItem->prevInChain();
00184                                                  else
00185                                                         break;
00186                                           }
00187                                           ite = nextItem;
00188                                           ite->DrawObj(painter, rd);
00189                                    }
00190                                    else
00191                                           ite->DrawObj(painter, rd);
00192                             }
00193                      }
00194               }
00195               delete painter;
00196               ScMW->doc->RePos = false;
00197               if (ScMW->doc->m_Selection->count() != 0)
00198               {
00199                      ScMW->doc->m_Selection->itemAt(0)->emitAllToGUI();
00200                      ScMW->HaveNewSel(ScMW->doc->m_Selection->itemAt(0)->itemType());
00201               }
00202               else
00203                      ScMW->HaveNewSel(-1);
00204               ScMW->view->DrawNew();
00205               //CB Really only need (want?) this for new docs, but we need it after a call to ScMW doFileNew.
00206               //We don't want it in cmddoc calls as itll interact with the GUI before a script may be finished.
00207               ScMW->HaveNewDoc();
00208        }
00209 }
00210 
00211 void ScripterCore::runScriptDialog()
00212 {
00213        QString fileName;
00214        QString curDirPath = QDir::currentDirPath();
00215        RunScriptDialog dia( ScCore->primaryMainWindow(), m_enableExtPython );
00216        if (dia.exec())
00217        {
00218               fileName = dia.selectedFile();
00219               slotRunScriptFile(fileName, dia.extensionRequested());
00220 
00221               if (RecentScripts.findIndex(fileName) == -1)
00222                      RecentScripts.prepend(fileName);
00223               else
00224               {
00225                      RecentScripts.remove(fileName);
00226                      RecentScripts.prepend(fileName);
00227               }
00228               rebuildRecentScriptsMenu();
00229        }
00230        QDir::setCurrent(curDirPath);
00231        FinishScriptRun();
00232 }
00233 
00234 void ScripterCore::StdScript(QString basefilename)
00235 {
00236        QString pfad = ScPaths::instance().scriptDir();
00237        QString pfad2;
00238        pfad2 = QDir::convertSeparators(pfad);
00239        QString fn = pfad2+basefilename+".py";
00240        QFileInfo fd(fn);
00241        if (!fd.exists())
00242               return;
00243        slotRunScriptFile(fn);
00244        FinishScriptRun();
00245 }
00246 
00247 void ScripterCore::RecentScript(QString fn)
00248 {
00249        QFileInfo fd(fn);
00250        if (!fd.exists())
00251        {
00252               RecentScripts.remove(fn);
00253               rebuildRecentScriptsMenu();
00254               return;
00255        }
00256        slotRunScriptFile(fn);
00257        FinishScriptRun();
00258 }
00259 
00260 void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
00261 {
00262        PyThreadState *stateo = NULL;
00263        PyThreadState *state = NULL;
00264        QFileInfo fi(fileName);
00265        QCString na = fi.fileName().latin1();
00266        // Set up a sub-interpreter if needed:
00267        if (!inMainInterpreter)
00268        {
00269               ScCore->primaryMainWindow()->ScriptRunning = true;
00270               qApp->setOverrideCursor(QCursor(waitCursor), false);
00271               // Create the sub-interpreter
00272               // FIXME: This calls abort() in a Python debug build. We're doing something wrong.
00273               stateo = PyEval_SaveThread();
00274               state = Py_NewInterpreter();
00275               // Chdir to the dir the script is in
00276               QDir::setCurrent(fi.dirPath(true));
00277               // Init the scripter module in the sub-interpreter
00278               initscribus(ScCore->primaryMainWindow());
00279        }
00280        // Make sure sys.argv[0] is the path to the script
00281        char* comm[2];
00282        comm[0] = na.data();
00283        // and tell the script if it's running in the main intepreter or
00284        // a subinterpreter using the second argument, ie sys.argv[1]
00285        if (inMainInterpreter)
00286               comm[1] = const_cast<char*>("ext");
00287        else
00288               comm[1] = const_cast<char*>("sub");
00289        PySys_SetArgv(2, comm);
00290        // call python script
00291        PyObject* m = PyImport_AddModule((char*)"__main__");
00292        if (m == NULL)
00293               qDebug("Failed to get __main__ - aborting script");
00294        else
00295        {
00296               // FIXME: If filename contains chars outside 7bit ascii, might be problems
00297               PyObject* globals = PyModule_GetDict(m);
00298               // Build the Python code to run the script
00299               //QString cm = QString("from __future__ import division\n"); removed due #5252 PV
00300               QString cm = QString("import sys\n");
00301               cm        += QString("import cStringIO\n");
00302               /* Implementation of the help() in pydoc.py reads some OS variables
00303                * for output settings. I use ugly hack to stop freezing calling help()
00304                * in script. pv. */
00305               cm        += QString("import os\nos.environ['PAGER'] = '/bin/false'\n"); // HACK
00306               cm        += QString("sys.path[0] = \"%1\"\n").arg(fi.dirPath(true));
00307               // Replace sys.stdin with a dummy StringIO that always returns
00308               // "" for read
00309               cm        += QString("sys.stdin = cStringIO.StringIO()\n");
00310               cm        += QString("try:\n");
00311               cm        += QString("    execfile(\"%1\")\n").arg(fileName);
00312               cm        += QString("except SystemExit:\n");
00313               cm        += QString("    pass\n");
00314               // Capture the text of any other exception that's raised by the interpreter
00315               // into a StringIO buffer for later extraction.
00316               cm        += QString("except:\n");
00317               cm        += QString("    import traceback\n");
00318               cm        += QString("    import scribus\n");                  // we stash our working vars here
00319               cm        += QString("    scribus._f=cStringIO.StringIO()\n");
00320               cm        += QString("    traceback.print_exc(file=scribus._f)\n");
00321               cm        += QString("    _errorMsg = scribus._f.getvalue()\n");
00322               cm        += QString("    del(scribus._f)\n");
00323               // We re-raise the exception so the return value of PyRun_StringFlags reflects
00324               // the fact that an exception has ocurred.
00325               cm        += QString("    raise\n");
00326               // FIXME: if cmd contains chars outside 7bit ascii, might be problems
00327               QCString cmd = cm.latin1();
00328               // Now run the script in the interpreter's global scope. It'll run in a
00329               // sub-interpreter if we created and switched to one earlier, otherwise
00330               // it'll run in the main interpreter.
00331               PyObject* result = PyRun_String(cmd.data(), Py_file_input, globals, globals);
00332               // NULL is returned if an exception is set. We don't care about any
00333               // other return value (most likely None anyway) and can ignore it.
00334               if (result == NULL)
00335               {
00336                      // We've already saved the exception text, so clear the exception
00337                      PyErr_Clear();
00338                      PyObject* errorMsgPyStr = PyMapping_GetItemString(globals, (char*)"_errorMsg");
00339                      if (errorMsgPyStr == NULL)
00340                      {
00341                             // It's rather unlikely that this will ever be reached - to get here
00342                             // we'd have to fail to retrive the string we just created.
00343                             qDebug("Error retrieving error message content after script exception!");
00344                             qDebug("Exception was:");
00345                             PyErr_Print();
00346                      }
00347                      else
00348                      {
00349                             QString errorMsg = PyString_AsString(errorMsgPyStr);
00350                             // Display a dialog to the user with the exception
00351                             QClipboard *cp = QApplication::clipboard();
00352                             cp->setText(errorMsg);
00353                             ScCore->closeSplash();
00354                             QMessageBox::warning(ScCore->primaryMainWindow(),
00355                                                                tr("Script error"),
00356                                                                "<qt><p>"
00357                                                                + tr("If you are running an official script report it at <a href=\"http://bugs.scribus.net\">bugs.scribus.net</a> please.")
00358                                                                + "</p><pre>" +errorMsg + "</pre><p>"
00359                                                                + tr("This message is in your clipboard too. Use Ctrl+V to paste it into bug tracker.")
00360                                                                + "</p></qt>");
00361                      }
00362               } // end if result == NULL
00363               // Because 'result' may be NULL, not a PyObject*, we must call PyXDECREF not Py_DECREF
00364               Py_XDECREF(result);
00365        } // end if m == NULL
00366        if (!inMainInterpreter)
00367        {
00368               Py_EndInterpreter(state);
00369               PyEval_RestoreThread(stateo);
00370               qApp->restoreOverrideCursor();
00371        }
00372        ScCore->primaryMainWindow()->ScriptRunning = false;
00373 }
00374 
00375 void ScripterCore::slotRunScript(const QString Script)
00376 {
00377        ScCore->primaryMainWindow()->ScriptRunning = true;
00378        inValue = Script;
00379        QString cm;
00380        cm = "# -*- coding: utf8 -*- \n";
00381        if (PyThreadState_Get() != NULL)
00382        {
00383               initscribus(ScCore->primaryMainWindow());
00384               /* HACK: following loop handles all input line by line.
00385               It *should* use I.C. because of docstrings etc. I.I. cannot
00386               handle docstrings right.
00387               Calling all code in one command:
00388               ia = code.InteractiveInterpreter() ia.runsource(getval())
00389               works fine in plain Python. Not here. WTF? */
00390               cm += ("import cStringIO\n"
00391                             "scribus._bu = cStringIO.StringIO()\n"
00392                             "sys.stdout = scribus._bu\n"
00393                             "sys.stderr = scribus._bu\n"
00394                             "sys.argv = ['scribus', 'ext']\n" // this is the PySys_SetArgv replacement
00395                             "for i in scribus.getval().splitlines():\n"
00396                             "    scribus._ia.push(i)\n"
00397                             "scribus.retval(scribus._bu.getvalue())\n"
00398                             "sys.stdout = sys.__stdout__\n"
00399                             "sys.stderr = sys.__stderr__\n");
00400        }
00401        // Set up sys.argv
00402        /* PV - WARNING: THIS IS EVIL! This code summons a crash - see
00403        bug #3510. I don't know why as the Python C API is a little
00404        bit magic for me. It looks like it replaces the cm QString or what...
00405        "In file tools/qgarray.cpp, line 147: Out of memory"
00406        Anyway - sys.argv is set above
00407        char* comm[1];
00408        comm[0] = const_cast<char*>("scribus");
00409        // the scripter console runs everything in the main interpreter
00410        // tell the code it's running there.
00411        comm[1] = const_cast<char*>("ext");
00412        PySys_SetArgv(2, comm); */
00413        // then run the code
00414        PyObject* m = PyImport_AddModule((char*)"__main__");
00415        if (m == NULL)
00416               qDebug("Failed to get __main__ - aborting script");
00417        else
00418        {
00419               PyObject* globals = PyModule_GetDict(m);
00420               PyObject* result = PyRun_String(cm.utf8().data(), Py_file_input, globals, globals);
00421               if (result == NULL)
00422               {
00423                      PyErr_Print();
00424                      QMessageBox::warning(ScCore->primaryMainWindow(), tr("Script error"),
00425                                    "<qt>" + tr("There was an internal error while trying the "
00426                                       "command you entered. Details were printed to "
00427                                       "stderr. ") + "</qt>");
00428               }
00429        }
00430        ScCore->primaryMainWindow()->ScriptRunning = false;
00431 }
00432 
00433 void ScripterCore::slotInteractiveScript(bool visible)
00434 {
00435        QObject::disconnect( scrScripterActions["scripterShowConsole"], SIGNAL(toggled(bool)) , this, SLOT(slotInteractiveScript(bool)) );
00436 
00437        scrScripterActions["scripterShowConsole"]->setOn(visible);
00438        pcon->setFonts();
00439        pcon->setShown(visible);
00440 
00441        QObject::connect( scrScripterActions["scripterShowConsole"], SIGNAL(toggled(bool)) , this, SLOT(slotInteractiveScript(bool)) );
00442 }
00443 
00444 void ScripterCore::slotExecute()
00445 {
00446        slotRunScript(pcon->command());
00447        pcon->outputEdit->append(returnString);
00448        pcon->commandEdit->ensureCursorVisible();
00449        FinishScriptRun();
00450 }
00451 
00452 void ScripterCore::ReadPlugPrefs()
00453 {
00454        PrefsContext* prefs = PrefsManager::instance()->prefsFile->getPluginContext("scriptplugin");
00455        if (!prefs)
00456        {
00457               qDebug("scriptplugin: Unable to load prefs");
00458               return;
00459        }
00460        PrefsTable* prefRecentScripts = prefs->getTable("recentscripts");
00461        if (!prefRecentScripts)
00462        {
00463               qDebug("scriptplugin: Unable to get recent scripts");
00464               return;
00465        }
00466        // Load recent scripts from the prefs
00467        for (int i = 0; i < prefRecentScripts->getRowCount(); i++)
00468               SavedRecentScripts.append(prefRecentScripts->get(i,0));
00469        // then get more general preferences
00470        m_enableExtPython = prefs->getBool("extensionscripts",false);
00471        m_importAllNames = prefs->getBool("importall",true);
00472        m_startupScript = prefs->get("startupscript", QString::null);
00473        // and have the console window set up its position
00474 }
00475 
00476 void ScripterCore::SavePlugPrefs()
00477 {
00478        PrefsContext* prefs = PrefsManager::instance()->prefsFile->getPluginContext("scriptplugin");
00479        if (!prefs)
00480        {
00481               qDebug("scriptplugin: Unable to load prefs");
00482               return;
00483        }
00484        PrefsTable* prefRecentScripts = prefs->getTable("recentscripts");
00485        if (!prefRecentScripts)
00486        {
00487               qDebug("scriptplugin: Unable to get recent scripts");
00488               return;
00489        }
00490        for (uint i = 0; i < RecentScripts.count(); i++)
00491               prefRecentScripts->set(i, 0, RecentScripts[i]);
00492        // then save more general preferences
00493        prefs->set("extensionscripts", m_enableExtPython);
00494        prefs->set("importall", m_importAllNames);
00495        prefs->set("startupscript", m_startupScript);
00496 }
00497 
00498 void ScripterCore::aboutScript()
00499 {
00500        QString fname = ScCore->primaryMainWindow()->CFileDialog(".", tr("Examine Script"), tr("Python Scripts (*.py *.PY);;All Files (*)"), "", fdNone);
00501        if (fname == QString::null)
00502               return;
00503        QString html("<html><body>");
00504        QFileInfo fi = QFileInfo(fname);
00505        QFile input(fname);
00506        if(!input.open(IO_ReadOnly))
00507               return;
00508        QTextStream intputstream(&input);
00509        QString content = intputstream.read();
00510        QString docstring = content.section("\"\"\"", 1, 1);
00511        if (!docstring.isEmpty())
00512        {
00513               html += QString("<h1>%1 %2</h1>").arg( tr("Documentation for:")).arg(fi.fileName());
00514               html += QString("<p>%1</p>").arg(docstring.replace("\n\n", "<br><br>"));
00515        }
00516        else
00517        {
00518               html += QString("<p><b>%1 %2 %3</b></p>").arg( tr("Script")).arg(fi.fileName()).arg( tr(" doesn't contain any docstring!"));
00519               html += QString("<pre>%4</pre>").arg(content);
00520        }
00521        html += "</body></html>";
00522        input.close();
00523        HelpBrowser *dia = new HelpBrowser(0, QObject::tr("About Script") + " " + fi.fileName(), "en");
00524        dia->setText(html);
00525        dia->show();
00526 }
00527 
00528 void ScripterCore::initExtensionScripts()
00529 {
00530        // Nothing to do currently
00531 }
00532 
00533 void ScripterCore::runStartupScript()
00534 {
00535        if ((m_enableExtPython) && (m_startupScript))
00536        {
00537               if (QFile::exists(this->m_startupScript))
00538               {
00539                      // run the script in the main interpreter. The user will be informed
00540                      // with a dialog if something has gone wrong.
00541                      this->slotRunScriptFile(this->m_startupScript, true);
00542               }
00543               else
00544                      qDebug("Startup script enabled, but couln't find script %s.", m_startupScript.ascii());
00545        }
00546 }
00547 
00548 void ScripterCore::languageChange()
00549 {
00550        scrScripterActions["scripterExecuteScript"]->setMenuText(QObject::tr("&Execute Script..."));
00551        scrScripterActions["scripterShowConsole"]->setMenuText(QObject::tr("Show &Console"));
00552        scrScripterActions["scripterAboutScript"]->setMenuText(QObject::tr("&About Script..."));
00553 
00554        menuMgr->setMenuText("Scripter", QObject::tr("&Script"));
00555        menuMgr->setMenuText("ScribusScripts", QObject::tr("&Scribus Scripts"));
00556        menuMgr->setMenuText("RecentScripts", QObject::tr("&Recent Scripts"));
00557 }
00558 
00559 bool ScripterCore::setupMainInterpreter()
00560 {
00561        QString cm = QString(
00562               "# -*- coding: utf-8 -*-\n"
00563               "import scribus\n"
00564               "import sys\n"
00565               "sys.path[0] = \"%1\"\n"
00566               "import cStringIO\n"
00567               "sys.stdin = cStringIO.StringIO()\n"
00568               "import code\n"
00569               "scribus._ia = code.InteractiveConsole(globals())\n"
00570               ).arg(ScPaths::instance().scriptDir());
00571        if (m_importAllNames)
00572               cm += "from scribus import *\n";
00573        QCString cmd = cm.utf8();
00574        if (PyRun_SimpleString(cmd.data()))
00575        {
00576               PyErr_Print();
00577               QMessageBox::warning(ScCore->primaryMainWindow(), tr("Script error"),
00578                             tr("Setting up the Python plugin failed. "
00579                                "Error details were printed to stderr. "));
00580               return false;
00581        }
00582        else
00583               return true;
00584 }
00585 
00586 void ScripterCore::setStartupScript(const QString& newScript)
00587 {
00588        m_startupScript = newScript;
00589 }
00590 
00591 void ScripterCore::setExtensionsEnabled(bool enable)
00592 {
00593        m_enableExtPython = enable;
00594 }
00595 
00596 const QString & ScripterCore::startupScript() const
00597 {
00598        return m_startupScript;
00599 }
00600 
00601 bool ScripterCore::extensionsEnabled() const
00602 {
00603        return m_enableExtPython;
00604 }