summaryrefslogtreecommitdiff
path: root/src/fqterm/fqterm_python.cpp
blob: fcb071f464e95a4a57a0b2cfc9d3e5d61a066cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/***************************************************************************
 *   fqterm, a terminal emulator for both BBS and *nix.                    *
 *   Copyright (C) 2008 fqterm development group.                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.               *
 ***************************************************************************/



#include <QEvent>
#include <QTextStream>
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QByteArray>
#include <QMutex>
#include <QThread>
#include "fqterm.h"
#ifdef HAVE_PYTHON
#include <Python.h>
#endif
class SleeperThread : public QThread
{
public:
  static void msleep(unsigned long msecs)
  {
    QThread::msleep(msecs);
  }
};

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_PYTHON

#include "fqterm_window.h"
#include "fqterm_buffer.h"
#include "fqterm_text_line.h"
#include "fqterm_param.h"
#include "fqterm_session.h"
#include "fqterm_python.h"
#include "fqterm_path.h"
#include "common.h"
namespace FQTerm {

/* **************************************************************************
 *
 *				Pythons Embedding
 *
 * ***************************************************************************/

QString getException() {
  PyObject *pType = NULL,  *pValue = NULL,  *pTb = NULL,  *pName,  *pTraceback;

  PyErr_Fetch(&pType, &pValue, &pTb);

  pName = PyString_FromString("traceback");
  pTraceback = PyImport_Import(pName);
  Py_DECREF(pName);

  if (pTraceback == NULL) {
    return "General Error in Python Callback";
  }

  pName = PyString_FromString("format_exception");
  PyObject *pRes = PyObject_CallMethodObjArgs(pTraceback, pName, pType, pValue,
                                              pTb, NULL);
  Py_DECREF(pName);

  Py_DECREF(pTraceback);

  Py_XDECREF(pType);
  Py_XDECREF(pValue);
  Py_XDECREF(pTb);

  if (pRes == NULL) {
    return "General Error in Python Callback";
  }

  pName = PyString_FromString("string");
  PyObject *pString = PyImport_Import(pName);
  Py_DECREF(pName);

  if (pString == NULL) {
    return "General Error in Python Callback";
  }

  pName = PyString_FromString("join");
  PyObject *pErr = PyObject_CallMethodObjArgs(pString, pName, pRes, NULL);
  Py_DECREF(pName);

  Py_DECREF(pString);
  Py_DECREF(pRes);

  if (pErr == NULL) {
    return "General Error in Python Callback";
  }

  QString str(PyString_AsString(pErr));
  Py_DECREF(pErr);

  return str;
}

QString getErrOutputFile(FQTermWindow *lp) {
  // file name
  QString str2;
  str2.setNum(long(lp));
  str2 += ".err";
  // path
  return getPath(USER_CONFIG) + str2;
}

// copy current article, for backward-compatible use only.
// for new code, please use getArticle
static PyObject *fqterm_copyArticle(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  FQTermWindow *termWindow_ = (FQTermWindow*)lp;

  QStringList strList;
  QString strArticle;
  QReadWriteLock& bufferLock = termWindow_->getSession()->getBufferLock();
  QReadLocker locker(&bufferLock);
  while (1) {
    // check if there is duplicated string
    // it starts from the end in the range of one screen height
    // so this is a non-greedy match
    QString strTemp;
    termWindow_->getSession()->getBuffer()->getTextLineInTerm(0)->getAllPlainText(strTemp);
    strTemp = strTemp.trimmed();
    int i = 0;
    int start = 0;
    for (QStringList::Iterator it = strList.end();
         it != strList.begin() &&  i < termWindow_->getSession()->getBuffer()->getNumRows() - 1;  // not exceeeding the last screen
		 --it, i++) {
      if (*it != strTemp) {
        continue;
      }
      QStringList::Iterator it2 = it;
      bool dup = true;
      // match more to see if it's duplicated
      for (int j = 0; j <= i; j++, it2++) {
        QString str1;
        termWindow_->getSession()->getBuffer()->getTextLineInTerm(j)->getAllPlainText(str1);
        if (*it2 != str1.trimmed()) {
          dup = false;
          break;
        }
      }
      if (dup) {
        // set the start point
        start = i + 1;
        break;
      }
    }
    // add new lines
    for (i = start; i < termWindow_->getSession()->getBuffer()->getNumRows() - 1; i++) {
      QString tmp;
      termWindow_->getSession()->getBuffer()->getTextLineInTerm(i)->getAllPlainText(tmp);
      strList += tmp.trimmed();
    }

    // the end of article
    QString testEnd;
    termWindow_->getSession()->getBuffer()->getTextLineInTerm(termWindow_->getSession()->getBuffer()->getNumRows() - 1)->getAllPlainText(testEnd);
    if (testEnd.indexOf("%") == -1) {
      break;
    }
    // continue
    termWindow_->writeString_ts(" ");

    // TODO: fixme
    if (!termWindow_->getSession()->getWaitCondition().wait(&bufferLock, 10000)) {
    //  // timeout
      break;
    }
  }
#if defined(_OS_WIN32_) || defined(Q_OS_WIN32)
  strArticle = strList.join("\r\n");
#else
  strArticle = strList.join("\n");
#endif

  PyObject *py_text = PyString_FromString(strArticle.toUtf8());

  Py_INCREF(py_text);
  return py_text;
}

static PyObject *fqterm_getArticle(PyObject *, PyObject *args) {
  long lp;
  int timeout;
  int succeed = 1;

  if (!PyArg_ParseTuple(args, "li", &lp, &timeout)) {
    return NULL;
  }
  FQTermWindow *termWindow_ = (FQTermWindow*)lp;

  QStringList strList;
  QString strArticle;
  QReadWriteLock& bufferLock = termWindow_->getSession()->getBufferLock();
  while (!bufferLock.tryLockForRead()) {}
  while (1) {
    // check it there is duplicated string
    // it starts from the end in the range of one screen height
    // so this is a non-greedy match
    QString strTemp;
    termWindow_->getSession()->getBuffer()->getTextLineInTerm(0)->getAllPlainText(strTemp);
    strTemp = strTemp.trimmed();

    int i = 0;
    int start = 0;
    for (QStringList::Iterator it = strList.end();
         it != strList.begin() && i < termWindow_->getSession()->getBuffer()->getNumRows() - 1;  // not exceeeding the last screen
         --it, i++) {
      if (it == strList.end() || *it != strTemp) {
        continue;
      }
      QStringList::Iterator it2 = it;
      bool dup = true;
      // match more to see if it's duplicated
      for (int j = 0; j <= i && it2 != strList.end(); j++, it2++) {
        QString str1;
        termWindow_->getSession()->getBuffer()->getTextLineInTerm(j)->getAllPlainText(str1);
        if (*it2 != str1.trimmed()) {
          dup = false;
          break;
        }
      }
      if (dup) {
        // set the start point
        start = i + 1;
        break;
      }
    }
    // add new lines
    for (i = start; i < termWindow_->getSession()->getBuffer()->getNumRows() - 1; i++) {
      QString tmp;
      termWindow_->getSession()->getBuffer()->getTextLineInTerm(i)->getAllPlainText(tmp);
      strList += tmp.trimmed();
    }

    // the end of article
    QString testEnd;
    termWindow_->getSession()->getBuffer()->getTextLineInTerm(termWindow_->getSession()->getBuffer()->getNumRows() - 1)->getAllPlainText(testEnd);
    if (testEnd.indexOf("%") == -1) {
      break;
    }
    // continue
    termWindow_->writeString_ts(" ");

    // TODO: fixme
    if (!termWindow_->getSession()->getWaitCondition().wait(&bufferLock, timeout)) { // timeout
    	succeed = 0;
    	break;
    }
  }
  bufferLock.unlock();
  if (succeed)
    strArticle = strList.join(OS_NEW_LINE);

  PyObject *py_res = Py_BuildValue("si", (const char*)strArticle.toUtf8().data(),
                                   succeed);

  Py_INCREF(py_res);

  return py_res;

}

static PyObject *fqterm_formatError(PyObject *, PyObject *args) {
  long lp;

  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  QString strErr;
  QString filename = getErrOutputFile((FQTermWindow*)lp);

  QDir d;
  if (d.exists(filename)) {
    QFile file(filename);
    file.open(QIODevice::ReadOnly);
    QTextStream is(&file);
    while (!is.atEnd()) {
      strErr += is.readLine(); // line of text excluding '\n'
      strErr += '\n';
    }
    file.close();
    d.remove(filename);
  }

  if (!strErr.isEmpty()) {
    ((FQTermWindow*)lp)->getPythonErrorMessage() = strErr;
    // TODO: fixme
    //qApp->postEvent((FQTermWindow*)lp, new QCustomEvent(PYE_ERROR));
  } else {
    // TODO: fixme
    //qApp->postEvent((FQTermWindow*)lp, new QCustomEvent(PYE_FINISH));
  }


  Py_INCREF(Py_None);
  return Py_None;
}

// caret x
static PyObject *fqterm_caretX(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }
  int x = ((FQTermWindow*)lp)->getSession()->getBuffer()->getCaretColumn();
  PyObject *py_x = Py_BuildValue("i", x);
  Py_INCREF(py_x);
  return py_x;
}

// caret y
static PyObject *fqterm_caretY(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }
  int y = ((FQTermWindow*)lp)->getSession()->getBuffer()->getCaretRow();
  PyObject *py_y = Py_BuildValue("i", y);
  Py_INCREF(py_y);
  return py_y;

}

// columns
static PyObject *fqterm_columns(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  int numColumns = ((FQTermWindow*)lp)->getSession()->getBuffer()->getNumColumns();
  PyObject *py_columns = Py_BuildValue("i", numColumns);

  Py_INCREF(py_columns);
  return py_columns;

}

// rows
static PyObject *fqterm_rows(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  int rows = ((FQTermWindow*)lp)->getSession()->getBuffer()->getNumRows();
  PyObject *py_rows = Py_BuildValue("i", rows);

  Py_INCREF(py_rows);
  return py_rows;
}

// sned string to server
static PyObject *fqterm_sendString(PyObject *, PyObject *args) {
  char *pstr;
  long lp;

  if (!PyArg_ParseTuple(args, "ls", &lp, &pstr)) {
    return NULL;
  }

  ((FQTermWindow*)lp)->writeRawString_ts(U82U(pstr));

  Py_INCREF(Py_None);
  return Py_None;
}

// input should be utf8.
// same as above except parsing string first "\n" "^p" etc
static PyObject *fqterm_sendParsedString(PyObject *, PyObject *args) {
  char *pstr;
  long lp;
  int len;

  if (!PyArg_ParseTuple(args, "ls", &lp, &pstr)) {
    return NULL;
  }
  len = strlen(pstr);

  ((FQTermWindow*)lp)->writeString_ts(U82U(pstr));

  Py_INCREF(Py_None);
  return Py_None;
}

// get text at line
static PyObject *fqterm_getText(PyObject *, PyObject *args) {
  long lp;
  int numRows;
  if (!PyArg_ParseTuple(args, "li", &lp, &numRows)) {
    return NULL;
  }
  QString str;
  if (numRows < ((FQTermWindow*)lp)->getSession()->getBuffer()->getNumRows())
    ((FQTermWindow*)lp)->getSession()->getBuffer()->getTextLineInTerm(numRows)->getAllPlainText(str);

  PyObject *py_text = PyString_FromString(U2U8(str));

  Py_INCREF(py_text);
  return py_text;
}

// get text with attributes
static PyObject *fqterm_getAttrText(PyObject *, PyObject *args) {
  long lp;
  int numRows;
  if (!PyArg_ParseTuple(args, "li", &lp, &numRows)) {
    return NULL;
  }
  
  QString str;
  if (numRows < ((FQTermWindow*)lp)->getSession()->getBuffer()->getNumRows())
    ((FQTermWindow*)lp)->getSession()->getBuffer()->getTextLineInTerm(numRows)->getAllAnsiText(str);

  PyObject *py_text = PyString_FromString(U2U8(str));

  Py_INCREF(py_text);
  return py_text;
}

// is host connected
static PyObject *fqterm_isConnected(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  bool connected = ((FQTermWindow*)lp)->isConnected();
  PyObject *py_connected = Py_BuildValue("i", connected ? 1 : 0);

  Py_INCREF(py_connected);
  return py_connected;
}

// disconnect from host
static PyObject *fqterm_disconnect(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  ((FQTermWindow*)lp)->disconnect();

  Py_INCREF(Py_None);
  return Py_None;
}

// reconnect to host
static PyObject *fqterm_reconnect(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  ((FQTermWindow*)lp)->getSession()->reconnect();

  Py_INCREF(Py_None);
  return Py_None;
}

// bbs encoding 0-GBK 1-BIG5
//FIXME: UTF8 and HKSCS
static PyObject *fqterm_getBBSCodec(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  PyObject *py_codec = PyString_FromString(((FQTermWindow*)lp)
                                           ->getSession()->param().serverEncodingID_ == 0 ? "GBK" : "Big5");
  Py_INCREF(py_codec);

  return py_codec;
}

// host address
static PyObject *fqterm_getAddress(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  PyObject *py_addr = PyString_FromString(((FQTermWindow*)lp)
                                          ->getSession()->param().hostAddress_.toLocal8Bit());
  Py_INCREF(py_addr);
  return py_addr;
}

// host port number
static PyObject *fqterm_getPort(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  PyObject *py_port = Py_BuildValue("i", ((FQTermWindow*)lp)->getSession()->param().port_);
  Py_INCREF(py_port);
  return py_port;
}

// connection protocol 0-telnet 1-SSH1 2-SSH2
static PyObject *fqterm_getProtocol(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  PyObject *py_port = Py_BuildValue("i", ((FQTermWindow*)lp)
                                    ->getSession()->param().protocolType_);
  Py_INCREF(py_port);
  return py_port;
}

// key to reply msg
static PyObject *fqterm_getReplyKey(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  PyObject *py_key = PyString_FromString(((FQTermWindow*)lp)
                                         ->getSession()->param().replyKeyCombination_.toLocal8Bit());
  Py_INCREF(py_key);
  return py_key;
}

// url under mouse
static PyObject *fqterm_getURL(PyObject *, PyObject *args) {
  long lp;
  if (!PyArg_ParseTuple(args, "l", &lp)) {
    return NULL;
  }

  PyObject *py_url = PyString_FromString(((FQTermWindow*)lp)->getSession()->getUrl().toUtf8().constData());
  Py_INCREF(py_url);
  return py_url;
}

// preview image link
static PyObject *fqterm_previewImage(PyObject *, PyObject *args) {
  long lp;
  char *url;
  if (!PyArg_ParseTuple(args, "ls", &lp, &url)) {
    return NULL;
  }

  ((FQTermWindow*)lp)->getHttpHelper(url, true);

  Py_INCREF(Py_None);
  return Py_None;

}

// convert string from UTF8 to specified encoding
static PyObject *fqterm_fromUTF8(PyObject *, PyObject *args) {
  char *str,  *enc;
  if (!PyArg_ParseTuple(args, "ss", &str, &enc)) {
    return NULL;
  }
  QTextCodec *encodec = QTextCodec::codecForName(enc);
  QTextCodec *utf8 = QTextCodec::codecForName("utf8");

  PyObject *py_str = PyString_FromString(encodec->fromUnicode(utf8->toUnicode
                                                              (str)));
  Py_INCREF(py_str);
  return py_str;
}

// convert string from specified encoding to UTF8
static PyObject *fqterm_toUTF8(PyObject *, PyObject *args) {
  char *str,  *enc;
  if (!PyArg_ParseTuple(args, "ss", &str, &enc)) {
    return NULL;
  }
  QTextCodec *encodec = QTextCodec::codecForName(enc);
  QTextCodec *utf8 = QTextCodec::codecForName("utf8");

  PyObject *py_str = PyString_FromString(utf8->fromUnicode(encodec->toUnicode
                                                           (str)));
  Py_INCREF(py_str);
  return py_str;
}

static PyObject *fqterm_wait(PyObject *, PyObject *args) {
  long t;
  if (!PyArg_ParseTuple(args, "l", &t)) {
    return NULL;
  }
  SleeperThread::msleep(t);

  Py_INCREF(Py_None);
  return Py_None;
}

PyMethodDef fqterm_methods[] =  {
  {
    "formatError", (PyCFunction)fqterm_formatError, METH_VARARGS,
	"get the traceback info"
  }

  ,

  {
    "getArticle", (PyCFunction)fqterm_getArticle, METH_VARARGS,
	"copy current article"
  }

  ,

  {
    "copyArticle", (PyCFunction)fqterm_copyArticle, METH_VARARGS,
	"copy current article (obsolete)"
  }

  ,

  {
    "getText", (PyCFunction)fqterm_getText, METH_VARARGS, "get text at line#"
  }

  ,

  {
    "getAttrText", (PyCFunction)fqterm_getAttrText, METH_VARARGS,
	"get attr text at line#"
  }

  ,

  {
    "sendString", (PyCFunction)fqterm_sendString, METH_VARARGS,
	"send string to server"
  }

  ,

  {
    "sendParsedString", (PyCFunction)fqterm_sendParsedString, METH_VARARGS,
	"send string with escape"
  }

  ,

  {
    "caretX", (PyCFunction)fqterm_caretX, METH_VARARGS, "caret x"
  }

  ,

  {
    "caretY", (PyCFunction)fqterm_caretY, METH_VARARGS, "caret y"
  }

  ,

  {
    "columns", (PyCFunction)fqterm_columns, METH_VARARGS, "screen width"
  }

  ,

  {
    "rows", (PyCFunction)fqterm_rows, METH_VARARGS, "screen height"
  }

  ,

  {
    "isConnected", (PyCFunction)fqterm_isConnected, METH_VARARGS,
	"connected to server or not"
  }

  ,

  {
    "disconnect", (PyCFunction)fqterm_disconnect, METH_VARARGS,
	"disconnect from server"
  }

  ,

  {
    "reconnect", (PyCFunction)fqterm_reconnect, METH_VARARGS, "reconnect"
  }

  ,

  {
    "getBBSCodec", (PyCFunction)fqterm_getBBSCodec, METH_VARARGS,
	"get the bbs encoding, GBK or Big5"
  }

  ,

  {
    "getAddress", (PyCFunction)fqterm_getAddress, METH_VARARGS,
	"get the bbs address"
  }

  ,

  {
    "getPort", (PyCFunction)fqterm_getPort, METH_VARARGS, "get the bbs port number"
  }

  ,

  {
    "getProtocol", (PyCFunction)fqterm_getProtocol, METH_VARARGS,
	"get the bbs protocol, 0/1/2 TELNET/SSH1/SSH2"
  }

  ,

  {
    "getReplyKey", (PyCFunction)fqterm_getReplyKey, METH_VARARGS,
	"get the key to reply messages"
  }

  ,

  {
    "getURL", (PyCFunction)fqterm_getURL, METH_VARARGS,
	"get the url string under mouse"
  }

  ,

  {
    "previewImage", (PyCFunction)fqterm_previewImage, METH_VARARGS,
	"preview the image link"
  }

  ,

  {
    "fromUTF8", (PyCFunction)fqterm_fromUTF8, METH_VARARGS,
	"decode from utf8 to string in specified codec"
  }

  ,

  {
    "toUTF8", (PyCFunction)fqterm_toUTF8, METH_VARARGS,
	"decode from string in specified codec to utf8"
  }

  ,

  {
    "wait", (PyCFunction)fqterm_wait, METH_VARARGS,
      "wait for x ms"
  }

  ,

  {
    NULL, (PyCFunction)NULL, 0, NULL
  }
};



FQTermPythonHelper::FQTermPythonHelper() 
	: mainThreadState_(NULL) {

    // initialize Python
    Py_Initialize();
	// initialize thread support
	PyEval_InitThreads();
	// save a pointer to the main PyThreadState object
	mainThreadState_ = PyThreadState_Get();
    // add path
    PyRun_SimpleString("import sys\n");
    QString pathCmd;
    pathCmd = "sys.path.insert(0,'";
    pathCmd += getPath(RESOURCE)+"script')";
    PyRun_SimpleString(fq_strdup(pathCmd.toUtf8().data()));
    
    Py_InitModule4("fqterm", fqterm_methods,
			NULL,(PyObject*)NULL,PYTHON_API_VERSION);
    // release the lock
    PyEval_ReleaseLock();
}

FQTermPythonHelper::~FQTermPythonHelper() {
    // shut down the interpreter
    PyInterpreterState * mainInterpreterState = mainThreadState_->interp;
    // create a thread state object for this thread
    PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState);
    PyThreadState_Swap(myThreadState);
    PyEval_AcquireLock();
    Py_Finalize();
}
  
}  // namespace FQTerm

#endif //HAVE_PYTHON