konsolekalendar

konsolekalendarexports.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * konsolekalendarexports.cpp *
3  * *
4  * KonsoleKalendar is a command line interface to KDE calendars *
5  * Copyright (C) 2002-2004 Tuukka Pasanen <illuusio@mailcity.com> *
6  * Copyright (C) 2003-2005 Allen Winter <winter@kde.org> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21  * *
22  * As a special exception, permission is given to link this program *
23  * with any edition of TQt, and distribute the resulting executable, *
24  * without including the source code for TQt in the source distribution. *
25  * *
26  ******************************************************************************/
33 #include <stdlib.h>
34 #include <iostream>
35 
36 #include <tqdatetime.h>
37 
38 #include <kdebug.h>
39 #include <tdelocale.h>
40 
41 #include <libkcal/calendarlocal.h>
42 #include <libkcal/calendar.h>
43 #include <libkcal/event.h>
44 
45 #include "konsolekalendarexports.h"
46 
47 using namespace KCal;
48 using namespace std;
49 
51 {
52  m_variables = vars;
53  m_firstEntry = true;
54 }
55 
56 
58 {
59 }
60 
61 bool KonsoleKalendarExports::exportAsTxt( TQTextStream *ts,
62  Event *event, TQDate date )
63 {
64 
65  // Export "Text" Format:
66  //
67  // Date:\t<Incidence Date>(dddd yyyy-MM-dd)
68  // [\t<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm)]
69  // Summary:
70  // \t<Incidence Summary | "(no summary available)">
71  // Location:
72  // \t<Incidence Location | "(no location available)">
73  // Description:
74  // \t<Incidence Description | "(no description available)">
75  // UID:
76  // \t<Incidence UID>
77  // --------------------------------------------------
78 
79  // Print Event Date (in user's prefered format)
80  *ts << i18n( "Date:" )
81  << "\t"
82  << TDEGlobal::locale()->formatDate( date )
83  << endl;
84 
85  // Print Event Starttime - Endtime, for Non-Floating Events Only
86  if ( !event->doesFloat() ) {
87  *ts << "\t"
88  << TDEGlobal::locale()->formatTime( event->dtStart().time() )
89  << " - "
90  << TDEGlobal::locale()->formatTime( event->dtEnd().time() );
91  }
92  *ts << endl;
93 
94  // Print Event Summary
95  *ts << i18n( "Summary:" )
96  << endl;
97  if ( !event->summary().isEmpty() ) {
98  *ts << "\t"
99  << event->summary()
100  << endl;
101  } else {
102  *ts << "\t"
103  << i18n( "(no summary available)" )
104  << endl;
105  }
106 
107  // Print Event Location
108  *ts << i18n( "Location:" )
109  << endl;
110  if ( !event->location().isEmpty() ) {
111  *ts << "\t"
112  <<event->location()
113  << endl;
114  } else {
115  *ts << "\t"
116  << i18n( "(no location available)" )
117  << endl;
118  }
119 
120  // Print Event Description
121  *ts << i18n( "Description:" )
122  << endl;
123  if ( !event->description().isEmpty() ) {
124  *ts << "\t"
125  << event->description()
126  << endl;
127  } else {
128  *ts << "\t"
129  << i18n( "(no description available)" )
130  << endl;
131  }
132 
133  // Print Event UID
134  *ts << i18n( "UID:" )
135  << endl
136  << "\t"
137  << event->uid()
138  << endl;
139 
140  // Print Line Separator
141  *ts << "--------------------------------------------------"
142  << endl;
143 
144  return true;
145 }
146 
148  Event *event, TQDate date,
149  bool sameday )
150 {
151 
152  // Export "Text-Short" Format:
153  //
154  // [--------------------------------------------------]
155  // {<Incidence Date>(dddd yyyy-MM-dd)]
156  // [<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm) | "\t"]
157  // \t<Incidence Summary | \t>[, <Incidence Location>]
158  // \t\t<Incidence Description | "\t">
159 
160  if ( !sameday ) {
161  // If a new date, then Print the Event Date (in user's prefered format)
162  *ts << TDEGlobal::locale()->formatDate( date ) << ":"
163  << endl;
164  }
165 
166  // Print Event Starttime - Endtime
167  if ( !event->doesFloat() ) {
168  *ts << TDEGlobal::locale()->formatTime( event->dtStart().time() )
169  << " - "
170  << TDEGlobal::locale()->formatTime( event->dtEnd().time() );
171  } else {
172  *ts << i18n( "[all day]\t" );
173  }
174  *ts << "\t";
175 
176  // Print Event Summary
177  *ts << event->summary().replace( TQChar( '\n' ), TQChar( ' ' ) );
178 
179  // Print Event Location
180  if ( !event->location().isEmpty() ) {
181  if ( !event->summary().isEmpty() ) {
182  *ts << ", ";
183  }
184  *ts << event->location().replace( TQChar( '\n' ), TQChar( ' ' ) );
185  }
186  *ts << endl;
187 
188  // Print Event Description
189  if ( !event->description().isEmpty() ) {
190  *ts << "\t\t\t"
191  << event->description().replace( TQChar( '\n' ), TQChar( ' ' ) )
192  << endl;
193  }
194 
195 // By user request, no longer print UIDs if export-type==short
196 
197  return true;
198 }
199 
200 TQString KonsoleKalendarExports::processField( TQString field, TQString dquote )
201 {
202  // little function that processes a field for CSV compliance:
203  // 1. Replaces double quotes by a pair of consecutive double quotes
204  // 2. Surrounds field with double quotes
205 
206  TQString double_dquote = dquote + dquote;
207  TQString retField = dquote + field.replace( dquote, double_dquote ) + dquote;
208  return retField;
209 }
210 
211 #define pF( x ) processField( ( x ), dquote )
212 
213 bool KonsoleKalendarExports::exportAsCSV( TQTextStream *ts,
214  Event *event, TQDate date )
215 {
216 
217  // Export "CSV" Format:
218  //
219  // startdate,starttime,enddate,endtime,summary,location,description,UID
220 
221  TQString delim = i18n( "," ); // character to use as CSV field delimiter
222  TQString dquote = i18n( "\"" ); // character to use to quote CSV fields
223 
224  if ( !event->doesFloat() ) {
225  *ts << pF( TDEGlobal::locale()->formatDate( date ) )
226  << delim << pF( TDEGlobal::locale()->formatTime( event->dtStart().time() ) )
227  << delim << pF( TDEGlobal::locale()->formatDate( date ) )
228  << delim << pF( TDEGlobal::locale()->formatTime( event->dtEnd().time() ) );
229  } else {
230  *ts << pF( TDEGlobal::locale()->formatDate( date ) )
231  << delim << pF( "" )
232  << delim << pF( TDEGlobal::locale()->formatDate( date ) )
233  << delim << pF( "" );
234  }
235 
236  *ts << delim << pF( event->summary().replace( TQChar('\n'), TQChar(' ') ) )
237  << delim << pF( event->location().replace( TQChar('\n'), TQChar(' ') ) )
238  << delim << pF( event->description().replace( TQChar('\n'), TQChar(' ') ) )
239  << delim << pF( event->uid() )
240  << endl;
241 
242  return true;
243 }
bool exportAsTxtShort(TQTextStream *ts, Event *event, TQDate date, bool sameday)
Export the Event in Short Text Mode.
bool exportAsCSV(TQTextStream *ts, Event *event, TQDate date)
Export the Event in Comma-Separated Values (CSV) Mode.
KonsoleKalendarExports(KonsoleKalendarVariables *vars=0)
Constructs a KonsoleKalendarChange object from command line arguments.
bool exportAsTxt(TQTextStream *ts, Event *event, TQDate date)
Export the Event in Text Mode.
This class provides all the variables for the program.
Provides the KonsoleKalendarExports class definition.