1/*
2 This file is part of the kcalutils library.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
7 Copyright (c) 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
23*/
24/**
25 @file
26 This file is part of the API for handling calendar data and provides
27 static functions for formatting Incidence properties for various purposes.
28
29 @brief
30 Provides methods to format Incidence properties in various ways for display purposes.
31
32 @author Cornelius Schumacher \<schumacher@kde.org\>
33 @author Reinhold Kainhofer \<reinhold@kainhofer.com\>
34 @author Allen Winter \<allen@kdab.com\>
35*/
36#include "stringify.h"
37
38#include <kcalcore/exceptions.h>
39using namespace KCalCore;
40
41#include <KLocale>
42#include <KLocalizedString>
43#include <KGlobal>
44#include <KSystemTimeZone>
45
46using namespace KCalUtils;
47using namespace Stringify;
48
49QString Stringify::incidenceType(Incidence::IncidenceType type)
50{
51 switch (type) {
52 case Incidence::TypeEvent:
53 return i18nc("@item incidence type is event", "event");
54 case Incidence::TypeTodo:
55 return i18nc("@item incidence type is to-do/task", "to-do");
56 case Incidence::TypeJournal:
57 return i18nc("@item incidence type is journal", "journal");
58 case Incidence::TypeFreeBusy:
59 return i18nc("@item incidence type is freebusy", "free/busy");
60 default:
61 return QString();
62 }
63}
64
65QString Stringify::todoCompletedDateTime(const Todo::Ptr &todo,
66 bool shortfmt)
67{
68 return KGlobal::locale()->formatDateTime(todo->completed().dateTime(),
69 (shortfmt ? KLocale::ShortDate :
70 KLocale::LongDate));
71}
72
73QString Stringify::incidenceSecrecy(Incidence::Secrecy secrecy)
74{
75 switch (secrecy) {
76 case Incidence::SecrecyPublic:
77 return i18nc("@item incidence access if for everyone", "Public");
78 case Incidence::SecrecyPrivate:
79 return i18nc("@item incidence access is by owner only", "Private");
80 case Incidence::SecrecyConfidential:
81 return i18nc("@item incidence access is by owner and a controlled group", "Confidential");
82 default: // to make compiler happy
83 return QString();
84 }
85}
86
87QStringList Stringify::incidenceSecrecyList()
88{
89 QStringList list;
90 list << incidenceSecrecy(Incidence::SecrecyPublic);
91 list << incidenceSecrecy(Incidence::SecrecyPrivate);
92 list << incidenceSecrecy(Incidence::SecrecyConfidential);
93
94 return list;
95}
96
97QString Stringify::incidenceStatus(Incidence::Status status)
98{
99 switch (status) {
100 case Incidence::StatusTentative:
101 return i18nc("@item event is tentative", "Tentative");
102 case Incidence::StatusConfirmed:
103 return i18nc("@item event is definite", "Confirmed");
104 case Incidence::StatusCompleted:
105 return i18nc("@item to-do is complete", "Completed");
106 case Incidence::StatusNeedsAction:
107 return i18nc("@item to-do needs action", "Needs-Action");
108 case Incidence::StatusCanceled:
109 return i18nc("@item event orto-do is canceled; journal is removed", "Canceled");
110 case Incidence::StatusInProcess:
111 return i18nc("@item to-do is in process", "In-Process");
112 case Incidence::StatusDraft:
113 return i18nc("@item journal is in draft form", "Draft");
114 case Incidence::StatusFinal:
115 return i18nc("@item journal is in final form", "Final");
116 case Incidence::StatusX:
117 case Incidence::StatusNone:
118 default:
119 return QString();
120 }
121}
122
123QString Stringify::incidenceStatus(const Incidence::Ptr &incidence)
124{
125 if (incidence->status() == Incidence::StatusX) {
126 return incidence->customStatus();
127 } else {
128 return incidenceStatus(incidence->status());
129 }
130}
131
132QString Stringify::attendeeRole(Attendee::Role role)
133{
134 switch (role) {
135 case Attendee::Chair:
136 return i18nc("@item chairperson", "Chair");
137 break;
138 default:
139 case Attendee::ReqParticipant:
140 return i18nc("@item participation is required", "Participant");
141 break;
142 case Attendee::OptParticipant:
143 return i18nc("@item participation is optional", "Optional Participant");
144 break;
145 case Attendee::NonParticipant:
146 return i18nc("@item non-participant copied for information", "Observer");
147 break;
148 }
149}
150
151QStringList Stringify::attendeeRoleList()
152{
153 QStringList list;
154 list << attendeeRole(Attendee::ReqParticipant);
155 list << attendeeRole(Attendee::OptParticipant);
156 list << attendeeRole(Attendee::NonParticipant);
157 list << attendeeRole(Attendee::Chair);
158
159 return list;
160}
161
162QString Stringify::attendeeStatus(Attendee::PartStat status)
163{
164 switch (status) {
165 default:
166 case Attendee::NeedsAction:
167 return i18nc("@item event, to-do or journal needs action", "Needs Action");
168 break;
169 case Attendee::Accepted:
170 return i18nc("@item event, to-do or journal accepted", "Accepted");
171 break;
172 case Attendee::Declined:
173 return i18nc("@item event, to-do or journal declined", "Declined");
174 break;
175 case Attendee::Tentative:
176 return i18nc("@item event or to-do tentatively accepted", "Tentative");
177 break;
178 case Attendee::Delegated:
179 return i18nc("@item event or to-do delegated", "Delegated");
180 break;
181 case Attendee::Completed:
182 return i18nc("@item to-do completed", "Completed");
183 break;
184 case Attendee::InProcess:
185 return i18nc("@item to-do in process of being completed", "In Process");
186 break;
187 case Attendee::None:
188 return i18nc("@item event or to-do status unknown", "Unknown");
189 break;
190 }
191}
192
193QStringList Stringify::attendeeStatusList()
194{
195 QStringList list;
196 list << attendeeStatus(Attendee::NeedsAction);
197 list << attendeeStatus(Attendee::Accepted);
198 list << attendeeStatus(Attendee::Declined);
199 list << attendeeStatus(Attendee::Tentative);
200 list << attendeeStatus(Attendee::Delegated);
201 list << attendeeStatus(Attendee::Completed);
202 list << attendeeStatus(Attendee::InProcess);
203
204 return list;
205}
206
207QString Stringify::formatTime(const KDateTime &dt, bool shortfmt, const KDateTime::Spec &spec)
208{
209 if (spec.isValid()) {
210
211 QString timeZone;
212 if (spec.timeZone() != KSystemTimeZones::local()) {
213 timeZone = QLatin1Char(' ') + spec.timeZone().name();
214 }
215
216 return KGlobal::locale()->formatTime(dt.toTimeSpec(spec).time(), !shortfmt) + timeZone;
217 } else {
218 return KGlobal::locale()->formatTime(dt.time(), !shortfmt);
219 }
220}
221
222QString Stringify::formatDate(const KDateTime &dt, bool shortfmt, const KDateTime::Spec &spec)
223{
224 if (spec.isValid()) {
225
226 QString timeZone;
227 if (spec.timeZone() != KSystemTimeZones::local()) {
228 timeZone = QLatin1Char(' ') + spec.timeZone().name();
229 }
230
231 return
232 KGlobal::locale()->formatDate(dt.toTimeSpec(spec).date(),
233 (shortfmt ? KLocale::ShortDate : KLocale::LongDate)) +
234 timeZone;
235 } else {
236 return
237 KGlobal::locale()->formatDate(dt.date(),
238 (shortfmt ? KLocale::ShortDate : KLocale::LongDate));
239 }
240}
241
242QString Stringify::formatDateTime(const KDateTime &dt, bool allDay,
243 bool shortfmt, const KDateTime::Spec &spec)
244{
245 if (allDay) {
246 return formatDate(dt, shortfmt, spec);
247 }
248
249 if (spec.isValid()) {
250 QString timeZone;
251 if (spec.timeZone() != KSystemTimeZones::local()) {
252 timeZone = QLatin1Char(' ') + spec.timeZone().name();
253 }
254
255 return KGlobal::locale()->formatDateTime(
256 dt.toTimeSpec(spec).dateTime(),
257 (shortfmt ? KLocale::ShortDate : KLocale::LongDate)) + timeZone;
258 } else {
259 return KGlobal::locale()->formatDateTime(
260 dt.dateTime(),
261 (shortfmt ? KLocale::ShortDate : KLocale::LongDate));
262 }
263}
264
265QString Stringify::errorMessage(const Exception &exception)
266{
267 QString message;
268
269 switch (exception.code()) {
270 case Exception::LoadError:
271 message = i18nc("@item", "Load Error");
272 break;
273 case Exception::SaveError:
274 message = i18nc("@item", "Save Error");
275 break;
276 case Exception::ParseErrorIcal:
277 message = i18nc("@item", "Parse Error in libical");
278 break;
279 case Exception::ParseErrorKcal:
280 message = i18nc("@item", "Parse Error in the kcalcore library");
281 break;
282 case Exception::NoCalendar:
283 message = i18nc("@item", "No calendar component found.");
284 break;
285 case Exception::CalVersion1:
286 message = i18nc("@item", "Expected iCalendar, got vCalendar format");
287 break;
288 case Exception::CalVersion2:
289 message = i18nc("@item", "iCalendar Version 2.0 detected.");
290 break;
291 case Exception::CalVersionUnknown:
292 message = i18nc("@item", "Expected iCalendar, got unknown format");
293 break;
294 case Exception::Restriction:
295 message = i18nc("@item", "Restriction violation");
296 break;
297 case Exception::NoWritableFound:
298 message = i18nc("@item", "No writable resource found");
299 break;
300 case Exception::SaveErrorOpenFile:
301 Q_ASSERT(exception.arguments().count() == 1);
302 message = i18nc("@item", "Error saving to '%1'.", exception.arguments()[0]);
303 break;
304 case Exception::SaveErrorSaveFile:
305 Q_ASSERT(exception.arguments().count() == 1);
306 message = i18nc("@item", "Could not save '%1'", exception.arguments()[0]);
307 break;
308 case Exception::LibICalError:
309 message = i18nc("@item", "libical error");
310 break;
311 case Exception::VersionPropertyMissing:
312 message = i18nc("@item", "No VERSION property found");
313 break;
314 case Exception::ExpectedCalVersion2:
315 message = i18nc("@item", "Expected iCalendar, got vCalendar format");
316 break;
317 case Exception::ExpectedCalVersion2Unknown:
318 message = i18nc("@item", "Expected iCalendar, got unknown format");
319 break;
320 case Exception::ParseErrorNotIncidence:
321 message = i18nc("@item", "object is not a freebusy, event, todo or journal");
322 break;
323 case Exception::ParseErrorEmptyMessage:
324 message = i18nc("@item", "messageText is empty, unable to parse into a ScheduleMessage");
325 break;
326 case Exception::ParseErrorUnableToParse:
327 message = i18nc("@item", "icalparser is unable to parse messageText into a ScheduleMessage");
328 break;
329 case Exception::ParseErrorMethodProperty:
330 message = i18nc("@item", "message does not contain ICAL_METHOD_PROPERTY");
331 break;
332 case Exception::UserCancel:
333 // no real error; the user canceled the operation
334 break;
335
336 }
337
338 return message;
339}
340
341QString Stringify::scheduleMessageStatus(ScheduleMessage::Status status)
342{
343 switch (status) {
344 case ScheduleMessage::PublishNew:
345 return i18nc("@item this is a new scheduling message",
346 "New Scheduling Message");
347 case ScheduleMessage::PublishUpdate:
348 return i18nc("@item this is an update to an existing scheduling message",
349 "Updated Scheduling Message");
350 case ScheduleMessage::Obsolete:
351 return i18nc("@item obsolete status", "Obsolete");
352 case ScheduleMessage::RequestNew:
353 return i18nc("@item this is a request for a new scheduling message",
354 "New Scheduling Message Request");
355 case ScheduleMessage::RequestUpdate:
356 return i18nc("@item this is a request for an update to an existing scheduling message",
357 "Updated Scheduling Message Request");
358 default:
359 return i18nc("@item unknown status", "Unknown Status: %1", int(status));
360 }
361}
362
363QString Stringify::secrecyName(Incidence::Secrecy secrecy)
364{
365 switch (secrecy) {
366 case Incidence::SecrecyPublic:
367 return i18nc("@item incidence access if for everyone", "Public");
368 case Incidence::SecrecyPrivate:
369 return i18nc("@item incidence access is by owner only", "Private");
370 case Incidence::SecrecyConfidential:
371 return i18nc("@item incidence access is by owner and a controlled group", "Confidential");
372 default:
373 return QString(); // to make compilers happy
374 }
375}
376
377QStringList Stringify::secrecyList()
378{
379 QStringList list;
380 list << secrecyName(Incidence::SecrecyPublic);
381 list << secrecyName(Incidence::SecrecyPrivate);
382 list << secrecyName(Incidence::SecrecyConfidential);
383
384 return list;
385}
386