File indexing completed on 2024-11-24 04:42:28

0001 /*
0002  *  calendarupdater.cpp  -  base class to update a calendar to current KAlarm format
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2020 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "calendarupdater.h"
0010 
0011 #include "lib/desktop.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QCoreApplication>
0016 #include <QThread>
0017 
0018 
0019 /*=============================================================================
0020 = Class to prompt the user to update the storage format for a resource, if it
0021 = currently uses an old KAlarm storage format.
0022 =============================================================================*/
0023 
0024 QList<CalendarUpdater*> CalendarUpdater::mInstances;
0025 
0026 CalendarUpdater::CalendarUpdater(ResourceId resourceId, bool ignoreKeepFormat, QObject* parent, QWidget* promptParent)
0027     : QObject(parent)
0028     , mResourceId(resourceId)
0029     , mParent(parent)
0030     , mPromptParent(promptParent ? promptParent : Desktop::mainWindow())
0031     , mIgnoreKeepFormat(ignoreKeepFormat)
0032     , mDuplicate(containsResource(resourceId))
0033 {
0034     mInstances.append(this);
0035 }
0036 
0037 CalendarUpdater::~CalendarUpdater()
0038 {
0039     mInstances.removeAll(this);
0040 }
0041 
0042 bool CalendarUpdater::containsResource(ResourceId id)
0043 {
0044     for (CalendarUpdater* instance : std::as_const(mInstances))
0045     {
0046         if (instance->mResourceId == id)
0047             return true;
0048     }
0049     return false;
0050 }
0051 
0052 /******************************************************************************
0053 * Wait until all instances have completed and been deleted.
0054 */
0055 void CalendarUpdater::waitForCompletion()
0056 {
0057     while (!mInstances.isEmpty())
0058     {
0059         for (int i = mInstances.count();  --i >= 0;  )
0060             if (mInstances.at(i)->isComplete())
0061                 delete mInstances.at(i);    // the destructor removes the instance from mInstances
0062 
0063         QCoreApplication::processEvents();
0064         if (!mInstances.isEmpty())     //cppcheck-suppress knownConditionTrueFalse; 'delete' removes from mInstances
0065             QThread::msleep(100);
0066     }
0067 }
0068 
0069 /******************************************************************************
0070 * Mark the instance as completed, and schedule its deletion.
0071 */
0072 void CalendarUpdater::setCompleted()
0073 {
0074     mCompleted = true;
0075     deleteLater();
0076 }
0077 
0078 /******************************************************************************
0079 * Return a prompt string to ask the user whether to convert the calendar to the
0080 * current format.
0081 */
0082 QString CalendarUpdater::conversionPrompt(const QString& calendarName, const QString& calendarVersion, bool whole)
0083 {
0084     const QString msg = whole
0085                 ? xi18nc("@info", "Calendar <resource>%1</resource> is in an old format (<application>KAlarm</application> version %2), "
0086                         "and will be read-only unless you choose to update it to the current format.",
0087                         calendarName, calendarVersion)
0088                 : xi18nc("@info", "Some or all of the alarms in calendar <resource>%1</resource> are in an old <application>KAlarm</application> format, "
0089                         "and will be read-only unless you choose to update them to the current format.",
0090                         calendarName);
0091     return xi18nc("@info", "<para>%1</para><para>"
0092                  "<warning>Do not update the calendar if it is also used with an older version of <application>KAlarm</application> "
0093                  "(e.g. on another computer). If you do so, the calendar may become unusable there.</warning></para>"
0094                  "<para>Do you wish to update the calendar?</para>", msg);
0095 }
0096 
0097 #include "moc_calendarupdater.cpp"
0098 
0099 // vim: et sw=4: