File indexing completed on 2024-12-22 04:17:45
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2011 Joshua Netterfield * 0004 * joshua.netterfield@gmail.com * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "scriptserver.h" 0014 #include "labelscriptinterface.h" 0015 #include "viewitemscriptinterface.h" 0016 #include "arrowscriptinterface.h" 0017 #include "plotscriptinterface.h" 0018 #include "legendscriptinterface.h" 0019 0020 #include "stringscriptinterface.h" 0021 #include "scalarscriptinterface.h" 0022 #include "vectorscriptinterface.h" 0023 #include "matrixscriptinterface.h" 0024 0025 #include "relationscriptinterface.h" 0026 0027 #include "dataobjectscriptinterface.h" 0028 0029 #include "sessionmodel.h" 0030 #include "updateserver.h" 0031 #include "datagui.h" 0032 0033 //viewitems 0034 #include "arrowitem.h" 0035 #include "boxitem.h" 0036 #include "buttonitem.h" 0037 #include "lineedititem.h" 0038 #include "circleitem.h" 0039 #include "ellipseitem.h" 0040 #include "labelitem.h" 0041 #include "layoutboxitem.h" 0042 #include "lineitem.h" 0043 #include "pictureitem.h" 0044 #include "plotitem.h" 0045 #include "svgitem.h" 0046 #include "viewitemdialog.h" 0047 #include "document.h" 0048 0049 #include "curve.h" 0050 #include "equation.h" 0051 #include "editablevector.h" 0052 #include "matrix.h" 0053 #include "histogram.h" 0054 #include "psd.h" 0055 #include "eventmonitorentry.h" 0056 #include "image.h" 0057 #include "csd.h" 0058 #include "basicplugin.h" 0059 #include "dialog.h" 0060 #include "editablematrix.h" 0061 0062 #include "datasourcepluginmanager.h" 0063 0064 #include <updatemanager.h> 0065 0066 #include <QLocalSocket> 0067 #include <iostream> 0068 #include <QFile> 0069 #include <QStringBuilder> 0070 0071 namespace Kst { 0072 0073 ScriptServer::ScriptServer(ObjectStore *obj) : _server(new QLocalServer(this)), _store(obj),_interface(0) { 0074 0075 QString initial; 0076 0077 initial = kstApp->userName(); 0078 0079 serverNameSet = false; 0080 0081 // The command line hasn't been parsed yet, so 0082 // we can't rely on that to get the server name. 0083 QStringList args= qApp->arguments(); 0084 for(int i=0;i<args.size();i++) { 0085 if (args.at(i).startsWith("--serverName=")) { 0086 initial=args.at(i); 0087 initial.remove("--serverName="); 0088 serverNameSet = true; 0089 } 0090 } 0091 0092 serverName=initial; 0093 0094 int j = 1; 0095 while(1) { 0096 QLocalSocket socket; 0097 socket.connectToServer(serverName); 0098 socket.waitForConnected(300); 0099 if(socket.state()!=QLocalSocket::ConnectedState) { 0100 _server->removeServer(serverName); 0101 _server->listen(serverName); 0102 break; 0103 } 0104 socket.disconnectFromServer(); 0105 serverName=initial+"-"+QString::number(j++); 0106 } 0107 connect(_server,SIGNAL(newConnection()),this,SLOT(procConnection())); 0108 0109 _fnMap.insert("newDataVector()",&ScriptServer::newDataVector); 0110 _fnMap.insert("newGeneratedVector()",&ScriptServer::newGeneratedVector); 0111 _fnMap.insert("newEditableVector()",&ScriptServer::newEditableVector); 0112 0113 _fnMap.insert("getVectorList()",&ScriptServer::getVectorList); 0114 _fnMap.insert("getDataVectorList()",&ScriptServer::getDataVectorList); 0115 _fnMap.insert("getGeneratedVectorList()",&ScriptServer::getGeneratedVectorList); 0116 _fnMap.insert("getEditableVectorList()",&ScriptServer::getEditableVectorList); 0117 0118 _fnMap.insert("getMatrixList()",&ScriptServer::getMatrixList); 0119 _fnMap.insert("newDataMatrix()",&ScriptServer::newDataMatrix); 0120 0121 _fnMap.insert("getEditableMatrixList()",&ScriptServer::getEditableMatrixList); 0122 _fnMap.insert("newEditableMatrix()",&ScriptServer::newEditableMatrix); 0123 0124 _fnMap.insert("getScalarList()",&ScriptServer::getScalarList); 0125 _fnMap.insert("newGeneratedScalar()",&ScriptServer::newGeneratedScalar); 0126 _fnMap.insert("newDataScalar()",&ScriptServer::newDataScalar); 0127 _fnMap.insert("newVectorScalar()",&ScriptServer::newVScalar); 0128 0129 _fnMap.insert("getStringList()",&ScriptServer::getStringList); 0130 _fnMap.insert("newGeneratedString()",&ScriptServer::newGeneratedString); 0131 _fnMap.insert("newDataString()",&ScriptServer::newDataString); 0132 0133 _fnMap.insert("getCurveList()",&ScriptServer::getCurveList); 0134 _fnMap.insert("newCurve()",&ScriptServer::newCurve); 0135 0136 _fnMap.insert("getEquationList()",&ScriptServer::getEquationList); 0137 _fnMap.insert("newEquation()",&ScriptServer::newEquation); 0138 0139 _fnMap.insert("getHistogramList()",&ScriptServer::getHistogramList); 0140 _fnMap.insert("newHistogram()",&ScriptServer::newHistogram); 0141 0142 _fnMap.insert("getSpectrumList()",&ScriptServer::getSpectrumList); 0143 _fnMap.insert("newSpectrum()",&ScriptServer::newSpectrum); 0144 0145 _fnMap.insert("getPluginList()", &ScriptServer::getPluginList); 0146 _fnMap.insert("newPlugin()",&ScriptServer::newPlugin); 0147 0148 _fnMap.insert("getImageList()",&ScriptServer::getImageList); 0149 _fnMap.insert("newImage()",&ScriptServer::newImage); 0150 0151 _fnMap.insert("getCSDList()",&ScriptServer::getCSDList); 0152 // _fnMap.insert("newCSD()",&ScriptServer::newCSD); 0153 0154 _fnMap.insert("getBasicPluginList()",&ScriptServer::getBasicPluginList); 0155 _fnMap.insert("getBasicPluginTypeList()",&ScriptServer::getBasicPluginTypeList); 0156 // _fnMap.insert("newBasicPlugin()",&ScriptServer::newBasicPlugin); 0157 0158 _fnMap.insert("getArrowList()",&ScriptServer::getArrowList); 0159 _fnMap.insert("newArrow()",&ScriptServer::newArrow); 0160 0161 _fnMap.insert("getBoxList()",&ScriptServer::getBoxList); 0162 _fnMap.insert("newBox()",&ScriptServer::newBox); 0163 0164 _fnMap.insert("getLegendList()",&ScriptServer::getLegendList); 0165 _fnMap.insert("newLegend()",&ScriptServer::newLegend); 0166 0167 _fnMap.insert("getButtonList()",&ScriptServer::getButtonList); 0168 _fnMap.insert("newButton()",&ScriptServer::newButton); 0169 0170 _fnMap.insert("getLineEditList()",&ScriptServer::getLineEditList); 0171 _fnMap.insert("newLineEdit()",&ScriptServer::newLineEdit); 0172 0173 _fnMap.insert("getCircleList()",&ScriptServer::getCircleList); 0174 _fnMap.insert("newCircle()",&ScriptServer::newCircle); 0175 0176 _fnMap.insert("getEllipseList()",&ScriptServer::getEllipseList); 0177 _fnMap.insert("newEllipse()",&ScriptServer::newEllipse); 0178 0179 _fnMap.insert("getLabelList()",&ScriptServer::getLabelList); 0180 _fnMap.insert("newLabel()",&ScriptServer::newLabel); 0181 0182 _fnMap.insert("getLineList()",&ScriptServer::getLineList); 0183 _fnMap.insert("newLine()",&ScriptServer::newLine); 0184 0185 _fnMap.insert("getPictureList()",&ScriptServer::getPictureList); 0186 _fnMap.insert("newPicture()",&ScriptServer::newPicture); 0187 0188 _fnMap.insert("getPlotList()",&ScriptServer::getPlotList); 0189 _fnMap.insert("newPlot()",&ScriptServer::newPlot); 0190 0191 #ifndef KST_NO_SVG 0192 _fnMap.insert("getSvgItemList()",&ScriptServer::getSvgItemList); 0193 _fnMap.insert("newSvgItem()",&ScriptServer::newSvgItem); 0194 #endif 0195 0196 _fnMap.insert("beginEdit()",&ScriptServer::beginEdit); 0197 _fnMap.insert("endEdit()",&ScriptServer::endEdit); 0198 0199 _fnMap.insert("eliminate()",&ScriptServer::eliminate); 0200 0201 _fnMap.insert("done()",&ScriptServer::done); 0202 _fnMap.insert("clear()",&ScriptServer::clear); 0203 0204 _fnMap.insert("tabCount()",&ScriptServer::tabCount); 0205 _fnMap.insert("newTab()",&ScriptServer::newTab); 0206 _fnMap.insert("setTab()",&ScriptServer::setTab); 0207 _fnMap.insert("renameTab()",&ScriptServer::renameTab); 0208 _fnMap.insert("screenBack()",&ScriptServer::screenBack); 0209 _fnMap.insert("screenForward()",&ScriptServer::screenForward); 0210 _fnMap.insert("countFromEnd()",&ScriptServer::countFromEnd); 0211 _fnMap.insert("readToEnd()",&ScriptServer::readToEnd); 0212 _fnMap.insert("setPaused()",&ScriptServer::setPaused); 0213 _fnMap.insert("unsetPaused()",&ScriptServer::unsetPaused); 0214 _fnMap.insert("hide()", &ScriptServer::hide); 0215 _fnMap.insert("minimize()", &ScriptServer::minimize); 0216 _fnMap.insert("show()", &ScriptServer::show_window); 0217 _fnMap.insert("maximize()", &ScriptServer::maximize); 0218 _fnMap.insert("quit()", &ScriptServer::quit_kst); 0219 0220 _fnMap.insert("fileOpen()", &ScriptServer::fileOpen); 0221 _fnMap.insert("fileSave()", &ScriptServer::fileSave); 0222 _fnMap.insert("exportGraphics()", &ScriptServer::exportGraphics); 0223 0224 _fnMap.insert("setDatasourceBoolConfig()", &ScriptServer::setDatasourceBoolConfig); 0225 _fnMap.insert("setDatasourceIntConfig()", &ScriptServer::setDatasourceIntConfig); 0226 _fnMap.insert("setDatasourceStringConfig()", &ScriptServer::setDatasourceStringConfig); 0227 0228 _fnMap.insert("cleanupLayout()", &ScriptServer::cleanupLayout); 0229 0230 _fnMap.insert("testCommand()", &ScriptServer::testCommand); 0231 0232 #if 0 0233 0234 _fnMap.insert("EditableVector::setBinaryArray()",&ScriptServer::editableVectorSetBinaryArray); 0235 _fnMap.insert("EditableMatrix::setBinaryArray()",&ScriptServer::editableMatrixSetBinaryArray); 0236 _fnMap.insert("EditableVector::set()",&ScriptServer::editableVectorSet); 0237 _fnMap.insert("Vector::getBinaryArray()",&ScriptServer::vectorGetBinaryArray); 0238 _fnMap.insert("Matrix::getBinaryArray()",&ScriptServer::matrixGetBinaryArray); 0239 _fnMap.insert("String::value()",&ScriptServer::stringValue); 0240 _fnMap.insert("String::setValue()",&ScriptServer::stringSetValue); 0241 _fnMap.insert("Scalar::value()",&ScriptServer::scalarValue); 0242 _fnMap.insert("Scalar::setValue()",&ScriptServer::scalarSetValue); 0243 #endif 0244 0245 } 0246 0247 ScriptServer::~ScriptServer() 0248 { 0249 delete _server; 0250 delete _interface; 0251 } 0252 0253 void ScriptServer::setScriptServerName(QString initial) 0254 { 0255 int j = 1; 0256 0257 _server->close(); 0258 serverName = initial; 0259 while(1) { 0260 QLocalSocket socket; 0261 socket.connectToServer(serverName); 0262 socket.waitForConnected(300); 0263 if(socket.state()!=QLocalSocket::ConnectedState) { 0264 _server->removeServer(serverName); 0265 _server->listen(serverName); 0266 break; 0267 } 0268 socket.disconnectFromServer(); 0269 serverName=initial+"-"+QString::number(j++); 0270 } 0271 serverNameSet = true; 0272 //connect(_server,SIGNAL(newConnection()),this,SLOT(procConnection())); 0273 } 0274 0275 /** Conv. function which takes a response, and executes if 'if' statement is unexistant or true. */ 0276 QByteArray handleResponse(const QByteArray& response, QLocalSocket* s) 0277 { 0278 if(s) { 0279 if(response.isEmpty()) { 0280 s->write(" "); 0281 } else { 0282 s->write(response); 0283 } 0284 s->waitForBytesWritten(); 0285 } 0286 0287 return response.isEmpty()?" ":response; 0288 } 0289 0290 /** @sa outputViewItemList() */ 0291 template<class T> QByteArray outputObjectList( 0292 QLocalSocket* s,ObjectStore*_store) { 0293 0294 ObjectList<T> vl=_store->getObjects<T>(); 0295 QByteArray a; 0296 typename ObjectList<T>::ConstIterator it = vl.constBegin(); 0297 bool first = true; 0298 for(; it != vl.constEnd(); ++it) { 0299 SharedPtr<T> v = (*it); 0300 v->readLock(); 0301 if (!first) { 0302 a += '|'; 0303 } 0304 first = false; 0305 a+=v->Name(); 0306 v->unlock(); 0307 } 0308 if(a.size()) { 0309 return handleResponse(a,s); 0310 } else { 0311 return handleResponse("NO_OBJECTS",s); 0312 } 0313 } 0314 0315 /** @sa outputObjectList() */ 0316 template<class T> QByteArray outputViewItemList(QLocalSocket* s) { 0317 QList<T *> vl=ViewItem::getItems<T>(); 0318 QByteArray a; 0319 typename QList<T*>::iterator it = vl.begin(); 0320 for(; it != vl.end(); ++it) { 0321 T* v = (*it); 0322 a+='['%v->Name()%']'; 0323 } 0324 if(a.size()) { 0325 return handleResponse(a,s); 0326 } else { 0327 return handleResponse("NO_OBJECTS",s); 0328 } 0329 } 0330 0331 /** Connects pending connections to readSomething. */ 0332 void ScriptServer::procConnection() { 0333 while(_server->hasPendingConnections()) { 0334 QLocalSocket* s=_server->nextPendingConnection(); 0335 connect(s,SIGNAL(readyRead()),this,SLOT(readSomething())); 0336 } 0337 } 0338 0339 /** Processes a socket with data. */ 0340 void ScriptServer::readSomething() 0341 { 0342 QLocalSocket* s=qobject_cast<QLocalSocket*>(sender()); 0343 Q_ASSERT(s); 0344 QByteArray command=s->read(1000000); 0345 if(command.startsWith("attachTo(")) { 0346 QString search=command.remove(0,9).remove(command.lastIndexOf(")"),9999); 0347 for(int h=0;h<2;h++) { 0348 for(int i=0;i<vi.size();i++) { 0349 if(search.contains(vi[i]->shortName().toLatin1())) { 0350 ButtonItem* bi=qobject_cast<ButtonItem*>(vi[i]); 0351 if(bi) { 0352 bi->addSocket(s); 0353 disconnect(s,SIGNAL(readyRead()),this,SLOT(readSomething())); 0354 return; 0355 } 0356 LineEditItem* li=qobject_cast<LineEditItem*>(vi[i]); 0357 if(li) { 0358 li->addSocket(s); 0359 disconnect(s,SIGNAL(readyRead()),this,SLOT(readSomething())); 0360 return; 0361 } 0362 } 0363 } 0364 vi=ViewItem::getItems<ViewItem>(); 0365 } 0366 return; 0367 } 0368 exec(command,s); 0369 } 0370 0371 /** {"Bob", "Fred} -> "Bob"+r+"Fred" */ 0372 inline QByteArray join(const QByteArrayList&n,const char&r) { 0373 QByteArray ret; 0374 for(int i=0;i<n.size();i++) { 0375 ret+=n[i]%r; 0376 } 0377 return ret; 0378 } 0379 0380 /** The heart of the script server. This function is what performs all the actions. s may be null. */ 0381 QByteArray ScriptServer::exec(QByteArray command, QLocalSocket *s) 0382 { 0383 if(command.isEmpty()) { 0384 return handleResponse("",s); 0385 } 0386 0387 // Map 0388 QByteArray ycommand(command); 0389 ycommand.remove(ycommand.indexOf("("),9999999); 0390 ycommand+="()"; 0391 ScriptMemberFn fn=_fnMap.value(ycommand,&ScriptServer::noSuchFn); 0392 if(fn!=&ScriptServer::noSuchFn) { 0393 return CALL_MEMBER_FN(*this,fn)(command, s,_store); 0394 } else { 0395 if(_interface) { 0396 return handleResponse(_interface->doCommand(command).toLatin1(),s); //magic 0397 } else { 0398 return handleResponse("Unknown command!",s); 0399 } 0400 } 0401 0402 return "?"; 0403 } 0404 0405 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 0406 0407 QByteArray ScriptServer::getVectorList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0408 0409 return outputObjectList<Vector>(s,_store); 0410 } 0411 0412 QByteArray ScriptServer::getDataVectorList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0413 0414 return outputObjectList<DataVector>(s,_store); 0415 } 0416 0417 QByteArray ScriptServer::getGeneratedVectorList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0418 0419 return outputObjectList<GeneratedVector>(s,_store); 0420 } 0421 0422 0423 QByteArray ScriptServer::getEditableVectorList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0424 0425 return outputObjectList<EditableVector>(s,_store); 0426 } 0427 0428 QByteArray ScriptServer::newEditableVector(QByteArray&, QLocalSocket* s,ObjectStore*) { 0429 if(_interface) { 0430 return handleResponse("To access this function, first call endEdit()",s); 0431 } else { 0432 _interface = EditableVectorSI::newVector(_store); return handleResponse("Ok",s); 0433 } 0434 } 0435 0436 0437 QByteArray ScriptServer::newDataVector(QByteArray&, QLocalSocket* s,ObjectStore*) { 0438 if(_interface) { 0439 return handleResponse("To access this function, first call endEdit()",s); 0440 } else { 0441 _interface = DataVectorSI::newVector(_store); return handleResponse("Ok",s); 0442 } 0443 } 0444 0445 0446 QByteArray ScriptServer::newGeneratedVector(QByteArray&, QLocalSocket* s,ObjectStore*) { 0447 if(_interface) { 0448 return handleResponse("To access this function, first call endEdit()",s); 0449 } else { 0450 _interface = GeneratedVectorSI::newVector(_store); return handleResponse("Ok",s); 0451 } 0452 } 0453 0454 0455 0456 QByteArray ScriptServer::getScalarList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0457 0458 return outputObjectList<Scalar>(s,_store); 0459 } 0460 0461 0462 QByteArray ScriptServer::newGeneratedScalar(QByteArray&, QLocalSocket* s,ObjectStore*) { 0463 if(_interface) { 0464 return handleResponse("To access this function, first call endEdit()",s); 0465 } else { 0466 _interface = ScalarGenSI::newScalar(_store); return handleResponse("Ok",s); 0467 } 0468 } 0469 0470 0471 QByteArray ScriptServer::newDataScalar(QByteArray&, QLocalSocket* s,ObjectStore*) { 0472 if(_interface) { 0473 return handleResponse("To access this function, first call endEdit()",s); 0474 } else { 0475 _interface = ScalarDataSI::newScalar(_store); return handleResponse("Ok",s); 0476 } 0477 } 0478 0479 0480 QByteArray ScriptServer::newVScalar(QByteArray&, QLocalSocket* s,ObjectStore*) { 0481 if(_interface) { 0482 return handleResponse("To access this function, first call endEdit()",s); 0483 } else { 0484 _interface = ScalarVectorSI::newScalar(_store); return handleResponse("Ok",s); 0485 } 0486 } 0487 0488 0489 QByteArray ScriptServer::getMatrixList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0490 0491 return outputObjectList<Matrix>(s,_store); 0492 } 0493 0494 0495 QByteArray ScriptServer::newDataMatrix(QByteArray&, QLocalSocket* s,ObjectStore*) { 0496 if(_interface) { 0497 return handleResponse("To access this function, first call endEdit()",s); 0498 } else { 0499 _interface = DataMatrixSI::newMatrix(_store); return handleResponse("Ok",s); 0500 } 0501 } 0502 0503 0504 QByteArray ScriptServer::getEditableMatrixList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0505 0506 return outputObjectList<EditableMatrix>(s,_store); 0507 } 0508 0509 QByteArray ScriptServer::newEditableMatrix(QByteArray&, QLocalSocket* s,ObjectStore*) { 0510 if(_interface) { 0511 return handleResponse("To access this function, first call endEdit()",s); 0512 } else { 0513 _interface = EditableMatrixSI::newMatrix(_store); return handleResponse("Ok",s); 0514 } 0515 } 0516 0517 0518 0519 QByteArray ScriptServer::getStringList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0520 0521 return outputObjectList<String>(s,_store); 0522 } 0523 0524 QByteArray ScriptServer::newGeneratedString(QByteArray&, QLocalSocket* s,ObjectStore*) { 0525 if(_interface) { 0526 return handleResponse("To access this function, first call endEdit()",s); 0527 } else { 0528 _interface = StringGenSI::newString(_store); return handleResponse("Ok",s); 0529 } 0530 } 0531 0532 0533 QByteArray ScriptServer::newDataString(QByteArray&, QLocalSocket* s,ObjectStore*) { 0534 0535 if(_interface) { 0536 return handleResponse("To access this function, first call endEdit()",s); 0537 } else { 0538 _interface = StringDataSI::newString(_store); return handleResponse("Ok",s); 0539 } 0540 } 0541 0542 0543 0544 QByteArray ScriptServer::getCurveList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0545 0546 return outputObjectList<Curve>(s,_store); 0547 } 0548 0549 0550 QByteArray ScriptServer::newCurve(QByteArray&, QLocalSocket* s,ObjectStore*) { 0551 0552 if(_interface) { 0553 return handleResponse("To access this function, first call endEdit()",s); 0554 } else { 0555 _interface = CurveSI::newCurve(_store); return handleResponse("Ok",s); 0556 } 0557 } 0558 0559 0560 0561 QByteArray ScriptServer::getEquationList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0562 0563 return outputObjectList<Equation>(s,_store); 0564 } 0565 0566 QByteArray ScriptServer::newEquation(QByteArray&, QLocalSocket* s,ObjectStore*) { 0567 0568 if(_interface) { 0569 return handleResponse("To access this function, first call endEdit()",s); 0570 } else { 0571 _interface = EquationSI::newEquation(_store); return handleResponse("Ok",s); 0572 } 0573 } 0574 0575 0576 QByteArray ScriptServer::getHistogramList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0577 0578 return outputObjectList<Histogram>(s,_store); 0579 } 0580 0581 0582 QByteArray ScriptServer::newHistogram(QByteArray&, QLocalSocket* s,ObjectStore*) { 0583 if (_interface) { 0584 return handleResponse("To access this function, first call endEdit()",s); 0585 } else { 0586 _interface = HistogramSI::newHistogram(_store); return handleResponse("Ok",s); 0587 } 0588 } 0589 0590 0591 QByteArray ScriptServer::getSpectrumList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0592 0593 return outputObjectList<PSD>(s,_store); 0594 } 0595 0596 0597 QByteArray ScriptServer::newSpectrum(QByteArray&, QLocalSocket* s,ObjectStore*) { 0598 0599 if(_interface) { 0600 return handleResponse("To access this function, first call endEdit()",s); 0601 } else { 0602 _interface = SpectrumSI::newSpectrum(_store); return handleResponse("Ok",s); 0603 } 0604 } 0605 0606 0607 QByteArray ScriptServer::getPluginList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0608 0609 return outputObjectList<BasicPlugin>(s,_store); 0610 } 0611 0612 0613 QByteArray ScriptServer::newPlugin(QByteArray& plugin, QLocalSocket* s,ObjectStore* store) { 0614 0615 if(_interface) { 0616 return handleResponse("To access this function, first call endEdit()",s); 0617 } else { 0618 plugin.replace("newPlugin(",""); 0619 plugin.remove(plugin.lastIndexOf(")"),1); 0620 _interface = PluginSI::newPlugin(store, plugin); 0621 return handleResponse("Ok",s); 0622 } 0623 } 0624 0625 0626 QByteArray ScriptServer::getImageList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0627 0628 return outputObjectList<Image>(s,_store); 0629 } 0630 0631 QByteArray ScriptServer::newImage(QByteArray&, QLocalSocket* s,ObjectStore*) { 0632 0633 if(_interface) { 0634 return handleResponse("To access this function, first call endEdit()",s); 0635 } else { 0636 _interface = ImageSI::newImage(_store); return handleResponse("Ok",s); 0637 } 0638 } 0639 0640 0641 QByteArray ScriptServer::getCSDList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0642 0643 return outputObjectList<CSD>(s,_store); 0644 } 0645 0646 /* 0647 QByteArray ScriptServer::newCSD(QByteArray&, QLocalSocket* s,ObjectStore*,const int&ifMode, 0648 0649 if(_interface) { return handleResponse("To access this function, first call endEdit()",s);} 0650 else { 0651 _interface = DialogLauncherSI::self->showCSDDialog(); 0652 return handleResponse("Ok",s); 0653 } 0654 } 0655 */ 0656 0657 0658 QByteArray ScriptServer::getBasicPluginList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0659 0660 return outputObjectList<BasicPlugin>(s,_store); 0661 } 0662 0663 QByteArray ScriptServer::getBasicPluginTypeList(QByteArray&, QLocalSocket* s,ObjectStore*) { 0664 0665 QString a; 0666 for(int i=0;i<DataObject::dataObjectPluginList().size();i++) { 0667 a.push_back(DataObject::dataObjectPluginList()[i].toLatin1()+'\n'); 0668 } 0669 return handleResponse(a.toLatin1(),s); 0670 } 0671 0672 0673 0674 QByteArray ScriptServer::getArrowList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0675 0676 return outputViewItemList<ArrowItem>(s); 0677 } 0678 0679 QByteArray ScriptServer::newArrow(QByteArray&, QLocalSocket* s,ObjectStore*) { 0680 0681 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0682 else { _interface = ArrowSI::newArrow(); return handleResponse("Ok",s); } 0683 } 0684 0685 0686 0687 QByteArray ScriptServer::getBoxList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0688 0689 return outputViewItemList<BoxItem>(s); 0690 } 0691 0692 QByteArray ScriptServer::newBox(QByteArray&, QLocalSocket* s,ObjectStore*) { 0693 0694 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0695 else { _interface = ViewItemSI::newBox(); return handleResponse("Ok",s); } 0696 } 0697 0698 QByteArray ScriptServer::getLegendList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0699 0700 return outputViewItemList<LegendItem>(s); 0701 } 0702 0703 QByteArray ScriptServer::newLegend(QByteArray&command, QLocalSocket* s,ObjectStore*) { 0704 0705 if(_interface) { 0706 return handleResponse("To access this function, first call endEdit()",s); } 0707 else { 0708 _interface = LegendSI::newLegend(command.replace("newLegend(","").replace(")","")); 0709 return handleResponse("Ok",s); 0710 } 0711 } 0712 0713 0714 QByteArray ScriptServer::getButtonList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0715 0716 return outputViewItemList<ButtonItem>(s); 0717 } 0718 0719 QByteArray ScriptServer::newButton(QByteArray&, QLocalSocket* s,ObjectStore*) { 0720 0721 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0722 else { _interface = ViewItemSI::newButton(); return handleResponse("Ok",s); } 0723 } 0724 0725 0726 0727 QByteArray ScriptServer::getLineEditList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0728 0729 return outputViewItemList<LineEditItem>(s); 0730 } 0731 0732 QByteArray ScriptServer::newLineEdit(QByteArray&, QLocalSocket* s,ObjectStore*) { 0733 0734 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0735 else { _interface = ViewItemSI::newLineEdit(); return handleResponse("Ok",s); } 0736 } 0737 0738 0739 0740 QByteArray ScriptServer::getCircleList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0741 0742 return outputViewItemList<CircleItem>(s); 0743 } 0744 0745 QByteArray ScriptServer::newCircle(QByteArray&, QLocalSocket* s,ObjectStore*) { 0746 0747 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0748 else { _interface = ViewItemSI::newCircle(); return handleResponse("Ok",s); } 0749 } 0750 0751 0752 0753 QByteArray ScriptServer::getEllipseList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0754 0755 return outputViewItemList<EllipseItem>(s); 0756 } 0757 0758 QByteArray ScriptServer::newEllipse(QByteArray&, QLocalSocket* s,ObjectStore*) { 0759 0760 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0761 else { _interface = ViewItemSI::newEllipse(); return handleResponse("Ok",s); } 0762 } 0763 0764 0765 0766 QByteArray ScriptServer::getLabelList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0767 0768 return outputViewItemList<LabelItem>(s); 0769 } 0770 0771 QByteArray ScriptServer::newLabel(QByteArray&, QLocalSocket* s,ObjectStore*) { 0772 0773 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0774 else { _interface = LabelSI::newLabel(); return handleResponse("Ok",s); } 0775 } 0776 0777 0778 0779 QByteArray ScriptServer::getLineList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0780 0781 return outputViewItemList<LineItem>(s); 0782 } 0783 0784 QByteArray ScriptServer::newLine(QByteArray&, QLocalSocket* s,ObjectStore*) { 0785 0786 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0787 else { _interface = ViewItemSI::newLine(); return handleResponse("Ok",s); } 0788 } 0789 0790 0791 0792 QByteArray ScriptServer::getPictureList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0793 0794 0795 return outputViewItemList<PictureItem>(s); 0796 } 0797 0798 QByteArray ScriptServer::newPicture(QByteArray&command, QLocalSocket* s,ObjectStore*) { 0799 0800 command.replace("newPicture(",""); 0801 command.remove(command.lastIndexOf(")"),1); 0802 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0803 else { _interface = ViewItemSI::newPicture(command); return handleResponse("Ok",s); } 0804 } 0805 0806 0807 /***********************************/ 0808 /* Plot related scripting commands */ 0809 /***********************************/ 0810 0811 QByteArray ScriptServer::getPlotList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0812 0813 return outputViewItemList<PlotItem>(s); 0814 } 0815 0816 QByteArray ScriptServer::newPlot(QByteArray&, QLocalSocket* s,ObjectStore*) { 0817 0818 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0819 else { _interface = PlotSI::newPlot(); return handleResponse("Ok",s); } 0820 } 0821 0822 0823 #ifndef KST_NO_SVG 0824 QByteArray ScriptServer::getSvgItemList(QByteArray&, QLocalSocket* s,ObjectStore*_store) { 0825 0826 return outputViewItemList<SvgItem>(s); 0827 } 0828 0829 QByteArray ScriptServer::newSvgItem(QByteArray&command, QLocalSocket* s,ObjectStore*) { 0830 0831 command.replace("newSvgItem(",""); 0832 command.remove(command.lastIndexOf(")"),1); 0833 if(_interface) { return handleResponse("To access this function, first call endEdit()",s); } 0834 else { _interface = ViewItemSI::newSvgItem(command); return handleResponse("Ok",s); } 0835 0836 } 0837 #endif 0838 0839 0840 QByteArray ScriptServer::beginEdit(QByteArray&command, QLocalSocket* s,ObjectStore*_store) { 0841 0842 if(_interface) { 0843 return handleResponse("To access this function, first call endEdit()",s); 0844 } 0845 command.replace("beginEdit(",""); 0846 0847 command.remove(command.lastIndexOf(")"),999999); 0848 0849 ViewItem *view_item = ViewItem::retrieveItem<ViewItem>(command); 0850 if (view_item) { 0851 _interface = view_item->scriptInterface(); 0852 return handleResponse("Ok",s); 0853 } else { 0854 ObjectPtr o=_store->retrieveObject(command); 0855 if (o) { 0856 _interface = o->scriptInterface(); 0857 if (_interface) { 0858 return handleResponse("Ok",s); 0859 } else { 0860 return handleResponse("Not supported",s); 0861 } 0862 } 0863 return handleResponse("Unknown error",s); 0864 } 0865 } 0866 0867 QByteArray ScriptServer::eliminate(QByteArray&command, QLocalSocket* s,ObjectStore*_store) { 0868 0869 if(_interface) { 0870 return handleResponse("To access this function, first call endEdit()",s); 0871 } 0872 command.replace("beginEdit(",""); 0873 // check if object 0874 command.remove(command.lastIndexOf(")"),999999); 0875 ObjectPtr o=_store->retrieveObject(command); 0876 if(!o) { 0877 // check if view item 0878 for(int h=0;h<2;h++) { 0879 for(int i=0;i<vi.size();i++) { 0880 if(command.contains(vi[i]->shortName().toLatin1())) { 0881 vi[i]->hide(); // goodbye, memory. 0882 return handleResponse("Done",s); 0883 } 0884 } 0885 vi=ViewItem::getItems<ViewItem>(); 0886 } 0887 0888 return handleResponse("No such object",s); 0889 } else { 0890 if (RelationPtr relation = kst_cast<Relation>(o)) { 0891 Data::self()->removeCurveFromPlots(relation); 0892 } 0893 _store->removeObject(o); 0894 UpdateServer::self()->requestUpdateSignal(); 0895 0896 return handleResponse("Done",s); 0897 } 0898 } 0899 0900 QByteArray ScriptServer::endEdit(QByteArray&, QLocalSocket* s,ObjectStore*) { 0901 0902 if(!_interface) { 0903 return handleResponse("No interface open.",s); 0904 } 0905 0906 if(!_interface->isValid()) { 0907 _interface=0; 0908 return handleResponse("The interface isn't valid.",s); 0909 } 0910 0911 QByteArray x=_interface->endEditUpdate(); 0912 _interface=0; 0913 return handleResponse(x,s); 0914 } 0915 0916 QByteArray ScriptServer::done(QByteArray&, QLocalSocket* s,ObjectStore*) { 0917 0918 if(!s) { 0919 return "Invalid... no socket..."; 0920 } 0921 s->write("Bye."); 0922 s->flush(); 0923 s->close(); 0924 delete s; 0925 return "Bye."; 0926 } 0927 0928 0929 QByteArray ScriptServer::clear(QByteArray&, QLocalSocket* s,ObjectStore*) { 0930 0931 kstApp->mainWindow()->newDoc(true); 0932 return handleResponse("Done",s); 0933 } 0934 0935 0936 QByteArray ScriptServer::tabCount(QByteArray&, QLocalSocket* s,ObjectStore*) { 0937 0938 return handleResponse(QByteArray::number(kstApp->mainWindow()->tabWidget()->count()),s); 0939 } 0940 0941 QByteArray ScriptServer::newTab(QByteArray&, QLocalSocket* s,ObjectStore*) { 0942 0943 kstApp->mainWindow()->tabWidget()->createView(); 0944 kstApp->mainWindow()->tabWidget()->setCurrentIndex(kstApp->mainWindow()->tabWidget()->count()-1); 0945 return handleResponse("Done",s); 0946 } 0947 0948 QByteArray ScriptServer::setTab(QByteArray&command, QLocalSocket* s,ObjectStore*) { 0949 0950 kstApp->mainWindow()->tabWidget()->setCurrentIndex(command.replace("setTab(","").replace(")","").toInt()); 0951 return handleResponse("Done",s); 0952 } 0953 0954 QByteArray ScriptServer::renameTab(QByteArray&command, QLocalSocket* s,ObjectStore*) { 0955 0956 int index = kstApp->mainWindow()->tabWidget()->currentIndex(); 0957 QString new_name = ScriptInterface::getArg(command); 0958 kstApp->mainWindow()->tabWidget()->setTabText(index, new_name); 0959 return handleResponse("Done",s); 0960 } 0961 0962 QByteArray ScriptServer::screenBack(QByteArray&, QLocalSocket* s,ObjectStore*) { 0963 0964 kstApp->mainWindow()->_backAct->trigger(); 0965 return handleResponse("Done",s); 0966 } 0967 0968 QByteArray ScriptServer::screenForward(QByteArray&, QLocalSocket* s,ObjectStore*) { 0969 0970 kstApp->mainWindow()->_forwardAct->trigger(); 0971 return handleResponse("Done",s); 0972 } 0973 0974 QByteArray ScriptServer::countFromEnd(QByteArray&, QLocalSocket* s,ObjectStore*) { 0975 0976 kstApp->mainWindow()->_readFromEndAct->trigger(); 0977 return handleResponse("Done",s); 0978 } 0979 0980 QByteArray ScriptServer::readToEnd(QByteArray&, QLocalSocket* s,ObjectStore*) { 0981 0982 kstApp->mainWindow()->_readToEndAct->trigger(); 0983 return handleResponse("Done",s); 0984 } 0985 0986 QByteArray ScriptServer::setPaused(QByteArray&, QLocalSocket* s,ObjectStore*) { 0987 0988 if(!kstApp->mainWindow()->_pauseAct->isChecked()) { 0989 kstApp->mainWindow()->_pauseAct->trigger(); 0990 } 0991 return handleResponse("Done",s); 0992 } 0993 0994 QByteArray ScriptServer::unsetPaused(QByteArray&, QLocalSocket* s,ObjectStore*) { 0995 0996 if(kstApp->mainWindow()->_pauseAct->isChecked()) { 0997 kstApp->mainWindow()->_pauseAct->trigger(); 0998 } 0999 return handleResponse("Done",s); 1000 1001 } 1002 1003 QByteArray ScriptServer::hide(QByteArray&, QLocalSocket* s,ObjectStore*) { 1004 kstApp->mainWindow()->hide(); 1005 1006 return handleResponse("Done",s); 1007 } 1008 1009 QByteArray ScriptServer::minimize(QByteArray&, QLocalSocket* s,ObjectStore*) { 1010 kstApp->mainWindow()->showMinimized(); 1011 1012 return handleResponse("Done",s); 1013 } 1014 1015 QByteArray ScriptServer::maximize(QByteArray&, QLocalSocket* s,ObjectStore*) { 1016 kstApp->mainWindow()->showMaximized(); 1017 1018 return handleResponse("Done",s); 1019 } 1020 1021 QByteArray ScriptServer::show_window(QByteArray&, QLocalSocket* s,ObjectStore*) { 1022 kstApp->mainWindow()->showNormal(); 1023 1024 return handleResponse("Done",s); 1025 } 1026 1027 1028 QByteArray ScriptServer::quit_kst(QByteArray&, QLocalSocket* s,ObjectStore*) { 1029 kstApp->quit(); 1030 1031 return handleResponse("Done",s); 1032 } 1033 1034 1035 QByteArray ScriptServer::fileOpen(QByteArray&command, QLocalSocket* s, ObjectStore*) { 1036 command.replace("fileOpen(", ""); 1037 command.chop(1); 1038 1039 kstApp->mainWindow()->openFile(command); 1040 1041 return handleResponse("Done",s); 1042 } 1043 1044 QByteArray ScriptServer::fileSave(QByteArray&command, QLocalSocket* s, ObjectStore*) { 1045 command.replace("fileSave(", ""); 1046 command.chop(1); 1047 1048 kstApp->mainWindow()->document()->save(command); 1049 1050 return handleResponse("Done",s); 1051 } 1052 1053 QByteArray ScriptServer::exportGraphics(QByteArray&command, QLocalSocket* s, ObjectStore*) { 1054 QStringList args = ScriptInterface::getArgs(command); 1055 1056 if (args.length() == 7) { 1057 QString filename = args[0]; 1058 QString format = args[1]; 1059 int width = args[2].toInt(); 1060 int height = args[3].toInt(); 1061 int display = args[4].toInt(); 1062 bool allTabs = (args[5].trimmed().toLower() == "true"); 1063 int autosave_period = args[6].toInt(); 1064 1065 kstApp->mainWindow()->exportGraphicsFile(filename, format, width, height, display, allTabs, autosave_period*1000); 1066 } 1067 return handleResponse("Done",s); 1068 } 1069 1070 QByteArray ScriptServer::testCommand(QByteArray&command, QLocalSocket* s,ObjectStore*) { 1071 static int i=0; 1072 1073 qDebug() << "testCommand: " << i++; 1074 return handleResponse("Done",s); 1075 } 1076 1077 1078 QByteArray ScriptServer::cleanupLayout(QByteArray&command, QLocalSocket* s,ObjectStore*) { 1079 1080 QString param = command.replace("cleanupLayout(","").replace(")",""); 1081 bool isNum = true; 1082 int n_cols = param.toInt(&isNum); 1083 1084 if (isNum) { // columns 1085 kstApp->mainWindow()->tabWidget()->currentView()->createLayout(false, n_cols); 1086 } else if (param.toLower() == "protect") { 1087 kstApp->mainWindow()->tabWidget()->currentView()->createLayout(); 1088 } else { 1089 kstApp->mainWindow()->tabWidget()->currentView()->createUnprotectedLayout(); 1090 } 1091 1092 kstApp->mainWindow()->tabWidget()->setCurrentIndex(command.replace("setTab(","").replace(")","").toInt()); 1093 return handleResponse("Done",s); 1094 } 1095 1096 /*** Interface for changing datasource configuration settings ***/ 1097 QByteArray ScriptServer::setDatasourceBoolConfig(QByteArray& command, QLocalSocket* s, ObjectStore*) { 1098 QStringList args = ScriptInterface::getArgs(command); 1099 if (args.length()==4) { 1100 QString ds_type = args[0]; // eg "Ascii File" 1101 QString filename = args[1]; 1102 QString setting = args[2]; 1103 bool value = (args[3].trimmed().toLower() == "true"); 1104 bool set_default = (filename == "$DEFAULT"); 1105 1106 DataSourcePluginManager::settingsObject().beginGroup(ds_type); 1107 if (!set_default) { 1108 DataSourcePluginManager::settingsObject().beginGroup(filename); 1109 } 1110 DataSourcePluginManager::settingsObject().setValue(setting, value); 1111 if (!set_default) { 1112 DataSourcePluginManager::settingsObject().endGroup(); 1113 } 1114 DataSourcePluginManager::settingsObject().endGroup(); 1115 } 1116 return handleResponse("Done",s); 1117 1118 } 1119 1120 QByteArray ScriptServer::setDatasourceIntConfig(QByteArray& command, QLocalSocket* s, ObjectStore*) { 1121 QStringList args = ScriptInterface::getArgs(command); 1122 if (args.length()==4) { 1123 QString ds_type = args[0]; // eg "Ascii File" 1124 QString filename = args[1]; 1125 QString setting = args[2]; 1126 int value = args[3].toInt(); 1127 bool set_default = (filename == "$DEFAULT"); 1128 1129 DataSourcePluginManager::settingsObject().beginGroup(ds_type); 1130 if (!set_default) { 1131 DataSourcePluginManager::settingsObject().beginGroup(filename); 1132 } 1133 DataSourcePluginManager::settingsObject().setValue(setting, value); 1134 if (!set_default) { 1135 DataSourcePluginManager::settingsObject().endGroup(); 1136 } 1137 DataSourcePluginManager::settingsObject().endGroup(); 1138 } 1139 return handleResponse("Done",s); 1140 1141 } 1142 1143 QByteArray ScriptServer::setDatasourceStringConfig(QByteArray& command, QLocalSocket* s, ObjectStore*) { 1144 QStringList args = ScriptInterface::getArgs(command); 1145 if (args.length()==4) { 1146 QString ds_type = args[0]; // eg "Ascii File" 1147 QString filename = args[1]; 1148 QString setting = args[2]; 1149 QString value = args[3].replace('`', ','); // , is replaced with `. switch back. 1150 bool set_default = (filename == "$DEFAULT"); 1151 1152 DataSourcePluginManager::settingsObject().beginGroup(ds_type); 1153 if (!set_default) { 1154 DataSourcePluginManager::settingsObject().beginGroup(filename); 1155 } 1156 DataSourcePluginManager::settingsObject().setValue(setting, value); 1157 if (!set_default) { 1158 DataSourcePluginManager::settingsObject().endGroup(); 1159 } 1160 DataSourcePluginManager::settingsObject().endGroup(); 1161 } 1162 return handleResponse("Done",s); 1163 1164 } 1165 1166 }