File indexing completed on 2024-05-05 05:48:56

0001 /*
0002     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kioLogFileReader.h"
0008 
0009 #include <QIODevice>
0010 
0011 #include <kio/filejob.h>
0012 #include <kio/job.h>
0013 
0014 #include <KDirWatch>
0015 
0016 #include "ksystemlog_debug.h"
0017 
0018 #define READ_SIZE 10
0019 
0020 KioLogFileReader::KioLogFileReader(const LogFile &logFile)
0021     : mLogFile(logFile)
0022     , mFileWatch(new KDirWatch(this))
0023 {
0024     connect(mFileWatch, &KDirWatch::dirty, this, &KioLogFileReader::watchFile);
0025     mFileWatch->addFile(logFile.url().toLocalFile());
0026     /*
0027     fileWatch.setInterval(1000);
0028     connect(& (fileWatch), SIGNAL(timeout()), this, SLOT(watchFile()));
0029     */
0030 
0031     qCDebug(KSYSTEMLOG) << "Starting " << logFile.url().toLocalFile();
0032 }
0033 
0034 KioLogFileReader::~KioLogFileReader()
0035 {
0036 }
0037 
0038 void KioLogFileReader::open()
0039 {
0040     qCDebug(KSYSTEMLOG) << "Opening...";
0041     mFileJob = KIO::open(mLogFile.url(), QIODevice::ReadOnly | QIODevice::Text);
0042 
0043     connect(mFileJob, &KIO::FileJob::open, this, &KioLogFileReader::openDone);
0044     connect(mFileJob, SIGNAL(close(KIO::Job *)), this, SLOT(closeDone(KIO::Job *)));
0045 
0046     connect(mFileJob, &KIO::FileJob::data, this, &KioLogFileReader::dataReceived);
0047     connect(mFileJob, &KIO::FileJob::mimeTypeFound, this, &KioLogFileReader::mimetypeReceived);
0048     qCDebug(KSYSTEMLOG) << "File opened.";
0049 }
0050 
0051 void KioLogFileReader::close()
0052 {
0053     mFileJob->close();
0054 }
0055 
0056 void KioLogFileReader::openDone(KIO::Job * /*job*/)
0057 {
0058     qCDebug(KSYSTEMLOG) << "Opening done...";
0059 
0060     mFileJob->read(READ_SIZE);
0061 }
0062 
0063 void KioLogFileReader::closeDone(KIO::Job * /*job*/)
0064 {
0065     qCDebug(KSYSTEMLOG) << "Closing done...";
0066 }
0067 
0068 void KioLogFileReader::dataReceived(KIO::Job *job, const QByteArray &data)
0069 {
0070     if (job != mFileJob) {
0071         qCDebug(KSYSTEMLOG) << "Not the good job";
0072         return;
0073     }
0074 
0075     if (data.isEmpty()) {
0076         return;
0077     }
0078 
0079     // qCDebug(KSYSTEMLOG) << "Receiving data... (" << totalRead << ")";
0080     mBuffer.append(QLatin1String(data));
0081     mTotalRead += data.size();
0082 
0083     emitCompleteLines();
0084 
0085     qCDebug(KSYSTEMLOG) << "Total read : " << mTotalRead << " of " << mFileJob->size();
0086     if (mTotalRead < mFileJob->size()) {
0087         mFileJob->read(READ_SIZE);
0088     } else {
0089         qCDebug(KSYSTEMLOG) << "Entire file read, beginning file watching...";
0090         mFileWatch->startScan();
0091     }
0092 
0093     // qCDebug(KSYSTEMLOG) << "Data received : " << buffer;
0094 
0095     // totalRead++;
0096 }
0097 
0098 void KioLogFileReader::emitCompleteLines()
0099 {
0100     int endLinePos = mBuffer.indexOf(QLatin1String("\n"));
0101     while (true) {
0102         if (endLinePos == -1) {
0103             break;
0104         }
0105 
0106         Q_EMIT lineRead(mBuffer.left(endLinePos));
0107 
0108         // Remove the emitted line and the end line character
0109         mBuffer.remove(0, endLinePos + 1);
0110 
0111         endLinePos = mBuffer.indexOf(QLatin1String("\n"));
0112     }
0113 
0114     // If this is the end line and it does not terminate by a \n, we return it
0115     if (mTotalRead == mFileJob->size()) {
0116         Q_EMIT lineRead(mBuffer);
0117         mBuffer.clear();
0118     }
0119 }
0120 
0121 void KioLogFileReader::mimetypeReceived(KIO::Job * /*job*/, const QString &type)
0122 {
0123     qCDebug(KSYSTEMLOG) << "Mimetype received " << type;
0124 }
0125 
0126 void KioLogFileReader::watchFile(const QString &path)
0127 {
0128     qCDebug(KSYSTEMLOG) << "Watch file : size : " << path;
0129 }
0130 
0131 #include "moc_kioLogFileReader.cpp"