File indexing completed on 2024-04-28 17:02:20
0001 /* 0002 This file is part of Massif Visualizer 0003 0004 Copyright 2012 Milian Wolff <mail@milianw.de> 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License as 0008 published by the Free Software Foundation; either version 2 of 0009 the License or (at your option) version 3 or any later version 0010 accepted by the membership of KDE e.V. (or its successor approved 0011 by the membership of KDE e.V.), which shall act as a proxy 0012 defined in Section 14 of version 3 of the license. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #include "parseworker.h" 0024 #include "parser.h" 0025 #include "filedata.h" 0026 0027 #include <QUrl> 0028 #include <QTemporaryFile> 0029 0030 #include <KFilterDev> 0031 #include <KIO/FileCopyJob> 0032 #include <KLocalizedString> 0033 0034 namespace Massif { 0035 0036 ParseWorker::ParseWorker(QObject* parent) 0037 : QObject(parent) 0038 { 0039 0040 } 0041 0042 void ParseWorker::parse(const QUrl& url, const QStringList& allocators) 0043 { 0044 // process in background thread 0045 if (QThread::currentThread() != thread()) { 0046 QMetaObject::invokeMethod(this, "parse", Q_ARG(QUrl, url), Q_ARG(QStringList, allocators)); 0047 return; 0048 } 0049 0050 m_shouldStop = 0; 0051 QTemporaryFile tempFile; 0052 QString filePath; 0053 if (!url.isLocalFile()) { 0054 if (!tempFile.open() || !KIO::file_copy(url, QUrl::fromLocalFile(tempFile.fileName()), -1, KIO::Overwrite | KIO::HideProgressInfo)->exec()) { 0055 emit error(i18n("Download Failed"), 0056 i18n("Failed to download remote massif data file <i>%1</i>.", url.toString())); 0057 return; 0058 } 0059 filePath = tempFile.fileName(); 0060 } else { 0061 filePath = url.toLocalFile(); 0062 } 0063 0064 KFilterDev device(filePath); 0065 if (!device.open(QIODevice::ReadOnly)) { 0066 emit error(i18n("Read Failed"), 0067 i18n("Could not open file <i>%1</i> for reading.", filePath)); 0068 return; 0069 } 0070 0071 Parser p; 0072 emit progressRange(0, 100); 0073 connect(&p, &Parser::progress, this, &ParseWorker::progress); 0074 QScopedPointer<FileData> data(p.parse(&device, allocators, &m_shouldStop)); 0075 0076 if (!data) { 0077 if (!m_shouldStop) { 0078 emit error(i18n("Parser Failed"), 0079 i18n("Could not parse file <i>%1</i>.<br>" 0080 "Parse error in line %2:<br>%3", 0081 url.toString(), p.errorLine() + 1, 0082 QString::fromLatin1(p.errorLineString()))); 0083 } 0084 return; 0085 } else if (data->snapshots().isEmpty()) { 0086 emit error(i18n("Empty data file <i>%1</i>.", url.toString()), 0087 i18n("Empty Data File")); 0088 return; 0089 } 0090 0091 emit progress(100); 0092 0093 // success! 0094 emit finished(url, data.take()); 0095 } 0096 0097 void ParseWorker::stop() 0098 { 0099 m_shouldStop = 1; 0100 } 0101 0102 }