summaryrefslogtreecommitdiff
path: root/src/unite/internal/session.cpp
blob: 6964287796b9715fe0cefd4bd2548a9fbe195eda (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
/***************************************************************************
*   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 "state.h"
#include "fqterm.h"
#include <QApplication>
#include <algorithm>
#include "statebuilder.h"
namespace FQTerm{

void FQTermUniteSessionContext::write(const QByteArray& str) {
  session_->write(str);
}

FQTermUniteSessionContext::FQTermUniteSessionContext(FQTermUniteSession* session) : QObject(session), session_(session) {
  stateTable_[WELCOME] = new FQTermWelcomeState(this);
  stateTable_[EXITING] = new FQTermExitState(this);
  stateTable_[READING] = new FQTermReadingState(this);
  stateTable_[HELP] = new FQTermHelpState(this);
  setCurrentState(WELCOME);
}

void FQTermUniteSessionContext::processInput(const QByteArray& input) {
  inputBuffer_ += input;
  while(true) {
    int consumed = currentState_->processInput(inputBuffer_);
    inputBuffer_ = inputBuffer_.mid(consumed);
    if (!consumed || inputBuffer_.size() == 0) {
      break;
    }
  }
}

void FQTermUniteSessionContext::quit() {
  session_->quit();
}



FQTermUniteState* FQTermUniteSessionContext::setCurrentState(int state, bool init /*= true*/) {
  StateTableIterator it = stateTable_.find(state);
  if (it != stateTable_.end()) {
    currentState_ = it->second;
    if (init)
      currentState_->initialize();
  }
  return currentState_;
}



FQTermUniteSession::FQTermUniteSession(int socketDescriptor ) : context_(NULL) {
  moveToThread(this);
  socket_ = new QTcpSocket;
  FQ_VERIFY(connect(socket_, SIGNAL(readyRead()),
    this, SLOT(readyRead()),Qt::DirectConnection));
  FQ_VERIFY(connect(socket_, SIGNAL(disconnected()),
    this, SLOT(disconnected()),Qt::DirectConnection));
  FQ_VERIFY(connect(socket_, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
    this, SLOT(stateChanged(QAbstractSocket::SocketState)),Qt::DirectConnection));
  FQ_VERIFY(connect(socket_, SIGNAL(error(QAbstractSocket::SocketError)),
    this, SLOT(error(QAbstractSocket::SocketError)),Qt::DirectConnection));

  //  FQ_VERIFY(connect(socket_, SIGNAL(connected()),
  //    this, SLOT(welcome()),Qt::DirectConnection));

  socket_->setSocketDescriptor(socketDescriptor);
  socket_->moveToThread(this);
  setTerminationEnabled();

  StateBuilder sb;
  sb.BuildState();
}


FQTermUniteSession::~FQTermUniteSession() {
  socket_->close();
  delete socket_;
  socket_ = NULL;
}

void FQTermUniteSession::welcome()
{
  context_ = new FQTermUniteSessionContext(this);
}

void FQTermUniteSession::readyRead() {
  if (context_) {
    context_->processInput(socket_->readAll());
  }
}

void FQTermUniteSession::write( const QByteArray& output ) {
  socket_->write(output);
}

void FQTermUniteSession::run() {
  welcome();
  exec();
  disconnected();
}


void FQTermUniteSession::disconnected() {
  if (!isRunning())
    return;
  quit();
  moveToThread(QApplication::instance()->thread());
  deleteLater();  
}

void FQTermUniteSession::stateChanged( QAbstractSocket::SocketState socketState ) {
}

void FQTermUniteSession::error(QAbstractSocket::SocketError socketError) {
}


}

#include "session.moc"