kalarm
alarmtext.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kalarm.h"
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
00071
00072 QString AlarmText::displayText() const
00073 {
00074 if (mIsEmail)
00075 {
00076
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
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
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
00119
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
00142
00143
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
00164
00165
00166
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
00205
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
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
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
00258
00259
00260
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
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;
00286 }
00287 if (newline == static_cast<int>(text.length()) - 1)
00288 return text.left(newline);
00289 if (truncated)
00290 *truncated = true;
00291 return text.left(newline + (maxLines <= 1 ? 0 : 1)) + QLatin1String("...");
00292 }