konsolekalendar Library API Documentation

konsolekalendar.cpp

00001 /*******************************************************************************
00002  * konsolekalendar.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-2004  Allen Winter <awinterz@users.sourceforge.net>      *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
00021  *                                                                             *
00022  * As a special exception, permission is given to link this program            *
00023  * with any edition of Qt, and distribute the resulting executable,            *
00024  * without including the source code for Qt in the source distribution.        *
00025  *                                                                             *
00026  ******************************************************************************/
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <iostream>
00031 
00032 #include <qdatetime.h>
00033 #include <qfile.h>
00034 #include <qtextstream.h>
00035 
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038 #include <kstandarddirs.h>
00039 
00040 #include <libkcal/calendarlocal.h>
00041 #include <libkcal/resourcecalendar.h>
00042 #include <libkcal/calendarresources.h>
00043 #include <libkcal/calendar.h>
00044 #include <libkcal/event.h>
00045 #include <libkcal/htmlexport.h>
00046 
00047 #include "konsolekalendar.h"
00048 #include "konsolekalendaradd.h"
00049 #include "konsolekalendarchange.h"
00050 #include "konsolekalendardelete.h"
00051 #include "konsolekalendarexports.h"
00052 
00053 using namespace KCal;
00054 using namespace std;
00055 
00056 KonsoleKalendar::KonsoleKalendar(KonsoleKalendarVariables *variables)
00057 {
00058   m_variables = variables;
00059   // m_Calendar =  new ResourceCalendar;
00060 }
00061 
00062 KonsoleKalendar::~KonsoleKalendar()
00063 {
00064 }
00065 
00066 bool KonsoleKalendar::importCalendar()
00067 {
00068   KonsoleKalendarAdd add( m_variables );
00069 
00070   kdDebug() << "konsolecalendar.cpp::importCalendar() | importing now!"
00071             << endl;
00072   return( add.addImportedCalendar() );
00073 }
00074 
00075 bool KonsoleKalendar::createCalendar()
00076 {
00077   bool status = false;
00078   CalendarLocal newCalendar;
00079 
00080   if ( m_variables->isDryRun() ) {
00081     cout << i18n("Create Calendar <Dry Run>: %1").
00082       arg( m_variables->getCalendarFile() ).local8Bit()
00083          << endl;
00084   } else {
00085     kdDebug() << "konsolekalendar.cpp::createCalendar() | "
00086               << "Creating calendar file: "
00087               << m_variables->getCalendarFile().local8Bit()
00088               << endl;
00089 
00090     if ( m_variables->isVerbose() ) {
00091       cout << i18n("Create Calendar <Verbose>: %1").
00092         arg( m_variables->getCalendarFile() ).local8Bit()
00093            << endl;
00094     }
00095 
00096     if ( newCalendar.save( m_variables->getCalendarFile() ) ) {
00097       newCalendar.close();
00098       status = true;
00099     }
00100   }
00101   return status;
00102 }
00103 
00104 bool KonsoleKalendar::showInstance()
00105 {
00106   bool status = true;
00107   QFile f;
00108   QString title;
00109   Event::List *eventList;
00110   Event *event;
00111 
00112   if ( m_variables->isDryRun() ) {
00113     cout << i18n("View Events <Dry Run>:").local8Bit()
00114          << endl;
00115     printSpecs();
00116   } else {
00117 
00118     kdDebug() << "konsolekalendar.cpp::showInstance() | "
00119               << "open export file"
00120               << endl;
00121 
00122     if ( m_variables->isExportFile() ) {
00123       f.setName( m_variables->getExportFile() );
00124       if ( !f.open( IO_WriteOnly ) ) {
00125     status = false;
00126     kdDebug() << "konsolekalendar.cpp::showInstance() | "
00127                   << "unable to open export file "
00128                   << m_variables->getExportFile()
00129                   << endl;
00130       }
00131     } else {
00132       f.open( IO_WriteOnly, stdout );
00133     }
00134 
00135     if ( status ) {
00136       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00137                 << "opened successful"
00138                 << endl;
00139 
00140       if ( m_variables->isVerbose() ) {
00141     cout << i18n("View Event <Verbose>:").local8Bit()
00142              << endl;
00143     printSpecs();
00144       }
00145 
00146       QTextStream ts( &f );
00147 
00148       if ( m_variables->getExportType() != HTML ) {
00149 
00150     if ( m_variables->getAll() ) {
00151       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00152                     << "view all events sorted list"
00153                     << endl;
00154 
00155       Event::List sortedList = allEventsSorted();
00156 
00157       QDate dt, firstdate, lastdate;
00158       firstdate = sortedList.first()->dtStart().date();
00159       lastdate = sortedList.last()->dtStart().date();
00160       for ( dt=firstdate;
00161                 dt<=lastdate && status != false;
00162                 dt=dt.addDays(1) ) {
00163         Event::List events = m_variables->getCalendar()->events( dt, true );
00164         status = printEventList( &ts, &events, dt );
00165       }
00166 
00167     } else if ( m_variables->isUID() ) {
00168       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00169                     << "view events by uid list"
00170                     << endl;
00171       //TODO: support a list of UIDs
00172       event = m_variables->getCalendar()->event( m_variables->getUID() );
00173       //If this UID represents a recurring Event,
00174           //only the first day of the Event will be printed
00175       status = printEvent ( &ts, event, event->dtStart().date() );
00176 
00177         } else if ( m_variables->isNext() ) {
00178           kdDebug() << "konsolekalendar.cpp::showInstance() | "
00179                     << "Show next activity in calendar"
00180                     << endl;
00181 
00182           QDateTime datetime = m_variables->getStartDateTime();
00183           datetime = datetime.addDays( 720 );
00184 
00185       QDate dt;
00186       for ( dt=m_variables->getStartDateTime().date();
00187                 dt<=datetime.date() && status != false;
00188                 dt=dt.addDays(1) ) {
00189         Event::List events = m_variables->getCalendar()->events( dt, true );
00190         status = printEventList( &ts, &events, dt );
00191 
00192         // when we get next event we exit..
00193         if ( events.count() ) {
00194           kdDebug() << "konsolekalendar.cpp::showInstance() | "
00195                         << "Next event"
00196                         << endl;
00197           return true;
00198         }
00199           }
00200     } else {
00201       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00202                     << "view raw events within date range list"
00203                     << endl;
00204 
00205       QDate dt;
00206       for ( dt=m_variables->getStartDateTime().date();
00207                 dt<=m_variables->getEndDateTime().date() && status != false;
00208                 dt=dt.addDays(1) ) {
00209         Event::List events = m_variables->getCalendar()->events( dt, true );
00210         status = printEventList( &ts, &events, dt );
00211       }
00212     }
00213       } else {
00214     QDate firstdate, lastdate;
00215     if ( m_variables->getAll() ) {
00216       // TODO: this is broken since the date on last() may not be last date
00217           // (this is the case for me)
00218       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00219                     << "HTML view all events sorted list"
00220                     << endl;
00221       eventList =
00222             new Event::List ( m_variables->getCalendar()->rawEvents() );
00223       firstdate = eventList->first()->dtStart().date();
00224       lastdate = eventList->last()->dtStart().date();
00225       delete eventList;
00226     } else if ( m_variables->isUID() ) {
00227       // TODO
00228       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00229                     << "HTML view events by uid list" << endl;
00230       cout << i18n("Sorry, export to HTML by UID is not supported yet")
00231             .local8Bit() << endl;
00232       return( false );
00233     } else {
00234       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00235                     << "HTML view raw events within date range list"
00236                     << endl;
00237       firstdate = m_variables->getStartDateTime().date();
00238       lastdate = m_variables->getEndDateTime().date();
00239     }
00240 
00241         KCal::HtmlExport *Export;
00242         if ( !m_variables->isCalendarResources() ) {
00243           Export = new HtmlExport( m_variables->getCalendar() );
00244         } else {
00245           Export = new HtmlExport( m_variables->getCalendarResources() );
00246         }
00247 
00248     title = "Appointments for " + firstdate.toString(Qt::TextDate);
00249     if ( firstdate != lastdate ) {
00250       title += " - " + lastdate.toString(Qt::TextDate);
00251     }
00252     Export->setTitle( title );
00253     Export->setEmail( "" );
00254     Export->setFullName( "" );
00255         //TODO: get progname and url from the values set in main
00256     Export->setCredit( "KonsoleKalendar",
00257                            "http://pim.kde.org/components/konsolekalendar.php");
00258 
00259     Export->setMonthViewEnabled( false );
00260     Export->setEventsEnabled( true );
00261     Export->setCategoriesEventEnabled( true );
00262     Export->setAttendeesEventEnabled( true );
00263     Export->setExcludePrivateEventEnabled( true );
00264     Export->setExcludeConfidentialEventEnabled( true );
00265 // Not supporting Todos yet
00266     title = "To-Do List for " + firstdate.toString(Qt::TextDate);
00267     if ( firstdate != lastdate ) {
00268       title += " - " + lastdate.toString(Qt::TextDate);
00269     }
00270     Export->setTitleTodo( title );
00271     Export->setTodosEnabled( false );
00272     Export->setCategoriesTodoEnabled( false );
00273     Export->setAttendeesTodoEnabled( false );
00274     Export->setExcludePrivateTodoEnabled( false );
00275     Export->setExcludeConfidentialTodoEnabled( false );
00276     Export->setDueDateEnabled( false );
00277 
00278     Export->setDateRange( firstdate, lastdate );
00279 
00280     status = Export->save( &ts );
00281       }
00282       f.close();
00283     }
00284   }
00285   return status;
00286 }
00287 
00288 bool KonsoleKalendar::printEventList( QTextStream *ts,
00289                                       Event::List *eventList, QDate date )
00290 {
00291   bool status = true;
00292 
00293   if ( eventList->count() ) {
00294     Event *singleEvent;
00295     Event::List::ConstIterator it;
00296 
00297     for ( it = eventList->begin();
00298           it != eventList->end() && status != false;
00299           ++it ) {
00300       singleEvent = *it;
00301 
00302       status = printEvent( ts, singleEvent, date );
00303     }
00304   }
00305   return( status );
00306 }
00307 
00308 bool KonsoleKalendar::printEvent( QTextStream *ts, Event *event, QDate dt )
00309 {
00310   bool status = false;
00311   bool sameDay = true;
00312   KonsoleKalendarExports exports;
00313 
00314   if ( event )
00315   {
00316     switch ( m_variables->getExportType() ) {
00317 
00318     case CSV:
00319       kdDebug() << "konsolekalendar.cpp::printEvent() | "
00320                 << "CSV export"
00321                 << endl;
00322       status = exports.exportAsCSV( ts, event, dt );
00323       break;
00324 
00325     case TEXT_SHORT:
00326       kdDebug()
00327         << "konsolekalendar.cpp::printEvent() | "
00328         << "TEXT-SHORT export"
00329         << endl;
00330       if ( dt.daysTo( m_saveDate ) ) {
00331         sameDay = false;
00332         m_saveDate = dt;
00333       }
00334       status = exports.exportAsTxtShort( ts, event, dt, sameDay );
00335       break;
00336 
00337     case HTML:
00338       // this is handled separately for now
00339       break;
00340 
00341     default:// Default ExportType is TEXT_KONSOLEKALENDAR
00342       kdDebug() << "konsolekalendar.cpp::printEvent() | "
00343                 << "TEXT export"
00344                 << endl;
00345       status = exports.exportAsTxt( ts, event, dt );
00346       break;
00347     }
00348   }
00349   return( status );
00350 }
00351 
00352 bool KonsoleKalendar::addEvent()
00353 {
00354   kdDebug() << "konsolecalendar.cpp::addEvent() | "
00355             << "Create Adding"
00356             << endl;
00357   KonsoleKalendarAdd add( m_variables );
00358   kdDebug() << "konsolecalendar.cpp::addEvent() | "
00359             << "Adding Event now!"
00360             << endl;
00361   return( add.addEvent() );
00362 }
00363 
00364 bool KonsoleKalendar::changeEvent()
00365 {
00366 
00367   kdDebug() << "konsolecalendar.cpp::changeEvent() | "
00368             << "Create Changing"
00369             << endl;
00370   KonsoleKalendarChange change( m_variables );
00371   kdDebug() << "konsolecalendar.cpp::changeEvent() | "
00372             << "Changing Event now!"
00373             << endl;
00374   return( change.changeEvent() );
00375 }
00376 
00377 bool KonsoleKalendar::deleteEvent()
00378 {
00379   kdDebug() << "konsolecalendar.cpp::deleteEvent() | "
00380             << "Create Deleting"
00381             << endl;
00382   KonsoleKalendarDelete del( m_variables );
00383   kdDebug() << "konsolecalendar.cpp::deleteEvent() | "
00384             << "Deleting Event now!"
00385             << endl;
00386   return( del.deleteEvent() );
00387 }
00388 
00389 bool KonsoleKalendar::isEvent( QDateTime startdate,
00390                                QDateTime enddate, QString summary )
00391 {
00392   // Search for an event with specified start and end datetime stamp and summary
00393 
00394   Event *event;
00395   Event::List::ConstIterator it;
00396 
00397   bool found = false;
00398 
00399   Event::List eventList( m_variables->getCalendar()->
00400              rawEventsForDate( startdate.date(), true ));
00401   for ( it =  eventList.begin(); it != eventList.end(); ++it ) {
00402     event = *it;
00403     if ( event->dtEnd()==enddate && event->summary()==summary ) {
00404       found = true;
00405       break;
00406     }
00407   }
00408   return found;
00409 }
00410 
00411 Event::List KonsoleKalendar::allEventsSorted()
00412 {
00413   Event::List *eventList =
00414     new Event::List ( m_variables->getCalendar()->rawEvents( ) );
00415 
00416   // Sort based on Event Starting DateTime
00417   Event::List::ConstIterator it;
00418   Event::List eventListSorted;
00419   Event::List::Iterator sortIt;
00420   for ( it = eventList->begin(); it != eventList->end(); ++it ) {
00421     sortIt = eventListSorted.begin();
00422     while ( sortIt != eventListSorted.end() &&
00423             (*it)->dtStart() >= (*sortIt)->dtStart() ) {
00424       ++sortIt;
00425     }
00426     eventListSorted.insert( sortIt, *it );
00427   }
00428   return ( eventListSorted );
00429 }
00430 
00431 void KonsoleKalendar::printSpecs()
00432 {
00433   cout << i18n("  What:  %1").
00434     arg( m_variables->getSummary() ).local8Bit()
00435        << endl;
00436 
00437   cout << i18n("  Begin: %1").
00438     arg( m_variables->getStartDateTime().toString(Qt::TextDate) ).local8Bit()
00439        << endl;
00440 
00441   cout << i18n("  End:   %1").
00442     arg( m_variables->getEndDateTime().toString(Qt::TextDate) ).local8Bit()
00443        << endl;
00444 
00445   if ( m_variables->getFloating() == true ) {
00446     cout << i18n("  No Time Associated with Event").local8Bit()
00447          << endl;
00448   }
00449 
00450   cout << i18n("  Desc:  %1").
00451     arg( m_variables->getDescription() ).local8Bit()
00452        << endl;
00453 
00454   cout << i18n("  Location:  %1").
00455     arg( m_variables->getLocation() ).local8Bit()
00456        << endl;
00457 }
KDE Logo
This file is part of the documentation for konsolekalendar Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:56:55 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003