kalarm

calendarcompat.cpp

00001 /*
00002  *  calendarcompat.cpp -  compatibility for old calendar file formats
00003  *  Program:  kalarm
00004  *  Copyright (C) 2001 - 2005 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 <qfile.h>
00024 #include <qtextstream.h>
00025 
00026 #include <kapplication.h>
00027 #include <kaboutdata.h>
00028 #include <kdebug.h>
00029 
00030 extern "C" {
00031 #include <libical/ical.h>
00032 }
00033 #include <libkcal/calendar.h>
00034 
00035 #include "alarmevent.h"
00036 #include "functions.h"
00037 #include "preferences.h"
00038 #include "calendarcompat.h"
00039 
00040 using namespace KCal;
00041 
00042 
00043 /******************************************************************************
00044 * Find the version of KAlarm which wrote the calendar file, and do any
00045 * necessary conversions to the current format. The calendar is not saved - any
00046 * conversions will only be saved if changes are made later.
00047 */
00048 void CalendarCompat::fix(KCal::Calendar& calendar, const QString& localFile)
00049 {
00050     bool version057_UTC = false;
00051     QString subVersion;
00052     int version = readKAlarmVersion(calendar, subVersion);
00053     if (!version)
00054     {
00055         // The calendar was created either by the current version of KAlarm,
00056         // or another program, so don't do any conversions
00057         return;
00058     }
00059     if (version == KAlarm::Version(0,5,7)  &&  !localFile.isEmpty())
00060     {
00061         // KAlarm version 0.5.7 - check whether times are stored in UTC, in which
00062         // case it is the KDE 3.0.0 version, which needs adjustment of summer times.
00063         version057_UTC = isUTC(localFile);
00064         kdDebug(5950) << "CalendarCompat::fix(): KAlarm version 0.5.7 (" << (version057_UTC ? "" : "non-") << "UTC)\n";
00065     }
00066     else
00067         kdDebug(5950) << "CalendarCompat::fix(): KAlarm version " << version << endl;
00068 
00069     // Convert events to current KAlarm format for when calendar is saved
00070     KAEvent::convertKCalEvents(calendar, version, version057_UTC);
00071 }
00072 
00073 /******************************************************************************
00074 * Return the KAlarm version which wrote the calendar which has been loaded.
00075 * The format is, for example, 000507 for 0.5.7.
00076 * Reply = 0 if the calendar was created by the current version of KAlarm,
00077 *           KAlarm pre-0.3.5, or another program.
00078 */
00079 int CalendarCompat::readKAlarmVersion(KCal::Calendar& calendar, QString& subVersion)
00080 {
00081     subVersion = QString::null;
00082     const QString& prodid = calendar.productId();
00083 
00084     // Find the KAlarm identifier
00085     QString progname = QString::fromLatin1(" KAlarm ");
00086     int i = prodid.find(progname, 0, false);
00087     if (i < 0)
00088     {
00089         // Older versions used KAlarm's translated name in the product ID, which
00090         // could have created problems using a calendar in different locales.
00091         progname = QString(" ") + kapp->aboutData()->programName() + " ";
00092         i = prodid.find(progname, 0, false);
00093         if (i < 0)
00094             return 0;    // calendar wasn't created by KAlarm
00095     }
00096 
00097     // Extract the KAlarm version string
00098     QString ver = prodid.mid(i + progname.length()).stripWhiteSpace();
00099     i = ver.find('/');
00100     int j = ver.find(' ');
00101     if (j >= 0  &&  j < i)
00102         i = j;
00103     if (i <= 0)
00104         return 0;    // missing version string
00105     ver = ver.left(i);     // ver now contains the KAlarm version string
00106     if (ver == KALARM_VERSION)
00107         return 0;      // the calendar was created by the current KAlarm version
00108     return KAlarm::getVersionNumber(ver, &subVersion);
00109 }
00110 
00111 /******************************************************************************
00112  * Check whether the calendar file has its times stored as UTC times,
00113  * indicating that it was written by the KDE 3.0.0 version of KAlarm 0.5.7.
00114  * Reply = true if times are stored in UTC
00115  *       = false if the calendar is a vCalendar, times are not UTC, or any error occurred.
00116  */
00117 bool CalendarCompat::isUTC(const QString& localFile)
00118 {
00119     // Read the calendar file into a QString
00120     QFile file(localFile);
00121     if (!file.open(IO_ReadOnly))
00122         return false;
00123     QTextStream ts(&file);
00124     ts.setEncoding(QTextStream::UnicodeUTF8);
00125     QString text = ts.read();
00126     file.close();
00127 
00128     // Extract the CREATED property for the first VEVENT from the calendar
00129     bool result = false;
00130     icalcomponent* calendar = icalcomponent_new_from_string(text.local8Bit().data());
00131     if (calendar)
00132     {
00133         if (icalcomponent_isa(calendar) == ICAL_VCALENDAR_COMPONENT)
00134         {
00135             icalcomponent* c = icalcomponent_get_first_component(calendar, ICAL_VEVENT_COMPONENT);
00136             if (c)
00137             {
00138                 icalproperty* p = icalcomponent_get_first_property(c, ICAL_CREATED_PROPERTY);
00139                 if (p)
00140                 {
00141                     struct icaltimetype datetime = icalproperty_get_created(p);
00142                     if (datetime.is_utc)
00143                         result = true;
00144                 }
00145             }
00146         }
00147         icalcomponent_free(calendar);
00148     }
00149     return result;
00150 }
KDE Home | KDE Accessibility Home | Description of Access Keys