#include "lcdproc.h" #include /** * Constructor. Initiates the connection to the LCDproc server * specified in the config file. */ LCDproc::LCDproc(Config* conf):QObject(){ config = conf; sendok = false; connect(&con,SIGNAL(connected()), this,SLOT(LCDinit())); connect(&con,SIGNAL(readyRead()), this,SLOT(feedback())); connect(&con,SIGNAL(error(int)), this,SLOT(error(int))); con.connectToHost(config->getOption("lcdserver"), config->getOption("lcdport").toInt()); }; /** * This function is called when the connection to the LCDproc * server was successul. The LCD widgets are created here. */ void LCDproc::LCDinit(){ qDebug("Connection to LCDproc established"); sendCmd("hello"); sendCmd("client_set -name IRFC2"); sendCmd("screen_add lcd"); sendCmd("screen_set lcd -wid -priority 192 "+ config->getOption("lcdwidth")+ " -hgt "+ config->getOption("lcdheight")+ " -heartbeat off"); sendok = true; msgline = 1; if (config->getOption("lcdheight").toInt() > 1){ sendCmd("widget_add lcd title string"); sendCmd(QString("widget_set lcd title %1 1 {-=IRFC2=-}") .arg((config->getOption("lcdwidth").toInt()/2)-4)); msgline = 2; } sendCmd("widget_add lcd msg scroller"); LCDwrite("Welcome to IRFC2"); } /** * SLOT. Send the given string to the LCD displays message scroller */ void LCDproc::LCDwrite(QString msg){ if(!sendok) return; QString cmd("widget_set lcd msg 1 %1 %2 %3 h 2 {%4}"); cmd = cmd.arg(msgline); cmd = cmd.arg(config->getOption("lcdwidth")); cmd = cmd.arg(msgline); cmd = cmd.arg(msg); sendCmd(cmd); } /** * Send a command to the LCDproc server. A newline is added before * send. */ void LCDproc::sendCmd(QString cmd){ con.writeBlock(cmd+"\n",cmd.length()+1); } /** * SLOT. Is called when the connection to the LCDproc server fails. */ void LCDproc::error(int i){ if(i == QSocket::ErrConnectionRefused){ qWarning("LCDproc: the connection was refused"); }else if(i == QSocket::ErrHostNotFound){ qWarning("LCDproc: the host was not found"); }else if(i == QSocket::ErrSocketRead){ qWarning("LCDproc: read from the socket failed"); } qWarning("Connection to LCDproc failed - No LCD support"); sendok=false; } /** * SLOT. Prints all feedback except 'success' messages from LCDproc * to STDERR */ void LCDproc::feedback(){ QString msg; while(con.canReadLine()){ msg = con.readLine().stripWhiteSpace(); if(msg == "success") continue; qDebug("LCDproc: %s",msg.latin1()); } }