Save log files to the same directory where the output file is located.
[simple-x264-launcher.git] / src / model_logFile.cpp
blobbd8de1df4dd2ce6be44c4da2a33280ed94924a14
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "model_logFile.h"
23 #include "thread_encode.h"
25 #include <QIcon>
26 #include <QApplication>
27 #include <QClipboard>
28 #include <QDir>
29 #include <QTextStream>
30 #include <QDateTime>
32 static const QLatin1String FMT_TIMESTAMP("[yyyy-MM-dd][HH:mm:ss] ");
34 LogFileModel::LogFileModel(const QString &sourceName, const QString &outputName, const QString &configName)
36 const qint64 timeStamp = QDateTime::currentMSecsSinceEpoch();
37 m_lines << qMakePair(timeStamp, QString("Job not started yet."));
38 m_lines << qMakePair(timeStamp, QString());
39 m_lines << qMakePair(timeStamp, QString("Scheduled source: %1").arg(QDir::toNativeSeparators(sourceName)));
40 m_lines << qMakePair(timeStamp, QString("Scheduled output: %1").arg(QDir::toNativeSeparators(outputName)));
41 m_lines << qMakePair(timeStamp, QString("Scheduled config: %1").arg(configName));
42 m_firstLine = true;
45 LogFileModel::~LogFileModel(void)
49 ///////////////////////////////////////////////////////////////////////////////
50 // Model interface
51 ///////////////////////////////////////////////////////////////////////////////
53 int LogFileModel::columnCount(const QModelIndex &parent) const
55 return 1;
58 int LogFileModel::rowCount(const QModelIndex &parent) const
60 return m_lines.count();
63 QVariant LogFileModel::headerData(int section, Qt::Orientation orientation, int role) const
65 return QVariant();
68 QModelIndex LogFileModel::index(int row, int column, const QModelIndex &parent) const
70 return createIndex(row, column, NULL);
73 QModelIndex LogFileModel::parent(const QModelIndex &index) const
75 return QModelIndex();
78 QVariant LogFileModel::data(const QModelIndex &index, int role) const
80 if((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
82 if(index.row() >= 0 && index.row() < m_lines.count() && index.column() == 0)
84 if (role == Qt::ToolTipRole)
86 const LogEntry &entry = m_lines.at(index.row());
87 const QString timeStamp = QDateTime::fromMSecsSinceEpoch(entry.first).toString(FMT_TIMESTAMP);
88 return timeStamp + entry.second;
90 else
92 return m_lines.at(index.row()).second;
97 return QVariant();
100 ///////////////////////////////////////////////////////////////////////////////
101 // Public API
102 ///////////////////////////////////////////////////////////////////////////////
104 void LogFileModel::copyToClipboard(void) const
106 QClipboard *const clipboard = QApplication::clipboard();
107 QStringList buffer;
108 for (QList<LogEntry>::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
110 const QString timeStamp = QDateTime::fromMSecsSinceEpoch(iter->first).toString(FMT_TIMESTAMP);
111 buffer << (timeStamp + iter->second);
113 clipboard->setText(buffer.join("\r\n"));
116 bool LogFileModel::saveToLocalFile(const QString &fileName) const
118 QFile file(fileName);
119 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
121 return false;
124 QTextStream stream(&file);
125 stream.setCodec("UTF-8");
126 stream.setGenerateByteOrderMark(true);
128 for(QList<LogEntry>::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
130 const QString timeStamp = QDateTime::fromMSecsSinceEpoch(iter->first).toString(FMT_TIMESTAMP);
131 stream << timeStamp << iter->second << QLatin1String("\r\n");
132 if(stream.status() != QTextStream::Status::Ok)
134 file.close();
135 return false;
139 stream.flush();
140 if(stream.status() != QTextStream::Status::Ok)
142 file.close();
143 return false;
146 file.close();
147 return true;
150 ///////////////////////////////////////////////////////////////////////////////
151 // Slots
152 ///////////////////////////////////////////////////////////////////////////////
154 void LogFileModel::addLogMessage(const QUuid &jobId, const qint64 &timeStamp, const QString &text)
156 beginInsertRows(QModelIndex(), m_lines.count(), m_lines.count());
158 if(m_firstLine)
160 m_firstLine = false;
161 m_lines.clear();
164 const QStringList lines = text.split("\n");
165 for(QStringList::ConstIterator iter = lines.constBegin(); iter != lines.constEnd(); iter++)
167 m_lines << qMakePair(timeStamp, (*iter));
170 endInsertRows();