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;
59 static const KCatalogLoader loader(
"libkalarmcal");
64 #ifndef KALARMCAL_USE_KRESOURCES
65 const QLatin1String MIME_BASE(
"application/x-vnd.kde.alarm");
66 const QLatin1String MIME_ACTIVE(
"application/x-vnd.kde.alarm.active");
67 const QLatin1String MIME_ARCHIVED(
"application/x-vnd.kde.alarm.archived");
68 const QLatin1String MIME_TEMPLATE(
"application/x-vnd.kde.alarm.template");
71 static const QByteArray VERSION_PROPERTY(
"VERSION");
73 static bool isUTC(
const QString& localFile);
79 #ifndef KALARMCAL_USE_KRESOURCES
80 static int readKAlarmVersion(
const FileStorage::Ptr&, QString& subVersion, QString& versionString);
82 static int readKAlarmVersion(CalendarLocal&,
const QString& localFile, QString& subVersion, QString& versionString);
85 static QByteArray mIcalProductId;
88 QByteArray Private::mIcalProductId;
94 const QByteArray
APPNAME(
"KALARM");
96 void setProductId(
const QByteArray& progName,
const QByteArray& progVersion)
98 Private::mIcalProductId = QByteArray(
"-//K Desktop Environment//NONSGML " + progName +
" " + progVersion +
"//EN");
103 return Private::mIcalProductId.isEmpty() ? QByteArray(
"-//K Desktop Environment//NONSGML //EN") : Private::mIcalProductId;
109 #ifndef KALARMCAL_USE_KRESOURCES
112 calendar->setCustomProperty(APPNAME, VERSION_PROPERTY, QString::fromLatin1(KAEvent::currentCalendarVersionString()));
115 void setKAlarmVersion(CalendarLocal& calendar)
117 calendar.setCustomProperty(APPNAME, VERSION_PROPERTY, QString::fromLatin1(KAEvent::currentCalendarVersionString()));
127 #ifndef KALARMCAL_USE_KRESOURCES
130 int updateVersion(CalendarLocal& calendar,
const QString& localFile, QString& versionString)
134 #ifndef KALARMCAL_USE_KRESOURCES
135 int version = Private::readKAlarmVersion(fileStorage, subVersion, versionString);
137 int version = Private::readKAlarmVersion(calendar, localFile, subVersion, versionString);
146 #ifndef KALARMCAL_USE_KRESOURCES
147 const QString localFile = fileStorage->fileName();
150 if (version == KAlarmCal::Version(0,5,7) && !localFile.isEmpty())
154 if (isUTC(localFile))
156 kDebug() <<
"KAlarm version 0.5.7 (" << (ver < 0 ?
"" :
"non-") <<
"UTC)";
159 kDebug() <<
"KAlarm version" << version;
162 #ifndef KALARMCAL_USE_KRESOURCES
163 KAEvent::convertKCalEvents(fileStorage->calendar(), ver);
165 KAEvent::convertKCalEvents(calendar, ver);
179 #ifndef KALARMCAL_USE_KRESOURCES
180 int Private::readKAlarmVersion(
const FileStorage::Ptr& fileStorage, QString& subVersion, QString& versionString)
182 int Private::readKAlarmVersion(CalendarLocal& calendar,
const QString& localFile, QString& subVersion, QString& versionString)
186 #ifndef KALARMCAL_USE_KRESOURCES
188 versionString = calendar->customProperty(KACalendar::APPNAME, VERSION_PROPERTY);
189 kDebug() <<
"File=" << fileStorage->fileName() <<
", version=" << versionString;
192 versionString = calendar.customProperty(KACalendar::APPNAME, VERSION_PROPERTY);
195 if (versionString.isEmpty())
199 #ifndef KALARMCAL_USE_KRESOURCES
200 const QString prodid = calendar->productId();
202 const QString prodid = calendar.productId();
204 if (prodid.isEmpty())
208 #ifndef KALARMCAL_USE_KRESOURCES
209 QFileInfo fi(fileStorage->fileName());
211 QFileInfo fi(localFile);
214 return KACalendar::CurrentFormat;
218 QString progname = QLatin1String(
" KAlarm ");
219 int i = prodid.indexOf(progname, 0, Qt::CaseInsensitive);
224 progname = QString(
" ") + i18n(
"KAlarm") +
' ';
225 i = prodid.indexOf(progname, 0, Qt::CaseInsensitive);
227 return KACalendar::IncompatibleFormat;
231 versionString = prodid.mid(i + progname.length()).trimmed();
232 i = versionString.indexOf(
'/');
233 int j = versionString.indexOf(
' ');
237 return KACalendar::IncompatibleFormat;
238 versionString = versionString.left(i);
240 if (versionString == KAEvent::currentCalendarVersionString())
241 return KACalendar::CurrentFormat;
242 int ver = KAlarmCal::getVersionNumber(versionString, &subVersion);
243 if (ver == KAEvent::currentCalendarVersion())
244 return KACalendar::CurrentFormat;
245 return KAlarmCal::getVersionNumber(versionString, &subVersion);
254 bool isUTC(
const QString& localFile)
257 QFile file(localFile);
258 if (!file.open(QIODevice::ReadOnly))
260 QTextStream ts(&file);
261 ts.setCodec(
"ISO 8859-1");
262 QByteArray text = ts.readAll().toLocal8Bit();
266 const QByteArray BEGIN_VCALENDAR(
"BEGIN:VCALENDAR");
267 const QByteArray BEGIN_VEVENT(
"BEGIN:VEVENT");
268 const QByteArray CREATED(
"CREATED:");
269 const QList<QByteArray> lines = text.split(
'\n');
270 for (
int i = 0, end = lines.count(); i < end; ++i)
272 if (lines[i].startsWith(BEGIN_VCALENDAR))
276 if (lines[i].startsWith(BEGIN_VEVENT))
280 if (lines[i].startsWith(CREATED))
281 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(
'-');
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 int i =
property.indexOf(
';');
419 it = properties.constFind(property.left(i));
420 if (it == properties.constEnd())
423 *param =
property.mid(i + 1);
429 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())
465 event->setCustomProperty(KACalendar::APPNAME, staticStrings->STATUS_PROPERTY, text);
468 #ifndef KALARMCAL_USE_KRESOURCES
471 if (mimeType == MIME_ACTIVE)
473 if (mimeType == MIME_ARCHIVED)
475 if (mimeType == MIME_TEMPLATE)
480 Types
types(
const QStringList& mimeTypes)
483 foreach (
const QString&
type, 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)
Type type(const QString &mimeType)
Return the alarm Type for a mime type string.
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...
the event is an alarm template
QSharedPointer< Event > Ptr
not written by KAlarm, or a newer KAlarm version
QByteArray icalProductId()
Return the product ID string for use in calendars.
Type
The category of an event, indicated by the middle part of its UID.
const QByteArray APPNAME
The application name ("KALARM") used in calendar properties.
the event is currently being displayed
the event is currently active
void setKAlarmVersion(const Calendar::Ptr &calendar)
Set the KAlarm version custom property for a calendar.
QSharedPointer< FileStorage > Ptr
QStringList mimeTypes(Types types)
Return the mime type strings corresponding to alarm Types.
QSharedPointer< Calendar > Ptr
QString mimeType(Type type)
Return the mime type string corresponding to an alarm Type.
void setProductId(const QByteArray &progName, const QByteArray &progVersion)
Set the program name and version for use in calendars.
Types types(const QStringList &mimeTypes)
Return the alarm Types for a list of mime type strings.