Debian Codegraph @

src/qtermpython.cpp

raw symbols SCIP
1 //#include "qterm.h"
2
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 #ifdef HAVE_PYTHON
8 #include <Python.h>
9
10 // #include <qapplication.h>
11 // #include <qdir.h>
12 // #include <qfile.h>
13
14 #include "qtermwindow.h"
15 #include "qtermbuffer.h"
16 #include "qtermtextline.h"
17 #include "qtermtelnet.h"
18 #include "qtermparam.h"
19 #include "qtermbbs.h"
20 //Added by qt3to4:
21 #include <QCustomEvent>
22 #include <QTextStream>
23 #include <QApplication>
24 #include <QDir>
25 #include <QFile>
26 #include <QByteArray>
27
28 /* **************************************************************************
29 *
30 * Pythons Embedding
31 *
32 * ***************************************************************************/
33 extern QString pathCfg;
34
35 QString getException()
36 {
37 PyObject *pType=NULL, *pValue=NULL, *pTb=NULL, *pName, *pTraceback;
38
39 PyErr_Fetch(&pType, &pValue, &pTb);
40
41 pName = PyString_FromString("traceback");
42 pTraceback = PyImport_Import(pName);
43 Py_DECREF(pName);
44
45 if(pTraceback==NULL)
46 return "General Error in Python Callback";
47
48 pName = PyString_FromString("format_exception");
49 PyObject *pRes = PyObject_CallMethodObjArgs(pTraceback, pName,pType,pValue,pTb,NULL);
50 Py_DECREF(pName);
51
52 Py_DECREF(pTraceback);
53
54 Py_XDECREF(pType);
55 Py_XDECREF(pValue);
56 Py_XDECREF(pTb);
57
58 if(pRes==NULL)
59 return "General Error in Python Callback";
60
61 pName = PyString_FromString("string");
62 PyObject *pString = PyImport_Import(pName);
63 Py_DECREF(pName);
64
65 if(pString==NULL)
66 return "General Error in Python Callback";
67
68 pName = PyString_FromString("join");
69 PyObject *pErr = PyObject_CallMethodObjArgs(pString, pName, pRes,NULL);
70 Py_DECREF(pName);
71
72 Py_DECREF(pString);
73 Py_DECREF(pRes);
74
75 if(pErr==NULL)
76 return "General Error in Python Callback";
77
78 QString str(PyString_AsString(pErr));
79 Py_DECREF(pErr);
80
81 return str;
82 }
83
84 QString getErrOutputFile(Window* lp)
85 {
86 // file name
87 QString str2;
88 str2.setNum(long(lp));
89 str2 += ".err";
90 // path
91 return pathCfg+str2;
92 }
93
94 // copy current artcle for back compatible use only
95 // for new coder please use getArticle
96 static PyObject *qterm_copyArticle(PyObject *, PyObject *args)
97 {
98 long lp;
99 if (!PyArg_ParseTuple(args, "l", &lp))
100 return NULL;
101
102 Window *pWin=(Window*)lp;
103
104 QStringList strList;
105 QString strArticle;
106 while(1)
107 {
108 // check it there is duplicated string
109 // it starts from the end in the range of one screen height
110 // so this is a non-greedy match
111 QString strTemp = pWin->stripWhitespace(
112 pWin->m_pBuffer->screen(0)->getText());
113 int i=0;
114 int start=0;
115 for(QStringList::Iterator it=strList.fromLast();
116 it!=strList.begin(), i < pWin->m_pBuffer->line()-1; // not exceeeding the last screen
117 --it, i++)
118 {
119 if(*it!=strTemp)
120 continue;
121 QStringList::Iterator it2 = it;
122 bool dup=true;
123 // match more to see if its duplicated
124 for(int j=0; j<=i; j++, it2++)
125 {
126 QString str1 = pWin->stripWhitespace(
127 pWin->m_pBuffer->screen(j)->getText());
128 if(*it2!=str1)
129 {
130 dup = false;
131 break;
132 }
133 }
134 if(dup)
135 {
136 // set the start point
137 start = i+1;
138 break;
139 }
140 }
141 // add new lines
142 for(i=start;i<pWin->m_pBuffer->line()-1;i++)
143 strList+=pWin->stripWhitespace(
144 pWin->m_pBuffer->screen(i)->getText());
145
146 // the end of article
147 if( pWin->m_pBuffer->screen(
148 pWin->m_pBuffer->line()-1)->getText().find("%") == -1 )
149 break;
150 // continue
151 pWin->m_pTelnet->write(" ", 1);
152
153 if(!pWin->m_wcWaiting.wait(10000)) // timeout
154 break;
155 }
156 #if defined(_OS_WIN32_) || defined(Q_OS_WIN32)
157 strArticle = strList.join("\r\n");
158 #else
159 strArticle = strList.join("\n");
160 #endif
161
162 PyObject *py_text = PyString_FromString(strArticle.toLocal8Bit);
163
164 Py_INCREF(py_text);
165 return py_text;
166 }
167
168 static PyObject *qterm_getArticle(PyObject *, PyObject *args)
169 {
170 long lp;
171 int timeout;
172 int succeed=1;
173
174 if (!PyArg_ParseTuple(args, "li", &lp, &timeout))
175 return NULL;
176
177 Window *pWin=(Window*)lp;
178
179 QStringList strList;
180 QString strArticle;
181 while(1)
182 {
183 // check it there is duplicated string
184 // it starts from the end in the range of one screen height
185 // so this is a non-greedy match
186 QString strTemp = pWin->stripWhitespace(
187 pWin->m_pBuffer->screen(0)->getText());
188 int i=0;
189 int start=0;
190 for(QStringList::Iterator it=strList.fromLast();
191 it!=strList.begin(), i < pWin->m_pBuffer->line()-1; // not exceeeding the last screen
192 --it, i++)
193 {
194 if(*it!=strTemp)
195 continue;
196 QStringList::Iterator it2 = it;
197 bool dup=true;
198 // match more to see if its duplicated
199 for(int j=0; j<=i; j++, it2++)
200 {
201 QString str1 = pWin->stripWhitespace(
202 pWin->m_pBuffer->screen(j)->getText());
203 if(*it2!=str1)
204 {
205 dup = false;
206 break;
207 }
208 }
209 if(dup)
210 {
211 // set the start point
212 start = i+1;
213 break;
214 }
215 }
216 // add new lines
217 for(i=start;i<pWin->m_pBuffer->line()-1;i++)
218 strList+=pWin->stripWhitespace(
219 pWin->m_pBuffer->screen(i)->getText());
220
221 // the end of article
222 if( pWin->m_pBuffer->screen(
223 pWin->m_pBuffer->line()-1)->getText().find("%") == -1 )
224 break;
225 // continue
226 pWin->m_pTelnet->write(" ", 1);
227
228 if(!pWin->m_wcWaiting.wait(timeout*1000)) // timeout
229 {
230 succeed=0;
231 break;
232 }
233 }
234 #if defined(_OS_WIN32_) || defined(Q_OS_WIN32)
235 strArticle = strList.join("\r\n");
236 #else
237 strArticle = strList.join("\n");
238 #endif
239
240 PyObject *py_res = Py_BuildValue("si",(const char *)strArticle.toLocal8Bit(), succeed);
241
242 Py_INCREF(py_res);
243
244 return py_res;
245
246 }
247
248 static PyObject *qterm_formatError(PyObject *, PyObject *args)
249 {
250 long lp;
251
252 if (!PyArg_ParseTuple(args, "l", &lp))
253 return NULL;
254
255 QString strErr;
256 QString filename = getErrOutputFile((Window*)lp);
257
258 QDir d;
259 if(d.exists(filename))
260 {
261 QFile file(filename);
262 file.open(QIODevice::ReadOnly);
263 QTextStream is( &file );
264 while ( !is.atEnd() )
265 {
266 strErr += is.readLine(); // line of text excluding '\n'
267 strErr += '\n';
268 }
269 file.close();
270 d.remove( filename );
271 }
272
273 if( !strErr.isEmpty() )
274 {
275 ((Window*)lp)->m_strPythonError = strErr;
276 qApp->postEvent( (Window*)lp, new QCustomEvent(PYE_ERROR));
277 }
278 else
279 qApp->postEvent( (Window*)lp, new QCustomEvent(PYE_FINISH));
280
281
282 Py_INCREF(Py_None);
283 return Py_None;
284 }
285
286 // caret x
287 static PyObject *qterm_caretX(PyObject *, PyObject *args)
288 {
289 long lp;
290 if (!PyArg_ParseTuple(args, "l", &lp))
291 return NULL;
292 int x = ((Window*)lp)->m_pBuffer->caret().x();
293 PyObject * py_x =Py_BuildValue("i",x);
294 Py_INCREF(py_x);
295 return py_x;
296 }
297
298 // caret y
299 static PyObject *qterm_caretY(PyObject *, PyObject *args)
300 {
301 long lp;
302 if (!PyArg_ParseTuple(args, "l", &lp))
303 return NULL;
304 int y = ((Window*)lp)->m_pBuffer->caret().y();
305 PyObject * py_y =Py_BuildValue("i",y);
306 Py_INCREF(py_y);
307 return py_y;
308
309 }
310
311 // columns
312 static PyObject *qterm_columns(PyObject *, PyObject *args)
313 {
314 long lp;
315 if (!PyArg_ParseTuple(args, "l", &lp))
316 return NULL;
317
318 int columns = ((Window*)lp)->m_pBuffer->columns();
319 PyObject * py_columns = Py_BuildValue("i",columns);
320
321 Py_INCREF(py_columns);
322 return py_columns;
323
324 }
325
326 // rows
327 static PyObject *qterm_rows(PyObject *, PyObject *args)
328 {
329 long lp;
330 if (!PyArg_ParseTuple(args, "l", &lp))
331 return NULL;
332
333 int rows = ((Window*)lp)->m_pBuffer->line();
334 PyObject *py_rows = Py_BuildValue("i",rows);
335
336 Py_INCREF(py_rows);
337 return py_rows;
338 }
339
340 // sned string to server
341 static PyObject *qterm_sendString(PyObject *, PyObject *args)
342 {
343 char *pstr;
344 long lp;
345 int len;
346
347 if (!PyArg_ParseTuple(args, "ls", &lp, &pstr))
348 return NULL;
349
350 len = strlen(pstr);
351
352 ((Window*)lp)->m_pTelnet->write(pstr,len);
353
354 Py_INCREF(Py_None);
355 return Py_None;
356 }
357
358 // same as above except parsing string first "\n" "^p" etc
359 static PyObject *qterm_sendParsedString(PyObject *, PyObject *args)
360 {
361 char *pstr;
362 long lp;
363 int len;
364
365 if (!PyArg_ParseTuple(args, "ls", &lp, &pstr))
366 return NULL;
367 len = strlen(pstr);
368
369 ((Window*)lp)->sendParsedString(pstr);
370
371 Py_INCREF(Py_None);
372 return Py_None;
373 }
374
375 // get text at line
376 static PyObject *qterm_getText(PyObject *, PyObject *args)
377 {
378 long lp;
379 int line;
380 if (!PyArg_ParseTuple(args, "li", &lp, &line))
381 return NULL;
382 QByteArray cstr = ((Window*)lp)->m_pBuffer->screen(line)->getText();
383
384 PyObject *py_text = PyString_FromString(cstr);
385
386 Py_INCREF(py_text);
387 return py_text;
388 }
389
390 // get text with attributes
391 static PyObject *qterm_getAttrText(PyObject *, PyObject *args)
392 {
393 long lp;
394 int line;
395 if (!PyArg_ParseTuple(args, "li", &lp, &line))
396 return NULL;
397
398 QByteArray cstr = ((Window*)lp)->m_pBuffer->screen(line)->getAttrText();
399
400 PyObject *py_text = PyString_FromString(cstr);
401
402 Py_INCREF(py_text);
403 return py_text;
404 }
405
406 // is host connected
407 static PyObject *qterm_isConnected(PyObject *, PyObject *args)
408 {
409 long lp;
410 if (!PyArg_ParseTuple(args, "l", &lp))
411 return NULL;
412
413 bool connected = ((Window*)lp)->isConnected();
414 PyObject * py_connected =Py_BuildValue("i",connected?1:0);
415
416 Py_INCREF(py_connected);
417 return py_connected;
418 }
419
420 // disconnect from host
421 static PyObject *qterm_disconnect(PyObject *, PyObject *args)
422 {
423 long lp;
424 if (!PyArg_ParseTuple(args, "l", &lp))
425 return NULL;
426
427 ((Window*)lp)->disconnect();
428
429 Py_INCREF(Py_None);
430 return Py_None;
431 }
432
433 // reconnect to host
434 static PyObject *qterm_reconnect(PyObject *, PyObject *args)
435 {
436 long lp;
437 if (!PyArg_ParseTuple(args, "l", &lp))
438 return NULL;
439
440 ((Window*)lp)->reconnect();
441
442 Py_INCREF(Py_None);
443 return Py_None;
444 }
445
446 // bbs encoding 0-GBK 1-BIG5
447 static PyObject *qterm_getBBSCodec(PyObject *, PyObject *args)
448 {
449 long lp;
450 if (!PyArg_ParseTuple(args, "l", &lp))
451 return NULL;
452
453 PyObject *py_codec = PyString_FromString(
454 ((Window*)lp)->m_param.m_nBBSCode==0?"GBK":"Big5");
455 Py_INCREF(py_codec);
456
457 return py_codec;
458 }
459
460 // host address
461 static PyObject *qterm_getAddress(PyObject *, PyObject *args)
462 {
463 long lp;
464 if (!PyArg_ParseTuple(args, "l", &lp))
465 return NULL;
466
467 PyObject *py_addr = PyString_FromString(
468 ((Window*)lp)->m_param.m_strAddr.toLocal8Bit());
469 Py_INCREF(py_addr);
470 return py_addr;
471 }
472
473 // host port number
474 static PyObject *qterm_getPort(PyObject *, PyObject *args)
475 {
476 long lp;
477 if (!PyArg_ParseTuple(args, "l", &lp))
478 return NULL;
479
480 PyObject *py_port = Py_BuildValue("i", ((Window*)lp)->m_param.m_uPort);
481 Py_INCREF(py_port);
482 return py_port;
483 }
484
485 // connection protocol 0-telnet 1-SSH1 2-SSH2
486 static PyObject *qterm_getProtocol(PyObject *, PyObject *args)
487 {
488 long lp;
489 if (!PyArg_ParseTuple(args, "l", &lp))
490 return NULL;
491
492 PyObject *py_port = Py_BuildValue("i", ((Window*)lp)->m_param.m_nProtocolType);
493 Py_INCREF(py_port);
494 return py_port;
495 }
496
497 // key to reply msg
498 static PyObject *qterm_getReplyKey(PyObject *, PyObject *args)
499 {
500 long lp;
501 if(!PyArg_ParseTuple(args, "l", &lp))
502 return NULL;
503
504 PyObject *py_key = PyString_FromString(((Window*)lp)->m_param.m_strReplyKey.toLocal8Bit());
505 Py_INCREF(py_key);
506 return py_key;
507 }
508
509 // url under mouse
510 static PyObject *qterm_getURL(PyObject *, PyObject *args)
511 {
512 long lp;
513 if(!PyArg_ParseTuple(args, "l", &lp))
514 return NULL;
515
516 PyObject *py_url = PyString_FromString( ((Window*)lp)->m_pBBS->getUrl());
517 Py_INCREF(py_url);
518 return py_url;
519 }
520
521 // preview image link
522 static PyObject *qterm_previewImage(PyObject *, PyObject *args)
523 {
524 long lp;
525 char *url;
526 if (!PyArg_ParseTuple(args, "ls", &lp, &url))
527 return NULL;
528
529 ((Window*)lp)->getHttpHelper(url,true);
530
531 Py_INCREF(Py_None);
532 return Py_None;
533
534 }
535
536 // convert string from UTF8 to specified encoding
537 static PyObject *qterm_fromUTF8(PyObject *, PyObject *args)
538 {
539 char *str, *enc;
540 if (!PyArg_ParseTuple(args, "ss", &str, &enc))
541 return NULL;
542 QTextCodec *encodec = QTextCodec::codecForName(enc);
543 QTextCodec *utf8 = QTextCodec::codecForName("utf8");
544
545 PyObject *py_str = PyString_FromString(
546 encodec->fromUnicode(utf8->toUnicode(str)));
547 Py_INCREF(py_str);
548 return py_str;
549 }
550
551 // convert string from specified encoding to UTF8
552 static PyObject *qterm_toUTF8(PyObject *, PyObject *args)
553 {
554 char *str, *enc;
555 if (!PyArg_ParseTuple(args, "ss", &str, &enc))
556 return NULL;
557 QTextCodec *encodec = QTextCodec::codecForName(enc);
558 QTextCodec *utf8 = QTextCodec::codecForName("utf8");
559
560 PyObject *py_str = PyString_FromString(
561 utf8->fromUnicode(encodec->toUnicode(str)));
562 Py_INCREF(py_str);
563 return py_str;
564 }
565
566
567 PyMethodDef qterm_methods[] = {
568 {"formatError", (PyCFunction)qterm_formatError, METH_VARARGS,
569 "get the traceback info"},
570
571 {"getArticle", (PyCFunction)qterm_getArticle, METH_VARARGS,
572 "copy current article"},
573
574 {"copyArticle", (PyCFunction)qterm_copyArticle, METH_VARARGS,
575 "copy current article (obsolete)"},
576
577 {"getText", (PyCFunction)qterm_getText, METH_VARARGS,
578 "get text at line#"},
579
580 {"getAttrText", (PyCFunction)qterm_getAttrText, METH_VARARGS,
581 "get attr text at line#"},
582
583 {"sendString", (PyCFunction)qterm_sendString, METH_VARARGS,
584 "send string to server"},
585
586 {"sendParsedString",(PyCFunction)qterm_sendParsedString, METH_VARARGS,
587 "send string with escape"},
588
589 {"caretX", (PyCFunction)qterm_caretX, METH_VARARGS,
590 "caret x"},
591
592 {"caretY", (PyCFunction)qterm_caretY, METH_VARARGS,
593 "caret y"},
594
595 {"columns", (PyCFunction)qterm_columns, METH_VARARGS,
596 "screen width"},
597
598 {"rows", (PyCFunction)qterm_rows, METH_VARARGS,
599 "screen height"},
600
601 {"isConnected", (PyCFunction)qterm_isConnected, METH_VARARGS,
602 "connected to server or not"},
603
604 {"disconnect", (PyCFunction)qterm_disconnect, METH_VARARGS,
605 "disconnect from server"},
606
607 {"reconnect", (PyCFunction)qterm_reconnect, METH_VARARGS,
608 "reconnect"},
609
610 {"getBBSCodec", (PyCFunction)qterm_getBBSCodec, METH_VARARGS,
611 "get the bbs encoding, GBK or Big5"},
612
613 {"getAddress", (PyCFunction)qterm_getAddress, METH_VARARGS,
614 "get the bbs address"},
615
616 {"getPort", (PyCFunction)qterm_getPort, METH_VARARGS,
617 "get the bbs port number"},
618
619 {"getProtocol", (PyCFunction)qterm_getPort, METH_VARARGS,
620 "get the bbs protocol, 0/1/2 TELNET/SSH1/SSH2"},
621
622 {"getReplyKey", (PyCFunction)qterm_getReplyKey, METH_VARARGS,
623 "get the key to reply messages"},
624
625 {"getURL", (PyCFunction)qterm_getURL, METH_VARARGS,
626 "get the url string under mouse"},
627
628 {"previewImage", (PyCFunction)qterm_previewImage, METH_VARARGS,
629 "preview the image link"},
630
631 {"fromUTF8", (PyCFunction)qterm_fromUTF8, METH_VARARGS,
632 "decode from utf8 to string in specified codec"},
633
634 {"toUTF8", (PyCFunction)qterm_toUTF8, METH_VARARGS,
635 "decode from string in specified codec to utf8"},
636
637 {NULL, (PyCFunction)NULL, 0, NULL}
638 };
639 #endif //HAVE_PYTHON
640