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() && status != false;
00198                 dt = dt.addDays(1) ) {
00199         Event::List events =
00200               m_variables->getCalendar()->events( dt,
00201                                                   EventSortStartDate,
00202                                                   SortDirectionAscending );
00203         status = printEventList( &ts, &events, dt );
00204 
00205         // when we get next event we exit..
00206         if ( events.count() ) {
00207           kdDebug() << "konsolekalendar.cpp::showInstance() | "
00208                         << "Next event"
00209                         << endl;
00210           return true;
00211         }
00212           }
00213     } else {
00214       kdDebug() << "konsolekalendar.cpp::showInstance() | "
00215                     << "view raw events within date range list"
00216                     << endl;
00217 
00218       QDate 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     QDate 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() << 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(Qt::TextDate);
00287 //         if ( firstdate != lastdate ) {
00288 //           title += " - " + lastdate.toString(Qt::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( QDateTime( firstdate ) );
00297         htmlSettings.setDateEnd( QDateTime( 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( QTextStream *ts,
00311                                       Event::List *eventList, QDate 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( QTextStream *ts, Event *event, QDate 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( QDateTime startdate,
00412                                QDateTime enddate, QString 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()
00439        << endl;
00440 
00441   cout << i18n( "  Begin: %1" ).
00442     arg( m_variables->getStartDateTime().toString( Qt::TextDate ) ).local8Bit()
00443        << endl;
00444 
00445   cout << i18n( "  End:   %1" ).
00446     arg( m_variables->getEndDateTime().toString( Qt::TextDate ) ).local8Bit()
00447        << endl;
00448 
00449   if ( m_variables->getFloating() == true ) {
00450     cout << i18n( "  No Time Associated with Event" ).local8Bit()
00451          << endl;
00452   }
00453 
00454   cout << i18n( "  Desc:  %1" ).
00455     arg( m_variables->getDescription() ).local8Bit()
00456        << endl;
00457 
00458   cout << i18n( "  Location:  %1" ).
00459     arg( m_variables->getLocation() ).local8Bit()
00460        << endl;
00461 }
KDE Home | KDE Accessibility Home | Description of Access Keys