• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kalarm

calendarcompat.cpp

Go to the documentation of this file.
00001 /*
00002  *  calendarcompat.cpp -  compatibility for old calendar file formats
00003  *  Program:  kalarm
00004  *  Copyright © 2001-2008 by David Jarvie <djarvie@kde.org>
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"   //krazy:exclude=includes (kalarm.h must be first)
00022 #include "calendarcompat.h"
00023 
00024 #include "alarmevent.h"
00025 #include "alarmresource.h"
00026 #include "functions.h"
00027 #include "preferences.h"
00028 
00029 #include <QFile>
00030 #include <QFileInfo>
00031 #include <QTextStream>
00032 
00033 #include <kapplication.h>
00034 #include <kaboutdata.h>
00035 #include <klocale.h>
00036 #include <kmessagebox.h>
00037 #include <kdebug.h>
00038 
00039 #include <kcal/calendarlocal.h>
00040 
00041 
00042 using namespace KCal;
00043 
00044 static const QByteArray VERSION_PROPERTY("VERSION");     // X-KDE-KALARM-VERSION VCALENDAR property
00045 
00046 
00047 /******************************************************************************
00048 * Write the X-KDE-KALARM-VERSION custom property into the calendar.
00049 */
00050 void CalendarCompat::setID(KCal::CalendarLocal& calendar)
00051 {
00052     calendar.setCustomProperty(KCalendar::APPNAME, VERSION_PROPERTY, KAlarm::currentCalendarVersionString());
00053 }
00054 
00055 /******************************************************************************
00056 * Find the version of KAlarm which wrote the calendar file, and do any
00057 * necessary conversions to the current format. If it is a resource calendar,
00058 * the user is prompted whether to save the conversions. For a local calendar
00059 * file, any conversions will only be saved if changes are made later.
00060 * Reply = true if the calendar file is now in the current format.
00061 */
00062 KCalendar::Status CalendarCompat::fix(KCal::CalendarLocal& calendar, const QString& localFile, AlarmResource* resource, AlarmResource::FixFunc conv)
00063 {
00064     bool version057_UTC = false;
00065     QString subVersion, versionString;
00066     int version = readKAlarmVersion(calendar, localFile, subVersion, versionString);
00067     if (!version)
00068         return KCalendar::Current;     // calendar is in current KAlarm format
00069     if (version < 0  ||  version > KAlarm::Version())
00070         return KCalendar::Incompatible;    // calendar was created by another program, or an unknown version of KAlarm
00071 
00072     // Calendar was created by an earlier version of KAlarm.
00073     // Convert it to the current format, and prompt the user whether to update the calendar file.
00074     if (version == KAlarm::Version(0,5,7)  &&  !localFile.isEmpty())
00075     {
00076         // KAlarm version 0.5.7 - check whether times are stored in UTC, in which
00077         // case it is the KDE 3.0.0 version, which needs adjustment of summer times.
00078         version057_UTC = isUTC(localFile);
00079         kDebug() << "KAlarm version 0.5.7 (" << (version057_UTC ?"" :"non-") << "UTC)";
00080     }
00081     else
00082         kDebug() << "KAlarm version" << version;
00083 
00084     // Convert events to current KAlarm format for if the calendar is saved
00085     KAEvent::convertKCalEvents(calendar, version, version057_UTC);
00086     if (!resource)
00087         return KCalendar::Current;    // update non-shared calendars regardless
00088     if (resource->ResourceCached::readOnly()  ||  conv == AlarmResource::NO_CONVERT)
00089         return KCalendar::Convertible;
00090     // Update the calendar file now if the user wants it to be read-write
00091     if (conv == AlarmResource::PROMPT  ||  conv == AlarmResource::PROMPT_PART)
00092     {
00093         QString msg = (conv == AlarmResource::PROMPT)
00094                     ? i18nc("@info", "Resource <resource>%1</resource> is in an old format (<application>KAlarm</application> version %2), and will be read-only unless "
00095                            "you choose to update it to the current format.", resource->resourceName(), versionString)
00096                     : i18nc("@info", "Some or all of the alarms in resource <resource>%1</resource> are in an old <application>KAlarm</application> format, and will be read-only unless "
00097                            "you choose to update them to the current format.", resource->resourceName());
00098         if (KMessageBox::warningYesNo(0,
00099               i18nc("@info", "<para>%1</para><para>"
00100                    "<warning>Do not update the resource if it is shared with other users who run an older version "
00101                    "of <application>KAlarm</application>. If you do so, they may be unable to use it any more.</warning></para>"
00102                    "<para>Do you wish to update the resource?</para>", msg))
00103             != KMessageBox::Yes)
00104             return KCalendar::Convertible;
00105     }
00106     calendar.setCustomProperty(KCalendar::APPNAME, VERSION_PROPERTY, QLatin1String(KALARM_VERSION));
00107     return KCalendar::Converted;
00108 }
00109 
00110 /******************************************************************************
00111 * Return the KAlarm version which wrote the calendar which has been loaded.
00112 * The format is, for example, 000507 for 0.5.7.
00113 * Reply = 0 if the calendar was created by the current version of KAlarm
00114 *       = -1 if it was created by KAlarm pre-0.3.5, or another program
00115 *       = version number if created by another KAlarm version.
00116 */
00117 int CalendarCompat::readKAlarmVersion(KCal::CalendarLocal& calendar, const QString& localFile, QString& subVersion, QString& versionString)
00118 {
00119     subVersion.clear();
00120     versionString = calendar.customProperty(KCalendar::APPNAME, VERSION_PROPERTY);
00121     if (versionString.isEmpty())
00122     {
00123         // Pre-KAlarm 1.4 defined the KAlarm version number in the PRODID field.
00124         // If another application has written to the file, this may not be present.
00125         const QString prodid = calendar.productId();
00126         if (prodid.isEmpty())
00127         {
00128             // Check whether the calendar file is empty, in which case
00129             // it can be written to freely.
00130             QFileInfo fi(localFile);
00131             if (!fi.size())
00132                 return 0;
00133         }
00134 
00135         // Find the KAlarm identifier
00136         QString progname = QLatin1String(" KAlarm ");
00137         int i = prodid.indexOf(progname, 0, Qt::CaseInsensitive);
00138         if (i < 0)
00139         {
00140             // Older versions used KAlarm's translated name in the product ID, which
00141             // could have created problems using a calendar in different locales.
00142             progname = QString(" ") + KGlobal::mainComponent().aboutData()->programName() + ' ';
00143             i = prodid.indexOf(progname, 0, Qt::CaseInsensitive);
00144             if (i < 0)
00145                 return -1;    // calendar wasn't created by KAlarm
00146         }
00147 
00148         // Extract the KAlarm version string
00149         versionString = prodid.mid(i + progname.length()).trimmed();
00150         i = versionString.indexOf('/');
00151         int j = versionString.indexOf(' ');
00152         if (j >= 0  &&  j < i)
00153             i = j;
00154         if (i <= 0)
00155             return -1;    // missing version string
00156         versionString = versionString.left(i);   // 'versionString' now contains the KAlarm version string
00157     }
00158     if (versionString == KAlarm::currentCalendarVersionString())
00159         return 0;      // the calendar is in the current KAlarm format
00160     int ver = KAlarm::getVersionNumber(versionString, &subVersion);
00161     if (ver >= KAlarm::currentCalendarVersion()  &&  ver <= KAlarm::Version())
00162         return 0;      // the calendar is in the current KAlarm format
00163     return KAlarm::getVersionNumber(versionString, &subVersion);
00164 }
00165 
00166 /******************************************************************************
00167 * Check whether the calendar file has its times stored as UTC times,
00168 * indicating that it was written by the KDE 3.0.0 version of KAlarm 0.5.7.
00169 * Reply = true if times are stored in UTC
00170 *       = false if the calendar is a vCalendar, times are not UTC, or any error occurred.
00171 */
00172 bool CalendarCompat::isUTC(const QString& localFile)
00173 {
00174     // Read the calendar file into a string
00175     QFile file(localFile);
00176     if (!file.open(QIODevice::ReadOnly))
00177         return false;
00178     QTextStream ts(&file);
00179     ts.setCodec("ISO 8859-1");
00180     QByteArray text = ts.readAll().toLocal8Bit();
00181     file.close();
00182 
00183     // Extract the CREATED property for the first VEVENT from the calendar
00184     QList<QByteArray> lines = text.split('\n');
00185     for (int i = 0, end = lines.count();  i < end;  ++i)
00186     {
00187         if (lines[i].startsWith("BEGIN:VCALENDAR"))
00188         {
00189             while (++i < end)
00190             {
00191                 if (lines[i].startsWith("BEGIN:VEVENT"))
00192                 {
00193                     while (++i < end)
00194                     {
00195                         if (lines[i].startsWith("CREATED:"))
00196                             return lines[i].endsWith('Z');
00197                     }
00198                 }
00199             }
00200             break;
00201         }
00202     }
00203     return false;
00204 }
00205 

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal