Exported QFont to lua, for border text and clocks
[tagua/yd.git] / src / connection.cpp
blobd40d7d83a427cf93e0c4208d8644741fbb227bc5
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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 <QProcess>
17 #include <QTextStream>
18 #include <QTcpSocket>
19 #include <QTextStream>
20 #include <QFile>
22 #include "common.h"
23 #include "settings.h"
25 Connection::Connection()
26 : m_device(NULL)
27 , m_connected(false)
28 , m_initialized(false) {
29 QString logFileName;
30 if (!logFileName.isEmpty()) {
31 logFile = new QFile(logFileName);
32 logFile->open(QIODevice::WriteOnly);
33 logStream = 0; //= new QTextStream(logFile);
35 else
36 logStream = 0;
39 Connection::~Connection() {
40 if (logStream) {
41 delete logStream;
42 logFile->close();
43 delete logFile;
45 if(m_device) {
46 QProcess *p = qobject_cast<QProcess*>(m_device);
47 if(p)
48 p->kill();
49 delete m_device;
53 void Connection::establish(const char* host, quint16 port,
54 const QString& helper, const QString& helper_cmd) {
55 if(m_device) {
56 QProcess *p = qobject_cast<QProcess*>(m_device);
57 if(p)
58 p->kill();
59 delete m_device;
62 strncpy(m_host, host, 256);
63 m_port = port;
64 m_helper = helper;
65 m_helper_cmd = helper_cmd;
67 if(helper.isEmpty()) {
68 QTcpSocket *s = new QTcpSocket(this);
69 s->setObjectName("IcsSocket");
71 connect(s, SIGNAL(hostFound()), this, SIGNAL(hostFound()));
72 connect(s, SIGNAL(connected()), this, SIGNAL(established()));
74 emit hostLookup();
75 s->connectToHost(m_host, m_port);
77 m_device = s;
78 m_connected = true;
80 else {
81 m_device = new QProcess(this);
82 m_device->setObjectName("IcsHelper");
84 if(m_helper_cmd.contains("$(HOST_IP)")) {
85 QHostInfo::lookupHost(m_host, this, SLOT(lookedUp(const QHostInfo)));
87 else
88 lookedUp( QHostInfo() );
91 connect(m_device, SIGNAL(readyRead()), this, SLOT(processLine()));
94 void Connection::lookedUp(const QHostInfo &hi) {
95 QProcess *p = m_device ? qobject_cast<QProcess*>(m_device) : NULL;
96 if(!p)
97 return;
99 //std::cout << "Resolved to \"" << (hi.addresses().isEmpty() ? QString() :
100 // hi.addresses().first().toString ()) << "\" "
101 // << hi.errorString() << std::endl;
102 QString args = m_helper_cmd;
103 args.replace("$(HOST_NAME)", QString(m_host) );
104 args.replace("$(HOST_IP)", hi.addresses().isEmpty() ? QString() :
105 hi.addresses().first().toString () );
106 args.replace("$(PORT)", QString::number(m_port));
107 p->start(m_helper, args.split(' '));
109 m_connected = true;
110 if(!m_unsent_text.isEmpty()) {
111 QTextStream os(m_device);
112 os << m_unsent_text;
114 if (logStream)
115 (*logStream) << "> " << m_unsent_text;
116 m_unsent_text = QString();
120 void Connection::processLine() {
121 if(!m_device) {
122 std::cout << " --> Connection::processLine, error, no m_device" << std::endl;
123 return;
126 while (m_device->canReadLine()) {
127 QString line = m_device->readLine();
128 line = buffer + line.replace("\r", "");
129 emit 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 emit 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 std::cout << " --> Connection::sendText, error, no m_device" << std::endl;
156 return;
159 processLine();
160 QTextStream os(m_device);
161 os << text << "\n";
163 if (logStream)
164 (*logStream) << "> " << text << "\n";