File indexing completed on 2024-04-28 05:11:31

0001 /*
0002   SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
0003   SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "combinedincidenceeditor.h"
0009 
0010 #include "incidenceeditor_debug.h"
0011 
0012 using namespace IncidenceEditorNG;
0013 
0014 /// public methods
0015 
0016 CombinedIncidenceEditor::CombinedIncidenceEditor(QWidget *parent)
0017     : IncidenceEditor(parent)
0018 {
0019 }
0020 
0021 CombinedIncidenceEditor::~CombinedIncidenceEditor()
0022 {
0023     qDeleteAll(mCombinedEditors);
0024 }
0025 
0026 void CombinedIncidenceEditor::combine(IncidenceEditor *other)
0027 {
0028     Q_ASSERT(other);
0029     mCombinedEditors.append(other);
0030     connect(other, &IncidenceEditor::dirtyStatusChanged, this, &CombinedIncidenceEditor::handleDirtyStatusChange);
0031 }
0032 
0033 bool CombinedIncidenceEditor::isDirty() const
0034 {
0035     return mDirtyEditorCount > 0;
0036 }
0037 
0038 bool CombinedIncidenceEditor::isValid() const
0039 {
0040     for (IncidenceEditor *editor : std::as_const(mCombinedEditors)) {
0041         if (!editor->isValid()) {
0042             const QString reason = editor->lastErrorString();
0043             editor->focusInvalidField();
0044             if (!reason.isEmpty()) {
0045                 Q_EMIT showMessage(reason, KMessageWidget::Warning);
0046             }
0047             return false;
0048         }
0049     }
0050 
0051     return true;
0052 }
0053 
0054 void CombinedIncidenceEditor::handleDirtyStatusChange(bool isDirty)
0055 {
0056     const int prevDirtyCount = mDirtyEditorCount;
0057 
0058     Q_ASSERT(mDirtyEditorCount >= 0);
0059 
0060     if (isDirty) {
0061         ++mDirtyEditorCount;
0062     } else {
0063         --mDirtyEditorCount;
0064     }
0065 
0066     Q_ASSERT(mDirtyEditorCount >= 0);
0067 
0068     if (prevDirtyCount == 0) {
0069         Q_EMIT dirtyStatusChanged(true);
0070     }
0071     if (mDirtyEditorCount == 0) {
0072         Q_EMIT dirtyStatusChanged(false);
0073     }
0074 }
0075 
0076 void CombinedIncidenceEditor::load(const KCalendarCore::Incidence::Ptr &incidence)
0077 {
0078     mLoadedIncidence = incidence;
0079     for (IncidenceEditor *editor : std::as_const(mCombinedEditors)) {
0080         // load() may fire dirtyStatusChanged(), reset mDirtyEditorCount to make sure
0081         // we don't end up with an invalid dirty count.
0082         editor->blockSignals(true);
0083         editor->load(incidence);
0084         editor->blockSignals(false);
0085 
0086         if (editor->isDirty()) {
0087             // We are going to crash due to assert. Print some useful info before crashing.
0088             qCWarning(INCIDENCEEDITOR_LOG) << "Faulty editor was " << editor->objectName();
0089             qCWarning(INCIDENCEEDITOR_LOG) << "Incidence " << (incidence ? incidence->uid() : QStringLiteral("null"));
0090 
0091             editor->printDebugInfo();
0092 
0093             Q_ASSERT_X(false, "load", "editor shouldn't be dirty");
0094         }
0095     }
0096 
0097     mWasDirty = false;
0098     mDirtyEditorCount = 0;
0099     Q_EMIT dirtyStatusChanged(false);
0100 }
0101 
0102 void CombinedIncidenceEditor::load(const Akonadi::Item &item)
0103 {
0104     for (IncidenceEditor *editor : std::as_const(mCombinedEditors)) {
0105         // load() may fire dirtyStatusChanged(), reset mDirtyEditorCount to make sure
0106         // we don't end up with an invalid dirty count.
0107         editor->blockSignals(true);
0108         editor->load(item);
0109         editor->blockSignals(false);
0110 
0111         if (editor->isDirty()) {
0112             // We are going to crash due to assert. Print some useful info before crashing.
0113             qCWarning(INCIDENCEEDITOR_LOG) << "Faulty editor was " << editor->objectName();
0114             // qCWarning(INCIDENCEEDITOR_LOG) << "Incidence " << ( incidence ? incidence->uid() : "null" );
0115 
0116             editor->printDebugInfo();
0117 
0118             Q_ASSERT_X(false, "load", "editor shouldn't be dirty");
0119         }
0120     }
0121 
0122     mWasDirty = false;
0123     mDirtyEditorCount = 0;
0124     Q_EMIT dirtyStatusChanged(false);
0125 }
0126 
0127 void CombinedIncidenceEditor::save(const KCalendarCore::Incidence::Ptr &incidence)
0128 {
0129     for (IncidenceEditor *editor : std::as_const(mCombinedEditors)) {
0130         editor->save(incidence);
0131     }
0132 }
0133 
0134 void CombinedIncidenceEditor::save(Akonadi::Item &item)
0135 {
0136     for (IncidenceEditor *editor : std::as_const(mCombinedEditors)) {
0137         editor->save(item);
0138     }
0139 }
0140 
0141 #include "moc_combinedincidenceeditor.cpp"