konsolekalendar
konsolekalendarexports.cpp
Go to the documentation of this file.
00001 /******************************************************************************* 00002 * konsolekalendarexports.cpp * 00003 * * 00004 * KonsoleKalendar is a command line interface to KDE calendars * 00005 * Copyright (C) 2002-2004 Tuukka Pasanen <illuusio@mailcity.com> * 00006 * Copyright (C) 2003-2005 Allen Winter <winter@kde.org> * 00007 * * 00008 * This program is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation; either version 2 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program; if not, write to the Free Software * 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 00021 * * 00022 * As a special exception, permission is given to link this program * 00023 * with any edition of TQt, and distribute the resulting executable, * 00024 * without including the source code for TQt in the source distribution. * 00025 * * 00026 ******************************************************************************/ 00033 #include <stdlib.h> 00034 #include <iostream> 00035 00036 #include <tqdatetime.h> 00037 00038 #include <kdebug.h> 00039 #include <tdelocale.h> 00040 00041 #include <libkcal/calendarlocal.h> 00042 #include <libkcal/calendar.h> 00043 #include <libkcal/event.h> 00044 00045 #include "konsolekalendarexports.h" 00046 00047 using namespace KCal; 00048 using namespace std; 00049 00050 KonsoleKalendarExports::KonsoleKalendarExports( KonsoleKalendarVariables *vars ) 00051 { 00052 m_variables = vars; 00053 m_firstEntry = true; 00054 } 00055 00056 00057 KonsoleKalendarExports::~KonsoleKalendarExports() 00058 { 00059 } 00060 00061 bool KonsoleKalendarExports::exportAsTxt( TQTextStream *ts, 00062 Event *event, TQDate date ) 00063 { 00064 00065 // Export "Text" Format: 00066 // 00067 // Date:\t<Incidence Date>(dddd yyyy-MM-dd) 00068 // [\t<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm)] 00069 // Summary: 00070 // \t<Incidence Summary | "(no summary available)"> 00071 // Location: 00072 // \t<Incidence Location | "(no location available)"> 00073 // Description: 00074 // \t<Incidence Description | "(no description available)"> 00075 // UID: 00076 // \t<Incidence UID> 00077 // -------------------------------------------------- 00078 00079 // Print Event Date (in user's prefered format) 00080 *ts << i18n( "Date:" ) 00081 << "\t" 00082 << TDEGlobal::locale()->formatDate( date ) 00083 << endl; 00084 00085 // Print Event Starttime - Endtime, for Non-Floating Events Only 00086 if ( !event->doesFloat() ) { 00087 *ts << "\t" 00088 << TDEGlobal::locale()->formatTime( event->dtStart().time() ) 00089 << " - " 00090 << TDEGlobal::locale()->formatTime( event->dtEnd().time() ); 00091 } 00092 *ts << endl; 00093 00094 // Print Event Summary 00095 *ts << i18n( "Summary:" ) 00096 << endl; 00097 if ( !event->summary().isEmpty() ) { 00098 *ts << "\t" 00099 << event->summary() 00100 << endl; 00101 } else { 00102 *ts << "\t" 00103 << i18n( "(no summary available)" ) 00104 << endl; 00105 } 00106 00107 // Print Event Location 00108 *ts << i18n( "Location:" ) 00109 << endl; 00110 if ( !event->location().isEmpty() ) { 00111 *ts << "\t" 00112 <<event->location() 00113 << endl; 00114 } else { 00115 *ts << "\t" 00116 << i18n( "(no location available)" ) 00117 << endl; 00118 } 00119 00120 // Print Event Description 00121 *ts << i18n( "Description:" ) 00122 << endl; 00123 if ( !event->description().isEmpty() ) { 00124 *ts << "\t" 00125 << event->description() 00126 << endl; 00127 } else { 00128 *ts << "\t" 00129 << i18n( "(no description available)" ) 00130 << endl; 00131 } 00132 00133 // Print Event UID 00134 *ts << i18n( "UID:" ) 00135 << endl 00136 << "\t" 00137 << event->uid() 00138 << endl; 00139 00140 // Print Line Separator 00141 *ts << "--------------------------------------------------" 00142 << endl; 00143 00144 return true; 00145 } 00146 00147 bool KonsoleKalendarExports::exportAsTxtShort( TQTextStream *ts, 00148 Event *event, TQDate date, 00149 bool sameday ) 00150 { 00151 00152 // Export "Text-Short" Format: 00153 // 00154 // [--------------------------------------------------] 00155 // {<Incidence Date>(dddd yyyy-MM-dd)] 00156 // [<Incidence Start Time>(hh:mm) - <Incidence End Time>(hh:mm) | "\t"] 00157 // \t<Incidence Summary | \t>[, <Incidence Location>] 00158 // \t\t<Incidence Description | "\t"> 00159 00160 if ( !sameday ) { 00161 // If a new date, then Print the Event Date (in user's prefered format) 00162 *ts << TDEGlobal::locale()->formatDate( date ) << ":" 00163 << endl; 00164 } 00165 00166 // Print Event Starttime - Endtime 00167 if ( !event->doesFloat() ) { 00168 *ts << TDEGlobal::locale()->formatTime( event->dtStart().time() ) 00169 << " - " 00170 << TDEGlobal::locale()->formatTime( event->dtEnd().time() ); 00171 } else { 00172 *ts << i18n( "[all day]\t" ); 00173 } 00174 *ts << "\t"; 00175 00176 // Print Event Summary 00177 *ts << event->summary().replace( TQChar( '\n' ), TQChar( ' ' ) ); 00178 00179 // Print Event Location 00180 if ( !event->location().isEmpty() ) { 00181 if ( !event->summary().isEmpty() ) { 00182 *ts << ", "; 00183 } 00184 *ts << event->location().replace( TQChar( '\n' ), TQChar( ' ' ) ); 00185 } 00186 *ts << endl; 00187 00188 // Print Event Description 00189 if ( !event->description().isEmpty() ) { 00190 *ts << "\t\t\t" 00191 << event->description().replace( TQChar( '\n' ), TQChar( ' ' ) ) 00192 << endl; 00193 } 00194 00195 // By user request, no longer print UIDs if export-type==short 00196 00197 return true; 00198 } 00199 00200 TQString KonsoleKalendarExports::processField( TQString field, TQString dquote ) 00201 { 00202 // little function that processes a field for CSV compliance: 00203 // 1. Replaces double quotes by a pair of consecutive double quotes 00204 // 2. Surrounds field with double quotes 00205 00206 TQString double_dquote = dquote + dquote; 00207 TQString retField = dquote + field.replace( dquote, double_dquote ) + dquote; 00208 return retField; 00209 } 00210 00211 #define pF( x ) processField( ( x ), dquote ) 00212 00213 bool KonsoleKalendarExports::exportAsCSV( TQTextStream *ts, 00214 Event *event, TQDate date ) 00215 { 00216 00217 // Export "CSV" Format: 00218 // 00219 // startdate,starttime,enddate,endtime,summary,location,description,UID 00220 00221 TQString delim = i18n( "," ); // character to use as CSV field delimiter 00222 TQString dquote = i18n( "\"" ); // character to use to quote CSV fields 00223 00224 if ( !event->doesFloat() ) { 00225 *ts << pF( TDEGlobal::locale()->formatDate( date ) ) 00226 << delim << pF( TDEGlobal::locale()->formatTime( event->dtStart().time() ) ) 00227 << delim << pF( TDEGlobal::locale()->formatDate( date ) ) 00228 << delim << pF( TDEGlobal::locale()->formatTime( event->dtEnd().time() ) ); 00229 } else { 00230 *ts << pF( TDEGlobal::locale()->formatDate( date ) ) 00231 << delim << pF( "" ) 00232 << delim << pF( TDEGlobal::locale()->formatDate( date ) ) 00233 << delim << pF( "" ); 00234 } 00235 00236 *ts << delim << pF( event->summary().replace( TQChar('\n'), TQChar(' ') ) ) 00237 << delim << pF( event->location().replace( TQChar('\n'), TQChar(' ') ) ) 00238 << delim << pF( event->description().replace( TQChar('\n'), TQChar(' ') ) ) 00239 << delim << pF( event->uid() ) 00240 << endl; 00241 00242 return true; 00243 }