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