konsolekalendar

konsolekalendar.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * konsolekalendar.cpp *
3  * *
4  * KonsoleKalendar is a command line interface to KDE calendars *
5  * Copyright (C) 2002-2004 Tuukka Pasanen <illuusio@mailcity.com> *
6  * Copyright (C) 2003-2005 Allen Winter <winter@kde.org> *
7  * Copyright (C) 2010-2011 Timothy Pearson <kb9vqf@pearsoncomputing.net> *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the Free Software *
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  * *
23  * As a special exception, permission is given to link this program *
24  * with any edition of TQt, and distribute the resulting executable, *
25  * without including the source code for TQt in the source distribution. *
26  * *
27  ******************************************************************************/
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <iostream>
37 
38 #include <tqdatetime.h>
39 #include <tqfile.h>
40 #include <tqtextstream.h>
41 
42 #include <kdebug.h>
43 #include <klocale.h>
44 
45 #include <libkcal/calendarlocal.h>
46 #include <libkcal/resourcecalendar.h>
47 #include <libkcal/calendarresources.h>
48 #include <libkcal/calendar.h>
49 #include <libkcal/event.h>
50 #include <libkcal/htmlexport.h>
51 #include <libkdepim/kpimprefs.h>
52 
53 #include "konsolekalendar.h"
54 #include "konsolekalendaradd.h"
55 #include "konsolekalendarchange.h"
56 #include "konsolekalendardelete.h"
57 #include "konsolekalendarexports.h"
58 
59 using namespace KCal;
60 using namespace std;
61 
63 {
64  m_variables = variables;
65 }
66 
68 {
69 }
70 
72 {
73  KonsoleKalendarAdd add( m_variables );
74 
75  kdDebug() << "konsolecalendar.cpp::importCalendar() | importing now!"
76  << endl;
77  return( add.addImportedCalendar() );
78 }
79 
81 {
82  bool status = false;
83  CalendarLocal newCalendar( KPimPrefs::timezone() );
84 
85  if ( m_variables->isDryRun() ) {
86  cout << i18n( "Create Calendar <Dry Run>: %1" ).
87  arg( m_variables->getCalendarFile() ).local8Bit().data()
88  << endl;
89  } else {
90  kdDebug() << "konsolekalendar.cpp::createCalendar() | "
91  << "Creating calendar file: "
92  << m_variables->getCalendarFile().local8Bit().data()
93  << endl;
94 
95  if ( m_variables->isVerbose() ) {
96  cout << i18n( "Create Calendar <Verbose>: %1" ).
97  arg( m_variables->getCalendarFile() ).local8Bit().data()
98  << endl;
99  }
100 
101  if ( newCalendar.save( m_variables->getCalendarFile() ) ) {
102  newCalendar.close();
103  status = true;
104  }
105  }
106  return status;
107 }
108 
110 {
111  bool status = true;
112  TQFile f;
113  TQString title;
114  Event *event;
115 
116  if ( m_variables->isDryRun() ) {
117  cout << i18n( "View Events <Dry Run>:" ).local8Bit().data()
118  << endl;
119  printSpecs();
120  } else {
121 
122  kdDebug() << "konsolekalendar.cpp::showInstance() | "
123  << "open export file"
124  << endl;
125 
126  if ( m_variables->isExportFile() ) {
127  f.setName( m_variables->getExportFile() );
128  if ( !f.open( IO_WriteOnly ) ) {
129  status = false;
130  kdDebug() << "konsolekalendar.cpp::showInstance() | "
131  << "unable to open export file "
132  << m_variables->getExportFile()
133  << endl;
134  }
135  } else {
136  f.open( IO_WriteOnly, stdout );
137  }
138 
139  if ( status ) {
140  kdDebug() << "konsolekalendar.cpp::showInstance() | "
141  << "opened successful"
142  << endl;
143 
144  if ( m_variables->isVerbose() ) {
145  cout << i18n( "View Event <Verbose>:" ).local8Bit().data()
146  << endl;
147  printSpecs();
148  }
149 
150  TQTextStream ts( &f );
151 
152  if ( m_variables->getExportType() != ExportTypeHTML &&
153  m_variables->getExportType() != ExportTypeMonthHTML ) {
154 
155  if ( m_variables->getAll() ) {
156  kdDebug() << "konsolekalendar.cpp::showInstance() | "
157  << "view all events sorted list"
158  << endl;
159 
160  Event::List sortedList =
161  m_variables->getCalendar()->events( EventSortStartDate );
162  if( sortedList.count() > 0 )
163  {
164  TQDate dt, firstdate, lastdate;
165  firstdate = sortedList.first()->dtStart().date();
166  lastdate = sortedList.last()->dtStart().date();
167  for ( dt = firstdate;
168  dt <= lastdate && status != false;
169  dt = dt.addDays(1) ) {
170  Event::List events =
171  m_variables->getCalendar()->events( dt,
172  EventSortStartDate,
173  SortDirectionAscending );
174  status = printEventList( &ts, &events, dt );
175  }
176  }
177 
178  } else if ( m_variables->isUID() ) {
179  kdDebug() << "konsolekalendar.cpp::showInstance() | "
180  << "view events by uid list"
181  << endl;
182  //TODO: support a list of UIDs
183  event = m_variables->getCalendar()->event( m_variables->getUID() );
184  //If this UID represents a recurring Event,
185  //only the first day of the Event will be printed
186  status = printEvent ( &ts, event, event->dtStart().date() );
187 
188  } else if ( m_variables->isNext() ) {
189  kdDebug() << "konsolekalendar.cpp::showInstance() | "
190  << "Show next activity in calendar"
191  << endl;
192 
193  TQDateTime datetime = m_variables->getStartDateTime();
194  datetime = datetime.addDays( 720 );
195 
196  TQDate dt;
197  for ( dt = m_variables->getStartDateTime().date();
198  dt <= datetime.date();
199  dt = dt.addDays(1) ) {
200  Event::List events =
201  m_variables->getCalendar()->events( dt,
202  EventSortStartDate,
203  SortDirectionAscending );
204  // finished here when we get the next event
205  if ( events.count() > 0 ) {
206  kdDebug() << "konsolekalendar.cpp::showInstance() | "
207  << "Got the next event"
208  << endl;
209  printEvent( &ts, events.first(), dt );
210  return true;
211  }
212  }
213  } else {
214  kdDebug() << "konsolekalendar.cpp::showInstance() | "
215  << "view raw events within date range list"
216  << endl;
217 
218  TQDate dt;
219  for ( dt = m_variables->getStartDateTime().date();
220  dt <= m_variables->getEndDateTime().date() && status != false;
221  dt = dt.addDays(1) ) {
222  Event::List events =
223  m_variables->getCalendar()->events( dt,
224  EventSortStartDate,
225  SortDirectionAscending );
226  status = printEventList( &ts, &events, dt );
227  }
228  }
229  } else {
230  TQDate firstdate, lastdate;
231  if ( m_variables->getAll() ) {
232  kdDebug() << "konsolekalendar.cpp::showInstance() | "
233  << "HTML view all events sorted list"
234  << endl;
235  // sort the events for this date by start date
236  // in order to determine the date range.
237  Event::List *events =
238  new Event::List ( m_variables->getCalendar()->rawEvents(
239  EventSortStartDate,
240  SortDirectionAscending ) );
241  firstdate = events->first()->dtStart().date();
242  lastdate = events->last()->dtStart().date();
243  } else if ( m_variables->isUID() ) {
244  // TODO
245  kdDebug() << "konsolekalendar.cpp::showInstance() | "
246  << "HTML view events by uid list" << endl;
247  cout << i18n("Sorry, export to HTML by UID is not supported yet")
248  .local8Bit().data() << endl;
249  return( false );
250  } else {
251  kdDebug() << "konsolekalendar.cpp::showInstance() | "
252  << "HTML view raw events within date range list"
253  << endl;
254  firstdate = m_variables->getStartDateTime().date();
255  lastdate = m_variables->getEndDateTime().date();
256  }
257 
258  HTMLExportSettings htmlSettings( "Konsolekalendar" );
259 
260  //TODO: get progname and url from the values set in main
261  htmlSettings.setCreditName( "KonsoleKalendar" );
262  htmlSettings.setCreditURL( "http://pim.kde.org/components/konsolekalendar.php" );
263 
264  htmlSettings.setExcludePrivate( true );
265  htmlSettings.setExcludeConfidential( true );
266 
267  htmlSettings.setEventView( false );
268  htmlSettings.setMonthView( false );
269  if ( m_variables->getExportType() == ExportTypeMonthHTML ) {
270  title = i18n( "Events:" );
271  htmlSettings.setMonthView( true );
272  } else {
273  if ( firstdate == lastdate ) {
274  title = i18n( "Events: %1" )
275  .arg( firstdate.toString( Qt::TextDate ) );
276  } else {
277  title = i18n( "Events: %1 - %2" )
278  .arg( firstdate.toString( Qt::TextDate ) )
279  .arg( lastdate.toString( Qt::TextDate ) );
280  }
281  htmlSettings.setEventView( true );
282  }
283  htmlSettings.setEventTitle( title );
284  htmlSettings.setEventAttendees( true );
285 // Not supporting Todos yet
286 // title = "To-Do List for " + firstdate.toString(TQt::TextDate);
287 // if ( firstdate != lastdate ) {
288 // title += " - " + lastdate.toString(TQt::TextDate);
289 // }
290  htmlSettings.setTodoListTitle( title );
291  htmlSettings.setTodoView( false );
292 // htmlSettings.setTaskCategories( false );
293 // htmlSettings.setTaskAttendees( false );
294 // htmlSettings.setTaskDueDate( true );
295 
296  htmlSettings.setDateStart( TQDateTime( firstdate ) );
297  htmlSettings.setDateEnd( TQDateTime( lastdate ) ) ;
298 
299  KCal::HtmlExport *Export;
300  Export = new HtmlExport( m_variables->getCalendar(), &htmlSettings );
301  status = Export->save( &ts );
302  delete Export;
303  }
304  f.close();
305  }
306  }
307  return status;
308 }
309 
310 bool KonsoleKalendar::printEventList( TQTextStream *ts,
311  Event::List *eventList, TQDate date )
312 {
313  bool status = true;
314 
315  if ( eventList->count() ) {
316  Event *singleEvent;
317  Event::List::ConstIterator it;
318 
319  for ( it = eventList->begin();
320  it != eventList->end() && status != false;
321  ++it ) {
322  singleEvent = *it;
323 
324  status = printEvent( ts, singleEvent, date );
325  }
326  }
327 
328  return( status );
329 }
330 
331 bool KonsoleKalendar::printEvent( TQTextStream *ts, Event *event, TQDate dt )
332 {
333  bool status = false;
334  bool sameDay = true;
335  KonsoleKalendarExports exports;
336 
337  if ( event ) {
338  switch ( m_variables->getExportType() ) {
339 
340  case ExportTypeCSV:
341  kdDebug() << "konsolekalendar.cpp::printEvent() | "
342  << "CSV export"
343  << endl;
344  status = exports.exportAsCSV( ts, event, dt );
345  break;
346 
347  case ExportTypeTextShort:
348  kdDebug()
349  << "konsolekalendar.cpp::printEvent() | "
350  << "TEXT-SHORT export"
351  << endl;
352  if ( dt.daysTo( m_saveDate ) ) {
353  sameDay = false;
354  m_saveDate = dt;
355  }
356  status = exports.exportAsTxtShort( ts, event, dt, sameDay );
357  break;
358 
359  case ExportTypeHTML:
360  // this is handled separately for now
361  break;
362 
363  default:// Default export-type is ExportTypeText
364  kdDebug() << "konsolekalendar.cpp::printEvent() | "
365  << "TEXT export"
366  << endl;
367  status = exports.exportAsTxt( ts, event, dt );
368  break;
369  }
370  }
371  return( status );
372 }
373 
375 {
376  kdDebug() << "konsolecalendar.cpp::addEvent() | "
377  << "Create Adding"
378  << endl;
379  KonsoleKalendarAdd add( m_variables );
380  kdDebug() << "konsolecalendar.cpp::addEvent() | "
381  << "Adding Event now!"
382  << endl;
383  return( add.addEvent() );
384 }
385 
387 {
388 
389  kdDebug() << "konsolecalendar.cpp::changeEvent() | "
390  << "Create Changing"
391  << endl;
392  KonsoleKalendarChange change( m_variables );
393  kdDebug() << "konsolecalendar.cpp::changeEvent() | "
394  << "Changing Event now!"
395  << endl;
396  return( change.changeEvent() );
397 }
398 
400 {
401  kdDebug() << "konsolecalendar.cpp::deleteEvent() | "
402  << "Create Deleting"
403  << endl;
404  KonsoleKalendarDelete del( m_variables );
405  kdDebug() << "konsolecalendar.cpp::deleteEvent() | "
406  << "Deleting Event now!"
407  << endl;
408  return( del.deleteEvent() );
409 }
410 
411 bool KonsoleKalendar::isEvent( TQDateTime startdate,
412  TQDateTime enddate, TQString summary )
413 {
414  // Search for an event with specified start and end datetime stamp and summary
415 
416  Event *event;
417  Event::List::ConstIterator it;
418 
419  bool found = false;
420 
421  Event::List eventList( m_variables->getCalendar()->
422  rawEventsForDate( startdate.date(),
423  EventSortStartDate,
424  SortDirectionAscending ) );
425  for ( it = eventList.begin(); it != eventList.end(); ++it ) {
426  event = *it;
427  if ( event->dtEnd() == enddate && event->summary() == summary ) {
428  found = true;
429  break;
430  }
431  }
432  return found;
433 }
434 
435 void KonsoleKalendar::printSpecs()
436 {
437  cout << i18n( " What: %1" ).
438  arg( m_variables->getSummary() ).local8Bit().data()
439  << endl;
440 
441  cout << i18n( " Begin: %1" ).
442  arg( m_variables->getStartDateTime().toString( Qt::TextDate ) ).local8Bit().data()
443  << endl;
444 
445  cout << i18n( " End: %1" ).
446  arg( m_variables->getEndDateTime().toString( Qt::TextDate ) ).local8Bit().data()
447  << endl;
448 
449  if ( m_variables->getFloating() == true ) {
450  cout << i18n( " No Time Associated with Event" ).local8Bit().data()
451  << endl;
452  }
453 
454  cout << i18n( " Desc: %1" ).
455  arg( m_variables->getDescription() ).local8Bit().data()
456  << endl;
457 
458  cout << i18n( " Location: %1" ).
459  arg( m_variables->getLocation() ).local8Bit().data()
460  << endl;
461 }