File indexing completed on 2024-04-21 03:52:41

0001 /*
0002   This file is part of the kcalcore library.
0003 
0004   SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0005   SPDX-FileCopyrightText: 2005 Reinhold Kainhofer <reinhold@kainhofe.com>
0006 
0007   SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "filestorage.h"
0011 #include "memorycalendar.h"
0012 
0013 #include <QCommandLineParser>
0014 #include <QCoreApplication>
0015 #include <QDebug>
0016 #include <QFile>
0017 #include <QTextStream>
0018 #include <QTimeZone>
0019 
0020 using namespace KCalendarCore;
0021 
0022 static QString dumpTime(const QDateTime &dt, const QTimeZone &viewZone);
0023 
0024 int main(int argc, char **argv)
0025 {
0026     qputenv("TZ", "GMT");
0027 
0028     QCommandLineParser parser;
0029     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("verbose"), QStringLiteral("Verbose output")));
0030 
0031     parser.addPositionalArgument(QStringLiteral("input"), QStringLiteral("Name of input file"));
0032 
0033     parser.addPositionalArgument(QStringLiteral("output"), QStringLiteral("optional name of output file for the recurrence dates"));
0034 
0035     QCoreApplication app(argc, argv);
0036     QCoreApplication::setApplicationName(QStringLiteral("testrecurrencenew"));
0037     QCoreApplication::setApplicationVersion(QStringLiteral("0.1"));
0038     parser.process(app);
0039 
0040     QStringList parsedArgs = parser.positionalArguments();
0041 
0042     if (parsedArgs.isEmpty()) {
0043         parser.showHelp();
0044     }
0045     QString input = parsedArgs[0];
0046     qDebug() << "Input file:" << input;
0047 
0048     QTextStream *outstream = nullptr;
0049     outstream = nullptr;
0050     QString fn(QLatin1String(""));
0051     if (parsedArgs.count() > 1) {
0052         fn = parsedArgs[1];
0053         qDebug() << "We have a file name given:" << fn;
0054     }
0055     QFile outfile(fn);
0056     if (!fn.isEmpty() && outfile.open(QIODevice::WriteOnly)) {
0057         qDebug() << "Opened output file!!!";
0058         outstream = new QTextStream(&outfile);
0059     }
0060 
0061     MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc()));
0062 
0063     QTimeZone viewZone;
0064     FileStorage store(cal, input);
0065     if (!store.load()) {
0066         return 1;
0067     }
0068     QString tz = cal->nonKDECustomProperty("X-LibKCal-Testsuite-OutTZ");
0069     if (!tz.isEmpty()) {
0070         viewZone = QTimeZone(tz.toUtf8());
0071     }
0072     qDebug() << viewZone.id();
0073     const Incidence::List inc = cal->incidences();
0074 
0075     for (const Incidence::Ptr &incidence : inc) {
0076         qDebug() << "*+*+*+*+*+*+*+*+*+*";
0077         qDebug() << " ->" << incidence->summary() << "<-";
0078 
0079         incidence->recurrence()->dump();
0080 
0081         QDateTime dt = incidence->recurrence()->endDateTime();
0082         int i = 0;
0083         if (outstream) {
0084             if (!dt.isValid()) {
0085                 if (viewZone.isValid()) {
0086                     dt = QDateTime(QDate(2011, 1, 1), QTime(0, 0, 1), viewZone);
0087                 } else {
0088                     dt = QDateTime(QDate(2011, 1, 1), QTime(0, 0, 1));
0089                 }
0090             } else {
0091                 dt = dt.addYears(2);
0092             }
0093             qDebug() << "-------------------------------------------";
0094             qDebug() << " *~*~*~*~ Starting with date:" << dumpTime(dt, viewZone);
0095             // Output to file for testing purposes
0096             while (dt.isValid() && i < 500) {
0097                 ++i;
0098                 dt = incidence->recurrence()->getPreviousDateTime(dt);
0099                 if (dt.isValid()) {
0100                     (*outstream) << dumpTime(dt, viewZone) << '\n';
0101                 }
0102             }
0103         } else {
0104             if (!dt.isValid()) {
0105                 dt = QDateTime(QDate(2005, 7, 31), QTime(23, 59, 59), QTimeZone::UTC);
0106             } else {
0107                 dt = dt.addYears(2);
0108             }
0109             incidence->recurrence()->dump();
0110             qDebug() << "-------------------------------------------";
0111             qDebug() << " *~*~*~*~ Starting with date:" << dumpTime(dt, viewZone);
0112             // Output to konsole
0113             while (dt.isValid() && i < 50) {
0114                 ++i;
0115                 qDebug() << "-------------------------------------------";
0116                 dt = incidence->recurrence()->getPreviousDateTime(dt);
0117                 if (dt.isValid()) {
0118                     qDebug() << " *~*~*~*~ Previous date is:" << dumpTime(dt, viewZone);
0119                 }
0120             }
0121         }
0122     }
0123 
0124     delete outstream;
0125     outfile.close();
0126     return 0;
0127 }
0128 
0129 QString dumpTime(const QDateTime &dt, const QTimeZone &viewZone)
0130 {
0131     if (!dt.isValid()) {
0132         return QString();
0133     }
0134     const QDateTime vdt = viewZone.isValid() ? dt.toTimeZone(viewZone) : dt;
0135     QString format = QStringLiteral("yyyy-MM-ddThh:mm:ss t");
0136     if (viewZone.isValid()) {
0137         format += QStringLiteral(" '%1'").arg(QString::fromUtf8(viewZone.id()));
0138     }
0139     return vdt.toString(format);
0140 }