File indexing completed on 2024-05-12 05:14:51

0001 /*
0002  *  messagedisplay.h  -  base class to display an alarm or error message
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2001-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "messagedisplayhelper.h"
0012 #include "kalarmcalendar/kaevent.h"
0013 
0014 class DeferAlarmDlg;
0015 class EventId;
0016 class MessageDisplayHelper;
0017 
0018 using namespace KAlarmCal;
0019 
0020 /**
0021  * Abstract base class for alarm message display.
0022  */
0023 class MessageDisplay
0024 {
0025 public:
0026     /** Flags for constructor. */
0027     enum
0028     {
0029         NoReschedule     = 0x01,    // don't reschedule the event once it has displayed
0030         NoDefer          = 0x02,    // don't display an option to defer
0031         NoRecordCmdError = 0x04,    // don't record error executing command
0032         AlwaysHide       = 0x08,    // never show the window (e.g. for audio-only alarms)
0033         NoInitView       = 0x10     // for internal MessageDisplayHelper use only
0034     };
0035 
0036     /** Create a MessageDisplay alarm message instance.
0037      *  The instance type is dependent on the event->notify() value.
0038      */
0039     static MessageDisplay* create(const KAEvent& event, const KAAlarm& alarm, int flags);
0040 
0041     /** Show an error message about the execution of an alarm.
0042      *  The instance type is dependent on the event.notify() value.
0043      *  @param event          The event for the alarm.
0044      *  @param alarmDateTime  Date/time displayed in the message display.
0045      *  @param errmsgs        The error messages to display.
0046      *  @param dontShowAgain  The "don't show again" ID of the error message.
0047      */
0048     static void showError(const KAEvent& event, const DateTime& alarmDateTime,
0049                           const QStringList& errmsgs, const QString& dontShowAgain = QString());
0050 
0051     MessageDisplay(const MessageDisplay&) = delete;
0052     MessageDisplay& operator=(const MessageDisplay&) = delete;
0053 
0054     virtual ~MessageDisplay();
0055 
0056     bool isValid() const   { return mHelper->isValid(); }
0057     KAAlarm::Type alarmType() const   { return mHelper->mAlarmType; }
0058 
0059     /** Raise the alarm display, re-output any required audio notification, and
0060      *  reschedule the alarm in the calendar file.
0061      */
0062     virtual void repeat(const KAAlarm&) = 0;
0063 
0064     virtual bool hasDefer() const = 0;
0065     virtual void showDefer() = 0;
0066     virtual void showDateTime(const KAEvent&, const KAAlarm&)  {}
0067 
0068     /** Convert a reminder display into a normal alarm display. */
0069     virtual void cancelReminder(const KAEvent& event, const KAAlarm& alarm)
0070     {
0071         mHelper->cancelReminder(event, alarm);
0072     }
0073 
0074     /** Returns the existing message display (if any) which is showing the event with
0075      *  the specified ID.
0076      */
0077     static MessageDisplay* findEvent(const EventId& eventId, MessageDisplay* exclude = nullptr)
0078     { return MessageDisplayHelper::findEvent(eventId, exclude); }
0079 
0080     /** Redisplay alarms which were being shown when the program last exited. */
0081     static void redisplayAlarms();
0082 
0083     /** Retrieve the event with the current ID from the displaying calendar file,
0084      *  or if not found there, from the archive calendar.
0085      *  @param resource  Is set to the resource which originally contained the
0086      *                   event, or invalid if not known.
0087      */
0088     static bool retrieveEvent(const EventId&, KAEvent&, Resource&, bool& showEdit, bool& showDefer);
0089 
0090     void        playAudio()          { mHelper->playAudio(); }
0091     static void stopAudio()          { MessageDisplayHelper::stopAudio(); }
0092     static bool isAudioPlaying()     { return MessageDisplayHelper::isAudioPlaying(); }
0093 
0094     /** Called when the edit alarm dialog has been cancelled. */
0095     virtual void editDlgCancelled()  {}
0096 
0097     /** For use by MessageDisplayHelper.
0098      *  Returns the widget to act as parent for error messages, etc. */
0099     virtual QWidget* displayParent() = 0;
0100 
0101     /** For use by MessageDisplayHelper only. Close the alarm message display. */
0102     virtual void closeDisplay() = 0;
0103 
0104     /** Show the alarm message display. */
0105     virtual void showDisplay() = 0;
0106 
0107     /** For use by MessageDisplayHelper only. Raise the alarm message display. */
0108     virtual void raiseDisplay() = 0;
0109 
0110     static int instanceCount(bool excludeAlwaysHidden = false)    { return MessageDisplayHelper::instanceCount(excludeAlwaysHidden); }
0111 
0112     // Holds data required by defer dialog.
0113     // This is needed because the display may have closed when the defer dialog
0114     // is opened.
0115     struct DeferDlgData
0116     {
0117         DeferAlarmDlg*    dlg;
0118         QPointer<QObject> displayObj {nullptr};
0119         MessageDisplay*   display;
0120         EventId           eventId;
0121         KAAlarm::Type     alarmType;
0122         KAEvent::CmdErr   commandError;
0123 
0124         DeferDlgData(MessageDisplay* m, DeferAlarmDlg* d) : dlg(d), display(m) {}
0125         ~DeferDlgData();
0126     };
0127 
0128     /** For use by MainWindow only. Called when a defer alarm dialog completes. */
0129     static void processDeferDlg(DeferDlgData*, int result);
0130 
0131 protected:
0132     MessageDisplay();
0133     MessageDisplay(const KAEvent& event, const KAAlarm& alarm, int flags);
0134     MessageDisplay(const KAEvent& event, const DateTime& alarmDateTime,
0135                    const QStringList& errmsgs, const QString& dontShowAgain);
0136 
0137     explicit MessageDisplay(MessageDisplayHelper* helper);
0138 
0139     /** Called by MessageDisplayHelper to confirm that the alarm message should be
0140      *  acknowledged (closed).
0141      *  @return  true to close the alarm message, false to keep it open.
0142      */
0143     virtual bool confirmAcknowledgement()  { return true; }
0144 
0145     /** Set up the alarm message display. */
0146     virtual void setUpDisplay() = 0;
0147 
0148     void displayMainWindow();
0149 
0150     virtual bool isDeferButtonEnabled() const = 0;
0151     virtual void enableDeferButton(bool enable) = 0;
0152     virtual void enableEditButton(bool enable) = 0;
0153 
0154     DeferDlgData* createDeferDlg(QObject* thisObject, bool displayClosing);
0155     static void   executeDeferDlg(DeferDlgData*);
0156 
0157     MessageDisplayHelper* mHelper;
0158 
0159     // Access to MessageDisplayHelper data.
0160     KAAlarm::Type&      mAlarmType()             { return mHelper->mAlarmType; }
0161     KAEvent::SubAction  mAction() const          { return mHelper->mAction; }
0162     const EventId&      mEventId() const         { return mHelper->mEventId; }
0163     const KAEvent&      mEvent() const           { return mHelper->mEvent; }
0164     const KAEvent&      mOriginalEvent() const   { return mHelper->mOriginalEvent; }
0165     const QFont&        mFont() const            { return mHelper->mFont; }
0166     const QColor&       mBgColour() const        { return mHelper->mBgColour; }
0167     const QColor&       mFgColour() const        { return mHelper->mFgColour; }
0168     const QString&      mAudioFile() const       { return mHelper->mAudioFile; }
0169     float               mVolume() const          { return mHelper->mVolume; }
0170     float               mFadeVolume() const      { return mHelper->mFadeVolume; }
0171     int                 mDefaultDeferMinutes() const  { return mHelper->mDefaultDeferMinutes; }
0172     KAEvent::EmailId    mEmailId() const         { return mHelper->mEmailId; }
0173     KAEvent::CmdErr     mCommandError() const    { return mHelper->mCommandError; }
0174     bool&               mNoPostAction()          { return mHelper->mNoPostAction; }
0175 
0176     const DateTime&     mDateTime() const        { return mHelper->mDateTime; }
0177     EditAlarmDlg*       mEditDlg() const         { return mHelper->mEditDlg; }
0178     bool                mIsValid() const         { return !mHelper->mInvalid; }
0179     bool                mDisableDeferral() const { return mHelper->mDisableDeferral; }
0180     bool                mNoCloseConfirm() const  { return mHelper->mNoCloseConfirm; }
0181     bool                mAlwaysHidden() const    { return mHelper->mAlwaysHide; }
0182     bool                mErrorWindow() const     { return mHelper->mErrorWindow; }
0183     const QStringList&  mErrorMsgs() const       { return mHelper->mErrorMsgs; }
0184     bool&               mNoDefer()               { return mHelper->mNoDefer; }
0185     bool                mShowEdit() const        { return mHelper->mShowEdit; }
0186     const QString&      mDontShowAgain() const   { return mHelper->mDontShowAgain; }
0187 
0188 private:
0189     static bool reinstateFromDisplaying(const KCalendarCore::Event::Ptr&, KAEvent&, Resource&, bool& showEdit, bool& showDefer);
0190 
0191     static bool     mRedisplayed;   // redisplayAlarms() was called
0192 
0193 friend class MessageDisplayHelper;
0194 };
0195 
0196 // vim: et sw=4: