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>
49 #include <QTextStream>
51 #ifndef KALARMCAL_USE_KRESOURCES
52 using namespace KCalCore;
58 static const KCatalogLoader loader(QLatin1String(
"libkalarmcal"));
63 #ifndef KALARMCAL_USE_KRESOURCES
64 const QLatin1String MIME_BASE(
"application/x-vnd.kde.alarm");
65 const QLatin1String MIME_ACTIVE(
"application/x-vnd.kde.alarm.active");
66 const QLatin1String MIME_ARCHIVED(
"application/x-vnd.kde.alarm.archived");
67 const QLatin1String MIME_TEMPLATE(
"application/x-vnd.kde.alarm.template");
70 static const QByteArray VERSION_PROPERTY(
"VERSION");
72 static 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;
86 QByteArray Private::mIcalProductId;
93 const QByteArray
APPNAME(
"KALARM");
95 void 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
111 calendar->setCustomProperty(
APPNAME, VERSION_PROPERTY, QString::fromLatin1(KAEvent::currentCalendarVersionString()));
114 void setKAlarmVersion(CalendarLocal& calendar)
116 calendar.setCustomProperty(APPNAME, VERSION_PROPERTY, QString::fromLatin1(KAEvent::currentCalendarVersionString()));
126 #ifndef KALARMCAL_USE_KRESOURCES
129 int 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
162 KAEvent::convertKCalEvents(fileStorage->calendar(), ver);
164 KAEvent::convertKCalEvents(calendar, ver);
178 #ifndef KALARMCAL_USE_KRESOURCES
179 int Private::readKAlarmVersion(
const FileStorage::Ptr& fileStorage, QString& subVersion, QString& versionString)
181 int Private::readKAlarmVersion(CalendarLocal& calendar,
const QString& localFile, QString& subVersion, QString& versionString)
185 #ifndef KALARMCAL_USE_KRESOURCES
187 versionString = calendar->customProperty(KACalendar::APPNAME, VERSION_PROPERTY);
188 kDebug() <<
"File=" << fileStorage->fileName() <<
", version=" << versionString;
191 versionString = calendar.customProperty(KACalendar::APPNAME, VERSION_PROPERTY);
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);
213 return KACalendar::CurrentFormat;
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);
226 return KACalendar::IncompatibleFormat;
230 versionString = prodid.mid(i + progname.length()).trimmed();
231 i = versionString.indexOf(QLatin1Char(
'/'));
232 const int j = versionString.indexOf(QLatin1Char(
' '));
236 return KACalendar::IncompatibleFormat;
237 versionString = versionString.left(i);
239 if (versionString == QLatin1String(KAEvent::currentCalendarVersionString()))
240 return KACalendar::CurrentFormat;
241 const int ver = KAlarmCal::getVersionNumber(versionString, &subVersion);
242 if (ver == KAEvent::currentCalendarVersion())
243 return KACalendar::CurrentFormat;
244 return KAlarmCal::getVersionNumber(versionString, &subVersion);
253 bool 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;
324 K_GLOBAL_STATIC(StaticStrings, staticStrings)
329 QString 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
386 Type 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
445 void setStatus(
const Event::Ptr& event,
Type status,
const QString& param)
447 void 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;
460 event->removeCustomProperty(KACalendar::APPNAME, staticStrings->STATUS_PROPERTY);
463 if (!param.isEmpty())
464 text += QLatin1Char(
';') + param;
465 event->setCustomProperty(KACalendar::APPNAME, staticStrings->STATUS_PROPERTY, text);
468 #ifndef KALARMCAL_USE_KRESOURCES
480 Types
types(
const QStringList& mimeTypes)
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)