OSDN Git Service

Bump version.
[x264-launcher/x264-launcher.git] / src / model_logFile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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
31 LogFileModel::LogFileModel(const QString &sourceName, const QString &outputName, const QString &configName)
32 {
33         m_lines << "Job not started yet." << QString();
34         m_lines << QString("Scheduled source: %1").arg(QDir::toNativeSeparators(sourceName));
35         m_lines << QString("Scheduled output: %1").arg(QDir::toNativeSeparators(outputName));
36         m_lines << QString("Scheduled config: %1").arg(configName);
37         m_firstLine = true;
38 }
39
40 LogFileModel::~LogFileModel(void)
41 {
42 }
43
44 ///////////////////////////////////////////////////////////////////////////////
45 // Model interface
46 ///////////////////////////////////////////////////////////////////////////////
47
48 int LogFileModel::columnCount(const QModelIndex &parent) const
49 {
50         return 1;
51 }
52
53 int LogFileModel::rowCount(const QModelIndex &parent) const
54 {
55         return m_lines.count();
56 }
57
58 QVariant LogFileModel::headerData(int section, Qt::Orientation orientation, int role) const 
59 {
60         return QVariant();
61 }
62
63 QModelIndex LogFileModel::index(int row, int column, const QModelIndex &parent) const
64 {
65         return createIndex(row, column, NULL);
66 }
67
68 QModelIndex LogFileModel::parent(const QModelIndex &index) const
69 {
70         return QModelIndex();
71 }
72
73 QVariant LogFileModel::data(const QModelIndex &index, int role) const
74 {
75         if((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
76         {
77                 if(index.row() >= 0 && index.row() < m_lines.count() && index.column() == 0)
78                 {
79                         return m_lines.at(index.row());
80                 }
81         }
82
83         return QVariant();
84 }
85
86 ///////////////////////////////////////////////////////////////////////////////
87 // Public API
88 ///////////////////////////////////////////////////////////////////////////////
89
90 void LogFileModel::copyToClipboard(void)
91 {
92         QClipboard *clipboard = QApplication::clipboard();
93         clipboard->setText(m_lines.join("\r\n"));
94 }
95
96 bool  LogFileModel::saveToLocalFile(const QString &fileName)
97 {
98         QFile file(fileName);
99         if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
100         {
101                 return false;
102         }
103
104         QTextStream stream(&file);
105         stream.setCodec("UTF-8");
106         stream.setGenerateByteOrderMark(true);
107
108         for(QStringList::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
109         {
110                 stream << (*iter) << QLatin1String("\r\n");
111                 if(stream.status() != QTextStream::Status::Ok)
112                 {
113                         file.close();
114                         return false;
115                 }
116         }
117
118         stream.flush();
119         if(stream.status() != QTextStream::Status::Ok)
120         {
121                 file.close();
122                 return false;
123         }
124
125         file.close();
126         return true;
127 }
128
129 ///////////////////////////////////////////////////////////////////////////////
130 // Slots
131 ///////////////////////////////////////////////////////////////////////////////
132
133 void LogFileModel::addLogMessage(const QUuid &jobId, const QString &text)
134 {
135         beginInsertRows(QModelIndex(), m_lines.count(), m_lines.count());
136
137         if(m_firstLine)
138         {
139                 m_firstLine = false;
140                 m_lines.clear();
141         }
142
143         QStringList lines = text.split("\n");
144         for(int i = 0; i < lines.count(); i++)
145         {
146                 m_lines.append(lines.at(i));
147         }
148
149         endInsertRows();
150 }