konsolekalendar

konsolekalendar.cpp

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