kalarm

calendarcompat.cpp
1/*
2 * calendarcompat.cpp - compatibility for old calendar file formats
3 * Program: kalarm
4 * Copyright © 2001-2006 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
23#include <tqfile.h>
24#include <tqtextstream.h>
25#include <tqstringlist.h>
26
27#include <tdeapplication.h>
28#include <tdeaboutdata.h>
29#include <kdebug.h>
30
31#include <libkcal/calendar.h>
32
33#include "alarmevent.h"
34#include "functions.h"
35#include "preferences.h"
36#include "calendarcompat.h"
37
38using namespace KCal;
39
40
41/******************************************************************************
42* Find the version of KAlarm which wrote the calendar file, and do any
43* necessary conversions to the current format. The calendar is not saved - any
44* conversions will only be saved if changes are made later.
45*/
46void CalendarCompat::fix(KCal::Calendar& calendar, const TQString& localFile)
47{
48 bool version057_UTC = false;
49 TQString subVersion;
50 int version = readKAlarmVersion(calendar, subVersion);
51 if (!version)
52 {
53 // The calendar was created either by the current version of KAlarm,
54 // or another program, so don't do any conversions
55 return;
56 }
57 if (version == KAlarm::Version(0,5,7) && !localFile.isEmpty())
58 {
59 // KAlarm version 0.5.7 - check whether times are stored in UTC, in which
60 // case it is the KDE 3.0.0 version, which needs adjustment of summer times.
61 version057_UTC = isUTC(localFile);
62 kdDebug(5950) << "CalendarCompat::fix(): KAlarm version 0.5.7 (" << (version057_UTC ? "" : "non-") << "UTC)\n";
63 }
64 else
65 kdDebug(5950) << "CalendarCompat::fix(): KAlarm version " << version << endl;
66
67 // Convert events to current KAlarm format for when calendar is saved
68 KAEvent::convertKCalEvents(calendar, version, version057_UTC);
69}
70
71/******************************************************************************
72* Return the KAlarm version which wrote the calendar which has been loaded.
73* The format is, for example, 000507 for 0.5.7.
74* Reply = 0 if the calendar was created by the current version of KAlarm,
75* KAlarm pre-0.3.5, or another program.
76*/
77int CalendarCompat::readKAlarmVersion(KCal::Calendar& calendar, TQString& subVersion)
78{
79 subVersion = TQString();
80 const TQString& prodid = calendar.productId();
81
82 // Find the KAlarm identifier
83 TQString progname = TQString::fromLatin1(" KAlarm ");
84 int i = prodid.find(progname, 0, false);
85 if (i < 0)
86 {
87 // Older versions used KAlarm's translated name in the product ID, which
88 // could have created problems using a calendar in different locales.
89 progname = TQString(" ") + tdeApp->aboutData()->programName() + ' ';
90 i = prodid.find(progname, 0, false);
91 if (i < 0)
92 return 0; // calendar wasn't created by KAlarm
93 }
94
95 // Extract the KAlarm version string
96 TQString ver = prodid.mid(i + progname.length()).stripWhiteSpace();
97 i = ver.find('/');
98 int j = ver.find(' ');
99 if (j >= 0 && j < i)
100 i = j;
101 if (i <= 0)
102 return 0; // missing version string
103 ver = ver.left(i); // ver now contains the KAlarm version string
104 if (ver == KAlarm::currentCalendarVersionString())
105 return 0; // the calendar is in the current KAlarm format
106 return KAlarm::getVersionNumber(ver, &subVersion);
107}
108
109/******************************************************************************
110 * Check whether the calendar file has its times stored as UTC times,
111 * indicating that it was written by the KDE 3.0.0 version of KAlarm 0.5.7.
112 * Reply = true if times are stored in UTC
113 * = false if the calendar is a vCalendar, times are not UTC, or any error occurred.
114 */
115bool CalendarCompat::isUTC(const TQString& localFile)
116{
117 // Read the calendar file into a TQString
118 TQFile file(localFile);
119 if (!file.open(IO_ReadOnly))
120 return false;
121 TQTextStream ts(&file);
122 ts.setEncoding(TQTextStream::Latin1);
123 TQString text = ts.read();
124 file.close();
125
126 // Extract the CREATED property for the first VEVENT from the calendar
127 TQString VCALENDAR = TQString::fromLatin1("BEGIN:VCALENDAR");
128 TQString VEVENT = TQString::fromLatin1("BEGIN:VEVENT");
129 TQString CREATED = TQString::fromLatin1("CREATED:");
130 TQStringList lines = TQStringList::split(TQChar('\n'), text);
131 for (TQStringList::ConstIterator it = lines.begin(); it != lines.end(); ++it)
132 {
133 if ((*it).startsWith(VCALENDAR))
134 {
135 while (++it != lines.end())
136 {
137 if ((*it).startsWith(VEVENT))
138 {
139 while (++it != lines.end())
140 {
141 if ((*it).startsWith(CREATED))
142 return (*it).endsWith("Z");
143 }
144 }
145 }
146 break;
147 }
148 }
149 return false;
150}
represents calendar alarms and events
TQString productId()
miscellaneous functions