summaryrefslogtreecommitdiff
path: root/src/protocol/fqterm_local_socket.cpp
blob: dbfb5b11f6eae5dc1444b6244c1354724f44f4e3 (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
#include "fqterm.h"
#include "fqterm_local_socket.h"

namespace FQTerm {

FQTermLocalSocket::FQTermLocalSocket()
{
  shell_process_ = new QProcess();
  shell_process_->setProcessChannelMode(QProcess::MergedChannels);
  FQ_VERIFY(connect(shell_process_, SIGNAL(started()), this, SIGNAL(connected())));
  FQ_VERIFY(connect(shell_process_, SIGNAL(stateChanged(QProcess::ProcessState)), this , SLOT(stateChanged(QProcess::ProcessState))));
  FQ_VERIFY(connect(shell_process_, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus))));
  //FQ_VERIFY(connect(shell_process_, SIGNAL(delayedCloseFinished()), this, SIGNAL(delayedCloseFinished())));
  FQ_VERIFY(connect(shell_process_, SIGNAL(readyRead()), this, SIGNAL(readyRead())));
  //TODO: Error
  //FQ_VERIFY(connect(shell_process_, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError))));
  //FQ_VERIFY(connect(shell_process_, SIGNAL(socketState(int)), this, SIGNAL(socketState(int))));
}

FQTermLocalSocket::~FQTermLocalSocket()
{
  delete shell_process_;
}

void FQTermLocalSocket::connectToHost( const QString &host, quint16 port )
{
  if (shell_bin_!=NULL) {
    shell_process_->start(FQTermLocalSocket::shell_bin_->arg(QString::number(port), host),
                          QIODevice::ReadWrite | QIODevice::Unbuffered);
  } else {
    emit connectionClosed();
  }
}

void FQTermLocalSocket::close()
{
  shell_process_->close();
}

QByteArray FQTermLocalSocket::readBlock( unsigned long maxlen )
{
  return shell_process_->read(maxlen);
}

long FQTermLocalSocket::writeBlock( const QByteArray &data )
{
  int count = shell_process_->write(data);

  //char c;
//  shell_process_->getChar(&c);
//  shell_process_->ungetChar(c);
  if (bytesAvailable())
  {
    emit readyRead();
  }
  return count;
}

unsigned long FQTermLocalSocket::bytesAvailable()
{
  return shell_process_->bytesAvailable();
}

void FQTermLocalSocket::finished( int exitCode, QProcess::ExitStatus exitStatus )
{
  emit connectionClosed();
}
void FQTermLocalSocket::stateChanged(QProcess::ProcessState newState)
{
  switch(newState)
  {
  case QProcess::NotRunning:
    break;
  case QProcess::Starting:
    emit hostFound();
    //    shell_process_->write("dir\n");
    break;
  case QProcess::Running:
    break;

  }
}

}

#include "fqterm_local_socket.moc"