Changed my email in the copyright statements.
[tagua/yd.git] / src / connection.cpp
blob0dec27cb4cc4ac54c83a3764c4bf26bb50e90b5b
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "connection.h"
13 #include <iostream>
15 #include <QHostInfo>
16 #include <QTextStream>
17 #include <QTcpSocket>
18 #include <QTextStream>
19 #include <QFile>
21 #include <KProcess>
23 #include "common.h"
24 #include "settings.h"
26 Connection::Connection()
27 : m_device(NULL)
28 , m_connected(false)
29 , m_initialized(false) {
30 QString logFileName;
31 if (!logFileName.isEmpty()) {
32 logFile = new QFile(logFileName);
33 logFile->open(QIODevice::WriteOnly);
34 logStream = 0; //= new QTextStream(logFile);
36 else
37 logStream = 0;
40 Connection::~Connection() {
41 if (logStream) {
42 delete logStream;
43 logFile->close();
44 delete logFile;
46 if (m_device) {
47 KProcess* p = qobject_cast<KProcess*>(m_device);
48 if (p)
49 p->kill();
50 delete m_device;
54 void Connection::establish(const char* host, quint16 port,
55 const QString& helper, const QString& helper_cmd) {
56 if (m_device) {
57 KProcess* p = qobject_cast<KProcess*>(m_device);
58 if (p)
59 p->kill();
60 delete m_device;
63 strncpy(m_host, host, 256);
64 m_port = port;
65 m_helper = helper;
66 m_helper_cmd = helper_cmd;
68 if (helper.isEmpty()) {
69 QTcpSocket *s = new QTcpSocket(this);
70 s->setObjectName("IcsSocket");
72 connect(s, SIGNAL(hostFound()), this, SIGNAL(hostFound()));
73 connect(s, SIGNAL(connected()), this, SIGNAL(established()));
75 hostLookup();
76 s->connectToHost(m_host, m_port);
78 m_device = s;
79 m_connected = true;
81 else {
82 m_device = new KProcess(this);
83 m_device->setObjectName("IcsHelper");
85 if (m_helper_cmd.contains("$(HOST_IP)")) {
86 QHostInfo::lookupHost(m_host, this, SLOT(lookedUp(const QHostInfo)));
88 else {
89 lookedUp(QHostInfo());
93 connect(m_device, SIGNAL(readyRead()), this, SLOT(processLine()));
96 void Connection::lookedUp(const QHostInfo &hi) {
97 KProcess* p = m_device ? qobject_cast<KProcess*>(m_device) : NULL;
98 if (p) {
99 QString args = m_helper_cmd;
100 args.replace("$(HOST_NAME)", QString(m_host) );
101 args.replace("$(HOST_IP)", hi.addresses().isEmpty() ? QString() :
102 hi.addresses().first().toString () );
103 args.replace("$(PORT)", QString::number(m_port));
104 p->setOutputChannelMode(KProcess::OnlyStdoutChannel);
105 p->setProgram(m_helper, args.split(' '));
106 p->start();
108 m_connected = true;
109 if(!m_unsent_text.isEmpty()) {
110 QTextStream os(m_device);
111 os << m_unsent_text;
113 if (logStream)
114 (*logStream) << "> " << m_unsent_text;
115 m_unsent_text = QString();
120 void Connection::processLine() {
121 if(!m_device) {
122 ERROR("No m_device");
123 return;
126 while (m_device->canReadLine()) {
127 QString line = m_device->readLine();
128 line = buffer + line.replace("\r", "");
129 receivedLine(line, buffer.length());
130 if (logStream)
131 (*logStream) << "< " << line << "\n";
132 buffer = "";
135 int size;
136 if ((size = m_device->bytesAvailable()) > 0) {
137 char* data = new char[size + 1];
138 m_device->read(data, size);
139 data[size] = '\0';
140 int offset = buffer.length();
141 buffer += QString(data).replace("\r", "");
142 delete[] data;
144 receivedText(buffer, offset);
148 void Connection::sendText(const QString& text) {
149 if(!m_connected) {
150 m_unsent_text += text + "\n";
151 return;
154 if(!m_device) {
155 ERROR("No m_device");
156 return;
159 processLine();
160 QTextStream os(m_device);
161 os << text << "\n";
163 if (logStream)
164 (*logStream) << "> " << text << "\n";