Add a PromotionManager.
[tagua/yd.git] / src / connection.cpp
blob54313bdbd591999e5d0beb55fffaa6b8cf65ba0d
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"
12 #include <QHostInfo>
13 #include <QTextStream>
14 #include <QTcpSocket>
15 #include <QTextStream>
16 #include <QFile>
17 #include <KDebug>
18 #include <KProcess>
19 #include "common.h"
20 #include "settings.h"
22 Connection::Connection()
23 : m_device(NULL)
24 , m_connected(false)
25 , m_initialized(false) {
26 QString logFileName;
27 if (!logFileName.isEmpty()) {
28 logFile = new QFile(logFileName);
29 logFile->open(QIODevice::WriteOnly);
30 logStream = 0; //= new QTextStream(logFile);
32 else
33 logStream = 0;
36 Connection::~Connection() {
37 if (logStream) {
38 delete logStream;
39 logFile->close();
40 delete logFile;
42 if (m_device) {
43 KProcess* p = qobject_cast<KProcess*>(m_device);
44 if (p)
45 p->kill();
46 delete m_device;
50 void Connection::establish(const char* host, quint16 port,
51 const QString& helper, const QString& helper_cmd) {
52 if (m_device) {
53 KProcess* p = qobject_cast<KProcess*>(m_device);
54 if (p)
55 p->kill();
56 delete m_device;
59 strncpy(m_host, host, 256);
60 m_port = port;
61 m_helper = helper;
62 m_helper_cmd = helper_cmd;
64 if (helper.isEmpty()) {
65 QTcpSocket *s = new QTcpSocket(this);
66 s->setObjectName("IcsSocket");
68 connect(s, SIGNAL(hostFound()), this, SIGNAL(hostFound()));
69 connect(s, SIGNAL(connected()), this, SIGNAL(established()));
71 hostLookup();
72 s->connectToHost(m_host, m_port);
74 m_device = s;
75 m_connected = true;
77 else {
78 m_device = new KProcess(this);
79 m_device->setObjectName("IcsHelper");
81 if (m_helper_cmd.contains("$(HOST_IP)")) {
82 QHostInfo::lookupHost(m_host, this, SLOT(lookedUp(const QHostInfo)));
84 else {
85 lookedUp(QHostInfo());
89 connect(m_device, SIGNAL(readyRead()), this, SLOT(processLine()));
92 void Connection::lookedUp(const QHostInfo &hi) {
93 KProcess* p = m_device ? qobject_cast<KProcess*>(m_device) : NULL;
94 if (p) {
95 QString args = m_helper_cmd;
96 args.replace("$(HOST_NAME)", QString(m_host) );
97 args.replace("$(HOST_IP)", hi.addresses().isEmpty() ? QString() :
98 hi.addresses().first().toString () );
99 args.replace("$(PORT)", QString::number(m_port));
100 p->setOutputChannelMode(KProcess::OnlyStdoutChannel);
101 p->setProgram(m_helper, args.split(' '));
102 p->start();
104 m_connected = true;
105 if(!m_unsent_text.isEmpty()) {
106 QTextStream os(m_device);
107 os << m_unsent_text;
109 if (logStream)
110 (*logStream) << "> " << m_unsent_text;
111 m_unsent_text = QString();
116 void Connection::processLine() {
117 if(!m_device) {
118 kError() << "No device";
119 return;
122 while (m_device->canReadLine()) {
123 QString line = m_device->readLine();
124 line = buffer + line.replace("\r", "");
125 receivedLine(line, buffer.length());
126 if (logStream)
127 (*logStream) << "< " << line << "\n";
128 buffer = "";
131 int size;
132 if ((size = m_device->bytesAvailable()) > 0) {
133 char* data = new char[size + 1];
134 m_device->read(data, size);
135 data[size] = '\0';
136 int offset = buffer.length();
137 buffer += QString(data).replace("\r", "");
138 delete[] data;
140 receivedText(buffer, offset);
144 void Connection::sendText(const QString& text) {
145 if(!m_connected) {
146 m_unsent_text += text + "\n";
147 return;
150 if(!m_device) {
151 kDebug() << "No device";
152 return;
155 processLine();
156 QTextStream os(m_device);
157 os << text << "\n";
159 if (logStream)
160 (*logStream) << "> " << text << "\n";