OSDN Git Service

Updated copyright year.
[x264-launcher/x264-launcher.git] / src / model_logFile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2019 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "model_logFile.h"
23 #include "thread_encode.h"
24
25 #include <QIcon>
26 #include <QApplication>
27 #include <QClipboard>
28 #include <QDir>
29 #include <QTextStream>
30 #include <QDateTime>
31
32 static const QLatin1String FMT_TIMESTAMP("[yyyy-MM-dd][HH:mm:ss] ");
33
34 LogFileModel::LogFileModel(const QString &sourceName, const QString &outputName, const QString &configName)
35 {
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;
43 }
44
45 LogFileModel::~LogFileModel(void)
46 {
47 }
48
49 ///////////////////////////////////////////////////////////////////////////////
50 // Model interface
51 ///////////////////////////////////////////////////////////////////////////////
52
53 int LogFileModel::columnCount(const QModelIndex &parent) const
54 {
55         return 1;
56 }
57
58 int LogFileModel::rowCount(const QModelIndex &parent) const
59 {
60         return m_lines.count();
61 }
62
63 QVariant LogFileModel::headerData(int section, Qt::Orientation orientation, int role) const 
64 {
65         return QVariant();
66 }
67
68 QModelIndex LogFileModel::index(int row, int column, const QModelIndex &parent) const
69 {
70         return createIndex(row, column, NULL);
71 }
72
73 QModelIndex LogFileModel::parent(const QModelIndex &index) const
74 {
75         return QModelIndex();
76 }
77
78 QVariant LogFileModel::data(const QModelIndex &index, int role) const
79 {
80         if((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
81         {
82                 if(index.row() >= 0 && index.row() < m_lines.count() && index.column() == 0)
83                 {
84                         if (role == Qt::ToolTipRole)
85                         {
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;
89                         }
90                         else
91                         {
92                                 return m_lines.at(index.row()).second;
93                         }
94                 }
95         }
96
97         return QVariant();
98 }
99
100 ///////////////////////////////////////////////////////////////////////////////
101 // Public API
102 ///////////////////////////////////////////////////////////////////////////////
103
104 void LogFileModel::copyToClipboard(void) const
105 {
106         QClipboard *const clipboard = QApplication::clipboard();
107         QStringList buffer;
108         for (QList<LogEntry>::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
109         {
110                 const QString timeStamp = QDateTime::fromMSecsSinceEpoch(iter->first).toString(FMT_TIMESTAMP);
111                 buffer << (timeStamp + iter->second);
112         }
113         clipboard->setText(buffer.join("\r\n"));
114 }
115
116 bool LogFileModel::saveToLocalFile(const QString &fileName) const
117 {
118         QFile file(fileName);
119         if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
120         {
121                 return false;
122         }
123
124         QTextStream stream(&file);
125         stream.setCodec("UTF-8");
126         stream.setGenerateByteOrderMark(true);
127
128         for(QList<LogEntry>::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
129         {
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)
133                 {
134                         file.close();
135                         return false;
136                 }
137         }
138
139         stream.flush();
140         if(stream.status() != QTextStream::Status::Ok)
141         {
142                 file.close();
143                 return false;
144         }
145
146         file.close();
147         return true;
148 }
149
150 ///////////////////////////////////////////////////////////////////////////////
151 // Slots
152 ///////////////////////////////////////////////////////////////////////////////
153
154 void LogFileModel::addLogMessage(const QUuid &jobId, const qint64 &timeStamp, const QString &text)
155 {
156         beginInsertRows(QModelIndex(), m_lines.count(), m_lines.count());
157
158         if(m_firstLine)
159         {
160                 m_firstLine = false;
161                 m_lines.clear();
162         }
163
164         const QStringList lines = text.split("\n");
165         for(QStringList::ConstIterator iter = lines.constBegin(); iter != lines.constEnd(); iter++)
166         {
167                 m_lines << qMakePair(timeStamp, (*iter));
168         }
169
170         endInsertRows();
171 }