File indexing completed on 2024-04-21 03:52:43

0001 /*
0002   This file is part of the kcalcore library.
0003 
0004   SPDX-FileCopyrightText: 2001-2003 Cornelius Schumacher <schumacher@kde.org>
0005   SPDX-FileCopyrightText: 2003 David Jarvie <djarvie@kde.org>
0006   SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0007 
0008   SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 /**
0011   @file
0012   This file is part of the API for handling calendar data and
0013   defines the Alarm class.
0014 
0015   @author Cornelius Schumacher \<schumacher@kde.org\>
0016 */
0017 
0018 #ifndef KCALCORE_ALARM_H
0019 #define KCALCORE_ALARM_H
0020 
0021 #include "customproperties.h"
0022 #include "duration.h"
0023 #include "kcalendarcore_export.h"
0024 #include "person.h"
0025 
0026 #include <QDataStream>
0027 #include <QDateTime>
0028 #include <QList>
0029 #include <QMetaType>
0030 #include <QSharedPointer>
0031 #include <QString>
0032 #include <QStringList>
0033 
0034 class QTimeZone;
0035 
0036 namespace KCalendarCore
0037 {
0038 class Incidence;
0039 
0040 /**
0041   @brief
0042   Represents an alarm notification.
0043 
0044   Alarms are user notifications that occur at specified times.
0045   Notifications can be on-screen pop-up dialogs, email messages,
0046   the playing of audio files, or the running of another program.
0047 
0048   Alarms always belong to a parent Incidence.
0049 */
0050 class KCALENDARCORE_EXPORT Alarm : public CustomProperties
0051 {
0052 public:
0053     /**
0054       The different types of alarms.
0055     */
0056     enum Type {
0057         Invalid, /**< Invalid, or no alarm */
0058         Display, /**< Display a dialog box */
0059         Procedure, /**< Call a script */
0060         Email, /**< Send email */
0061         Audio, /**< Play an audio file */
0062     };
0063 
0064     /**
0065       A shared pointer to an Alarm object.
0066     */
0067     typedef QSharedPointer<Alarm> Ptr;
0068 
0069     /**
0070       List of alarms.
0071     */
0072     typedef QList<Ptr> List;
0073 
0074     /**
0075       Constructs an alarm belonging to the @p parent Incidence.
0076 
0077       @param parent is the Incidence this alarm will belong to.
0078     */
0079     // Can't find a way to use a shared pointer here.
0080     // Inside incidence.cpp, it does alarm->setParent( this )
0081     explicit Alarm(Incidence *parent);
0082 
0083     /**
0084       Copy constructor.
0085       @param other is the alarm to copy.
0086     */
0087     Alarm(const Alarm &other);
0088 
0089     /**
0090       Destroys the alarm.
0091     */
0092     ~Alarm() override;
0093 
0094     /**
0095       Copy operator.
0096     */
0097     Alarm &operator=(const Alarm &);
0098 
0099     /**
0100       Compares two alarms for equality.
0101       @param a is the comparison alarm.
0102     */
0103     bool operator==(const Alarm &a) const;
0104 
0105     /**
0106       Compares two alarms for inequality.
0107 
0108       @param a is the comparison alarm.
0109     */
0110     bool operator!=(const Alarm &a) const;
0111 
0112     /**
0113       Sets the @p parent Incidence of the alarm.
0114 
0115       @param parent is alarm parent Incidence to set.
0116 
0117       @see parentUid()
0118     */
0119     // Is there a way to use QSharedPointer here?
0120     // although it's safe, Incidence's dtor calls setParent( 0 )
0121     // se we don't dereference a deleted pointer here.
0122     // Also, I renamed "Incidence *parent()" to "QString parentUid()"
0123     // So we don't return raw pointers
0124     void setParent(Incidence *parent);
0125 
0126     /**
0127       Returns the parent's incidence UID of the alarm.
0128 
0129       @see setParent()
0130     */
0131     // We don't have a share pointer to return, so return the UID.
0132     Q_REQUIRED_RESULT QString parentUid() const;
0133 
0134     /**
0135       Sets the #Type for this alarm to @p type.
0136       If the specified type is different from the current type of the alarm,
0137       then the alarm's type-specific properties are re-initialized.
0138 
0139       @param type is the alarm #Type to set.
0140 
0141       @see type()
0142     */
0143     void setType(Type type);
0144 
0145     /**
0146       Returns the #Type of the alarm.
0147 
0148       @see setType()
0149     */
0150     Q_REQUIRED_RESULT Type type() const;
0151 
0152     /**
0153       Sets the #Display type for this alarm.
0154       If @p text is specified non-empty, then it is used as the description
0155       text to display when the alarm is triggered.
0156 
0157       @param text is the description to display when the alarm is triggered.
0158 
0159       @see setText(), text()
0160     */
0161     void setDisplayAlarm(const QString &text = QString());
0162 
0163     /**
0164       Sets the description @p text to be displayed when the alarm is triggered.
0165       Ignored if the alarm is not a display alarm.
0166 
0167       @param text is the description to display when the alarm is triggered.
0168 
0169       @see setDisplayAlarm(), text()
0170     */
0171     void setText(const QString &text);
0172 
0173     /**
0174       Returns the display text string for a #Display alarm type.
0175       Returns an empty string if the alarm is not a #Display type.
0176 
0177       @see setDisplayAlarm(), setText()
0178     */
0179     Q_REQUIRED_RESULT QString text() const;
0180 
0181     /**
0182       Sets the #Audio type for this alarm and the name of the audio file
0183       to play when the alarm is triggered.
0184 
0185       @param audioFile is the name of the audio file to play when the alarm
0186       is triggered.
0187 
0188       @see setAudioFile(), audioFile()
0189     */
0190     void setAudioAlarm(const QString &audioFile = QString());
0191 
0192     /**
0193       Sets the name of the audio file to play when the audio alarm is triggered.
0194       Ignored if the alarm is not an #Audio type.
0195 
0196       @param audioFile is the name of the audio file to play when the alarm
0197       is triggered.
0198 
0199       @see setAudioAlarm(), audioFile()
0200     */
0201     void setAudioFile(const QString &audioFile);
0202 
0203     /**
0204       Returns the audio file name for an #Audio alarm type.
0205       Returns an empty string if the alarm is not an #Audio type.
0206 
0207       @see setAudioAlarm(), setAudioFile()
0208     */
0209     Q_REQUIRED_RESULT QString audioFile() const;
0210 
0211     /**
0212       Sets the #Procedure type for this alarm and the program (with arguments)
0213       to execute when the alarm is triggered.
0214 
0215       @param programFile is the name of the program file to execute when
0216       the alarm is triggered.
0217       @param arguments is a string of arguments to supply to @p programFile.
0218 
0219       @see setProgramFile(), programFile(),
0220       setProgramArguments(), programArguments()
0221     */
0222     void setProcedureAlarm(const QString &programFile, const QString &arguments = QString());
0223 
0224     /**
0225       Sets the program file to execute when the alarm is triggered.
0226       Ignored if the alarm is not a #Procedure type.
0227 
0228       @param programFile is the name of the program file to execute when
0229       the alarm is triggered.
0230 
0231       @see setProcedureAlarm(), programFile(),
0232       setProgramArguments(), programArguments()
0233     */
0234     void setProgramFile(const QString &programFile);
0235 
0236     /**
0237       Returns the program file name for a #Procedure alarm type.
0238       Returns an empty string if the alarm is not a #Procedure type.
0239 
0240       @see setProcedureAlarm(), setProgramFile(),
0241       setProgramArguments(), programArguments()
0242     */
0243     Q_REQUIRED_RESULT QString programFile() const;
0244 
0245     /**
0246       Sets the program arguments string when the alarm is triggered.
0247       Ignored if the alarm is not a #Procedure type.
0248 
0249       @param arguments is a string of arguments to supply to the program.
0250 
0251       @see setProcedureAlarm(), setProgramFile(), programFile(),
0252       programArguments()
0253     */
0254     void setProgramArguments(const QString &arguments);
0255 
0256     /**
0257       Returns the program arguments string for a #Procedure alarm type.
0258       Returns an empty string if the alarm is not a #Procedure type.
0259 
0260       @see setProcedureAlarm(), setProgramFile(), programFile(),
0261       setProgramArguments()
0262     */
0263     Q_REQUIRED_RESULT QString programArguments() const;
0264 
0265     /**
0266       Sets the #Email type for this alarm and the email @p subject, @p text,
0267       @p addresses, and @p attachments that make up an email message to be
0268       sent when the alarm is triggered.
0269 
0270       @param subject is the email subject.
0271       @param text is a string containing the body of the email message.
0272       @param addressees is Person list of email addresses.
0273       @param attachments is a a QStringList of optional file names
0274       of email attachments.
0275 
0276       @see setMailSubject(), setMailText(), setMailAddresses(),
0277       setMailAttachments()
0278     */
0279     void setEmailAlarm(const QString &subject, const QString &text, const Person::List &addressees, const QStringList &attachments = QStringList());
0280 
0281     /**
0282       Sets the email address of an #Email type alarm.
0283       Ignored if the alarm is not an #Email type.
0284 
0285       @param mailAlarmAddress is a Person to receive a mail message when
0286       an #Email type alarm is triggered.
0287 
0288       @see setMailSubject(), setMailText(), setMailAddresses(),
0289       setMailAttachment(), setMailAttachments(), mailAddresses()
0290     */
0291     void setMailAddress(const Person &mailAlarmAddress);
0292 
0293     /**
0294       Sets a list of email addresses of an #Email type alarm.
0295       Ignored if the alarm is not an #Email type.
0296 
0297       @param mailAlarmAddresses is a Person list to receive a mail message
0298       when an #Email type alarm is triggered.
0299 
0300       @see setMailSubject(), setMailText(), setMailAddress(),
0301       setMailAttachments(), setMailAttachment(), mailAddresses()
0302     */
0303     void setMailAddresses(const Person::List &mailAlarmAddresses);
0304 
0305     /**
0306       Adds an address to the list of email addresses to send mail to when the
0307       alarm is triggered.
0308       Ignored if the alarm is not an #Email type.
0309 
0310       @param mailAlarmAddress is a Person to add to the list of addresses to
0311       receive a mail message when an #Email type alarm is triggered.
0312 
0313       @see setMailAddress(), setMailAddresses(), mailAddresses()
0314     */
0315     void addMailAddress(const Person &mailAlarmAddress);
0316 
0317     /**
0318       Returns the list of addresses for an #Email alarm type.
0319       Returns an empty list if the alarm is not an #Email type.
0320 
0321       @see addMailAddress(), setMailAddress(), setMailAddresses()
0322     */
0323     Q_REQUIRED_RESULT Person::List mailAddresses() const;
0324 
0325     /**
0326       Sets the subject line of a mail message for an #Email alarm type.
0327       Ignored if the alarm is not an #Email type.
0328 
0329       @param mailAlarmSubject is a string to be used as a subject line
0330       of an email message to send when the #Email type alarm is triggered.
0331 
0332       @see setMailText(), setMailAddress(), setMailAddresses(),
0333       setMailAttachment(), setMailAttachments(), mailSubject()
0334     */
0335     void setMailSubject(const QString &mailAlarmSubject);
0336 
0337     /**
0338       Returns the subject line string for an #Email alarm type.
0339       Returns an empty string if the alarm is not an #Email type.
0340 
0341       @see setMailSubject()
0342     */
0343     Q_REQUIRED_RESULT QString mailSubject() const;
0344 
0345     /**
0346       Sets the filename to attach to a mail message for an #Email alarm type.
0347       Ignored if the alarm is not an #Email type.
0348 
0349       @param mailAttachFile is a string containing a filename to be attached
0350       to an email message to send when the #Email type alarm is triggered.
0351 
0352       @see setMailSubject(), setMailText(), setMailAddress(),
0353       setMailAddresses(), setMailAttachments(), mailAttachments()
0354     */
0355     void setMailAttachment(const QString &mailAttachFile);
0356 
0357     /**
0358       Sets a list of filenames to attach to a mail message for an #Email
0359       alarm type. Ignored if the alarm is not an #Email type.
0360 
0361       @param mailAttachFiles is a QString list of filenames to attach to
0362       a mail message when an #Email type alarm is triggered.
0363 
0364       @see setMailSubject(), setMailText(), setMailAttachment(),
0365       setMailAddress(), setMailAddresses()
0366     */
0367     void setMailAttachments(const QStringList &mailAttachFiles);
0368 
0369     /**
0370       Adds a filename to the list of files to attach to a mail message for
0371       an #Email alarm type. Ignored if the alarm is not an #Email type.
0372 
0373       @param mailAttachFile is a string containing a filename to be attached
0374       to an email message to send when the #Email type alarm is triggered.
0375 
0376       @see setMailAttachment(), setMailAttachments(), mailAttachments()
0377     */
0378     void addMailAttachment(const QString &mailAttachFile);
0379 
0380     /**
0381       Returns the list of attachment filenames for an #Email alarm type.
0382       Returns an empty list if the alarm is not an #Email type.
0383 
0384       @see addMailAttachment(), setMailAttachment(), setMailAttachments()
0385     */
0386     Q_REQUIRED_RESULT QStringList mailAttachments() const;
0387 
0388     /**
0389       Sets the body text for an #Email alarm type.
0390       Ignored if the alarm is not an #Email type.
0391 
0392       @param text is a string containing the body text of a mail message
0393       when an #Email type alarm is triggered.
0394 
0395       @see setMailSubject(), setMailAddress(), setMailAddresses(),
0396       setMailAttachment(), setMailAttachments()
0397     */
0398     void setMailText(const QString &text);
0399 
0400     /**
0401       Returns the body text for an #Email alarm type.
0402       Returns an empty string if the alarm is not an #Email type.
0403 
0404       @see setMailText()
0405     */
0406     Q_REQUIRED_RESULT QString mailText() const;
0407 
0408     /**
0409       Sets the trigger time of the alarm.
0410 
0411       @param alarmTime is the QDateTime alarm trigger.
0412 
0413       @see time()
0414     */
0415     void setTime(const QDateTime &alarmTime);
0416 
0417     /**
0418       Returns the alarm trigger date/time.
0419 
0420       @see setTime()
0421     */
0422     Q_REQUIRED_RESULT QDateTime time() const;
0423 
0424     /**
0425       Returns the next alarm trigger date/time after given date/time.
0426       Takes recurrent incidences into account.
0427 
0428       @param preTime date/time from where to start
0429       @param ignoreRepetitions don't take repetitions into account
0430       @see nextRepetition()
0431     */
0432     Q_REQUIRED_RESULT QDateTime nextTime(const QDateTime &preTime, bool ignoreRepetitions = false) const;
0433 
0434     /**
0435       Returns the date/time when the last repetition of the alarm goes off.
0436       If the alarm does not repeat this is equivalent to calling time().
0437 
0438       @see setTime()
0439     */
0440     Q_REQUIRED_RESULT QDateTime endTime() const;
0441 
0442     /**
0443       Returns true if the alarm has a trigger date/time.
0444     */
0445     Q_REQUIRED_RESULT bool hasTime() const;
0446 
0447     /**
0448       Sets the alarm offset relative to the start of the parent Incidence.
0449 
0450       @param offset is a Duration to be used as a time relative to the
0451       start of the parent Incidence to be used as the alarm trigger.
0452 
0453       @see setEndOffset(), startOffset(), endOffset()
0454     */
0455     void setStartOffset(const Duration &offset);
0456 
0457     /**
0458       Returns offset of alarm in time relative to the start of the parent
0459       Incidence.  If the alarm's time is not defined in terms of an offset
0460       relative  to the start of the event, returns zero.
0461 
0462       @see setStartOffset(), hasStartOffset()
0463     */
0464     Q_REQUIRED_RESULT Duration startOffset() const;
0465 
0466     /**
0467       Returns whether the alarm is defined in terms of an offset relative
0468       to the start of the parent Incidence.
0469 
0470       @see startOffset(), setStartOffset()
0471     */
0472     bool hasStartOffset() const;
0473 
0474     /**
0475       Sets the alarm offset relative to the end of the parent Incidence.
0476 
0477       @param offset is a Duration to be used as a time relative to the
0478       end of the parent Incidence to be used as the alarm trigger.
0479 
0480       @see setStartOffset(), startOffset(), endOffset()
0481     */
0482     void setEndOffset(const Duration &offset);
0483 
0484     /**
0485       Returns offset of alarm in time relative to the end of the event.
0486       If the alarm's time is not defined in terms of an offset relative
0487       to the end of the event, returns zero.
0488 
0489       @see setEndOffset(), hasEndOffset()
0490     */
0491     Q_REQUIRED_RESULT Duration endOffset() const;
0492 
0493     /**
0494       Returns whether the alarm is defined in terms of an offset relative
0495       to the end of the event.
0496 
0497       @see endOffset(), setEndOffset()
0498     */
0499     bool hasEndOffset() const;
0500 
0501     /**
0502       Shift the times of the alarm so that they appear at the same clock
0503       time as before but in a new time zone. The shift is done from a viewing
0504       time zone rather than from the actual alarm time zone.
0505 
0506       For example, shifting an alarm whose start time is 09:00 America/New York,
0507       using an old viewing time zone (@p oldZone) of Europe/London, to a new
0508       time zone (@p newZone) of Europe/Paris, will result in the time being
0509       shifted from 14:00 (which is the London time of the alarm start) to
0510       14:00 Paris time.
0511 
0512       @param oldZone the time zone which provides the clock times
0513       @param newZone the new time zone
0514     */
0515     void shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone);
0516 
0517     /**
0518       Sets the snooze time interval for the alarm.
0519 
0520       @param alarmSnoozeTime the time between snoozes.
0521 
0522       @see snoozeTime()
0523     */
0524     void setSnoozeTime(const Duration &alarmSnoozeTime);
0525 
0526     /**
0527       Returns the snooze time interval.
0528 
0529       @see setSnoozeTime()
0530     */
0531     Q_REQUIRED_RESULT Duration snoozeTime() const;
0532 
0533     /**
0534       Sets how many times an alarm is to repeat itself after its initial
0535       occurrence (w/snoozes).
0536 
0537       @param alarmRepeatCount is the number of times an alarm may repeat,
0538       excluding the initial occurrence.
0539 
0540       @see repeatCount()
0541     */
0542     void setRepeatCount(int alarmRepeatCount);
0543 
0544     /**
0545       Returns how many times an alarm may repeats after its initial occurrence.
0546 
0547       @see setRepeatCount()
0548     */
0549     Q_REQUIRED_RESULT int repeatCount() const;
0550 
0551     /**
0552       Returns the date/time of the alarm's initial occurrence or its next
0553       repetition after a given time.
0554 
0555       @param preTime the date/time after which to find the next repetition.
0556 
0557       @return the date/time of the next repetition, or an invalid date/time
0558       if the specified time is at or after the alarm's last repetition.
0559 
0560       @see previousRepetition()
0561     */
0562     Q_REQUIRED_RESULT QDateTime nextRepetition(const QDateTime &preTime) const;
0563 
0564     /**
0565       Returns the date/time of the alarm's latest repetition or, if none,
0566       its initial occurrence before a given time.
0567 
0568       @param afterTime is the date/time before which to find the latest
0569       repetition.
0570 
0571       @return the date and time of the latest repetition, or an invalid
0572       date/time if the specified time is at or before the alarm's initial
0573       occurrence.
0574 
0575       @see nextRepetition()
0576     */
0577     Q_REQUIRED_RESULT QDateTime previousRepetition(const QDateTime &afterTime) const;
0578 
0579     /**
0580       Returns the interval between the alarm's initial occurrence and
0581       its final repetition.
0582     */
0583     Q_REQUIRED_RESULT Duration duration() const;
0584 
0585     /**
0586       Toggles the alarm status, i.e, an enable alarm becomes disabled
0587       and a disabled alarm becomes enabled.
0588 
0589       @see enabled(), setEnabled()
0590     */
0591     void toggleAlarm();
0592 
0593     /**
0594       Sets the enabled status of the alarm.
0595       @param enable if true, then enable the alarm; else disable the alarm.
0596 
0597       @see enabled(), toggleAlarm()
0598     */
0599     void setEnabled(bool enable);
0600 
0601     /**
0602       Returns the alarm enabled status: true (enabled) or false (disabled).
0603 
0604       @see setEnabled(), toggleAlarm()
0605     */
0606     Q_REQUIRED_RESULT bool enabled() const;
0607 
0608     /**
0609       Set if the location radius for the alarm has been defined.
0610       @param hasLocationRadius if true, then this alarm has a location radius.
0611 
0612       @see setLocationRadius()
0613     */
0614     void setHasLocationRadius(bool hasLocationRadius);
0615 
0616     /**
0617       Returns true if alarm has location radius defined.
0618 
0619       @see setLocationRadius()
0620     */
0621     Q_REQUIRED_RESULT bool hasLocationRadius() const;
0622 
0623     /**
0624       Set location radius for the alarm. This means that alarm will be
0625       triggered when user approaches the location. Given value will be
0626       stored into custom properties as X-LOCATION-RADIUS.
0627 
0628       @param locationRadius radius in meters
0629       @see locationRadius()
0630     */
0631     void setLocationRadius(int locationRadius);
0632 
0633     /**
0634       Returns the location radius in meters.
0635 
0636       @see setLocationRadius()
0637     */
0638     Q_REQUIRED_RESULT int locationRadius() const;
0639 
0640 protected:
0641     /**
0642       @copydoc
0643       CustomProperties::customPropertyUpdated()
0644     */
0645     void customPropertyUpdated() override;
0646 
0647     /**
0648       @copydoc
0649       IncidenceBase::virtual_hook()
0650     */
0651     virtual void virtual_hook(int id, void *data);
0652 
0653 private:
0654     //@cond PRIVATE
0655     class Private;
0656     Private *const d;
0657     //@endcond
0658     friend KCALENDARCORE_EXPORT QDataStream &operator<<(QDataStream &s, const KCalendarCore::Alarm::Ptr &);
0659     friend KCALENDARCORE_EXPORT QDataStream &operator>>(QDataStream &s, const KCalendarCore::Alarm::Ptr &);
0660 };
0661 /**
0662  * Alarm serializer.
0663  *
0664  * @since 4.12
0665  */
0666 KCALENDARCORE_EXPORT QDataStream &operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &);
0667 
0668 /**
0669  * Alarm deserializer.
0670  *
0671  * @since 4.12
0672  */
0673 KCALENDARCORE_EXPORT QDataStream &operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &);
0674 
0675 }
0676 
0677 //@cond PRIVATE
0678 Q_DECLARE_TYPEINFO(KCalendarCore::Alarm::Ptr, Q_RELOCATABLE_TYPE);
0679 Q_DECLARE_METATYPE(KCalendarCore::Alarm::Ptr)
0680 //@endcond
0681 
0682 #endif