Commit 5ca740eb authored by David Jarvie's avatar David Jarvie
Browse files

If command line action fails, quit application if not previously running

parent a8240866
Pipeline #51567 passed with stage
in 9 minutes and 38 seconds
KAlarm Change Log
=== Version 3.1.3 (KDE Applications 20.12.3) --- 18 February 2021 ===
=== Version 3.1.3 (KDE Applications 20.12.3) --- 19 February 2021 ===
* Fix error creating alarm from command line if the only active alarm calendar is not the default [KDE Bug 417108].
* If command line action fails, quit application if not previously running.
=== Version 3.1.2 (KDE Applications 20.12.2) --- 26 January 2021 ===
* Default to using alarm names.
......
......@@ -240,8 +240,6 @@ UpdateResult addEvent(KAEvent& event, Resource& resource, QWidget* msgParent, in
rc_options |= ResourcesCalendar::UseEventId;
if (options & NO_RESOURCE_PROMPT)
rc_options |= ResourcesCalendar::NoResourcePrompt;
if (options & USE_ONLY_RESOURCE)
rc_options |= ResourcesCalendar::UseOnlyResource;
if (!ResourcesCalendar::addEvent(event, resource, msgParent, rc_options, &cancelled))
{
status.status = UPDATE_FAILED;
......
......@@ -61,6 +61,10 @@ struct UpdateResult
UpdateResult& operator=(UpdateStatus s) { status = s; message.clear(); return *this; }
bool operator==(UpdateStatus s) const { return status == s; }
bool operator!=(UpdateStatus s) const { return status != s; }
bool operator>(UpdateStatus s) const { return status > s; }
bool operator>=(UpdateStatus s) const { return status >= s; }
bool operator<=(UpdateStatus s) const { return status <= s; }
bool operator<(UpdateStatus s) const { return status < s; }
void set(UpdateStatus s) { operator=(s); }
void set(UpdateStatus s, const QString& m) { status = s; message = m; }
};
......@@ -104,8 +108,7 @@ enum // 'options' parameter values for addEvent(). May be OR'ed together
{
USE_EVENT_ID = 0x01, // use event ID if it's provided
NO_RESOURCE_PROMPT = 0x02, // don't prompt for resource
USE_ONLY_RESOURCE = 0x04, // if there is only one enabled resource, use it as the default
ALLOW_KORG_UPDATE = 0x08 // allow change to be sent to KOrganizer
ALLOW_KORG_UPDATE = 0x04 // allow change to be sent to KOrganizer
};
/** Add a new active (non-archived) alarm to a resource.
......
......@@ -614,7 +614,11 @@ int KAlarmApp::activateInstance(const QStringList& args, const QString& workingD
exitCode = 1;
else
{
if (!scheduleEvent(options->editAction(), options->name(), options->text(),
QueuedAction flags = QueuedAction::CmdLine;
if (!MainWindow::count())
flags = static_cast<QueuedAction>(int(flags) + int(QueuedAction::ErrorExit));
if (!scheduleEvent(flags,
options->editAction(), options->name(), options->text(),
options->alarmTime(), options->lateCancel(), options->flags(),
options->bgColour(), options->fgColour(), QFont(),
options->audioFile(), options->audioVolume(),
......@@ -1019,8 +1023,10 @@ void KAlarmApp::processQueue()
}
// Process the first action in the queue.
const bool findUniqueId = int(entry.action) & int(QueuedAction::FindId);
const bool exitAfter = int(entry.action) & int(QueuedAction::Exit);
const bool findUniqueId = int(entry.action) & int(QueuedAction::FindId);
bool exitAfter = int(entry.action) & int(QueuedAction::Exit);
const bool exitAfterError = int(entry.action) & int(QueuedAction::ErrorExit);
const bool commandLine = int(entry.action) & int(QueuedAction::CmdLine);
const auto action = static_cast<QueuedAction>(int(entry.action) & int(QueuedAction::ActionMask));
bool ok = true;
......@@ -1036,8 +1042,38 @@ void KAlarmApp::processQueue()
break;
case QueuedAction::Handle:
{
Resource resource;
KAlarm::addEvent(entry.event, resource, nullptr, KAlarm::ALLOW_KORG_UPDATE | KAlarm::NO_RESOURCE_PROMPT | KAlarm::USE_ONLY_RESOURCE);
Resource resource = Resources::destination(CalEvent::ACTIVE, nullptr, Resources::NoResourcePrompt | Resources::UseOnlyResource);
if (!resource.isValid())
{
qCWarning(KALARM_LOG) << "KAlarmApp::processQueue: Error! Cannot create alarm (no default calendar is defined)";
#if 0
if (commandLine)
{
const QString errmsg = xi18nc("@info:shell", "Cannot create alarm: No default calendar is defined");
std::cerr << errmsg.toLocal8Bit().data() << std::endl;
}
#endif
ok = false;
}
else
{
const KAlarm::UpdateResult result = KAlarm::addEvent(entry.event, resource, nullptr, KAlarm::ALLOW_KORG_UPDATE | KAlarm::NO_RESOURCE_PROMPT);
if (result >= KAlarm::UPDATE_ERROR)
{
//TODO: display error message for command line action, but first ensure that one is returned by addEvent()!
#if 0
if (commandLine)
{
const QString errmsg = xi18nc("@info:shell", "Error creating alarm");
std::cerr << errmsg.toLocal8Bit().data();
}
#endif
ok = false;
}
}
if (!ok && exitAfterError)
exitAfter = true;
break;
}
case QueuedAction::List:
......@@ -1084,7 +1120,10 @@ void KAlarmApp::processQueue()
if (!result)
inhibit = true;
else if (result < 0 && exitAfter)
{
CommandOptions::printError(xi18nc("@info:shell", "%1: Event <resource>%2</resource> not found, or not unique", mCommandOption, entry.eventId.eventId()));
ok = false;
}
}
}
......@@ -1097,7 +1136,7 @@ void KAlarmApp::processQueue()
else if (exitAfter)
{
mActionQueue.clear(); // ensure that quitIf() actually exits the program
quitIf(ok ? 0 : 1);
quitIf((ok ? 0 : 1), exitAfterError);
return; // quitIf() can sometimes return, despite calling exit()
}
......@@ -1663,7 +1702,8 @@ bool KAlarmApp::needWindowFocusFix() const
* to command line options.
* Reply = true unless there was a parameter error or an error opening calendar file.
*/
bool KAlarmApp::scheduleEvent(KAEvent::SubAction action, const QString& name, const QString& text,
bool KAlarmApp::scheduleEvent(QueuedAction queuedActionFlags,
KAEvent::SubAction action, const QString& name, const QString& text,
const KADateTime& dateTime, int lateCancel, KAEvent::Flags flags,
const QColor& bg, const QColor& fg, const QFont& font,
const QString& audioFile, float audioVolume, int reminderMinutes,
......@@ -1718,7 +1758,8 @@ bool KAlarmApp::scheduleEvent(KAEvent::SubAction action, const QString& name, co
// Queue the alarm for insertion into the calendar file
qCDebug(KALARM_LOG) << "KAlarmApp::scheduleEvent: creating new alarm" << text;
mActionQueue.enqueue(ActionQEntry(event));
const QueuedAction qaction = static_cast<QueuedAction>(int(QueuedAction::Handle) + int(queuedActionFlags));
mActionQueue.enqueue(ActionQEntry(event, qaction));
if (mInitialised)
QTimer::singleShot(0, this, &KAlarmApp::processQueue);
return true;
......
/*
* kalarmapp.h - the KAlarm application object
* Program: kalarm
* SPDX-FileCopyrightText: 2001-2020 David Jarvie <djarvie@kde.org>
* SPDX-FileCopyrightText: 2001-2021 David Jarvie <djarvie@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -93,15 +93,21 @@ public:
bool windowFocusBroken() const;
bool needWindowFocusFix() const;
// Methods called indirectly by the D-Bus interface
bool scheduleEvent(KAEvent::SubAction, const QString& name, const QString& text,
const KADateTime&, int lateCancel, KAEvent::Flags flags,
const QColor& bg, const QColor& fg, const QFont&,
bool scheduleEvent(KAEvent::SubAction subAction, const QString& name, const QString& text,
const KADateTime& dt, int lateCancel, KAEvent::Flags flags,
const QColor& bg, const QColor& fg, const QFont& font,
const QString& audioFile, float audioVolume,
int reminderMinutes, const KARecurrence& recurrence,
const KCalendarCore::Duration& repeatInterval, int repeatCount,
uint mailFromID = 0, const KCalendarCore::Person::List& mailAddresses = KCalendarCore::Person::List(),
const QString& mailSubject = QString(),
const QStringList& mailAttachments = QStringList());
const QStringList& mailAttachments = QStringList())
{ return scheduleEvent(QueuedAction::NoAction,
subAction, name, text, dt, lateCancel, flags,
bg, fg, font, audioFile, audioVolume,
reminderMinutes, recurrence, repeatInterval, repeatCount,
mailFromID, mailAddresses, mailSubject, mailAttachments);
}
bool dbusTriggerEvent(const EventId& eventID) { return dbusHandleEvent(eventID, QueuedAction::Trigger); }
bool dbusDeleteEvent(const EventId& eventID) { return dbusHandleEvent(eventID, QueuedAction::Cancel); }
QString dbusList();
......@@ -156,6 +162,7 @@ private:
enum class QueuedAction
{
// Action to execute
NoAction = 0,
ActionMask = 0x07, // bit mask to extract action to execute
Handle = 0x01, // if the alarm is due, execute it and then reschedule it
Trigger = 0x02, // execute the alarm regardless, and then reschedule it if it's already due
......@@ -164,7 +171,9 @@ private:
List = 0x05, // list all alarms (command line option)
// Modifier flags
FindId = 0x10, // search all resources for unique event ID
Exit = 0x20 // exit application after executing action
Exit = 0x20, // exit application after executing action
ErrorExit = 0x40, // exit application after executing action if it fails
CmdLine = 0x80 // the action is from the command line, so output error messages
};
struct ProcData
{
......@@ -224,6 +233,16 @@ private:
void checkArchivedCalendar();
void queueAlarmId(const KAEvent&);
bool dbusHandleEvent(const EventId&, QueuedAction);
bool scheduleEvent(QueuedAction queuedActionFlags,
KAEvent::SubAction, const QString& name, const QString& text,
const KADateTime&, int lateCancel, KAEvent::Flags flags,
const QColor& bg, const QColor& fg, const QFont&,
const QString& audioFile, float audioVolume,
int reminderMinutes, const KARecurrence& recurrence,
const KCalendarCore::Duration& repeatInterval, int repeatCount,
uint mailFromID = 0, const KCalendarCore::Person::List& mailAddresses = KCalendarCore::Person::List(),
const QString& mailSubject = QString(),
const QStringList& mailAttachments = QStringList());
int handleEvent(const EventId&, QueuedAction, bool findUniqueId = false);
int rescheduleAlarm(KAEvent&, const KAAlarm&, bool updateCalAndDisplay,
const KADateTime& nextDt = KADateTime());
......
......@@ -327,8 +327,6 @@ bool ResourcesCalendar::addEvent(KAEvent& evnt, Resource& resource, QWidget* pro
Resources::DestOptions destOptions {};
if (options & NoResourcePrompt)
destOptions |= Resources::NoResourcePrompt;
if (options & UseOnlyResource)
destOptions |= Resources::UseOnlyResource;
resource = Resources::destination(type, promptParent, destOptions, cancelled);
if (!resource.isValid())
qCWarning(KALARM_LOG) << "ResourcesCalendar::addEvent: Error! Cannot create" << type << "(No default calendar is defined)";
......
......@@ -59,8 +59,7 @@ public:
{
NoOption = 0,
UseEventId = 0x01, // use event ID if it's provided
NoResourcePrompt = 0x02, // don't prompt for resource
UseOnlyResource = 0x04 // if there is only one enabled resource, use it as the default
NoResourcePrompt = 0x02 // don't prompt for resource
};
Q_DECLARE_FLAGS(AddEventOptions, AddEventOption)
......
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