Commit d83ba645 authored by David Jarvie's avatar David Jarvie
Browse files

Add CalendarUpdater::waitForCompletion()

parent a2c43bde
......@@ -151,7 +151,7 @@ bool AkonadiCalendarUpdater::update()
}
}
}
deleteLater();
setCompleted();
return result;
}
......
......@@ -51,6 +51,8 @@ public Q_SLOTS:
/** If the calendar is not in the current KAlarm format, prompt the user
* whether to convert to the current format, and then perform the conversion.
* This method calls deleteLater() on completion.
* @return false if the calendar is not in current format and the user
* chose not to update it; true otherwise.
*/
bool update() override;
......
......@@ -24,13 +24,16 @@
#include <KLocalizedString>
#include <QCoreApplication>
#include <QThread>
/*=============================================================================
= Class to prompt the user to update the storage format for a resource, if it
= currently uses an old KAlarm storage format.
=============================================================================*/
QList<CalendarUpdater*> CalendarUpdater::mInstances;
QVector<CalendarUpdater*> CalendarUpdater::mInstances;
CalendarUpdater::CalendarUpdater(ResourceId resourceId, bool ignoreKeepFormat, QObject* parent, QWidget* promptParent)
: QObject(parent)
......@@ -58,6 +61,32 @@ bool CalendarUpdater::containsResource(ResourceId id)
return false;
}
/******************************************************************************
* Wait until all instances have completed and been deleted.
*/
void CalendarUpdater::waitForCompletion()
{
while (!mInstances.isEmpty())
{
for (int i = mInstances.count(); --i >= 0; )
if (mInstances.at(i)->isComplete())
delete mInstances.at(i); // the destructor removes the instance from mInstances
QCoreApplication::processEvents();
if (!mInstances.isEmpty())
QThread::msleep(100);
}
}
/******************************************************************************
* Mark the instance as completed, and schedule its deletion.
*/
void CalendarUpdater::setCompleted()
{
mCompleted = true;
deleteLater();
}
/******************************************************************************
* Return a prompt string to ask the user whether to convert the calendar to the
* current format.
......
......@@ -32,14 +32,20 @@ public:
CalendarUpdater(ResourceId, bool ignoreKeepFormat, QObject* parent, QWidget* promptParent = nullptr);
virtual ~CalendarUpdater();
// Check whether any instance is for the given resource ID
/** Check whether any instance is for the given resource ID. */
static bool containsResource(ResourceId);
// Return whether another instance is already updating this collection
/** Return whether another instance is already updating this collection. */
bool isDuplicate() const { return mDuplicate; }
/** Return whether this instance has completed, and its deletion is pending. */
bool isComplete() const { return mCompleted; }
static bool pending() { return !mInstances.isEmpty(); }
/** Wait until all completed instances have completed and been deleted. */
static void waitForCompletion();
#if 0
/** If an existing resource calendar can be converted to the current KAlarm
* format, prompt the user whether to convert it, and if yes, tell the resource
......@@ -57,18 +63,24 @@ public Q_SLOTS:
/** If the calendar is not in the current KAlarm format, prompt the user
* whether to convert to the current format, and then perform the conversion.
* This method must call deleteLater() on completion.
* @return false if the calendar is not in current format and the user
* chose not to update it; true otherwise.
*/
virtual bool update() = 0;
protected:
/** Mark the instance as completed, and schedule its deletion. */
void setCompleted();
static QString conversionPrompt(const QString& calendarName, const QString& calendarVersion, bool whole);
static QList<CalendarUpdater*> mInstances;
static QVector<CalendarUpdater*> mInstances;
ResourceId mResourceId;
QObject* mParent;
QWidget* mPromptParent;
const bool mIgnoreKeepFormat;
const bool mDuplicate; // another instance is already updating this resource
const bool mDuplicate; // another instance is already updating this resource
bool mCompleted {false}; // completed, and deleteLater() called
};
#endif // CALENDARUPDATER_H
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment