kalarm

alarmtext.cpp

00001 /*
00002  *  alarmtext.cpp  -  text/email alarm text conversion
00003  *  Program:  kalarm
00004  *  Copyright (C) 2004, 2005 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kalarm.h"
00022 #include <qstringlist.h>
00023 #include <klocale.h>
00024 
00025 #include "alarmevent.h"
00026 #include "editdlg.h"
00027 #include "alarmtext.h"
00028 
00029 
00030 QString AlarmText::mFromPrefix;
00031 QString AlarmText::mToPrefix;
00032 QString AlarmText::mCcPrefix;
00033 QString AlarmText::mDatePrefix;
00034 QString AlarmText::mSubjectPrefix;
00035 QString AlarmText::mFromPrefixEn    = QString::fromLatin1("From:");
00036 QString AlarmText::mToPrefixEn      = QString::fromLatin1("To:");
00037 QString AlarmText::mCcPrefixEn      = QString::fromLatin1("Cc:");
00038 QString AlarmText::mDatePrefixEn    = QString::fromLatin1("Date:");
00039 QString AlarmText::mSubjectPrefixEn = QString::fromLatin1("Subject:");
00040 
00041 
00042 void AlarmText::setText(const QString& text)
00043 {
00044     mBody     = text;
00045     mIsScript = text.startsWith(QString::fromLatin1("#!"));
00046     mIsEmail  = false;
00047     mTo = mFrom = mCc = mTime = mSubject = QString::null;
00048     mKMailSerialNum = 0;
00049 }
00050 
00051 void AlarmText::setEmail(const QString& to, const QString& from, const QString& cc, const QString& time,
00052                          const QString& subject, const QString& body, unsigned long kmailSerialNumber)
00053 {
00054     mIsScript       = false;
00055     mIsEmail        = true;
00056     mTo             = to;
00057     mFrom           = from;
00058     mCc             = cc;
00059     mTime           = time;
00060     mSubject        = subject;
00061     mBody           = body;
00062     mKMailSerialNum = kmailSerialNumber;
00063 }
00064 
00065 /******************************************************************************
00066 *  Return the text for a text message alarm, in display format.
00067 */
00068 QString AlarmText::displayText() const
00069 {
00070     if (mIsEmail)
00071     {
00072         // Format the email into a text alarm
00073         setUpTranslations();
00074         QString text;
00075         text = mFromPrefix + '\t' + mFrom + '\n';
00076         text += mToPrefix + '\t' + mTo + '\n';
00077         if (!mCc.isEmpty())
00078             text += mCcPrefix + '\t' + mCc + '\n';
00079         if (!mTime.isEmpty())
00080             text += mDatePrefix + '\t' + mTime + '\n';
00081         text += mSubjectPrefix + '\t' + mSubject;
00082         if (!mBody.isEmpty())
00083         {
00084             text += "\n\n";
00085             text += mBody;
00086         }
00087         return text;
00088     }
00089     return mBody;
00090 }
00091 
00092 /******************************************************************************
00093 *  Return whether there is any text.
00094 */
00095 bool AlarmText::isEmpty() const
00096 {
00097     if (!mBody.isEmpty())
00098         return false;
00099     if (!mIsEmail)
00100         return true;
00101     return mFrom.isEmpty() && mTo.isEmpty() && mCc.isEmpty() && mTime.isEmpty() && mSubject.isEmpty();
00102 }
00103 
00104 /******************************************************************************
00105 *  Check whether a text is an email.
00106 */
00107 bool AlarmText::checkIfEmail(const QString& text)
00108 {
00109     QStringList lines = QStringList::split('\n', text);
00110     return emailHeaderCount(lines);
00111 }
00112 
00113 /******************************************************************************
00114 *  Check whether a text is an email.
00115 *  Reply = number of email header lines, or 0 if not an email.
00116 */
00117 int AlarmText::emailHeaderCount(const QStringList& lines)
00118 {
00119     setUpTranslations();
00120     int maxn = lines.count();
00121     if (maxn >= 4
00122     &&  lines[0].startsWith(mFromPrefix)
00123     &&  lines[1].startsWith(mToPrefix))
00124     {
00125         int n = 2;
00126         if (lines[2].startsWith(mCcPrefix))
00127             ++n;
00128         if (maxn > n + 1
00129         &&  lines[n].startsWith(mDatePrefix)
00130         &&  lines[n+1].startsWith(mSubjectPrefix))
00131             return n+2;
00132     }
00133     return 0;
00134 }
00135 
00136 /******************************************************************************
00137 *  Check whether a text is an email, and if so return its headers or optionally
00138 *  only its subject line.
00139 *  Reply = headers/subject line, or QString::null if not the text of an email.
00140 */
00141 QString AlarmText::emailHeaders(const QString& text, bool subjectOnly)
00142 {
00143     QStringList lines = QStringList::split('\n', text);
00144     int n = emailHeaderCount(lines);
00145     if (!n)
00146         return QString::null;
00147     if (subjectOnly)
00148         return lines[n-1].mid(mSubjectPrefix.length()).stripWhiteSpace();
00149     QString h = lines[0];
00150     for (int i = 1;  i < n;  ++i)
00151     {
00152         h += '\n';
00153         h += lines[i];
00154     }
00155     return h;
00156 }
00157 
00158 /******************************************************************************
00159 *  Translate an alarm calendar text to a display text.
00160 *  Translation is needed for email texts, since the alarm calendar stores
00161 *  untranslated email prefixes.
00162 *  'email' is set to indicate whether it is an email text.
00163 */
00164 QString AlarmText::fromCalendarText(const QString& text, bool& email)
00165 {
00166     QStringList lines = QStringList::split('\n', text);
00167     int maxn = lines.count();
00168     if (maxn >= 4
00169     &&  lines[0].startsWith(mFromPrefixEn)
00170     &&  lines[1].startsWith(mToPrefixEn))
00171     {
00172         int n = 2;
00173         if (lines[2].startsWith(mCcPrefixEn))
00174             ++n;
00175         if (maxn > n + 1
00176         &&  lines[n].startsWith(mDatePrefixEn)
00177         &&  lines[n+1].startsWith(mSubjectPrefixEn))
00178         {
00179             setUpTranslations();
00180             QString dispText;
00181             dispText = mFromPrefix + lines[0].mid(mFromPrefixEn.length()) + '\n';
00182             dispText += mToPrefix + lines[1].mid(mToPrefixEn.length()) + '\n';
00183             if (n == 3)
00184                 dispText += mCcPrefix + lines[2].mid(mCcPrefixEn.length()) + '\n';
00185             dispText += mDatePrefix + lines[n].mid(mDatePrefixEn.length()) + '\n';
00186             dispText += mSubjectPrefix + lines[n+1].mid(mSubjectPrefixEn.length());
00187             int i = text.find(mSubjectPrefixEn);
00188             i = text.find('\n', i);
00189             if (i > 0)
00190                 dispText += text.mid(i);
00191             email = true;
00192             return dispText;
00193         }
00194     }
00195     email = false;
00196     return text;
00197 }
00198 
00199 /******************************************************************************
00200 *  Return the text for a text message alarm, in alarm calendar format.
00201 *  (The prefix strings are untranslated in the calendar.)
00202 */
00203 QString AlarmText::toCalendarText(const QString& text)
00204 {
00205     setUpTranslations();
00206     QStringList lines = QStringList::split('\n', text);
00207     int maxn = lines.count();
00208     if (maxn >= 4
00209     &&  lines[0].startsWith(mFromPrefix)
00210     &&  lines[1].startsWith(mToPrefix))
00211     {
00212         int n = 2;
00213         if (lines[2].startsWith(mCcPrefix))
00214             ++n;
00215         if (maxn > n + 1
00216         &&  lines[n].startsWith(mDatePrefix)
00217         &&  lines[n+1].startsWith(mSubjectPrefix))
00218         {
00219             // Format the email into a text alarm
00220             QString calText;
00221             calText = mFromPrefixEn + lines[0].mid(mFromPrefix.length()) + '\n';
00222             calText += mToPrefixEn + lines[1].mid(mToPrefix.length()) + '\n';
00223             if (n == 3)
00224                 calText += mCcPrefixEn + lines[2].mid(mCcPrefix.length()) + '\n';
00225             calText += mDatePrefixEn + lines[n].mid(mDatePrefix.length()) + '\n';
00226             calText += mSubjectPrefixEn + lines[n+1].mid(mSubjectPrefix.length());
00227             int i = text.find(mSubjectPrefix);
00228             i = text.find('\n', i);
00229             if (i > 0)
00230                 calText += text.mid(i);
00231             return calText;
00232         }
00233     }
00234     return text;
00235 }
00236 
00237 /******************************************************************************
00238 *  Set up messages used by executeDropEvent() and emailHeaders().
00239 */
00240 void AlarmText::setUpTranslations()
00241 {
00242     if (mFromPrefix.isNull())
00243     {
00244         mFromPrefix    = EditAlarmDlg::i18n_EmailFrom();
00245         mToPrefix      = EditAlarmDlg::i18n_EmailTo();
00246         mCcPrefix      = i18n("Copy-to in email headers", "Cc:");
00247         mDatePrefix    = i18n("Date:");
00248         mSubjectPrefix = EditAlarmDlg::i18n_EmailSubject();
00249     }
00250 }
00251 
00252 /******************************************************************************
00253 *  Return the alarm summary text for either single line or tooltip display.
00254 *  The maximum number of line returned is determined by 'maxLines'.
00255 *  If 'truncated' is non-null, it will be set true if the text returned has been
00256 *  truncated, other than to strip a trailing newline.
00257 */
00258 QString AlarmText::summary(const KAEvent& event, int maxLines, bool* truncated)
00259 {
00260     QString text = (event.action() == KAEvent::EMAIL) ? event.emailSubject() : event.cleanText();
00261     if (event.action() == KAEvent::MESSAGE)
00262     {
00263         // If the message is the text of an email, return its headers or just subject line
00264         QString subject = emailHeaders(text, (maxLines <= 1));
00265         if (!subject.isNull())
00266         {
00267             if (truncated)
00268                 *truncated = true;
00269             return subject;
00270         }
00271     }
00272     if (truncated)
00273         *truncated = false;
00274     if (text.contains('\n') < maxLines)
00275         return text;
00276     int newline = -1;
00277     for (int i = 0;  i < maxLines;  ++i)
00278     {
00279         newline = text.find('\n', newline + 1);
00280         if (newline < 0)
00281             return text;       // not truncated after all !?!
00282     }
00283     if (newline == static_cast<int>(text.length()) - 1)
00284         return text.left(newline);    // text ends in newline
00285     if (truncated)
00286         *truncated = true;
00287     return text.left(newline + (maxLines <= 1 ? 0 : 1)) + QString::fromLatin1("...");
00288 }
KDE Home | KDE Accessibility Home | Description of Access Keys