File indexing completed on 2024-05-12 05:10:36

0001 /******************************************************************************
0002  * konsolekalendarexports.cpp                                                 *
0003  *                                                                            *
0004  * KonsoleKalendar is a command line interface to KDE calendars               *
0005  * SPDX-FileCopyrightText: 2002-2004 Tuukka Pasanen <illuusio@mailcity.com>   *
0006  * SPDX-FileCopyrightText: 2003-2005 Allen Winter <winter@kde.org>            *
0007  *                                                                            *
0008  * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 *
0009  *                                                                            *
0010  ******************************************************************************/
0011 /**
0012  * @file konsolekalendarexports.cpp
0013  * Provides the KonsoleKalendarExports class definition.
0014  * @author Tuukka Pasanen
0015  * @author Allen Winter
0016  */
0017 #include "konsolekalendarexports.h"
0018 
0019 #include "konsolekalendar_debug.h"
0020 
0021 #include <KCalendarCore/Event>
0022 
0023 #include <KLocalizedString>
0024 #include <QLocale>
0025 
0026 #include <cstdlib>
0027 #include <iostream>
0028 
0029 using namespace KCalendarCore;
0030 using namespace std;
0031 
0032 KonsoleKalendarExports::KonsoleKalendarExports(KonsoleKalendarVariables *vars)
0033 {
0034     m_variables = vars;
0035     m_firstEntry = true;
0036 }
0037 
0038 KonsoleKalendarExports::~KonsoleKalendarExports() = default;
0039 
0040 bool KonsoleKalendarExports::exportAsTxt(QTextStream *ts, const Event::Ptr &event, const QDate &date)
0041 {
0042     // Export "Text" Format:
0043     //
0044     // Date:\t<Incidence Date>(dddd yyyy-MM-dd)
0045     // [\t<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm)]
0046     // Summary:
0047     // \t<Incidence Summary | "(no summary available)">
0048     // Location:
0049     // \t<Incidence Location | "(no location available)">
0050     // Description:
0051     // \t<Incidence Description | "(no description available)">
0052     // UID:
0053     // \t<Incidence UID>
0054     // --------------------------------------------------
0055 
0056     QLocale locale;
0057 
0058     // Print Event Date (in user's preferred format)
0059     *ts << i18n("Date:") << "\t" << locale.toString(date) << '\n';
0060 
0061     // Print Event Starttime - Endtime, for Non-All-Day Events Only
0062     if (!event->allDay()) {
0063         *ts << "\t" << locale.toString(event->dtStart().time()) << " - " << locale.toString(event->dtEnd().time());
0064     }
0065     *ts << '\n';
0066 
0067     // Print Event Summary
0068     *ts << i18n("Summary:") << '\n';
0069     if (!event->summary().isEmpty()) {
0070         *ts << "\t" << event->summary() << '\n';
0071     } else {
0072         *ts << "\t" << i18n("(no summary available)") << '\n';
0073     }
0074 
0075     // Print Event Location
0076     *ts << i18n("Location:") << '\n';
0077     if (!event->location().isEmpty()) {
0078         *ts << "\t" << event->location() << '\n';
0079     } else {
0080         *ts << "\t" << i18n("(no location available)") << '\n';
0081     }
0082 
0083     // Print Event Description
0084     *ts << i18n("Description:") << '\n';
0085     if (!event->description().isEmpty()) {
0086         *ts << "\t" << event->description() << '\n';
0087     } else {
0088         *ts << "\t" << i18n("(no description available)") << '\n';
0089     }
0090 
0091     // Print Event UID
0092     *ts << i18n("UID:") << '\n' << "\t" << event->uid() << '\n';
0093 
0094     // Print Line Separator
0095     *ts << "--------------------------------------------------" << '\n';
0096 
0097     return true;
0098 }
0099 
0100 bool KonsoleKalendarExports::exportAsTxtShort(QTextStream *ts, const Event::Ptr &event, const QDate &date, bool sameday)
0101 {
0102     // Export "Text-Short" Format:
0103     //
0104     // [--------------------------------------------------]
0105     // {<Incidence Date>(dddd yyyy-MM-dd)]
0106     // [<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm) | "\t"]
0107     // \t<Incidence Summary | \t>[, <Incidence Location>]
0108     // \t\t<Incidence Description | "\t">
0109     QLocale locale;
0110 
0111     if (!sameday) {
0112         // If a new date, then Print the Event Date (in user's preferred format)
0113         *ts << locale.toString(date) << ":" << '\n';
0114     }
0115 
0116     // Print Event Starttime - Endtime
0117     if (!event->allDay()) {
0118         *ts << locale.toString(event->dtStart().time()) << " - " << locale.toString(event->dtEnd().time());
0119     } else {
0120         *ts << i18n("[all day]\t");
0121     }
0122     *ts << "\t";
0123 
0124     // Print Event Summary
0125     *ts << event->summary().replace(QLatin1Char('\n'), QLatin1Char(' '));
0126 
0127     // Print Event Location
0128     if (!event->location().isEmpty()) {
0129         if (!event->summary().isEmpty()) {
0130             *ts << ", ";
0131         }
0132         *ts << event->location().replace(QLatin1Char('\n'), QLatin1Char(' '));
0133     }
0134     *ts << '\n';
0135 
0136     // Print Event Description
0137     if (!event->description().isEmpty()) {
0138         *ts << "\t\t\t" << event->description().replace(QLatin1Char('\n'), QLatin1Char(' ')) << '\n';
0139     }
0140 
0141     // By user request, no longer print UIDs if export-type==short
0142 
0143     // Print Separator
0144     *ts << '\n';
0145 
0146     return true;
0147 }
0148 
0149 QString KonsoleKalendarExports::processField(const QString &field, const QString &dquote)
0150 {
0151     // little function that processes a field for CSV compliance:
0152     //   1. Replaces double quotes by a pair of consecutive double quotes
0153     //   2. Surrounds field with double quotes
0154 
0155     QString double_dquote = dquote + dquote;
0156     QString retField = field;
0157     retField = dquote + retField.replace(dquote, double_dquote) + dquote;
0158     return retField;
0159 }
0160 
0161 //@cond IGNORE
0162 #define pF(x) processField((x), dquote)
0163 //@endcond
0164 
0165 bool KonsoleKalendarExports::exportAsCSV(QTextStream *ts, const Event::Ptr &event, const QDate &date)
0166 {
0167     // Export "CSV" Format:
0168     //
0169     // startdate,starttime,enddate,endtime,summary,location,description,UID
0170 
0171     QString delim = i18n(","); // character to use as CSV field delimiter
0172     QString dquote = i18n("\""); // character to use to quote CSV fields
0173     if (!event->allDay()) {
0174         *ts << pF(date.toString(Qt::ISODate)) << delim << pF(event->dtStart().time().toString(Qt::ISODate)) << delim << pF(date.toString(Qt::ISODate)) << delim
0175             << pF(event->dtEnd().time().toString(Qt::ISODate));
0176     } else {
0177         *ts << pF(date.toString(Qt::ISODate)) << delim << pF(QLatin1StringView("")) << delim << pF(date.toString(Qt::ISODate)) << delim
0178             << pF(QLatin1StringView(""));
0179     }
0180 
0181     *ts << delim << pF(event->summary().replace(QLatin1Char('\n'), QLatin1Char(' '))) << delim
0182         << pF(event->location().replace(QLatin1Char('\n'), QLatin1Char(' '))) << delim << pF(event->description().replace(QLatin1Char('\n'), QLatin1Char(' ')))
0183         << delim << pF(event->uid()) << '\n';
0184 
0185     return true;
0186 }