kalarm
alarmtext.cpp00001
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 <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
00067
00068 QString AlarmText::displayText() const
00069 {
00070 if (mIsEmail)
00071 {
00072
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
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
00106
00107 bool AlarmText::checkIfEmail(const QString& text)
00108 {
00109 QStringList lines = QStringList::split('\n', text);
00110 return emailHeaderCount(lines);
00111 }
00112
00113
00114
00115
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
00138
00139
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
00160
00161
00162
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
00201
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
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
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
00254
00255
00256
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
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;
00282 }
00283 if (newline == static_cast<int>(text.length()) - 1)
00284 return text.left(newline);
00285 if (truncated)
00286 *truncated = true;
00287 return text.left(newline + (maxLines <= 1 ? 0 : 1)) + QString::fromLatin1("...");
00288 }
|