File indexing completed on 2025-02-16 04:48:37

0001 /*
0002  *  alarmtext.h  -  text/email alarm text conversion
0003  *  This file is part of kalarmcalendar library, which provides access to KAlarm
0004  *  calendar data.
0005  *  Program:  kalarm
0006  *  SPDX-FileCopyrightText: 2004-2022 David Jarvie <djarvie@kde.org>
0007  *
0008  *  SPDX-License-Identifier: LGPL-2.0-or-later
0009  */
0010 
0011 #pragma once
0012 
0013 #include "kalarmcal_export.h"
0014 #include "kaevent.h"
0015 
0016 #include <KCalendarCore/Todo>
0017 #include <QString>
0018 
0019 namespace KAlarmCal
0020 {
0021 
0022 class KAEvent;
0023 
0024 /**
0025  * @short Parses email, todo and script alarm texts.
0026  *
0027  * This class parses email, todo and script texts, enabling drag and drop of
0028  * these items to be recognised and interpreted. It also holds plain alarm
0029  * texts.
0030  *
0031  * - Email texts must contain headers (To, From, etc.) in normal RFC format.
0032  * - Todos should be in iCalendar format.
0033  * - Scripts are assumed if the alarm text starts with '#!'.
0034  *
0035  * @author David Jarvie <djarvie@kde.org>
0036  */
0037 
0038 class KALARMCAL_EXPORT AlarmText
0039 {
0040 public:
0041     /** Constructor which sets the alarm text.
0042      *  If @p text starts with '#!', it is flagged as a script, else plain text.
0043      *  @param text alarm text to set
0044      */
0045     explicit AlarmText(const QString& text = QString());
0046 
0047     AlarmText(const AlarmText& other);
0048     ~AlarmText();
0049     AlarmText& operator=(const AlarmText& other);
0050 
0051     /** Initialise the instance to an empty state. */
0052     void clear();
0053 
0054     /** Set the alarm text.
0055      *  If @p text starts with '#!', it is flagged as a script, else plain text.
0056      *  @param text alarm text to set
0057      */
0058     void setText(const QString& text);
0059 
0060     /** Set the instance contents to be a script.
0061      *  @param text text of script to set
0062      */
0063     void setScript(const QString& text);
0064 
0065     /** Set the instance contents to be an email.
0066      *  @param to       'To' header parameter
0067      *  @param from     'From' header parameter
0068      *  @param cc       'Cc' header parameter
0069      *  @param time     'Date' header parameter
0070      *  @param subject  'Subject' header parameter
0071      *  @param body     email body text
0072      *  @param itemId   Akonadi item ID of the email.
0073      */
0074     void setEmail(const QString& to, const QString& from, const QString& cc, const QString& time,
0075                   const QString& subject, const QString& body, KAEvent::EmailId emailId = -1);
0076 
0077     /** Set the instance contents to be a todo.
0078      *  @param todo Todo instance to set as the text
0079      */
0080     void setTodo(const KCalendarCore::Todo::Ptr& todo);
0081 
0082     /** Return the text for a text message alarm, in display format.
0083      *  - An email is returned as a sequence of headers followed by the message body.
0084      *  - A todo is returned as a subject, location and due date followed by any text.
0085      *  - A script or plain text is returned without interpretation.
0086      */
0087     QString displayText() const;
0088 
0089     /** Return the 'To' header parameter for an email alarm.
0090      *  @return 'from' value, or empty if not an email text.
0091      */
0092     QString to() const;
0093 
0094     /** Return the 'From' header parameter for an email alarm.
0095      *  @return 'from' value, or empty if not an email text.
0096      */
0097     QString from() const;
0098 
0099     /** Return the 'Cc' header parameter for an email alarm.
0100      *  @return 'cc' value, or empty if not an email text.
0101      */
0102     QString cc() const;
0103 
0104     /** Return the 'Date' header parameter for an email alarm.
0105      *  @return 'date' value, or empty if not an email text.
0106      */
0107     QString time() const;
0108 
0109     /** Return the 'Subject' header parameter for an email alarm.
0110      *  @return 'subject' value, or empty if not an email text.
0111      */
0112     QString subject() const;
0113 
0114     /** Return the email message body.
0115      *  @return message body, or empty if not an email text.
0116      */
0117     QString body() const;
0118 
0119     /** Return the summary text for a todo.
0120      *  @return summary text, or empty if not a todo.
0121      */
0122     QString summary() const;
0123 
0124     /** Return the location text for a todo.
0125      *  @return location text, or empty if not a todo.
0126      */
0127     QString location() const;
0128 
0129     /** Return the due date text for a todo.
0130      *  @return due date text, or empty if not a todo.
0131      */
0132     QString due() const;
0133 
0134     /** Return the description text for a todo.
0135      *  @return description text, or empty if not a todo.
0136      */
0137     QString description() const;
0138 
0139     /** Return whether the instance has any contents. */
0140     bool isEmpty() const;
0141 
0142     /** Return whether the instance contains the text of an email. */
0143     bool isEmail() const;
0144 
0145     /** Return whether the instance contains the text of a script. */
0146     bool isScript() const;
0147 
0148     /** Return whether the instance contains the text of a todo. */
0149     bool isTodo() const;
0150 
0151     /** Return the ID of an email.
0152      *  @return Email ID, or -1 if none.
0153      */
0154     KAEvent::EmailId emailId() const;
0155 
0156     /** Return the alarm summary text for either single line or tooltip display.
0157      *  @param event      event whose summary text is to be returned
0158      *  @param maxLines   the maximum number of lines returned
0159      *  @param truncated  if non-null, points to a variable which will be set true
0160      *                    if the text returned has been truncated, other than to
0161      *                    strip a trailing newline, or false otherwise
0162      */
0163     static QString summary(const KAEvent& event, int maxLines = 1, bool* truncated = nullptr);
0164 
0165     /** Return whether a text is an email, with at least To and From headers.
0166      *  @param text  text to check
0167      */
0168     static bool checkIfEmail(const QString& text);
0169 
0170     /** Check whether a text is an email (with at least To and From headers),
0171      *  and if so return its headers or optionally only its subject line.
0172      *  @param text         text to check
0173      *  @param subjectOnly  true to only return the subject line,
0174      *                      false to return all headers
0175      *  @return headers/subject line, or QString() if not the text of an email.
0176      */
0177     static QString emailHeaders(const QString& text, bool subjectOnly);
0178 
0179     /** Translate an alarm calendar text to a display text.
0180      *  Translation is needed for email texts, since the alarm calendar stores
0181      *  untranslated email prefixes.
0182      *  @param text   text to translate
0183      *  @param email  updated to indicate whether it is an email text
0184      */
0185     static QString fromCalendarText(const QString& text, bool& email);
0186 
0187     /** Return the text for an alarm message text, in alarm calendar format.
0188      *  (The prefix strings are untranslated in the calendar.)
0189      *  @param text  alarm message text
0190      */
0191     static QString toCalendarText(const QString& text);
0192 
0193 private:
0194     //@cond PRIVATE
0195     class Private;
0196     Private* const d;
0197     //@endcond
0198 };
0199 
0200 } // namespace KAlarmCal
0201 
0202 // vim: et sw=4: