File indexing completed on 2024-05-05 04:19:19
0001 // vim: set tabstop=4 shiftwidth=4 expandtab: 0002 /* 0003 Gwenview: an image viewer 0004 Copyright 2010 Aurélien Gâteau <agateau@kde.org> 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 0008 as published by the Free Software Foundation; either version 2 0009 of the License, or (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program; if not, write to the Free Software 0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA. 0019 0020 */ 0021 // Self 0022 #include "saveallhelper.h" 0023 0024 // Qt 0025 #include <QFuture> 0026 #include <QFutureWatcher> 0027 #include <QProgressDialog> 0028 #include <QSet> 0029 #include <QStringList> 0030 #include <QUrl> 0031 0032 // KF 0033 #include <KLocalizedString> 0034 #include <KMessageBox> 0035 0036 // Local 0037 #include <lib/document/document.h> 0038 #include <lib/document/documentfactory.h> 0039 #include <lib/document/documentjob.h> 0040 0041 namespace Gwenview 0042 { 0043 struct SaveAllHelperPrivate { 0044 QWidget *mParent = nullptr; 0045 QProgressDialog *mProgressDialog = nullptr; 0046 QSet<DocumentJob *> mJobSet; 0047 QStringList mErrorList; 0048 }; 0049 0050 SaveAllHelper::SaveAllHelper(QWidget *parent) 0051 : d(new SaveAllHelperPrivate) 0052 { 0053 d->mParent = parent; 0054 d->mProgressDialog = new QProgressDialog(parent); 0055 connect(d->mProgressDialog, &QProgressDialog::canceled, this, &SaveAllHelper::slotCanceled); 0056 d->mProgressDialog->setLabelText(i18nc("@info:progress saving all image changes", "Saving...")); 0057 d->mProgressDialog->setCancelButtonText(i18n("&Stop")); 0058 d->mProgressDialog->setMinimum(0); 0059 } 0060 0061 SaveAllHelper::~SaveAllHelper() 0062 { 0063 delete d; 0064 } 0065 0066 void SaveAllHelper::save() 0067 { 0068 const QList<QUrl> list = DocumentFactory::instance()->modifiedDocumentList(); 0069 d->mProgressDialog->setRange(0, list.size()); 0070 d->mProgressDialog->setValue(0); 0071 for (const QUrl &url : list) { 0072 Document::Ptr doc = DocumentFactory::instance()->load(url); 0073 DocumentJob *job = doc->save(url, doc->format()); 0074 connect(job, &DocumentJob::result, this, &SaveAllHelper::slotResult); 0075 d->mJobSet << job; 0076 } 0077 0078 d->mProgressDialog->exec(); 0079 0080 // Done, show message if necessary 0081 if (!d->mErrorList.isEmpty()) { 0082 QString msg = i18ncp("@info", "One document could not be saved:", "%1 documents could not be saved:", d->mErrorList.count()); 0083 msg += QLatin1String("<ul>"); 0084 for (const QString &item : qAsConst(d->mErrorList)) { 0085 msg += "<li>" + item + "</li>"; 0086 } 0087 msg += QLatin1String("</ul>"); 0088 KMessageBox::error(d->mParent, msg); 0089 } 0090 } 0091 0092 void SaveAllHelper::slotCanceled() 0093 { 0094 for (DocumentJob *job : qAsConst(d->mJobSet)) { 0095 job->kill(); 0096 } 0097 } 0098 0099 void SaveAllHelper::slotResult(KJob *_job) 0100 { 0101 auto job = static_cast<DocumentJob *>(_job); 0102 if (job->error()) { 0103 const QUrl url = job->document()->url(); 0104 const QString name = url.fileName().isEmpty() ? url.toDisplayString() : url.fileName(); 0105 d->mErrorList << xi18nc("@info %1 is the name of the document which failed to save, %2 is the reason for the failure", 0106 "<filename>%1</filename>: %2", 0107 name, 0108 kxi18n(qPrintable(job->errorString()))); 0109 } 0110 d->mJobSet.remove(job); 0111 d->mProgressDialog->setValue(d->mProgressDialog->value() + 1); 0112 } 0113 0114 } // namespace 0115 0116 #include "moc_saveallhelper.cpp"