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

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     QCommandLineParser parser;
0027     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("verbose"), QStringLiteral("Verbose output")));
0028     parser.addPositionalArgument(QStringLiteral("input"), QStringLiteral("Name of input file"));
0029     parser.addPositionalArgument(QStringLiteral("output"), QStringLiteral("optional name of output file for the recurrence dates"));
0030 
0031     QCoreApplication app(argc, argv);
0032     QCoreApplication::setApplicationName(QStringLiteral("testrecurrencenew"));
0033     QCoreApplication::setApplicationVersion(QStringLiteral("0.1"));
0034     parser.process(app);
0035 
0036     const QStringList parsedArgs = parser.positionalArguments();
0037 
0038     if (parsedArgs.isEmpty()) {
0039         parser.showHelp();
0040     }
0041 
0042     QString input = parsedArgs[0];
0043     qDebug() << "Input file:" << input;
0044 
0045     QTextStream *outstream = nullptr;
0046     QString fn;
0047     if (parsedArgs.count() > 1) {
0048         fn = parsedArgs[1];
0049         qDebug() << "We have a file name given:" << fn;
0050     }
0051     QFile outfile(fn);
0052     if (!fn.isEmpty() && outfile.open(QIODevice::WriteOnly)) {
0053         qDebug() << "Opened output file!!!";
0054         outstream = new QTextStream(&outfile);
0055     }
0056 
0057     MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc()));
0058 
0059     QTimeZone viewZone;
0060     FileStorage store(cal, input);
0061     if (!store.load()) {
0062         return 1;
0063     }
0064     QString tz = cal->nonKDECustomProperty("X-LibKCal-Testsuite-OutTZ");
0065     if (!tz.isEmpty()) {
0066         viewZone = QTimeZone(tz.toUtf8());
0067     }
0068 
0069     const Incidence::List inc = cal->incidences();
0070     for (const Incidence::Ptr &incidence : inc) {
0071         qDebug() << "*+*+*+*+*+*+*+*+*+*";
0072         qDebug() << " ->" << incidence->summary() << "<-";
0073 
0074         incidence->recurrence()->dump();
0075 
0076         QDateTime dt;
0077         if (incidence->allDay()) {
0078             dt = incidence->dtStart().addDays(-1);
0079         } else {
0080             dt = incidence->dtStart().addSecs(-1);
0081         }
0082         int i = 0;
0083         if (outstream) {
0084             // Output to file for testing purposes
0085             while (dt.isValid() && i < 500) {
0086                 ++i;
0087                 dt = incidence->recurrence()->getNextDateTime(dt);
0088                 if (dt.isValid()) {
0089                     (*outstream) << dumpTime(dt, viewZone) << '\n';
0090                 }
0091             }
0092         } else {
0093             incidence->recurrence()->dump();
0094             // Output to konsole
0095             while (dt.isValid() && i < 10) {
0096                 ++i;
0097                 qDebug() << "-------------------------------------------";
0098                 dt = incidence->recurrence()->getNextDateTime(dt);
0099                 if (dt.isValid()) {
0100                     qDebug() << " *~*~*~*~ Next date is:" << dumpTime(dt, viewZone);
0101                 }
0102             }
0103         }
0104     }
0105 
0106     delete outstream;
0107     outfile.close();
0108     return 0;
0109 }
0110 
0111 QString dumpTime(const QDateTime &dt, const QTimeZone &viewZone)
0112 {
0113     if (!dt.isValid()) {
0114         return QString();
0115     }
0116     QDateTime vdt = viewZone.isValid() ? dt.toTimeZone(viewZone) : dt;
0117     QString format = QStringLiteral("yyyy-MM-ddThh:mm:ss t");
0118     if (viewZone.isValid()) {
0119         format += QStringLiteral(" '%1'").arg(QString::fromUtf8(viewZone.id()));
0120     }
0121     return vdt.toString(format);
0122 }