23#include "kacalendar.h"
28#ifndef KALARMCAL_USE_KRESOURCES
29#include "collectionattribute.h"
35#include <kmessagebox.h>
37#include <kcal/event.h>
38#include <kcal/alarm.h>
39#include <kcal/calendarlocal.h>
43#include <klocalizedstring.h>
51#ifndef KALARMCAL_USE_KRESOURCES
52using namespace KCalCore;
58static const KCatalogLoader loader(QLatin1String(
"libkalarmcal"));
63#ifndef KALARMCAL_USE_KRESOURCES
64const QLatin1String MIME_BASE(
"application/x-vnd.kde.alarm");
65const QLatin1String MIME_ACTIVE(
"application/x-vnd.kde.alarm.active");
66const QLatin1String MIME_ARCHIVED(
"application/x-vnd.kde.alarm.archived");
67const QLatin1String MIME_TEMPLATE(
"application/x-vnd.kde.alarm.template");
70static const QByteArray VERSION_PROPERTY(
"VERSION");
72static bool isUTC(
const QString& localFile);
77#ifndef KALARMCAL_USE_KRESOURCES
78 static int readKAlarmVersion(
const FileStorage::Ptr&, QString& subVersion, QString& versionString);
80 static int readKAlarmVersion(CalendarLocal&,
const QString& localFile, QString& subVersion, QString& versionString);
83 static QByteArray mIcalProductId;
86QByteArray Private::mIcalProductId;
93const QByteArray
APPNAME(
"KALARM");
95void setProductId(
const QByteArray& progName,
const QByteArray& progVersion)
97 Private::mIcalProductId = QByteArray(
"-//K Desktop Environment//NONSGML " + progName +
" " + progVersion +
"//EN");
102 return Private::mIcalProductId.isEmpty() ? QByteArray(
"-//K Desktop Environment//NONSGML //EN") : Private::mIcalProductId;
108#ifndef KALARMCAL_USE_KRESOURCES
126#ifndef KALARMCAL_USE_KRESOURCES
129int updateVersion(CalendarLocal& calendar,
const QString& localFile, QString& versionString)
133#ifndef KALARMCAL_USE_KRESOURCES
134 const int version = Private::readKAlarmVersion(fileStorage, subVersion, versionString);
136 const int version = Private::readKAlarmVersion(calendar, localFile, subVersion, versionString);
145#ifndef KALARMCAL_USE_KRESOURCES
146 const QString localFile = fileStorage->fileName();
149 if (version == KAlarmCal::Version(0,5,7) && !localFile.isEmpty())
153 if (isUTC(localFile))
155 kDebug() <<
"KAlarm version 0.5.7 (" << (ver < 0 ?
"" :
"non-") <<
"UTC)";
158 kDebug() <<
"KAlarm version" << version;
161#ifndef KALARMCAL_USE_KRESOURCES
178#ifndef KALARMCAL_USE_KRESOURCES
179int Private::readKAlarmVersion(
const FileStorage::Ptr& fileStorage, QString& subVersion, QString& versionString)
181int Private::readKAlarmVersion(CalendarLocal& calendar,
const QString& localFile, QString& subVersion, QString& versionString)
185#ifndef KALARMCAL_USE_KRESOURCES
188 kDebug() <<
"File=" << fileStorage->fileName() <<
", version=" << versionString;
194 if (versionString.isEmpty())
198#ifndef KALARMCAL_USE_KRESOURCES
199 const QString prodid = calendar->productId();
201 const QString prodid = calendar.productId();
203 if (prodid.isEmpty())
207#ifndef KALARMCAL_USE_KRESOURCES
208 const QFileInfo fi(fileStorage->fileName());
210 const QFileInfo fi(localFile);
217 QString progname = QLatin1String(
" KAlarm ");
218 int i = prodid.indexOf(progname, 0, Qt::CaseInsensitive);
223 progname = QLatin1String(
" ") + i18n(
"KAlarm") + QLatin1Char(
' ');
224 i = prodid.indexOf(progname, 0, Qt::CaseInsensitive);
230 versionString = prodid.mid(i + progname.length()).trimmed();
231 i = versionString.indexOf(QLatin1Char(
'/'));
232 const int j = versionString.indexOf(QLatin1Char(
' '));
237 versionString = versionString.left(i);
241 const int ver = KAlarmCal::getVersionNumber(versionString, &subVersion);
244 return KAlarmCal::getVersionNumber(versionString, &subVersion);
253bool isUTC(
const QString& localFile)
256 QFile file(localFile);
257 if (!file.open(QIODevice::ReadOnly))
259 QTextStream ts(&file);
260 ts.setCodec(
"ISO 8859-1");
261 const QByteArray text = ts.readAll().toLocal8Bit();
265 const QByteArray BEGIN_VCALENDAR(
"BEGIN:VCALENDAR");
266 const QByteArray BEGIN_VEVENT(
"BEGIN:VEVENT");
267 const QByteArray CREATED(
"CREATED:");
268 const QList<QByteArray> lines = text.split(
'\n');
269 for (
int i = 0, end = lines.count(); i < end; ++i)
271 if (lines[i].startsWith(BEGIN_VCALENDAR))
275 if (lines[i].startsWith(BEGIN_VEVENT))
279 if (lines[i].startsWith(CREATED))
280 return lines[i].endsWith(
'Z');
300 : STATUS_PROPERTY(
"TYPE"),
301 ACTIVE_STATUS(QLatin1String(
"ACTIVE")),
302 TEMPLATE_STATUS(QLatin1String(
"TEMPLATE")),
303 ARCHIVED_STATUS(QLatin1String(
"ARCHIVED")),
304 DISPLAYING_STATUS(QLatin1String(
"DISPLAYING")),
305 ARCHIVED_UID(QLatin1String(
"-exp-")),
306 DISPLAYING_UID(QLatin1String(
"-disp-")),
307 TEMPLATE_UID(QLatin1String(
"-tmpl-"))
311 const QByteArray STATUS_PROPERTY;
312 const QString ACTIVE_STATUS;
313 const QString TEMPLATE_STATUS;
314 const QString ARCHIVED_STATUS;
315 const QString DISPLAYING_STATUS;
318 const QString ARCHIVED_UID;
319 const QString DISPLAYING_UID;
322 const QString TEMPLATE_UID;
324K_GLOBAL_STATIC(StaticStrings, staticStrings)
329QString uid(
const QString&
id,
Type status)
334 if ((i = result.indexOf(staticStrings->ARCHIVED_UID)) > 0)
337 len = staticStrings->ARCHIVED_UID.length();
339 else if ((i = result.indexOf(staticStrings->DISPLAYING_UID)) > 0)
342 len = staticStrings->DISPLAYING_UID.length();
347 i = result.lastIndexOf(QLatin1Char(
'-'));
357 if (status != oldType && i > 0)
362 case ARCHIVED: part = staticStrings->ARCHIVED_UID;
break;
363 case DISPLAYING: part = staticStrings->DISPLAYING_UID;
break;
367 default: part = QLatin1String(
"-");
break;
369 result.replace(i, len, part);
383#ifndef KALARMCAL_USE_KRESOURCES
386Type status(
const Event* event, QString* param)
390 typedef QMap<QString, Type> PropertyMap;
391 static PropertyMap properties;
392 if (properties.isEmpty())
394 properties[staticStrings->ACTIVE_STATUS] =
ACTIVE;
395 properties[staticStrings->TEMPLATE_STATUS] =
TEMPLATE;
396 properties[staticStrings->ARCHIVED_STATUS] =
ARCHIVED;
397 properties[staticStrings->DISPLAYING_STATUS] =
DISPLAYING;
405 if (alarms.isEmpty())
408 const QString
property =
event->customProperty(
KACalendar::APPNAME, staticStrings->STATUS_PROPERTY);
409 if (!property.isEmpty())
413 PropertyMap::ConstIterator it = properties.constFind(property);
414 if (it != properties.constEnd())
416 const int i =
property.indexOf(QLatin1Char(
';'));
419 it = properties.constFind(property.left(i));
420 if (it == properties.constEnd())
423 *param =
property.mid(i + 1);
429 const QString uid =
event->uid();
430 if (uid.indexOf(staticStrings->ARCHIVED_UID) > 0)
432 if (uid.indexOf(staticStrings->TEMPLATE_UID) > 0)
444#ifndef KALARMCAL_USE_KRESOURCES
445void setStatus(
const Event::Ptr& event,
Type status,
const QString& param)
447void setStatus(
Event* event,
Type status,
const QString& param)
455 case ACTIVE: text = staticStrings->ACTIVE_STATUS;
break;
456 case TEMPLATE: text = staticStrings->TEMPLATE_STATUS;
break;
457 case ARCHIVED: text = staticStrings->ARCHIVED_STATUS;
break;
458 case DISPLAYING: text = staticStrings->DISPLAYING_STATUS;
break;
463 if (!param.isEmpty())
464 text += QLatin1Char(
';') + param;
468#ifndef KALARMCAL_USE_KRESOURCES
485 if (
type == MIME_ACTIVE)
487 if (
type == MIME_ARCHIVED)
489 if (
type == MIME_TEMPLATE)
499 case ACTIVE:
return MIME_ACTIVE;
500 case ARCHIVED:
return MIME_ARCHIVED;
501 case TEMPLATE:
return MIME_TEMPLATE;
502 default:
return QString();
509 for (
int i = 1;
types; i <<= 1)
static int currentCalendarVersion()
Return the current KAlarm calendar storage format version.
static QByteArray currentCalendarVersionString()
Return the current KAlarm calendar storage format version.
static bool convertKCalEvents(const KCalCore::Calendar::Ptr &, int calendarVersion)
If a calendar was written by a previous version of KAlarm, do any necessary format conversions on the...
QSharedPointer< Calendar > Ptr
QSharedPointer< Event > Ptr
QSharedPointer< FileStorage > Ptr
Type type(const QString &mimeType)
Return the alarm Type for a mime type string.
QString mimeType(Type type)
Return the mime type string corresponding to an alarm Type.
QStringList mimeTypes(Types types)
Return the mime type strings corresponding to alarm Types.
Type
The category of an event, indicated by the middle part of its UID.
@ DISPLAYING
the event is currently being displayed
@ TEMPLATE
the event is an alarm template
@ ACTIVE
the event is currently active
@ ARCHIVED
the event is archived
@ EMPTY
the event has no alarms
Types types(const QStringList &mimeTypes)
Return the alarm Types for a list of mime type strings.
int updateVersion(const FileStorage::Ptr &fileStorage, QString &versionString)
Check the version of KAlarm which wrote a calendar file, and convert it in memory to the current KAla...
@ CurrentFormat
current KAlarm format
@ IncompatibleFormat
not written by KAlarm, or a newer KAlarm version
void setProductId(const QByteArray &progName, const QByteArray &progVersion)
Set the program name and version for use in calendars.
QByteArray icalProductId()
Return the product ID string for use in calendars.
const QByteArray APPNAME
The application name ("KALARM") used in calendar properties.
void setKAlarmVersion(const Calendar::Ptr &calendar)
Set the KAlarm version custom property for a calendar.