konsolekalendar Library API Documentation

konsolekalendar.cpp

00001 /*************************************************************************** 00002 konsolekalendar.cpp 00003 ------------------- 00004 begin : Sun Jan 6 11:50:14 EET 2002 00005 copyright : (C) 2002-2003 by Tuukka Pasanen 00006 copyright : (C) 2003 by Allen Winter 00007 email : illuusio@mailcity.com 00008 00009 ***************************************************************************/ 00010 00011 /*************************************************************************** 00012 * * 00013 * This program is free software; you can redistribute it and/or modify * 00014 * it under the terms of the GNU General Public License as published by * 00015 * the Free Software Foundation; either version 2 of the License, or * 00016 * (at your option) any later version. * 00017 * * 00018 ***************************************************************************/ 00019 00020 #include <stdio.h> 00021 #include <stdlib.h> 00022 #include <iostream> 00023 00024 #include <qdatetime.h> 00025 #include <qfile.h> 00026 #include <qtextstream.h> 00027 00028 #include <kdebug.h> 00029 #include <klocale.h> 00030 #include <kstandarddirs.h> 00031 00032 #include <libkcal/calendarlocal.h> 00033 #include <libkcal/resourcecalendar.h> 00034 #include <libkcal/calendarresources.h> 00035 #include <libkcal/calendar.h> 00036 #include <libkcal/event.h> 00037 #include <libkcal/htmlexport.h> 00038 00039 #include "konsolekalendar.h" 00040 #include "konsolekalendaradd.h" 00041 #include "konsolekalendarchange.h" 00042 #include "konsolekalendardelete.h" 00043 #include "konsolekalendarexports.h" 00044 00045 using namespace KCal; 00046 using namespace std; 00047 00048 KonsoleKalendar::KonsoleKalendar(KonsoleKalendarVariables *variables) 00049 { 00050 m_variables = variables; 00051 // m_Calendar = new ResourceCalendar; 00052 } 00053 00054 KonsoleKalendar::~KonsoleKalendar() 00055 { 00056 } 00057 00058 bool KonsoleKalendar::importCalendar() 00059 { 00060 KonsoleKalendarAdd add( m_variables ); 00061 00062 kdDebug() << "konsolecalendar.cpp::importCalendar() | importing now!" << endl; 00063 return( add.addImportedCalendar() ); 00064 } 00065 00066 bool KonsoleKalendar::createCalendar() 00067 { 00068 bool status = false; 00069 CalendarLocal newCalendar; 00070 00071 if( m_variables->isDryRun() ) { 00072 // TODO: put back after string freeze 00073 // cout << i18n("Create Calendar <Dry Run>:").local8Bit() << m_variables->getCalendarFile().local8Bit() << endl; 00074 } else { 00075 00076 kdDebug() << "konsolekalendar.cpp::createCalendar() | Creating calendar file: " << m_variables->getCalendarFile().local8Bit() << endl; 00077 00078 if( m_variables->isVerbose() ) { 00079 // TODO: put back after string freeze 00080 // cout << i18n("Create Calendar <Verbose>:").local8Bit() << m_variables->getCalendarFile().local8Bit() << endl; 00081 } 00082 00083 if( newCalendar.save( m_variables->getCalendarFile() ) ) { 00084 newCalendar.close(); 00085 status = true; 00086 } 00087 } 00088 return status; 00089 } 00090 00091 bool KonsoleKalendar::showInstance() 00092 { 00093 bool status = true; 00094 QFile f; 00095 QString title; 00096 Event::List *eventList; 00097 Event *event; 00098 00099 if( m_variables->isDryRun() ) { 00100 cout << i18n("View Events <Dry Run>:").local8Bit() << endl; 00101 printSpecs(); 00102 } else { 00103 00104 kdDebug() << "konsolekalendar.cpp::showInstance() | open export file" << endl; 00105 00106 if( m_variables->isExportFile() ) { 00107 f.setName( m_variables->getExportFile() ); 00108 if ( !f.open( IO_WriteOnly ) ) { 00109 status = false; 00110 kdDebug() << "konsolekalendar.cpp::showInstance() | unable to open export file " << m_variables->getExportFile() << endl; 00111 } // if 00112 } else { 00113 f.open( IO_WriteOnly, stdout ); 00114 } // else 00115 00116 if( status ) { 00117 kdDebug() << "konsolekalendar.cpp::showInstance() | opened successful" << endl; 00118 00119 if( m_variables->isVerbose() ) { 00120 cout << i18n("View Event <Verbose>:").local8Bit() << endl; 00121 printSpecs(); 00122 } 00123 00124 QTextStream ts( &f ); 00125 00126 if( m_variables->getExportType() != HTML ) { 00127 00128 if( m_variables->getAll() ) { 00129 kdDebug() << "konsolekalendar.cpp::showInstance() | view all events sorted list" << endl; 00130 00131 Event::List sortedList = allEventsSorted(); 00132 00133 QDate dt, firstdate, lastdate; 00134 firstdate = sortedList.first()->dtStart().date(); 00135 lastdate = sortedList.last()->dtStart().date(); 00136 for (dt=firstdate; dt<=lastdate && status != false; dt=dt.addDays(1)) { 00137 Event::List events = m_variables->getCalendar()->events( dt, true ); 00138 status = printEventList( &ts, &events, dt ); 00139 } 00140 } else if( m_variables->isUID() ) { 00141 kdDebug() << "konsolekalendar.cpp::showInstance() | view events by uid list" << endl; 00142 //TODO: support a list of UIDs 00143 event = m_variables->getCalendar()->event( m_variables->getUID() ); 00144 //If this UID represents a recurring Event, only the first day of the Event will be printed 00145 status = printEvent ( &ts, event,event->dtStart().date() ); 00146 } else if( m_variables->isNext() ) { 00147 kdDebug() << "konsolekalendar.cpp::showInstance() | Show next activity in calendar" << endl; 00148 00149 QDateTime datetime = m_variables->getStartDateTime(); 00150 datetime = datetime.addDays( 720 ); 00151 00152 QDate dt; 00153 for (dt=m_variables->getStartDateTime().date(); dt<=datetime.date() && status != false; dt=dt.addDays(1)) { 00154 Event::List events = m_variables->getCalendar()->events( dt, true ); 00155 status = printEventList( &ts, &events, dt ); 00156 00157 // when we get next event we exit.. 00158 if ( events.count() ) { 00159 kdDebug() << "konsolekalendar.cpp::showInstance() | Next event" << 00160 endl; 00161 return true; 00162 } 00163 00164 } 00165 00166 } else { 00167 00168 kdDebug() << "konsolekalendar.cpp::showInstance() | view raw events within date range list" << endl; 00169 00170 QDate dt; 00171 for (dt=m_variables->getStartDateTime().date(); dt<=m_variables->getEndDateTime().date() && status != false; dt=dt.addDays(1)) { 00172 Event::List events = m_variables->getCalendar()->events( dt, true ); 00173 status = printEventList( &ts, &events, dt ); 00174 } 00175 } 00176 00177 } else { 00178 00179 QDate firstdate, lastdate; 00180 if( m_variables->getAll() ) { 00181 // TODO: this is broken since the date on last() may not be last date (this is the case for me) 00182 kdDebug() << "konsolekalendar.cpp::showInstance() | HTML view all events sorted list" << endl; 00183 eventList = new Event::List ( m_variables->getCalendar()->rawEvents( ) ); 00184 firstdate = eventList->first()->dtStart().date(); 00185 lastdate = eventList->last()->dtStart().date(); 00186 delete eventList; 00187 } else if( m_variables->isUID() ) { 00188 // TODO 00189 kdDebug() << "konsolekalendar.cpp::showInstance() | HTML view events by uid list" << endl; 00190 cout << i18n("Sorry, export to HTML by UID is not supported yet").local8Bit() << endl; 00191 return( false ); 00192 } else { 00193 kdDebug() << "konsolekalendar.cpp::showInstance() | HTML view raw events within date range list" << endl; 00194 firstdate = m_variables->getStartDateTime().date(); 00195 lastdate = m_variables->getEndDateTime().date(); 00196 } 00197 00198 KCal::HtmlExport *Export; 00199 if ( !m_variables->isCalendarResources() ) { 00200 Export = new HtmlExport( m_variables->getCalendar() ); 00201 } else { 00202 Export = new HtmlExport( m_variables->getCalendarResources() ); 00203 } 00204 00205 title = "Appointments for " + firstdate.toString(Qt::TextDate); 00206 if( firstdate != lastdate ) { 00207 title += " - " + lastdate.toString(Qt::TextDate); 00208 } 00209 00210 Export->setTitle( title ); 00211 Export->setEmail( "" ); 00212 Export->setFullName( "" ); 00213 Export->setCredit( "KonsoleKalendar", "http://pim.kde.org/components/konsolekalendar.php" ); 00214 00215 Export->setMonthViewEnabled( false ); // month view would be another export mode, no? 00216 Export->setEventsEnabled( true ); 00217 Export->setCategoriesEventEnabled( true ); 00218 Export->setAttendeesEventEnabled( true ); 00219 Export->setExcludePrivateEventEnabled( true ); 00220 Export->setExcludeConfidentialEventEnabled( true ); 00221 // Not supporting Todos yet 00222 title = "To-Do List for " + firstdate.toString(Qt::TextDate); 00223 if( firstdate != lastdate ) { 00224 title += " - " + lastdate.toString(Qt::TextDate); 00225 } 00226 Export->setTitleTodo( title ); 00227 Export->setTodosEnabled( false ); 00228 Export->setCategoriesTodoEnabled( false ); 00229 Export->setAttendeesTodoEnabled( false ); 00230 Export->setExcludePrivateTodoEnabled( false ); 00231 Export->setExcludeConfidentialTodoEnabled( false ); 00232 Export->setDueDateEnabled( false ); 00233 00234 Export->setDateRange( firstdate, lastdate ); 00235 00236 status = Export->save( &ts ); 00237 } 00238 00239 f.close(); 00240 00241 } 00242 } 00243 00244 return status; 00245 } 00246 00247 bool KonsoleKalendar::printEventList( QTextStream *ts, Event::List *eventList, QDate date ) 00248 { 00249 00250 bool status = true; 00251 00252 if( eventList->count() ) { 00253 00254 Event *singleEvent; 00255 Event::List::ConstIterator it; 00256 00257 for( it = eventList->begin(); it != eventList->end() && status != false; ++it ) { 00258 singleEvent = *it; 00259 00260 status = printEvent( ts, singleEvent, date ); 00261 }// for 00262 00263 } 00264 00265 return( status ); 00266 } 00267 00268 bool KonsoleKalendar::printEvent( QTextStream *ts, Event *event, QDate dt ) 00269 { 00270 00271 bool status = false; 00272 KonsoleKalendarExports exports; 00273 00274 if( event ) 00275 { 00276 if( m_variables->getExportType() == CSV ) { 00277 status = exports.exportAsCSV( ts, event, dt ); 00278 kdDebug() << "konsolekalendar.cpp::printEvent() | CSV export" << endl; 00279 } else { // Default ExportType is TEXT_KONSOLEKALENDAR 00280 status = exports.exportAsTxt( ts, event, dt ); 00281 kdDebug() << "konsolekalendar.cpp::printEvent() | TEXT export" << endl; 00282 } //else 00283 } //if 00284 00285 return( status ); 00286 } 00287 00288 bool KonsoleKalendar::addEvent() 00289 { 00290 kdDebug() << "konsolecalendar.cpp::addEvent() | Create Adding" << endl; 00291 KonsoleKalendarAdd add( m_variables ); 00292 kdDebug() << "konsolecalendar.cpp::addEvent() | Adding Event now!" << endl; 00293 return( add.addEvent() ); 00294 } 00295 00296 bool KonsoleKalendar::changeEvent() 00297 { 00298 00299 kdDebug() << "konsolecalendar.cpp::changeEvent() | Create Changing" << endl; 00300 KonsoleKalendarChange change( m_variables ); 00301 kdDebug() << "konsolecalendar.cpp::changeEvent() | Changing Event now!" << endl; 00302 return( change.changeEvent() ); 00303 } 00304 00305 bool KonsoleKalendar::deleteEvent() 00306 { 00307 kdDebug() << "konsolecalendar.cpp::deleteEvent() | Create Deleting" << endl; 00308 KonsoleKalendarDelete del( m_variables ); 00309 kdDebug() << "konsolecalendar.cpp::deleteEvent() | Deleting Event now!" << endl; 00310 return( del.deleteEvent() ); 00311 } 00312 00313 bool KonsoleKalendar::isEvent( QDateTime startdate, QDateTime enddate, QString summary ) 00314 { 00315 // Search for an event with specified start and end date/time stamps and summaries. 00316 00317 Event *event; 00318 Event::List::ConstIterator it; 00319 00320 bool found = false; 00321 00322 Event::List eventList( m_variables->getCalendar()-> 00323 rawEventsForDate( startdate.date(), true )); 00324 for ( it = eventList.begin(); it != eventList.end(); ++it ) { 00325 event = *it; 00326 if ( event->dtEnd()==enddate && event->summary()==summary ) { 00327 found = true; 00328 break; 00329 } 00330 } 00331 return found; 00332 } 00333 00334 Event::List KonsoleKalendar::allEventsSorted() 00335 { 00336 Event::List *eventList = new Event::List ( m_variables->getCalendar()->rawEvents( ) ); 00337 00338 // Sort based on dtStart.toTime_t() 00339 Event::List::ConstIterator it; 00340 Event::List eventListSorted; 00341 Event::List::Iterator sortIt; 00342 for ( it = eventList->begin(); it != eventList->end(); ++it ) { 00343 sortIt = eventListSorted.begin(); 00344 while ( sortIt != eventListSorted.end() && 00345 (*it)->dtStart().toTime_t() >= (*sortIt)->dtStart().toTime_t() ) { 00346 ++sortIt; 00347 } 00348 eventListSorted.insert( sortIt, *it ); 00349 } 00350 return ( eventListSorted ); 00351 } 00352 00353 void KonsoleKalendar::printSpecs() 00354 { 00355 cout << i18n(" What: ").local8Bit() << m_variables->getSummary().local8Bit() << endl; 00356 cout << i18n(" Begin: ").local8Bit() << m_variables->getStartDateTime().toString(Qt::TextDate).local8Bit() << endl; 00357 cout << i18n(" End: ").local8Bit() << m_variables->getEndDateTime().toString(Qt::TextDate).local8Bit() << endl; 00358 if( m_variables->getFloating() == true ) { 00359 cout << i18n(" No Time Associated with Event").local8Bit() << endl; 00360 } 00361 cout << i18n(" Desc: ").local8Bit() << m_variables->getDescription().local8Bit() << endl; 00362 }
KDE Logo
This file is part of the documentation for konsolekalendar Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:58:15 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003