calendarcompat.cpp
00001 /* 00002 * calendarcompat.cpp - compatibility for old calendar file formats 00003 * Program: kalarm 00004 * Copyright © 2001-2006 by David Jarvie <software@astrojar.org.uk> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kalarm.h" 00022 00023 #include <tqfile.h> 00024 #include <tqtextstream.h> 00025 #include <tqstringlist.h> 00026 00027 #include <kapplication.h> 00028 #include <kaboutdata.h> 00029 #include <kdebug.h> 00030 00031 #include <libkcal/calendar.h> 00032 00033 #include "alarmevent.h" 00034 #include "functions.h" 00035 #include "preferences.h" 00036 #include "calendarcompat.h" 00037 00038 using namespace KCal; 00039 00040 00041 /****************************************************************************** 00042 * Find the version of KAlarm which wrote the calendar file, and do any 00043 * necessary conversions to the current format. The calendar is not saved - any 00044 * conversions will only be saved if changes are made later. 00045 */ 00046 void CalendarCompat::fix(KCal::Calendar& calendar, const TQString& localFile) 00047 { 00048 bool version057_UTC = false; 00049 TQString subVersion; 00050 int version = readKAlarmVersion(calendar, subVersion); 00051 if (!version) 00052 { 00053 // The calendar was created either by the current version of KAlarm, 00054 // or another program, so don't do any conversions 00055 return; 00056 } 00057 if (version == KAlarm::Version(0,5,7) && !localFile.isEmpty()) 00058 { 00059 // KAlarm version 0.5.7 - check whether times are stored in UTC, in which 00060 // case it is the KDE 3.0.0 version, which needs adjustment of summer times. 00061 version057_UTC = isUTC(localFile); 00062 kdDebug(5950) << "CalendarCompat::fix(): KAlarm version 0.5.7 (" << (version057_UTC ? "" : "non-") << "UTC)\n"; 00063 } 00064 else 00065 kdDebug(5950) << "CalendarCompat::fix(): KAlarm version " << version << endl; 00066 00067 // Convert events to current KAlarm format for when calendar is saved 00068 KAEvent::convertKCalEvents(calendar, version, version057_UTC); 00069 } 00070 00071 /****************************************************************************** 00072 * Return the KAlarm version which wrote the calendar which has been loaded. 00073 * The format is, for example, 000507 for 0.5.7. 00074 * Reply = 0 if the calendar was created by the current version of KAlarm, 00075 * KAlarm pre-0.3.5, or another program. 00076 */ 00077 int CalendarCompat::readKAlarmVersion(KCal::Calendar& calendar, TQString& subVersion) 00078 { 00079 subVersion = TQString(); 00080 const TQString& prodid = calendar.productId(); 00081 00082 // Find the KAlarm identifier 00083 TQString progname = TQString::fromLatin1(" KAlarm "); 00084 int i = prodid.find(progname, 0, false); 00085 if (i < 0) 00086 { 00087 // Older versions used KAlarm's translated name in the product ID, which 00088 // could have created problems using a calendar in different locales. 00089 progname = TQString(" ") + kapp->aboutData()->programName() + ' '; 00090 i = prodid.find(progname, 0, false); 00091 if (i < 0) 00092 return 0; // calendar wasn't created by KAlarm 00093 } 00094 00095 // Extract the KAlarm version string 00096 TQString ver = prodid.mid(i + progname.length()).stripWhiteSpace(); 00097 i = ver.find('/'); 00098 int j = ver.find(' '); 00099 if (j >= 0 && j < i) 00100 i = j; 00101 if (i <= 0) 00102 return 0; // missing version string 00103 ver = ver.left(i); // ver now contains the KAlarm version string 00104 if (ver == KAlarm::currentCalendarVersionString()) 00105 return 0; // the calendar is in the current KAlarm format 00106 return KAlarm::getVersionNumber(ver, &subVersion); 00107 } 00108 00109 /****************************************************************************** 00110 * Check whether the calendar file has its times stored as UTC times, 00111 * indicating that it was written by the KDE 3.0.0 version of KAlarm 0.5.7. 00112 * Reply = true if times are stored in UTC 00113 * = false if the calendar is a vCalendar, times are not UTC, or any error occurred. 00114 */ 00115 bool CalendarCompat::isUTC(const TQString& localFile) 00116 { 00117 // Read the calendar file into a TQString 00118 TQFile file(localFile); 00119 if (!file.open(IO_ReadOnly)) 00120 return false; 00121 TQTextStream ts(&file); 00122 ts.setEncoding(TQTextStream::Latin1); 00123 TQString text = ts.read(); 00124 file.close(); 00125 00126 // Extract the CREATED property for the first VEVENT from the calendar 00127 TQString VCALENDAR = TQString::fromLatin1("BEGIN:VCALENDAR"); 00128 TQString VEVENT = TQString::fromLatin1("BEGIN:VEVENT"); 00129 TQString CREATED = TQString::fromLatin1("CREATED:"); 00130 TQStringList lines = TQStringList::split(TQChar('\n'), text); 00131 for (TQStringList::ConstIterator it = lines.begin(); it != lines.end(); ++it) 00132 { 00133 if ((*it).startsWith(VCALENDAR)) 00134 { 00135 while (++it != lines.end()) 00136 { 00137 if ((*it).startsWith(VEVENT)) 00138 { 00139 while (++it != lines.end()) 00140 { 00141 if ((*it).startsWith(CREATED)) 00142 return (*it).endsWith("Z"); 00143 } 00144 } 00145 } 00146 break; 00147 } 00148 } 00149 return false; 00150 }