aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/logview/ExportOperation.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/logview/ExportOperation.cpp')
-rw-r--r--src/modules/logview/ExportOperation.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/modules/logview/ExportOperation.cpp b/src/modules/logview/ExportOperation.cpp
new file mode 100644
index 000000000..49c9f47be
--- /dev/null
+++ b/src/modules/logview/ExportOperation.cpp
@@ -0,0 +1,45 @@
+#include "ExportOperation.h"
+
+#include <QProgressDialog>
+#include <QFutureWatcher>
+#include <QVector>
+#include <QtConcurrent>
+
+#include "LogFile.h"
+#include "LogViewWindow.h"
+#include "KviFileUtils.h"
+
+ExportOperation::ExportOperation(const std::vector<std::shared_ptr<LogFile>> & logs, LogFile::ExportType type, QString szDir, QObject * parent)
+ : QObject(parent)
+ , m_logs(logs)
+ , m_type(type)
+ , m_szDir(szDir)
+{
+}
+
+void ExportOperation::start()
+{
+ QProgressDialog * pProgressDialog = new QProgressDialog("Exporting logs...", "Cancel", 0, m_logs.size());
+ QFutureWatcher<void> * pFutureWatcher = new QFutureWatcher<void>();
+
+ QObject::connect(pFutureWatcher, &QFutureWatcher<void>::finished, pProgressDialog, &QProgressDialog::deleteLater);
+ QObject::connect(pFutureWatcher, &QFutureWatcher<void>::finished, pFutureWatcher, &QFutureWatcher<void>::deleteLater);
+ QObject::connect(pFutureWatcher, &QFutureWatcher<void>::finished, this, &ExportOperation::deleteLater);
+
+ QObject::connect(pProgressDialog, &QProgressDialog::canceled, pFutureWatcher, &QFutureWatcher<void>::cancel);
+ QObject::connect(pFutureWatcher, &QFutureWatcher<void>::progressValueChanged, pProgressDialog, &QProgressDialog::setValue);
+
+ // The directory string and the export type could be captured by value if
+ // this function was inlined. However, because the QtConcurrent functions
+ // aside from QtConcurrent::run only operate on references the list of
+ // pointers might expire, hence the purpose of this class.
+ pFutureWatcher->setFuture(QtConcurrent::map(m_logs, [this](const std::shared_ptr<LogFile> & pLog) {
+ const QString szDate = pLog->date().toString("yyyy.MM.dd");
+ QString filename = QString("%1_%2.%3_%4").arg(pLog->typeString(), pLog->name(), pLog->network(), szDate);
+ filename.replace(QRegExp("[\\\\/:*?\"<>|]"), "_");
+ QString szLog = m_szDir + KVI_PATH_SEPARATOR_CHAR + filename;
+ KviFileUtils::adjustFilePath(szLog);
+ pLog->createLog(m_type, szLog);
+ }));
+ pProgressDialog->show();
+}