File indexing completed on 2024-11-24 04:50:39

0001 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: LGPL-3.0-or-later
0004 
0005 #include "importer.h"
0006 #include "merkuro_calendar_debug.h"
0007 #include <Akonadi/ICalImporter>
0008 #include <KLocalizedString>
0009 #include <QTimer>
0010 
0011 using namespace std::chrono_literals;
0012 
0013 Importer::Importer(QObject *parent)
0014     : QObject(parent)
0015 {
0016     connect(this, &Importer::calendarImportInProgressChanged, this, [this]() {
0017         if (m_calendarImportInProgress && !m_calendarFilesToImport.isEmpty()) {
0018             QTimer::singleShot(100ms, [this]() {
0019                 Q_EMIT importCalendarFromFile(m_calendarFilesToImport.takeFirst());
0020             });
0021         }
0022     });
0023 }
0024 
0025 void Importer::importCalendarFromUrl(const QUrl &url, bool merge, qint64 collectionId)
0026 {
0027     if (!m_calendar) {
0028         return;
0029     }
0030 
0031     auto importer = new Akonadi::ICalImporter(m_calendar->incidenceChanger());
0032     bool jobStarted;
0033 
0034     if (merge) {
0035         connect(importer, &Akonadi::ICalImporter::importIntoExistingFinished, this, &Importer::importFinished);
0036         connect(importer, &Akonadi::ICalImporter::importIntoExistingFinished, this, &Importer::importIntoExistingFinished);
0037         auto collection = m_calendar->collection(collectionId);
0038         jobStarted = importer->importIntoExistingResource(url, collection);
0039     } else {
0040         connect(importer, &Akonadi::ICalImporter::importIntoNewFinished, this, &Importer::importFinished);
0041         connect(importer, &Akonadi::ICalImporter::importIntoNewFinished, this, &Importer::importIntoNewFinished);
0042         jobStarted = importer->importIntoNewResource(url.path());
0043     }
0044 
0045     if (jobStarted) {
0046         Q_EMIT importStarted();
0047     } else if (!importer->errorMessage().isEmpty()) {
0048         // empty error message means user canceled.
0049         qCDebug(MERKURO_CALENDAR_LOG) << i18n("An error occurred: %1", importer->errorMessage());
0050         m_importErrorMessage = importer->errorMessage();
0051         Q_EMIT importErrorMessageChanged();
0052     }
0053 }
0054 
0055 QString Importer::importErrorMessage()
0056 {
0057     return m_importErrorMessage;
0058 }
0059 
0060 QAction *Importer::importAction() const
0061 {
0062     return m_importAction;
0063 }
0064 
0065 void Importer::setImportAction(QAction *importAction)
0066 {
0067     if (m_importAction == importAction) {
0068         return;
0069     }
0070 
0071     if (m_importAction) {
0072         disconnect(this, &Importer::importFinished, m_importAction, nullptr);
0073         disconnect(this, &Importer::importStarted, m_importAction, nullptr);
0074     }
0075 
0076     m_importAction = importAction;
0077     Q_EMIT importActionChanged();
0078 
0079     if (m_importAction) {
0080         connect(this, &Importer::importFinished, m_importAction, [this] {
0081             m_importAction->setEnabled(true);
0082         });
0083         connect(this, &Importer::importStarted, m_importAction, [this] {
0084             m_importAction->setEnabled(false);
0085         });
0086     }
0087 }
0088 
0089 #include "moc_importer.cpp"