1 /******************************************************************************
2  * konsolekalendarexports.cpp                                                 *
3  *                                                                            *
4  * KonsoleKalendar is a command line interface to KDE calendars               *
5  * SPDX-FileCopyrightText: 2002-2004 Tuukka Pasanen <illuusio@mailcity.com>   *
6  * SPDX-FileCopyrightText: 2003-2005 Allen Winter <winter@kde.org>            *
7  *                                                                            *
8  * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 *
9  *                                                                            *
10  ******************************************************************************/
11 /**
12  * @file konsolekalendarexports.cpp
13  * Provides the KonsoleKalendarExports class definition.
14  * @author Tuukka Pasanen
15  * @author Allen Winter
16  */
17 #include "konsolekalendarexports.h"
18 
19 #include "konsolekalendar_debug.h"
20 
21 #include <KCalendarCore/Event>
22 
23 #include <KLocalizedString>
24 #include <QLocale>
25 
26 #include <iostream>
27 #include <stdlib.h>
28 
29 using namespace KCalendarCore;
30 using namespace std;
31 
KonsoleKalendarExports(KonsoleKalendarVariables * vars)32 KonsoleKalendarExports::KonsoleKalendarExports(KonsoleKalendarVariables *vars)
33 {
34     m_variables = vars;
35     m_firstEntry = true;
36 }
37 
~KonsoleKalendarExports()38 KonsoleKalendarExports::~KonsoleKalendarExports()
39 {
40 }
41 
exportAsTxt(QTextStream * ts,const Event::Ptr & event,const QDate & date)42 bool KonsoleKalendarExports::exportAsTxt(QTextStream *ts, const Event::Ptr &event, const QDate &date)
43 {
44     // Export "Text" Format:
45     //
46     // Date:\t<Incidence Date>(dddd yyyy-MM-dd)
47     // [\t<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm)]
48     // Summary:
49     // \t<Incidence Summary | "(no summary available)">
50     // Location:
51     // \t<Incidence Location | "(no location available)">
52     // Description:
53     // \t<Incidence Description | "(no description available)">
54     // UID:
55     // \t<Incidence UID>
56     // --------------------------------------------------
57 
58     QLocale locale;
59 
60     // Print Event Date (in user's preferred format)
61     *ts << i18n("Date:") << "\t" << locale.toString(date) << '\n';
62 
63     // Print Event Starttime - Endtime, for Non-All-Day Events Only
64     if (!event->allDay()) {
65         *ts << "\t" << locale.toString(event->dtStart().time()) << " - " << locale.toString(event->dtEnd().time());
66     }
67     *ts << '\n';
68 
69     // Print Event Summary
70     *ts << i18n("Summary:") << '\n';
71     if (!event->summary().isEmpty()) {
72         *ts << "\t" << event->summary() << '\n';
73     } else {
74         *ts << "\t" << i18n("(no summary available)") << '\n';
75     }
76 
77     // Print Event Location
78     *ts << i18n("Location:") << '\n';
79     if (!event->location().isEmpty()) {
80         *ts << "\t" << event->location() << '\n';
81     } else {
82         *ts << "\t" << i18n("(no location available)") << '\n';
83     }
84 
85     // Print Event Description
86     *ts << i18n("Description:") << '\n';
87     if (!event->description().isEmpty()) {
88         *ts << "\t" << event->description() << '\n';
89     } else {
90         *ts << "\t" << i18n("(no description available)") << '\n';
91     }
92 
93     // Print Event UID
94     *ts << i18n("UID:") << '\n' << "\t" << event->uid() << '\n';
95 
96     // Print Line Separator
97     *ts << "--------------------------------------------------" << '\n';
98 
99     return true;
100 }
101 
exportAsTxtShort(QTextStream * ts,const Event::Ptr & event,const QDate & date,bool sameday)102 bool KonsoleKalendarExports::exportAsTxtShort(QTextStream *ts, const Event::Ptr &event, const QDate &date, bool sameday)
103 {
104     // Export "Text-Short" Format:
105     //
106     // [--------------------------------------------------]
107     // {<Incidence Date>(dddd yyyy-MM-dd)]
108     // [<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm) | "\t"]
109     // \t<Incidence Summary | \t>[, <Incidence Location>]
110     // \t\t<Incidence Description | "\t">
111     QLocale locale;
112 
113     if (!sameday) {
114         // If a new date, then Print the Event Date (in user's preferred format)
115         *ts << locale.toString(date) << ":" << '\n';
116     }
117 
118     // Print Event Starttime - Endtime
119     if (!event->allDay()) {
120         *ts << locale.toString(event->dtStart().time()) << " - " << locale.toString(event->dtEnd().time());
121     } else {
122         *ts << i18n("[all day]\t");
123     }
124     *ts << "\t";
125 
126     // Print Event Summary
127     *ts << event->summary().replace(QLatin1Char('\n'), QLatin1Char(' '));
128 
129     // Print Event Location
130     if (!event->location().isEmpty()) {
131         if (!event->summary().isEmpty()) {
132             *ts << ", ";
133         }
134         *ts << event->location().replace(QLatin1Char('\n'), QLatin1Char(' '));
135     }
136     *ts << '\n';
137 
138     // Print Event Description
139     if (!event->description().isEmpty()) {
140         *ts << "\t\t\t" << event->description().replace(QLatin1Char('\n'), QLatin1Char(' ')) << '\n';
141     }
142 
143     // By user request, no longer print UIDs if export-type==short
144 
145     // Print Separator
146     *ts << '\n';
147 
148     return true;
149 }
150 
processField(const QString & field,const QString & dquote)151 QString KonsoleKalendarExports::processField(const QString &field, const QString &dquote)
152 {
153     // little function that processes a field for CSV compliance:
154     //   1. Replaces double quotes by a pair of consecutive double quotes
155     //   2. Surrounds field with double quotes
156 
157     QString double_dquote = dquote + dquote;
158     QString retField = field;
159     retField = dquote + retField.replace(dquote, double_dquote) + dquote;
160     return retField;
161 }
162 
163 //@cond IGNORE
164 #define pF(x) processField((x), dquote)
165 //@endcond
166 
exportAsCSV(QTextStream * ts,const Event::Ptr & event,const QDate & date)167 bool KonsoleKalendarExports::exportAsCSV(QTextStream *ts, const Event::Ptr &event, const QDate &date)
168 {
169     // Export "CSV" Format:
170     //
171     // startdate,starttime,enddate,endtime,summary,location,description,UID
172 
173     QString delim = i18n(","); // character to use as CSV field delimiter
174     QString dquote = i18n("\""); // character to use to quote CSV fields
175     if (!event->allDay()) {
176         *ts << pF(date.toString(Qt::ISODate)) << delim << pF(event->dtStart().time().toString(Qt::ISODate)) << delim << pF(date.toString(Qt::ISODate)) << delim
177             << pF(event->dtEnd().time().toString(Qt::ISODate));
178     } else {
179         *ts << pF(date.toString(Qt::ISODate)) << delim << pF(QLatin1String("")) << delim << pF(date.toString(Qt::ISODate)) << delim << pF(QLatin1String(""));
180     }
181 
182     *ts << delim << pF(event->summary().replace(QLatin1Char('\n'), QLatin1Char(' '))) << delim
183         << pF(event->location().replace(QLatin1Char('\n'), QLatin1Char(' '))) << delim << pF(event->description().replace(QLatin1Char('\n'), QLatin1Char(' ')))
184         << delim << pF(event->uid()) << '\n';
185 
186     return true;
187 }
188