23 #include "alarmtext.h"
27 #ifdef KALARMCAL_USE_KRESOURCES
28 #include <kcal/todo.h>
31 #include <klocalizedstring.h>
33 #include <QStringList>
39 class AlarmText::Private
42 enum Type { None, Email, Script, Todo };
45 static void initialise();
46 static void setUpTranslations();
47 static int emailHeaderCount(
const QStringList&);
48 static QString todoTitle(
const QString& text);
50 static QString mFromPrefix;
51 static QString mToPrefix;
52 static QString mCcPrefix;
53 static QString mDatePrefix;
54 static QString mSubjectPrefix;
55 static QString mTitlePrefix;
56 static QString mLocnPrefix;
57 static QString mDuePrefix;
58 static QString mFromPrefixEn;
59 static QString mToPrefixEn;
60 static QString mCcPrefixEn;
61 static QString mDatePrefixEn;
62 static QString mSubjectPrefixEn;
63 static bool mInitialised;
65 QString mBody, mFrom, mTo, mCc, mTime, mSubject;
66 unsigned long mKMailSerialNum;
71 QString AlarmText::Private::mFromPrefix;
72 QString AlarmText::Private::mToPrefix;
73 QString AlarmText::Private::mCcPrefix;
74 QString AlarmText::Private::mDatePrefix;
75 QString AlarmText::Private::mSubjectPrefix;
76 QString AlarmText::Private::mTitlePrefix;
77 QString AlarmText::Private::mLocnPrefix;
78 QString AlarmText::Private::mDuePrefix;
79 QString AlarmText::Private::mFromPrefixEn;
80 QString AlarmText::Private::mToPrefixEn;
81 QString AlarmText::Private::mCcPrefixEn;
82 QString AlarmText::Private::mDatePrefixEn;
83 QString AlarmText::Private::mSubjectPrefixEn;
84 bool AlarmText::Private::mInitialised =
false;
86 void AlarmText::Private::initialise()
91 mFromPrefixEn = QLatin1String(
"From:");
92 mToPrefixEn = QLatin1String(
"To:");
93 mCcPrefixEn = QLatin1String(
"Cc:");
94 mDatePrefixEn = QLatin1String(
"Date:");
95 mSubjectPrefixEn = QLatin1String(
"Subject:");
102 Private::initialise();
107 : d(new Private(*other.d))
111 AlarmText::~AlarmText()
116 AlarmText& AlarmText::operator=(
const AlarmText& other)
127 if (text.startsWith(QLatin1String(
"#!")))
128 d->mType = Private::Script;
134 d->mType = Private::Script;
138 const QString& subject,
const QString& body,
unsigned long kmailSerialNumber)
141 d->mType = Private::Email;
151 #ifndef KALARMCAL_USE_KRESOURCES
158 d->mType = Private::Todo;
159 d->mSubject = todo->summary();
160 d->mBody = todo->description();
161 d->mTo = todo->location();
162 if (todo->hasDueDate())
164 KDateTime due = todo->dtDue(
false);
165 if (todo->hasStartDate() && todo->dtStart() != due)
167 d->mTime = todo->allDay() ? KGlobal::locale()->formatDate(due.date(), KLocale::ShortDate)
168 : KGlobal::locale()->formatDateTime(due.dateTime());
178 return d->displayText();
181 QString AlarmText::Private::displayText()
const
189 text = mFromPrefix + QLatin1Char(
'\t') + mFrom + QLatin1Char(
'\n');
190 text += mToPrefix + QLatin1Char(
'\t') + mTo + QLatin1Char(
'\n');
192 text += mCcPrefix + QLatin1Char(
'\t') + mCc + QLatin1Char(
'\n');
193 if (!mTime.isEmpty())
194 text += mDatePrefix + QLatin1Char(
'\t') + mTime + QLatin1Char(
'\n');
195 text += mSubjectPrefix + QLatin1Char(
'\t') + mSubject;
196 if (!mBody.isEmpty())
198 text += QLatin1String(
"\n\n");
205 if (!mSubject.isEmpty())
206 text = mTitlePrefix + QLatin1Char(
'\t') + mSubject + QLatin1Char(
'\n');
208 text += mLocnPrefix + QLatin1Char(
'\t') + mTo + QLatin1Char(
'\n');
209 if (!mTime.isEmpty())
210 text += mDuePrefix + QLatin1Char(
'\t') + mTime + QLatin1Char(
'\n');
211 if (!mBody.isEmpty())
214 text += QLatin1Char(
'\n');
221 return !text.isEmpty() ? text : mBody;
226 return (d->mType == Private::Email) ? d->mTo : QString();
231 return (d->mType == Private::Email) ? d->mFrom : QString();
236 return (d->mType == Private::Email) ? d->mCc : QString();
241 return (d->mType == Private::Email) ? d->mTime : QString();
246 return (d->mType == Private::Email) ? d->mSubject : QString();
251 return (d->mType == Private::Email) ? d->mBody : QString();
256 return (d->mType == Private::Todo) ? d->mSubject : QString();
261 return (d->mType == Private::Todo) ? d->mTo : QString();
266 return (d->mType == Private::Todo) ? d->mTime : QString();
271 return (d->mType == Private::Todo) ? d->mBody : QString();
279 if (!d->mBody.isEmpty())
281 if (d->mType != Private::Email)
283 return d->mFrom.isEmpty() && d->mTo.isEmpty() && d->mCc.isEmpty() && d->mTime.isEmpty() && d->mSubject.isEmpty();
288 return d->mType == Private::Email;
293 return d->mType == Private::Script;
298 return d->mType == Private::Todo;
303 return d->mKMailSerialNum;
314 static const QRegExp localfile(QLatin1String(
"^file:/+"));
319 text =
event.audioFile();
320 if (localfile.indexIn(text) >= 0)
321 text = text.mid(localfile.matchedLength() - 1);
324 text =
event.emailSubject();
327 text =
event.cleanText();
328 if (localfile.indexIn(text) >= 0)
329 text = text.mid(localfile.matchedLength() - 1);
332 text =
event.cleanText();
336 text =
event.cleanText();
339 if (!subject.isNull())
349 subject = Private::todoTitle(text);
350 if (!subject.isEmpty())
362 if (text.count(QLatin1Char(
'\n')) < maxLines)
365 for (
int i = 0; i < maxLines; ++i)
367 newline = text.indexOf(QLatin1Char(
'\n'), newline + 1);
371 if (newline == static_cast<int>(text.length()) - 1)
372 return text.left(newline);
375 return text.left(newline + (maxLines <= 1 ? 0 : 1)) + QLatin1String(
"...");
383 QStringList lines = text.split(QLatin1Char(
'\n'), QString::SkipEmptyParts);
384 return Private::emailHeaderCount(lines);
394 const QStringList lines = text.split(QLatin1Char(
'\n'), QString::SkipEmptyParts);
395 int n = Private::emailHeaderCount(lines);
399 return lines[n-1].mid(Private::mSubjectPrefix.length()).trimmed();
400 QString h = lines[0];
401 for (
int i = 1; i < n; ++i)
403 h += QLatin1Char(
'\n');
417 Private::initialise();
418 const QStringList lines = text.split(QLatin1Char(
'\n'), QString::SkipEmptyParts);
419 int maxn = lines.count();
421 && lines[0].startsWith(Private::mFromPrefixEn)
422 && lines[1].startsWith(Private::mToPrefixEn))
425 if (lines[2].startsWith(Private::mCcPrefixEn))
428 && lines[n].startsWith(Private::mDatePrefixEn)
429 && lines[n+1].startsWith(Private::mSubjectPrefixEn))
431 Private::setUpTranslations();
433 dispText = Private::mFromPrefix + lines[0].mid(Private::mFromPrefixEn.length()) + QLatin1Char(
'\n');
434 dispText += Private::mToPrefix + lines[1].mid(Private::mToPrefixEn.length()) + QLatin1Char(
'\n');
436 dispText += Private::mCcPrefix + lines[2].mid(Private::mCcPrefixEn.length()) + QLatin1Char(
'\n');
437 dispText += Private::mDatePrefix + lines[n].mid(Private::mDatePrefixEn.length()) + QLatin1Char(
'\n');
438 dispText += Private::mSubjectPrefix + lines[n+1].mid(Private::mSubjectPrefixEn.length());
439 int i = text.indexOf(Private::mSubjectPrefixEn);
440 i = text.indexOf(QLatin1Char(
'\n'), i);
442 dispText += text.mid(i);
457 Private::setUpTranslations();
458 const QStringList lines = text.split(QLatin1Char(
'\n'), QString::SkipEmptyParts);
459 int maxn = lines.count();
461 && lines[0].startsWith(Private::mFromPrefix)
462 && lines[1].startsWith(Private::mToPrefix))
465 if (lines[2].startsWith(Private::mCcPrefix))
468 && lines[n].startsWith(Private::mDatePrefix)
469 && lines[n+1].startsWith(Private::mSubjectPrefix))
473 calText = Private::mFromPrefixEn + lines[0].mid(Private::mFromPrefix.length()) + QLatin1Char(
'\n');
474 calText += Private::mToPrefixEn + lines[1].mid(Private::mToPrefix.length()) + QLatin1Char(
'\n');
476 calText += Private::mCcPrefixEn + lines[2].mid(Private::mCcPrefix.length()) + QLatin1Char(
'\n');
477 calText += Private::mDatePrefixEn + lines[n].mid(Private::mDatePrefix.length()) + QLatin1Char(
'\n');
478 calText += Private::mSubjectPrefixEn + lines[n+1].mid(Private::mSubjectPrefix.length());
479 int i = text.indexOf(Private::mSubjectPrefix);
480 i = text.indexOf(QLatin1Char(
'\n'), i);
482 calText += text.mid(i);
489 void AlarmText::Private::clear()
504 void AlarmText::Private::setUpTranslations()
507 if (mFromPrefix.isNull())
509 mFromPrefix = i18nc(
"@info/plain 'From' email address",
"From:");
510 mToPrefix = i18nc(
"@info/plain Email addressee",
"To:");
511 mCcPrefix = i18nc(
"@info/plain Copy-to in email headers",
"Cc:");
512 mDatePrefix = i18nc(
"@info/plain",
"Date:");
513 mSubjectPrefix = i18nc(
"@info/plain Email subject",
"Subject:");
515 mTitlePrefix = i18nc(
"@info/plain Todo calendar item's title field",
"To-do:");
516 mLocnPrefix = i18nc(
"@info/plain Todo calendar item's location field",
"Location:");
517 mDuePrefix = i18nc(
"@info/plain Todo calendar item's due date/time",
"Due:");
525 int AlarmText::Private::emailHeaderCount(
const QStringList& lines)
528 int maxn = lines.count();
530 && lines[0].startsWith(mFromPrefix)
531 && lines[1].startsWith(mToPrefix))
534 if (lines[2].startsWith(mCcPrefix))
537 && lines[n].startsWith(mDatePrefix)
538 && lines[n+1].startsWith(mSubjectPrefix))
547 QString AlarmText::Private::todoTitle(
const QString& text)
550 const QStringList lines = text.split(QLatin1Char(
'\n'), QString::SkipEmptyParts);
552 for (n = 0; n < lines.count() && lines[n].contains(QLatin1Char(
'\t')); ++n) ;
557 if (lines[i].startsWith(mTitlePrefix + QLatin1Char(
'\t')))
559 title = lines[i].mid(mTitlePrefix.length()).trimmed();
562 if (i < n && lines[i].startsWith(mLocnPrefix + QLatin1Char(
'\t')))
564 if (i < n && lines[i].startsWith(mDuePrefix + QLatin1Char(
'\t')))
569 if (!title.isEmpty())
571 if (n < lines.count())
QString summary() const
Return the summary text for a todo.
QString cc() const
Return the 'Cc' header parameter for an email alarm.
QString displayText() const
Return the text for a text message alarm, in display format.
static QString fromCalendarText(const QString &text, bool &email)
Translate an alarm calendar text to a display text.
bool isEmpty() const
Return whether there is any text.
QString subject() const
Return the 'Subject' header parameter for an email alarm.
QString location() const
Return the location text for a todo.
QString time() const
Return the 'Date' header parameter for an email alarm.
QString description() const
Return the description text for a todo.
Type
The category of an event, indicated by the middle part of its UID.
static QString emailHeaders(const QString &text, bool subjectOnly)
Check whether a text is an email (with at least To and From headers), and if so return its headers or...
bool isTodo() const
Return whether the instance contains the text of a todo.
static QString toCalendarText(const QString &text)
Return the text for an alarm message text, in alarm calendar format.
unsigned long kmailSerialNumber() const
Return the kmail serial number of an email.
bool isScript() const
Return whether the instance contains the text of a script.
QString due() const
Return the due date text for a todo.
void setEmail(const QString &to, const QString &from, const QString &cc, const QString &time, const QString &subject, const QString &body, unsigned long kmailSerialNumber=0)
Set the instance contents to be an email.
QString from() const
Return the 'From' header parameter for an email alarm.
Parses email, todo and script alarm texts.
display the contents of a file
void setScript(const QString &text)
Set the instance contents to be a script.
KAEvent represents a KAlarm event.
void setTodo(const KCalCore::Todo::Ptr &todo)
Set the instance contents to be a todo.
SubAction actionSubType() const
Return the action sub-type of the event's main alarm.
static bool checkIfEmail(const QString &text)
Return whether a text is an email, with at least To and From headers.
bool isEmail() const
Return whether the instance contains the text of an email.
void setText(const QString &text)
Set the alarm text.
AlarmText(const QString &text=QString())
Constructor which sets the alarm text.
QString body() const
Return the email message body.
QSharedPointer< Todo > Ptr
QString to() const
Return the 'To' header parameter for an email alarm.