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 void KonsoleKalendar::importCalendar()
00059 {
00060   KonsoleKalendarAdd add( m_variables );
00061 
00062   kdDebug() << "konsolecalendar.cpp::importCalendar() | importing now!"  << endl;
00063   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() << endl;
00074   } else {
00075 
00076     kdDebug() << "konsolekalendar.cpp::createCalendar() | Creating calendar file: " << m_variables->getCalendarFile() << endl;
00077 
00078     if( m_variables->isVerbose() ) {
00079 // TODO: put back after string freeze
00080 //      cout << i18n("Create Calendar <Verbose>:").local8Bit() << m_variables->getCalendarFile() << 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       Event::List sortedList = allEventsSorted( );
00131       status = printEventList ( &ts, &sortedList );
00132     } else if( m_variables->isUID() ) {
00133       kdDebug() << "konsolekalendar.cpp::showInstance() | view events by uid list" << endl;
00134       event = m_variables->getCalendar()->event( m_variables->getUID() );
00135       status = printEvent ( &ts, event );
00136         } else if( m_variables->isNext() ) {
00137           kdDebug() << "konsolekalendar.cpp::showInstance() | Show next activity in calendar" << endl;
00138           
00139           QDateTime datetime = m_variables->getStartDateTime();
00140           datetime = datetime.addDays( 90 );
00141           eventList = new Event::List ( m_variables->getCalendar()->rawEvents( 
00142                                         m_variables->getStartDateTime().date(),
00143                                         datetime.date(),
00144                                         true ) );
00145    
00146            if( eventList->count() ) {
00147                  Event::List::ConstIterator it = eventList->begin();
00148                  Event *singleEvent = *it;
00149                  printEvent( &ts, singleEvent );
00150            } else {
00151                  // if no events
00152                  ts << "(no events in next 90 days)" << endl;
00153            }
00154    
00155         } else {
00156       kdDebug() << "konsolekalendar.cpp::showInstance() | view raw events within date range list" << endl;
00157       eventList = new Event::List ( m_variables->getCalendar()->rawEvents( 
00158                       m_variables->getStartDateTime().date(),
00159                       m_variables->getEndDateTime().date(),
00160                       true ) );
00161 
00162       status = printEventList ( &ts, eventList );
00163       delete eventList;
00164     }
00165 
00166       } else {
00167 
00168     QDate firstdate, lastdate;
00169     if( m_variables->getAll() ) {
00170       // TODO: this is broken since the date on last() may not be last date (this is the case for me)
00171       kdDebug() << "konsolekalendar.cpp::showInstance() | HTML view all events sorted list" << endl;
00172       eventList = new Event::List ( m_variables->getCalendar()->rawEvents( ) );
00173       firstdate = eventList->first()->dtStart().date();
00174       lastdate = eventList->last()->dtStart().date();
00175       delete eventList;
00176     } else if( m_variables->isUID() ) {
00177       // TODO
00178       kdDebug() << "konsolekalendar.cpp::showInstance() | HTML view events by uid list" << endl;
00179       kdError() << i18n("Sorry, export to HTML by UID is not supported yet").local8Bit() << endl;
00180       return( false );
00181     } else {
00182       kdDebug() << "konsolekalendar.cpp::showInstance() | HTML view raw events within date range list" << endl;
00183       firstdate = m_variables->getStartDateTime().date();
00184       lastdate = m_variables->getStartDateTime().date();
00185     }
00186 
00187     KCal::HtmlExport Export( m_variables->getCalendarResources() );
00188 
00189     Export.setTitle( title );
00190     Export.setEmail( "" );
00191     Export.setFullName( "" );
00192     Export.setCredit( "KonsoleKalendar", "http://pim.kde.org/components/konsolekalendar.php" );
00193 
00194     Export.setMonthViewEnabled( false );  // month view would be another export mode, no?
00195     Export.setEventsEnabled( true );
00196     Export.setCategoriesEventEnabled( true );
00197     Export.setAttendeesEventEnabled( true );
00198     Export.setExcludePrivateEventEnabled( true );
00199     Export.setExcludeConfidentialEventEnabled( true );
00200 // Not supporting Todos yet
00201     title = "To-Do List for " + firstdate.toString(Qt::TextDate);
00202     if( firstdate != lastdate ) {
00203       title += " - " + lastdate.toString(Qt::TextDate);
00204     }
00205     Export.setTitleTodo( title );
00206     Export.setTodosEnabled( false );
00207     Export.setCategoriesTodoEnabled( false );
00208     Export.setAttendeesTodoEnabled( false );
00209     Export.setExcludePrivateTodoEnabled( false );
00210     Export.setExcludeConfidentialTodoEnabled( false );
00211     Export.setDueDateEnabled( false );
00212 
00213     Export.setDateRange( firstdate, lastdate );
00214 
00215     status = Export.save( &ts );
00216       }
00217 
00218       f.close();
00219 
00220     }
00221   }
00222 
00223   return status;
00224 }
00225 
00226 bool KonsoleKalendar::printEventList( QTextStream *ts, Event::List *eventList )
00227 {
00228 
00229   bool status = true;
00230 
00231   if( eventList->count() ) {
00232 
00233     Event *singleEvent;
00234     Event::List::ConstIterator it;
00235     KonsoleKalendarExports exports;
00236 
00237     for( it = eventList->begin(); it != eventList->end(); ++it ) {
00238       singleEvent = *it;
00239 
00240       status = printEvent( ts, singleEvent );
00241       if( ! status )break;
00242 
00243     }// for
00244 
00245   } else {
00246 
00247     // if no events
00248     *ts << "(no events)" << endl;
00249   }
00250 
00251   return( status );
00252 }
00253 
00254 bool KonsoleKalendar::printEvent( QTextStream *ts, Event *event )
00255 {
00256 
00257   bool status = false;
00258   KonsoleKalendarExports exports;
00259 
00260   if( event )
00261   {
00262     if( m_variables->getExportType() == CSV ) {
00263       status = exports.exportAsCSV( ts, event );
00264       kdDebug() << "konsolekalendar.cpp::printEvent() | CSV export" << endl;
00265     } else {  // Default ExportType is TEXT_KONSOLEKALENDAR
00266       status = exports.exportAsTxt( ts, event );
00267       kdDebug() << "konsolekalendar.cpp::printEvent() | TEXT export" << endl;  
00268     } //else
00269   } //if
00270 
00271   return( status );
00272 }
00273 
00274 bool KonsoleKalendar::addEvent()
00275 {
00276   kdDebug() << "konsolecalendar.cpp::addEvent() | Create Adding"  << endl;
00277   KonsoleKalendarAdd add( m_variables );
00278   kdDebug() << "konsolecalendar.cpp::addEvent() | Adding Event now!"  << endl;
00279   return( add.addEvent() );
00280 }
00281 
00282 bool KonsoleKalendar::changeEvent()
00283 {
00284 
00285   kdDebug() << "konsolecalendar.cpp::changeEvent() | Create Changing"  << endl;
00286   KonsoleKalendarChange change( m_variables );
00287   kdDebug() << "konsolecalendar.cpp::changeEvent() | Changing Event now!"  << endl;
00288   return( change.changeEvent() );
00289 }
00290 
00291 bool KonsoleKalendar::deleteEvent()
00292 {
00293   kdDebug() << "konsolecalendar.cpp::deleteEvent() | Create Deleting"  << endl;
00294   KonsoleKalendarDelete del( m_variables );
00295   kdDebug() << "konsolecalendar.cpp::deleteEvent() | Deleting Event now!"  << endl;
00296   return( del.deleteEvent() );
00297 }
00298 
00299 bool KonsoleKalendar::isEvent( QDateTime startdate, QDateTime enddate, QString summary )
00300 {
00301   // Search for an event with specified start and end date/time stamps and summaries.
00302 
00303   Event *event;
00304   Event::List::ConstIterator it;
00305  
00306   bool found = false;
00307   
00308   Event::List eventList( m_variables->getCalendar()->
00309              rawEventsForDate( startdate.date(), true ));
00310   for ( it =  eventList.begin(); it != eventList.end(); ++it ) {
00311     event = *it;
00312     if ( event->dtEnd()==enddate && event->summary()==summary ) {
00313       found = true;
00314       break;
00315     }
00316   }
00317   return found;
00318 }
00319 
00320 Event::List KonsoleKalendar::allEventsSorted()
00321 {
00322   Event::List *eventList = new Event::List ( m_variables->getCalendar()->rawEvents( ) );
00323 
00324   // Sort based on dtStart.toTime_t()
00325   Event::List::ConstIterator it;
00326   Event::List eventListSorted;
00327   Event::List::Iterator sortIt;
00328   for ( it = eventList->begin(); it != eventList->end(); ++it ) {
00329     sortIt = eventListSorted.begin();
00330     while ( sortIt != eventListSorted.end() &&
00331             (*it)->dtStart().toTime_t() >= (*sortIt)->dtStart().toTime_t() ) {
00332       ++sortIt;
00333     }
00334     eventListSorted.insert( sortIt, *it );
00335   }
00336   return ( eventListSorted );
00337 }
00338 
00339 void KonsoleKalendar::printSpecs()
00340 {
00341   cout << i18n("  What:  ").local8Bit() << m_variables->getSummary().local8Bit() << endl;
00342   cout << i18n("  Begin: ").local8Bit() << m_variables->getStartDateTime().toString(Qt::TextDate).local8Bit() << endl;
00343   cout << i18n("  End:   ").local8Bit() << m_variables->getEndDateTime().toString(Qt::TextDate).local8Bit() << endl;
00344   if( m_variables->getFloating() == true ) {
00345     cout << i18n("  No Time Associated with Event").local8Bit() << endl;
00346   }
00347   cout << i18n("  Desc:  ").local8Bit() << m_variables->getDescription().local8Bit() << endl;
00348 }
KDE Logo
This file is part of the documentation for konsolekalendar Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Mar 6 17:18:34 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003