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