kalarm

alarmtext.cpp
1 /*
2  * alarmtext.cpp - text/email alarm text conversion
3  * Program: kalarm
4  * Copyright (C) 2004, 2005 by David Jarvie <software@astrojar.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 #include <tqstringlist.h>
23 #include <tdelocale.h>
24 
25 #include "alarmevent.h"
26 #include "editdlg.h"
27 #include "alarmtext.h"
28 
29 
30 TQString AlarmText::mFromPrefix;
31 TQString AlarmText::mToPrefix;
32 TQString AlarmText::mCcPrefix;
33 TQString AlarmText::mDatePrefix;
34 TQString AlarmText::mSubjectPrefix;
35 TQString AlarmText::mFromPrefixEn = TQString::fromLatin1("From:");
36 TQString AlarmText::mToPrefixEn = TQString::fromLatin1("To:");
37 TQString AlarmText::mCcPrefixEn = TQString::fromLatin1("Cc:");
38 TQString AlarmText::mDatePrefixEn = TQString::fromLatin1("Date:");
39 TQString AlarmText::mSubjectPrefixEn = TQString::fromLatin1("Subject:");
40 
41 
42 void AlarmText::setText(const TQString& text)
43 {
44  mBody = text;
45  mIsScript = text.startsWith(TQString::fromLatin1("#!"));
46  mIsEmail = false;
47  mTo = mFrom = mCc = mTime = mSubject = TQString();
48  mKMailSerialNum = 0;
49 }
50 
51 void AlarmText::setEmail(const TQString& to, const TQString& from, const TQString& cc, const TQString& time,
52  const TQString& subject, const TQString& body, unsigned long kmailSerialNumber)
53 {
54  mIsScript = false;
55  mIsEmail = true;
56  mTo = to;
57  mFrom = from;
58  mCc = cc;
59  mTime = time;
60  mSubject = subject;
61  mBody = body;
62  mKMailSerialNum = kmailSerialNumber;
63 }
64 
65 /******************************************************************************
66 * Return the text for a text message alarm, in display format.
67 */
68 TQString AlarmText::displayText() const
69 {
70  if (mIsEmail)
71  {
72  // Format the email into a text alarm
73  setUpTranslations();
74  TQString text;
75  text = mFromPrefix + '\t' + mFrom + '\n';
76  text += mToPrefix + '\t' + mTo + '\n';
77  if (!mCc.isEmpty())
78  text += mCcPrefix + '\t' + mCc + '\n';
79  if (!mTime.isEmpty())
80  text += mDatePrefix + '\t' + mTime + '\n';
81  text += mSubjectPrefix + '\t' + mSubject;
82  if (!mBody.isEmpty())
83  {
84  text += "\n\n";
85  text += mBody;
86  }
87  return text;
88  }
89  return mBody;
90 }
91 
92 /******************************************************************************
93 * Return whether there is any text.
94 */
95 bool AlarmText::isEmpty() const
96 {
97  if (!mBody.isEmpty())
98  return false;
99  if (!mIsEmail)
100  return true;
101  return mFrom.isEmpty() && mTo.isEmpty() && mCc.isEmpty() && mTime.isEmpty() && mSubject.isEmpty();
102 }
103 
104 /******************************************************************************
105 * Check whether a text is an email.
106 */
107 bool AlarmText::checkIfEmail(const TQString& text)
108 {
109  TQStringList lines = TQStringList::split('\n', text);
110  return emailHeaderCount(lines);
111 }
112 
113 /******************************************************************************
114 * Check whether a text is an email.
115 * Reply = number of email header lines, or 0 if not an email.
116 */
117 int AlarmText::emailHeaderCount(const TQStringList& lines)
118 {
119  setUpTranslations();
120  int maxn = lines.count();
121  if (maxn >= 4
122  && lines[0].startsWith(mFromPrefix)
123  && lines[1].startsWith(mToPrefix))
124  {
125  int n = 2;
126  if (lines[2].startsWith(mCcPrefix))
127  ++n;
128  if (maxn > n + 1
129  && lines[n].startsWith(mDatePrefix)
130  && lines[n+1].startsWith(mSubjectPrefix))
131  return n+2;
132  }
133  return 0;
134 }
135 
136 /******************************************************************************
137 * Check whether a text is an email, and if so return its headers or optionally
138 * only its subject line.
139 * Reply = headers/subject line, or TQString() if not the text of an email.
140 */
141 TQString AlarmText::emailHeaders(const TQString& text, bool subjectOnly)
142 {
143  TQStringList lines = TQStringList::split('\n', text);
144  int n = emailHeaderCount(lines);
145  if (!n)
146  return TQString();
147  if (subjectOnly)
148  return lines[n-1].mid(mSubjectPrefix.length()).stripWhiteSpace();
149  TQString h = lines[0];
150  for (int i = 1; i < n; ++i)
151  {
152  h += '\n';
153  h += lines[i];
154  }
155  return h;
156 }
157 
158 /******************************************************************************
159 * Translate an alarm calendar text to a display text.
160 * Translation is needed for email texts, since the alarm calendar stores
161 * untranslated email prefixes.
162 * 'email' is set to indicate whether it is an email text.
163 */
164 TQString AlarmText::fromCalendarText(const TQString& text, bool& email)
165 {
166  TQStringList lines = TQStringList::split('\n', text);
167  int maxn = lines.count();
168  if (maxn >= 4
169  && lines[0].startsWith(mFromPrefixEn)
170  && lines[1].startsWith(mToPrefixEn))
171  {
172  int n = 2;
173  if (lines[2].startsWith(mCcPrefixEn))
174  ++n;
175  if (maxn > n + 1
176  && lines[n].startsWith(mDatePrefixEn)
177  && lines[n+1].startsWith(mSubjectPrefixEn))
178  {
179  setUpTranslations();
180  TQString dispText;
181  dispText = mFromPrefix + lines[0].mid(mFromPrefixEn.length()) + '\n';
182  dispText += mToPrefix + lines[1].mid(mToPrefixEn.length()) + '\n';
183  if (n == 3)
184  dispText += mCcPrefix + lines[2].mid(mCcPrefixEn.length()) + '\n';
185  dispText += mDatePrefix + lines[n].mid(mDatePrefixEn.length()) + '\n';
186  dispText += mSubjectPrefix + lines[n+1].mid(mSubjectPrefixEn.length());
187  int i = text.find(mSubjectPrefixEn);
188  i = text.find('\n', i);
189  if (i > 0)
190  dispText += text.mid(i);
191  email = true;
192  return dispText;
193  }
194  }
195  email = false;
196  return text;
197 }
198 
199 /******************************************************************************
200 * Return the text for a text message alarm, in alarm calendar format.
201 * (The prefix strings are untranslated in the calendar.)
202 */
203 TQString AlarmText::toCalendarText(const TQString& text)
204 {
205  setUpTranslations();
206  TQStringList lines = TQStringList::split('\n', text);
207  int maxn = lines.count();
208  if (maxn >= 4
209  && lines[0].startsWith(mFromPrefix)
210  && lines[1].startsWith(mToPrefix))
211  {
212  int n = 2;
213  if (lines[2].startsWith(mCcPrefix))
214  ++n;
215  if (maxn > n + 1
216  && lines[n].startsWith(mDatePrefix)
217  && lines[n+1].startsWith(mSubjectPrefix))
218  {
219  // Format the email into a text alarm
220  TQString calText;
221  calText = mFromPrefixEn + lines[0].mid(mFromPrefix.length()) + '\n';
222  calText += mToPrefixEn + lines[1].mid(mToPrefix.length()) + '\n';
223  if (n == 3)
224  calText += mCcPrefixEn + lines[2].mid(mCcPrefix.length()) + '\n';
225  calText += mDatePrefixEn + lines[n].mid(mDatePrefix.length()) + '\n';
226  calText += mSubjectPrefixEn + lines[n+1].mid(mSubjectPrefix.length());
227  int i = text.find(mSubjectPrefix);
228  i = text.find('\n', i);
229  if (i > 0)
230  calText += text.mid(i);
231  return calText;
232  }
233  }
234  return text;
235 }
236 
237 /******************************************************************************
238 * Set up messages used by executeDropEvent() and emailHeaders().
239 */
240 void AlarmText::setUpTranslations()
241 {
242  if (mFromPrefix.isNull())
243  {
244  mFromPrefix = EditAlarmDlg::i18n_EmailFrom();
245  mToPrefix = EditAlarmDlg::i18n_EmailTo();
246  mCcPrefix = i18n("Copy-to in email headers", "Cc:");
247  mDatePrefix = i18n("Date:");
248  mSubjectPrefix = EditAlarmDlg::i18n_EmailSubject();
249  }
250 }
251 
252 /******************************************************************************
253 * Return the alarm summary text for either single line or tooltip display.
254 * The maximum number of line returned is determined by 'maxLines'.
255 * If 'truncated' is non-null, it will be set true if the text returned has been
256 * truncated, other than to strip a trailing newline.
257 */
258 TQString AlarmText::summary(const KAEvent& event, int maxLines, bool* truncated)
259 {
260  TQString text = (event.action() == KAEvent::EMAIL) ? event.emailSubject() : event.cleanText();
261  if (event.action() == KAEvent::MESSAGE)
262  {
263  // If the message is the text of an email, return its headers or just subject line
264  TQString subject = emailHeaders(text, (maxLines <= 1));
265  if (!subject.isNull())
266  {
267  if (truncated)
268  *truncated = true;
269  return subject;
270  }
271  }
272  if (truncated)
273  *truncated = false;
274  if (text.contains('\n') < maxLines)
275  return text;
276  int newline = -1;
277  for (int i = 0; i < maxLines; ++i)
278  {
279  newline = text.find('\n', newline + 1);
280  if (newline < 0)
281  return text; // not truncated after all !?!
282  }
283  if (newline == static_cast<int>(text.length()) - 1)
284  return text.left(newline); // text ends in newline
285  if (truncated)
286  *truncated = true;
287  return text.left(newline + (maxLines <= 1 ? 0 : 1)) + TQString::fromLatin1("...");
288 }
represents calendar alarms and events
KAEvent corresponds to a KCal::Event instance.
Definition: alarmevent.h:232