• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kalarm

alarmtext.cpp

Go to the documentation of this file.
00001 /*
00002  *  alarmtext.cpp  -  text/email alarm text conversion
00003  *  Program:  kalarm
00004  *  Copyright © 2004,2005,2007,2008 by David Jarvie <djarvie@kde.org>
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"   //krazy:exclude=includes (kalarm.h must be first)
00022 #include "alarmtext.h"
00023 
00024 #include "alarmevent.h"
00025 
00026 #include <QStringList>
00027 #include <klocale.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    = QLatin1String("From:");
00036 QString AlarmText::mToPrefixEn      = QLatin1String("To:");
00037 QString AlarmText::mCcPrefixEn      = QLatin1String("Cc:");
00038 QString AlarmText::mDatePrefixEn    = QLatin1String("Date:");
00039 QString AlarmText::mSubjectPrefixEn = QLatin1String("Subject:");
00040 
00041 
00042 void AlarmText::setText(const QString& text)
00043 {
00044     mBody     = text;
00045     mIsScript = text.startsWith(QLatin1String("#!"));
00046     mIsEmail  = false;
00047     mTo.clear();
00048     mFrom.clear();
00049     mCc.clear();
00050     mTime.clear();
00051     mSubject.clear();
00052     mKMailSerialNum = 0;
00053 }
00054 
00055 void AlarmText::setEmail(const QString& to, const QString& from, const QString& cc, const QString& time,
00056                          const QString& subject, const QString& body, unsigned long kmailSerialNumber)
00057 {
00058     mIsScript       = false;
00059     mIsEmail        = true;
00060     mTo             = to;
00061     mFrom           = from;
00062     mCc             = cc;
00063     mTime           = time;
00064     mSubject        = subject;
00065     mBody           = body;
00066     mKMailSerialNum = kmailSerialNumber;
00067 }
00068 
00069 /******************************************************************************
00070 *  Return the text for a text message alarm, in display format.
00071 */
00072 QString AlarmText::displayText() const
00073 {
00074     if (mIsEmail)
00075     {
00076         // Format the email into a text alarm
00077         setUpTranslations();
00078         QString text;
00079         text = mFromPrefix + '\t' + mFrom + '\n';
00080         text += mToPrefix + '\t' + mTo + '\n';
00081         if (!mCc.isEmpty())
00082             text += mCcPrefix + '\t' + mCc + '\n';
00083         if (!mTime.isEmpty())
00084             text += mDatePrefix + '\t' + mTime + '\n';
00085         text += mSubjectPrefix + '\t' + mSubject;
00086         if (!mBody.isEmpty())
00087         {
00088             text += "\n\n";
00089             text += mBody;
00090         }
00091         return text;
00092     }
00093     return mBody;
00094 }
00095 
00096 /******************************************************************************
00097 *  Return whether there is any text.
00098 */
00099 bool AlarmText::isEmpty() const
00100 {
00101     if (!mBody.isEmpty())
00102         return false;
00103     if (!mIsEmail)
00104         return true;
00105     return mFrom.isEmpty() && mTo.isEmpty() && mCc.isEmpty() && mTime.isEmpty() && mSubject.isEmpty();
00106 }
00107 
00108 /******************************************************************************
00109 *  Check whether a text is an email.
00110 */
00111 bool AlarmText::checkIfEmail(const QString& text)
00112 {
00113     QStringList lines = text.split('\n', QString::SkipEmptyParts);
00114     return emailHeaderCount(lines);
00115 }
00116 
00117 /******************************************************************************
00118 *  Check whether a text is an email.
00119 *  Reply = number of email header lines, or 0 if not an email.
00120 */
00121 int AlarmText::emailHeaderCount(const QStringList& lines)
00122 {
00123     setUpTranslations();
00124     int maxn = lines.count();
00125     if (maxn >= 4
00126     &&  lines[0].startsWith(mFromPrefix)
00127     &&  lines[1].startsWith(mToPrefix))
00128     {
00129         int n = 2;
00130         if (lines[2].startsWith(mCcPrefix))
00131             ++n;
00132         if (maxn > n + 1
00133         &&  lines[n].startsWith(mDatePrefix)
00134         &&  lines[n+1].startsWith(mSubjectPrefix))
00135             return n+2;
00136     }
00137     return 0;
00138 }
00139 
00140 /******************************************************************************
00141 *  Check whether a text is an email, and if so return its headers or optionally
00142 *  only its subject line.
00143 *  Reply = headers/subject line, or QString() if not the text of an email.
00144 */
00145 QString AlarmText::emailHeaders(const QString& text, bool subjectOnly)
00146 {
00147     QStringList lines = text.split('\n', QString::SkipEmptyParts);
00148     int n = emailHeaderCount(lines);
00149     if (!n)
00150         return QString();
00151     if (subjectOnly)
00152         return lines[n-1].mid(mSubjectPrefix.length()).trimmed();
00153     QString h = lines[0];
00154     for (int i = 1;  i < n;  ++i)
00155     {
00156         h += '\n';
00157         h += lines[i];
00158     }
00159     return h;
00160 }
00161 
00162 /******************************************************************************
00163 *  Translate an alarm calendar text to a display text.
00164 *  Translation is needed for email texts, since the alarm calendar stores
00165 *  untranslated email prefixes.
00166 *  'email' is set to indicate whether it is an email text.
00167 */
00168 QString AlarmText::fromCalendarText(const QString& text, bool& email)
00169 {
00170     QStringList lines = text.split('\n', QString::SkipEmptyParts);
00171     int maxn = lines.count();
00172     if (maxn >= 4
00173     &&  lines[0].startsWith(mFromPrefixEn)
00174     &&  lines[1].startsWith(mToPrefixEn))
00175     {
00176         int n = 2;
00177         if (lines[2].startsWith(mCcPrefixEn))
00178             ++n;
00179         if (maxn > n + 1
00180         &&  lines[n].startsWith(mDatePrefixEn)
00181         &&  lines[n+1].startsWith(mSubjectPrefixEn))
00182         {
00183             setUpTranslations();
00184             QString dispText;
00185             dispText = mFromPrefix + lines[0].mid(mFromPrefixEn.length()) + '\n';
00186             dispText += mToPrefix + lines[1].mid(mToPrefixEn.length()) + '\n';
00187             if (n == 3)
00188                 dispText += mCcPrefix + lines[2].mid(mCcPrefixEn.length()) + '\n';
00189             dispText += mDatePrefix + lines[n].mid(mDatePrefixEn.length()) + '\n';
00190             dispText += mSubjectPrefix + lines[n+1].mid(mSubjectPrefixEn.length());
00191             int i = text.indexOf(mSubjectPrefixEn);
00192             i = text.indexOf('\n', i);
00193             if (i > 0)
00194                 dispText += text.mid(i);
00195             email = true;
00196             return dispText;
00197         }
00198     }
00199     email = false;
00200     return text;
00201 }
00202 
00203 /******************************************************************************
00204 *  Return the text for a text message alarm, in alarm calendar format.
00205 *  (The prefix strings are untranslated in the calendar.)
00206 */
00207 QString AlarmText::toCalendarText(const QString& text)
00208 {
00209     setUpTranslations();
00210     QStringList lines = text.split('\n', QString::SkipEmptyParts);
00211     int maxn = lines.count();
00212     if (maxn >= 4
00213     &&  lines[0].startsWith(mFromPrefix)
00214     &&  lines[1].startsWith(mToPrefix))
00215     {
00216         int n = 2;
00217         if (lines[2].startsWith(mCcPrefix))
00218             ++n;
00219         if (maxn > n + 1
00220         &&  lines[n].startsWith(mDatePrefix)
00221         &&  lines[n+1].startsWith(mSubjectPrefix))
00222         {
00223             // Format the email into a text alarm
00224             QString calText;
00225             calText = mFromPrefixEn + lines[0].mid(mFromPrefix.length()) + '\n';
00226             calText += mToPrefixEn + lines[1].mid(mToPrefix.length()) + '\n';
00227             if (n == 3)
00228                 calText += mCcPrefixEn + lines[2].mid(mCcPrefix.length()) + '\n';
00229             calText += mDatePrefixEn + lines[n].mid(mDatePrefix.length()) + '\n';
00230             calText += mSubjectPrefixEn + lines[n+1].mid(mSubjectPrefix.length());
00231             int i = text.indexOf(mSubjectPrefix);
00232             i = text.indexOf('\n', i);
00233             if (i > 0)
00234                 calText += text.mid(i);
00235             return calText;
00236         }
00237     }
00238     return text;
00239 }
00240 
00241 /******************************************************************************
00242 *  Set up messages used by executeDropEvent() and emailHeaders().
00243 */
00244 void AlarmText::setUpTranslations()
00245 {
00246     if (mFromPrefix.isNull())
00247     {
00248         mFromPrefix    = i18nc("@info/plain 'From' email address", "From:");
00249         mToPrefix      = i18nc("@info/plain Email addressee", "To:");
00250         mCcPrefix      = i18nc("@info/plain Copy-to in email headers", "Cc:");
00251         mDatePrefix    = i18nc("@info/plain", "Date:");
00252         mSubjectPrefix = i18nc("@info/plain Email subject", "Subject:");
00253     }
00254 }
00255 
00256 /******************************************************************************
00257 *  Return the alarm summary text for either single line or tooltip display.
00258 *  The maximum number of line returned is determined by 'maxLines'.
00259 *  If 'truncated' is non-null, it will be set true if the text returned has been
00260 *  truncated, other than to strip a trailing newline.
00261 */
00262 QString AlarmText::summary(const KAEvent* event, int maxLines, bool* truncated)
00263 {
00264     QString text = (event->action() == KAEvent::EMAIL) ? event->emailSubject() : event->cleanText();
00265     if (event->action() == KAEvent::MESSAGE)
00266     {
00267         // If the message is the text of an email, return its headers or just subject line
00268         QString subject = emailHeaders(text, (maxLines <= 1));
00269         if (!subject.isNull())
00270         {
00271             if (truncated)
00272                 *truncated = true;
00273             return subject;
00274         }
00275     }
00276     if (truncated)
00277         *truncated = false;
00278     if (text.count('\n') < maxLines)
00279         return text;
00280     int newline = -1;
00281     for (int i = 0;  i < maxLines;  ++i)
00282     {
00283         newline = text.indexOf('\n', newline + 1);
00284         if (newline < 0)
00285             return text;       // not truncated after all !?!
00286     }
00287     if (newline == static_cast<int>(text.length()) - 1)
00288         return text.left(newline);    // text ends in newline
00289     if (truncated)
00290         *truncated = true;
00291     return text.left(newline + (maxLines <= 1 ? 0 : 1)) + QLatin1String("...");
00292 }

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal