1 /*
2     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "kioLogFileReader.h"
8 
9 #include <QIODevice>
10 
11 #include <kio/filejob.h>
12 #include <kio/job.h>
13 #include <kio_version.h>
14 
15 #include <KDirWatch>
16 
17 #include "ksystemlog_debug.h"
18 
19 #define READ_SIZE 10
20 
KioLogFileReader(const LogFile & logFile)21 KioLogFileReader::KioLogFileReader(const LogFile &logFile)
22     : mLogFile(logFile)
23     , mFileWatch(new KDirWatch(this))
24 {
25     connect(mFileWatch, &KDirWatch::dirty, this, &KioLogFileReader::watchFile);
26     mFileWatch->addFile(logFile.url().toLocalFile());
27     /*
28     fileWatch.setInterval(1000);
29     connect(& (fileWatch), SIGNAL(timeout()), this, SLOT(watchFile()));
30     */
31 
32     qCDebug(KSYSTEMLOG) << "Starting " << logFile.url().toLocalFile();
33 }
34 
~KioLogFileReader()35 KioLogFileReader::~KioLogFileReader()
36 {
37 }
38 
open()39 void KioLogFileReader::open()
40 {
41     qCDebug(KSYSTEMLOG) << "Opening...";
42     mFileJob = KIO::open(mLogFile.url(), QIODevice::ReadOnly | QIODevice::Text);
43 
44     connect(mFileJob, &KIO::FileJob::open, this, &KioLogFileReader::openDone);
45     connect(mFileJob, SIGNAL(close(KIO::Job *)), this, SLOT(closeDone(KIO::Job *)));
46 
47     connect(mFileJob, &KIO::FileJob::data, this, &KioLogFileReader::dataReceived);
48     connect(mFileJob, &KIO::FileJob::mimeTypeFound, this, &KioLogFileReader::mimetypeReceived);
49     qCDebug(KSYSTEMLOG) << "File opened.";
50 }
51 
close()52 void KioLogFileReader::close()
53 {
54     mFileJob->close();
55 }
56 
openDone(KIO::Job *)57 void KioLogFileReader::openDone(KIO::Job * /*job*/)
58 {
59     qCDebug(KSYSTEMLOG) << "Opening done...";
60 
61     mFileJob->read(READ_SIZE);
62 }
63 
closeDone(KIO::Job *)64 void KioLogFileReader::closeDone(KIO::Job * /*job*/)
65 {
66     qCDebug(KSYSTEMLOG) << "Closing done...";
67 }
68 
dataReceived(KIO::Job * job,const QByteArray & data)69 void KioLogFileReader::dataReceived(KIO::Job *job, const QByteArray &data)
70 {
71     if (job != mFileJob) {
72         qCDebug(KSYSTEMLOG) << "Not the good job";
73         return;
74     }
75 
76     if (data.isEmpty()) {
77         return;
78     }
79 
80     // qCDebug(KSYSTEMLOG) << "Receiving data... (" << totalRead << ")";
81     mBuffer.append(QLatin1String(data));
82     mTotalRead += data.size();
83 
84     emitCompleteLines();
85 
86     qCDebug(KSYSTEMLOG) << "Total read : " << mTotalRead << " of " << mFileJob->size();
87     if (mTotalRead < mFileJob->size()) {
88         mFileJob->read(READ_SIZE);
89     } else {
90         qCDebug(KSYSTEMLOG) << "Entire file read, beginning file watching...";
91         mFileWatch->startScan();
92     }
93 
94     // qCDebug(KSYSTEMLOG) << "Data received : " << buffer;
95 
96     // totalRead++;
97 }
98 
emitCompleteLines()99 void KioLogFileReader::emitCompleteLines()
100 {
101     int endLinePos = mBuffer.indexOf(QLatin1String("\n"));
102     while (true) {
103         if (endLinePos == -1) {
104             break;
105         }
106 
107         Q_EMIT lineRead(mBuffer.left(endLinePos));
108 
109         // Remove the emitted line and the end line character
110         mBuffer.remove(0, endLinePos + 1);
111 
112         endLinePos = mBuffer.indexOf(QLatin1String("\n"));
113     }
114 
115     // If this is the end line and it does not terminate by a \n, we return it
116     if (mTotalRead == mFileJob->size()) {
117         Q_EMIT lineRead(mBuffer);
118         mBuffer.clear();
119     }
120 }
121 
mimetypeReceived(KIO::Job *,const QString & type)122 void KioLogFileReader::mimetypeReceived(KIO::Job * /*job*/, const QString &type)
123 {
124     qCDebug(KSYSTEMLOG) << "Mimetype received " << type;
125 }
126 
watchFile(const QString & path)127 void KioLogFileReader::watchFile(const QString &path)
128 {
129     qCDebug(KSYSTEMLOG) << "Watch file : size : " << path;
130 }
131