#ifndef DATAREADER_H#define DATAREADER_H#include <QThread>#include <QTextStream>class DataReader : public QThread{ Q_OBJECTpublic: explicit DataReader(QObject *parent = 0); void run(); signals: void ftpExit(); void ftpPrint(); public slots: };#endif // DATAREADER_H
#include "datareader.h"DataReader::DataReader(QObject *parent) : QThread(parent){}void DataReader::run() { QString data; QTextStream in(stdin); data = in.readLine(); if (data.contains("stop")){ this->ftpExit(); fflush(stdin); this->quit(); } else { if (data.contains("notprint")){ this->ftpPrint(); fflush(stdin); } } exec();}
FtpChecker *ftp = new FtpChecker(filePath,pdfPath,server,ftpUser,ftpPass,userName,fileTime,pdfTime,print); DataReader *reader = new DataReader(); QObject::connect(reader,SIGNAL(finished()),&a,SLOT(quit())); QObject::connect(reader,SIGNAL(ftpExit()),ftp,SLOT(onFtpExit())); QObject::connect(reader,SIGNAL(ftpPrint()),ftp,SLOT(onFtpPrint())); reader->start(); reader->moveToThread(reader->thread());
data = in.readLine();
C++ (Qt)while (!in.canReadLine()) QThread::msleep(100);data = in.readLine();
void FtpChecker::onFtpExit() { fflush(stdin); qDebug("Exiting.\r\n"); ftp->abort(); //ftp здесь - объект QFtp ftp->close(); timer->deleteLater(); ftp->deleteLater();}void FtpChecker::onFtpPrint() { canPrint = false;}
case QFtp::Get: { flist.first()->close(); QString curFile = flist.first()->fileName(); curFile.replace(0,curFile.lastIndexOf("/") + 1,""); qDebug(QString("File " + curFile + " downloaded.\r\n").toUtf8()); fflush(stdout); if (curFile.endsWith(".pdf",Qt::CaseInsensitive)) { if (canPrint) { Poppler::Document *doc = Poppler::Document::load(filePath + "/" + curFile); bool isOk = false; if (!doc || doc->isLocked()) { qDebug(QString("No access to file " + filePath + "/" + curFile + "\r\n").toUtf8()); fflush(stdout); } else { QPrinterInfo *printerInf = new QPrinterInfo(); QString printerName; if (print == "default") { printerName = printerInf->defaultPrinter().printerName(); } else { bool flag = false; for (int i = 0; i < printerInf->availablePrinters().count(); i++) { if (printerInf->availablePrinters().at(i).printerName() == print) { printerName = print; flag = true; break; } } if (!flag) { printerName = printerInf->defaultPrinter().printerName(); qDebug(QString("Printer " + print + " was not found. Using default printer.\r\n").toUtf8()); fflush(stdout); } } QPrinter *printer = new QPrinter(QPrinter::HighResolution); printer->setPrinterName(printerName); printer->setResolution(300); int i = 0; while (1) { Poppler::Page* pdfPage = doc->page(i); if (pdfPage == 0) { break; } else { printer->setFullPage(true); printer->setPaperSize(pdfPage->pageSizeF(),QPrinter::Point); switch (pdfPage->orientation()) { case Poppler::Page::Seascape: printer->setOrientation(QPrinter::Landscape); break; case Poppler::Page::Landscape: printer->setOrientation(QPrinter::Landscape); break; case Poppler::Page::Portrait: printer->setOrientation(QPrinter::Portrait); break; case Poppler::Page::UpsideDown: printer->setOrientation(QPrinter::Portrait); break; } QImage image = pdfPage->renderToImage(300,300); if (image.isNull()) { qDebug("Poppler error: can't create image.\r\n"); fflush(stdout); } else { QPainter painter; if (painter.begin(printer)) { painter.drawImage(0, 0, image); if (painter.end()) { isOk = true; } else { isOk = false; break; } } } } delete pdfPage; i++; } } delete doc; if (isOk) { QDir dir(pdfPath); if (!dir.exists()) dir.mkpath(pdfPath); QFile file(filePath + "/" + curFile); file.rename(pdfPath + "/" + curFile); qDebug(QString(curFile + " printed\r\n").toUtf8()); fflush(stdout); } else { qDebug(QString(curFile + " NOT printed\r\n").toUtf8()); fflush(stdout); } } else { QFile file(filePath + "/" + curFile); file.rename(pdfPath + "/" + curFile); } } ftp->remove(curFile); flist.removeFirst(); break; }
void DataReader::run() { QString data; QTextStream in(stdin); while (1) { data = in.readLine(); this->msleep(100); if (data.contains("stop")){ this->ftpExit(); fflush(stdin); this->quit(); break; } else { if (data.contains("notprint")){ this->ftpPrint(); fflush(stdin); } } }}