• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

KonsoleKalendar

  • sources
  • kde-4.12
  • kdepim
  • console
  • 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  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21  * *
22  * As a special exception, permission is given to link this program *
23  * with any edition of Qt, and distribute the resulting executable, *
24  * without including the source code for Qt in the source distribution. *
25  * *
26  ******************************************************************************/
33 #include "konsolekalendar.h"
34 #include "konsolekalendaradd.h"
35 #include "konsolekalendarchange.h"
36 #include "konsolekalendardelete.h"
37 #include "konsolekalendarexports.h"
38 
39 #include <kdebug.h>
40 #include <klocale.h>
41 #include <ksystemtimezone.h>
42 
43 #include <KCalCore/Event>
44 #include <KCalUtils/HtmlExport>
45 #include <KCalUtils/HTMLExportSettings>
46 #include <Akonadi/AgentManager>
47 #include <Akonadi/AgentInstanceCreateJob>
48 #include <Akonadi/CollectionFetchJob>
49 #include <Akonadi/Collection>
50 #include <Akonadi/CollectionFetchScope>
51 
52 #include <QtCore/QDateTime>
53 #include <QtCore/QFile>
54 #include <QtCore/QTextStream>
55 #include <QEventLoop>
56 
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <iostream>
60 
61 using namespace KCalCore;
62 using namespace std;
63 
64 KonsoleKalendar::KonsoleKalendar( KonsoleKalendarVariables *variables )
65 {
66  m_variables = variables;
67 }
68 
69 KonsoleKalendar::~KonsoleKalendar()
70 {
71 }
72 
73 bool KonsoleKalendar::importCalendar()
74 {
75  KonsoleKalendarAdd add( m_variables );
76 
77  kDebug() << "konsolecalendar.cpp::importCalendar() | importing now!";
78  return add.addImportedCalendar();
79 }
80 
81 bool KonsoleKalendar::printCalendarList()
82 {
83  Akonadi::CollectionFetchJob *job = new Akonadi::CollectionFetchJob(Akonadi::Collection::root(),
84  Akonadi::CollectionFetchJob::Recursive);
85  QStringList mimeTypes = QStringList() << QLatin1String("text/calendar")
86  << KCalCore::Event::eventMimeType()
87  << KCalCore::Todo::todoMimeType()
88  << KCalCore::Journal::journalMimeType();
89  job->fetchScope().setContentMimeTypes( mimeTypes );
90  QEventLoop loop;
91  QObject::connect(job, SIGNAL(result(KJob*)), &loop, SLOT(quit()));
92  job->start();
93  loop.exec();
94 
95  if (job->error() != 0)
96  return false;
97 
98  Akonadi::Collection::List collections = job->collections();
99 
100  if (collections.isEmpty()) {
101  cout << i18n("There are no calendars available.").toLocal8Bit().data() << endl;
102  } else {
103  cout << "--------------------------" << endl;
104  QSet<QString> mimeTypeSet = mimeTypes.toSet();
105  foreach(const Akonadi::Collection &collection, collections) {
106  if (!mimeTypeSet.intersect(collection.contentMimeTypes().toSet()).isEmpty()) {
107  QString colId = QString::number(collection.id()).leftJustified(6, QLatin1Char(' '));
108  colId += QLatin1String("- ");
109 
110  bool readOnly = !( collection.rights() & Akonadi::Collection::CanCreateItem ||
111  collection.rights() & Akonadi::Collection::CanChangeItem ||
112  collection.rights() & Akonadi::Collection::CanDeleteItem );
113 
114  QString readOnlyString = readOnly ? i18n("(Read only)") + QLatin1Char(' ') : QString();
115 
116  cout << colId.toLocal8Bit().data() << readOnlyString.toLocal8Bit().constData() << collection.displayName().toLocal8Bit().data() << endl;
117  }
118  }
119  }
120 
121  return true;
122 }
123 
124 bool KonsoleKalendar::createAkonadiResource(const QString &icalFileName)
125 {
126  Akonadi::AgentType type = Akonadi::AgentManager::self()->type(QLatin1String("akonadi_ical_resource"));
127  Akonadi::AgentInstanceCreateJob *job = new Akonadi::AgentInstanceCreateJob(type);
128  job->setProperty("path", icalFileName);
129  QEventLoop loop;
130  QObject::connect(job, SIGNAL(result(KJob*)), &loop, SLOT(quit()));
131  job->start();
132  loop.exec();
133  return job->error() == 0;
134 }
135 
136 bool KonsoleKalendar::createCalendar()
137 {
138  bool status = false;
139 
140  const QString filename = m_variables->getCalendarFile();
141 
142  if ( m_variables->isDryRun() ) {
143  cout << i18n( "Create Calendar &lt;Dry Run&gt;: %1", filename ).toLocal8Bit().data()
144  << endl;
145  } else {
146  kDebug() << "konsolekalendar.cpp::createCalendar() |"
147  << "Creating calendar file: "
148  << filename.toLocal8Bit().data();
149 
150  if ( m_variables->isVerbose() ) {
151  cout << i18n( "Create Calendar &lt;Verbose&gt;: %1", filename ).toLocal8Bit().data()
152  << endl;
153  }
154 
155  status = createAkonadiResource(filename);
156  }
157 
158  return status;
159 }
160 
161 bool KonsoleKalendar::showInstance()
162 {
163  bool status = true;
164  QFile f;
165  QString title;
166  Event::Ptr event;
167  const KDateTime::Spec timeSpec = m_variables->getCalendar()->timeSpec();
168  Akonadi::CalendarBase::Ptr calendar = m_variables->getCalendar();
169 
170  if ( m_variables->isDryRun() ) {
171  cout << i18n( "View Events &lt;Dry Run&gt;:" ).toLocal8Bit().data()
172  << endl;
173  printSpecs();
174  } else {
175  kDebug() << "konsolekalendar.cpp::showInstance() |"
176  << "open export file";
177 
178  if ( m_variables->isExportFile() ) {
179  f.setFileName( m_variables->getExportFile() );
180  if ( !f.open( QIODevice::WriteOnly ) ) {
181  status = false;
182  kDebug() << "konsolekalendar.cpp::showInstance() |"
183  << "unable to open export file"
184  << m_variables->getExportFile();
185  }
186  } else {
187  f.open( stdout, QIODevice::WriteOnly );
188  }
189 
190  if ( status ) {
191  kDebug() << "konsolekalendar.cpp::showInstance() |"
192  << "opened successful";
193 
194  if ( m_variables->isVerbose() ) {
195  cout << i18n( "View Event &lt;Verbose&gt;:" ).toLocal8Bit().data()
196  << endl;
197  printSpecs();
198  }
199 
200  QTextStream ts( &f );
201 
202  if ( m_variables->getExportType() != ExportTypeHTML &&
203  m_variables->getExportType() != ExportTypeMonthHTML ) {
204 
205  if ( m_variables->getAll() ) {
206  kDebug() << "konsolekalendar.cpp::showInstance() |"
207  << "view all events sorted list";
208 
209  Event::List sortedList = calendar->events( EventSortStartDate );
210  kDebug() << "Found" << sortedList.count() << "events";
211  if ( !sortedList.isEmpty() ) {
212  // The code that was here before the akonadi port was really slow with 200 events
213  // this is much faster:
214  foreach( const KCalCore::Event::Ptr &event, sortedList ) {
215  status &= printEvent( &ts, event, event->dtStart().date() );
216  }
217  }
218  } else if ( m_variables->isUID() ) {
219  kDebug() << "konsolekalendar.cpp::showInstance() |"
220  << "view events by uid list";
221  //TODO: support a list of UIDs
222  event = calendar->event( m_variables->getUID() );
223  //If this UID represents a recurring Event,
224  //only the first day of the Event will be printed
225  status = printEvent ( &ts, event, event->dtStart().date() );
226 
227  } else if ( m_variables->isNext() ) {
228  kDebug() << "konsolekalendar.cpp::showInstance() |"
229  << "Show next activity in calendar";
230 
231  QDateTime datetime = m_variables->getStartDateTime();
232  datetime = datetime.addDays( 720 );
233 
234  QDate dt;
235  for ( dt = m_variables->getStartDateTime().date();
236  dt <= datetime.date();
237  dt = dt.addDays( 1 ) ) {
238  Event::List events = calendar->events( dt, timeSpec,
239  EventSortStartDate,
240  SortDirectionAscending );
241  kDebug() << "2-Found" << events.count() << "events on date" << dt;
242  // finished here when we get the next event
243  if ( !events.isEmpty() ) {
244  kDebug() << "konsolekalendar.cpp::showInstance() |"
245  << "Got the next event";
246  printEvent( &ts, events.first(), dt );
247  return true;
248  }
249  }
250  } else {
251  kDebug() << "konsolekalendar.cpp::showInstance() |"
252  << "view raw events within date range list";
253 
254  QDate dt;
255  for ( dt = m_variables->getStartDateTime().date();
256  dt <= m_variables->getEndDateTime().date() && status != false;
257  dt = dt.addDays( 1 ) ) {
258  Event::List events = calendar->events( dt, timeSpec,
259  EventSortStartDate,
260  SortDirectionAscending );
261  kDebug() << "3-Found" << events.count() << "events on date: " << dt;
262  status = printEventList( &ts, &events, dt );
263  }
264  }
265  } else {
266  QDate firstdate, lastdate;
267  if ( m_variables->getAll() ) {
268  kDebug() << "konsolekalendar.cpp::showInstance() |"
269  << "HTML view all events sorted list";
270  // sort the events for this date by start date
271  // in order to determine the date range.
272  Event::List *events =
273  new Event::List ( calendar->rawEvents( EventSortStartDate,
274  SortDirectionAscending ) );
275  firstdate = events->first()->dtStart().date();
276  lastdate = events->last()->dtStart().date();
277  } else if ( m_variables->isUID() ) {
278  // TODO
279  kDebug() << "konsolekalendar.cpp::showInstance() |"
280  << "HTML view events by uid list";
281  cout << i18n( "Sorry, export to HTML by UID is not supported yet" ).
282  toLocal8Bit().data() << endl;
283  return false;
284  } else {
285  kDebug() << "konsolekalendar.cpp::showInstance() |"
286  << "HTML view raw events within date range list";
287  firstdate = m_variables->getStartDateTime().date();
288  lastdate = m_variables->getEndDateTime().date();
289  }
290 
291  KCalUtils::HTMLExportSettings htmlSettings( QLatin1String("Konsolekalendar") );
292 
293  //TODO: get progname and url from the values set in main
294  htmlSettings.setCreditName( QLatin1String("KonsoleKalendar") );
295  htmlSettings.setCreditURL(
296  QLatin1String("http://pim.kde.org/components/konsolekalendar.php") );
297 
298  htmlSettings.setExcludePrivate( true );
299  htmlSettings.setExcludeConfidential( true );
300 
301  htmlSettings.setEventView( false );
302  htmlSettings.setMonthView( false );
303  if ( m_variables->getExportType() == ExportTypeMonthHTML ) {
304  title = i18n( "Events:" );
305  htmlSettings.setMonthView( true );
306  } else {
307  if ( firstdate == lastdate ) {
308  title = i18n( "Events: %1",
309  firstdate.toString( Qt::TextDate ) );
310  } else {
311  title = i18n( "Events: %1 - %2",
312  firstdate.toString( Qt::TextDate ),
313  lastdate.toString( Qt::TextDate ) );
314  }
315  htmlSettings.setEventView( true );
316  }
317  htmlSettings.setEventTitle( title );
318  htmlSettings.setEventAttendees( true );
319 // Not supporting Todos yet
320 // title = "To-Do List for " + firstdate.toString(Qt::TextDate);
321 // if ( firstdate != lastdate ) {
322 // title += " - " + lastdate.toString(Qt::TextDate);
323 // }
324  htmlSettings.setTodoListTitle( title );
325  htmlSettings.setTodoView( false );
326 // htmlSettings.setTaskCategories( false );
327 // htmlSettings.setTaskAttendees( false );
328 // htmlSettings.setTaskDueDate( true );
329 
330  htmlSettings.setDateStart( QDateTime( firstdate ) );
331  htmlSettings.setDateEnd( QDateTime( lastdate ) ) ;
332 
333  KCalUtils::HtmlExport *exp = new KCalUtils::HtmlExport( calendar.data(), &htmlSettings );
334  status = exp->save( &ts );
335  delete exp;
336  }
337  f.close();
338  }
339  }
340  return status;
341 }
342 
343 bool KonsoleKalendar::printEventList( QTextStream *ts,
344  Event::List *eventList, QDate date )
345 {
346  bool status = true;
347 
348  kDebug() << eventList->count();
349  if ( !eventList->isEmpty() ) {
350  Event::Ptr singleEvent;
351  Event::List::ConstIterator it;
352 
353  for ( it = eventList->constBegin();
354  it != eventList->constEnd() && status != false;
355  ++it ) {
356  singleEvent = *it;
357 
358  status = printEvent( ts, singleEvent, date );
359  }
360  }
361 
362  return status;
363 }
364 
365 bool KonsoleKalendar::printEvent( QTextStream *ts, const Event::Ptr &event, QDate dt )
366 {
367  bool status = false;
368  bool sameDay = true;
369  KonsoleKalendarExports exports;
370 
371  if ( event ) {
372  switch ( m_variables->getExportType() ) {
373 
374  case ExportTypeCSV:
375  kDebug() << "konsolekalendar.cpp::printEvent() |"
376  << "CSV export";
377  status = exports.exportAsCSV( ts, event, dt );
378  break;
379 
380  case ExportTypeTextShort:
381  kDebug() << "konsolekalendar.cpp::printEvent() |"
382  << "TEXT-SHORT export";
383  if ( dt.daysTo( m_saveDate ) ) {
384  sameDay = false;
385  m_saveDate = dt;
386  }
387  status = exports.exportAsTxtShort( ts, event, dt, sameDay );
388  break;
389 
390  case ExportTypeHTML:
391  // this is handled separately for now
392  break;
393 
394  default:// Default export-type is ExportTypeText
395  kDebug() << "konsolekalendar.cpp::printEvent() |"
396  << "TEXT export";
397  status = exports.exportAsTxt( ts, event, dt );
398  break;
399  }
400  }
401  return status;
402 }
403 
404 bool KonsoleKalendar::addEvent()
405 {
406  kDebug() << "konsolecalendar.cpp::addEvent() |"
407  << "Create Adding";
408  KonsoleKalendarAdd add( m_variables );
409  kDebug() << "konsolecalendar.cpp::addEvent() |"
410  << "Adding Event now!";
411  return add.addEvent();
412 }
413 
414 bool KonsoleKalendar::changeEvent()
415 {
416 
417  kDebug() << "konsolecalendar.cpp::changeEvent() |"
418  << "Create Changing";
419  KonsoleKalendarChange change( m_variables );
420  kDebug() << "konsolecalendar.cpp::changeEvent() |"
421  << "Changing Event now!";
422  return change.changeEvent();
423 }
424 
425 bool KonsoleKalendar::deleteEvent()
426 {
427  kDebug() << "konsolecalendar.cpp::deleteEvent() |"
428  << "Create Deleting";
429  KonsoleKalendarDelete del( m_variables );
430  kDebug() << "konsolecalendar.cpp::deleteEvent() |"
431  << "Deleting Event now!";
432  return del.deleteEvent();
433 }
434 
435 bool KonsoleKalendar::isEvent( const QDateTime &startdate,
436  const QDateTime &enddate, const QString &summary )
437 {
438  // Search for an event with specified start and end datetime stamp and summary
439 
440  Event::Ptr event;
441  Event::List::ConstIterator it;
442 
443  bool found = false;
444 
445  KDateTime::Spec timeSpec = m_variables->getCalendar()->timeSpec();
446  Event::List eventList( m_variables->getCalendar()->
447  rawEventsForDate( startdate.date(), timeSpec,
448  EventSortStartDate,
449  SortDirectionAscending ) );
450  for ( it = eventList.constBegin(); it != eventList.constEnd(); ++it ) {
451  event = *it;
452  if ( event->dtEnd().toTimeSpec( timeSpec ).dateTime() == enddate &&
453  event->summary() == summary ) {
454  found = true;
455  break;
456  }
457  }
458  return found;
459 }
460 
461 void KonsoleKalendar::printSpecs()
462 {
463  cout << i18n( " What: %1",
464  m_variables->getSummary() ).toLocal8Bit().data()
465  << endl;
466 
467  cout << i18n( " Begin: %1",
468  m_variables->getStartDateTime().toString( Qt::TextDate ) ).toLocal8Bit().data()
469  << endl;
470 
471  cout << i18n( " End: %1",
472  m_variables->getEndDateTime().toString( Qt::TextDate ) ).toLocal8Bit().data()
473  << endl;
474 
475  if ( m_variables->getFloating() == true ) {
476  cout << i18n( " No Time Associated with Event" ).toLocal8Bit().data()
477  << endl;
478  }
479 
480  cout << i18n( " Desc: %1",
481  m_variables->getDescription() ).toLocal8Bit().data()
482  << endl;
483 
484  cout << i18n( " Location: %1",
485  m_variables->getLocation() ).toLocal8Bit().data()
486  << endl;
487 }
ExportTypeMonthHTML
Export HTML for the time span on month boundaries.
Definition: konsolekalendarvariables.h:56
KonsoleKalendarChange::changeEvent
bool changeEvent()
Modify the Event.
Definition: konsolekalendarchange.cpp:55
KonsoleKalendarAdd
Class to manage the Event insertion capability.
Definition: konsolekalendaradd.h:44
KonsoleKalendar::changeEvent
bool changeEvent()
Change event.
Definition: konsolekalendar.cpp:414
KonsoleKalendar::printCalendarList
bool printCalendarList()
Prints the available calendars.
Definition: konsolekalendar.cpp:81
konsolekalendaradd.h
Provides the KonsoleKalendarAdd class definition.
ExportTypeTextShort
Export as compact text.
Definition: konsolekalendarvariables.h:52
KonsoleKalendarExports
Class to manage the Export functionality.
Definition: konsolekalendarexports.h:46
KonsoleKalendarExports::exportAsTxtShort
bool exportAsTxtShort(QTextStream *ts, const KCalCore::Event::Ptr &event, const QDate &date, bool sameday)
Export the Event in Short Text Mode.
Definition: konsolekalendarexports.cpp:146
KonsoleKalendarExports::exportAsTxt
bool exportAsTxt(QTextStream *ts, const KCalCore::Event::Ptr &event, const QDate &date)
Export the Event in Text Mode.
Definition: konsolekalendarexports.cpp:60
KonsoleKalendarVariables
This class provides all the variables for the program.
Definition: konsolekalendarvariables.h:73
KonsoleKalendarDelete
Class to manage the Event removal capability.
Definition: konsolekalendardelete.h:46
KonsoleKalendarDelete::deleteEvent
bool deleteEvent()
Delete the Event.
Definition: konsolekalendardelete.cpp:54
konsolekalendar.h
Provides the KonsoleKalendar class definition.
konsolekalendarchange.h
Provides the KonsoleKalendarChange class definition.
konsolekalendardelete.h
Provides the KonsoleKalendarDelete class definition.
KonsoleKalendarAdd::addImportedCalendar
bool addImportedCalendar()
Imports calendar file to current Calendar.
Definition: konsolekalendaradd.cpp:137
KonsoleKalendarAdd::addEvent
bool addEvent()
Add the Event.
Definition: konsolekalendaradd.cpp:69
KonsoleKalendar::addEvent
bool addEvent()
Add event to calendar.
Definition: konsolekalendar.cpp:404
KonsoleKalendarChange
Class to manage the Event modification capability.
Definition: konsolekalendarchange.h:46
konsolekalendarexports.h
Provides the KonsoleKalendarExports class definition.
KonsoleKalendar::showInstance
bool showInstance()
Visualize what we need.
Definition: konsolekalendar.cpp:161
ExportTypeHTML
Export HTML for the specified time span.
Definition: konsolekalendarvariables.h:54
KonsoleKalendar::importCalendar
bool importCalendar()
Imports calendar file.
Definition: konsolekalendar.cpp:73
KonsoleKalendarExports::exportAsCSV
bool exportAsCSV(QTextStream *ts, const KCalCore::Event::Ptr &event, const QDate &date)
Export the Event in Comma-Separated Values (CSV) Mode.
Definition: konsolekalendarexports.cpp:219
KonsoleKalendar::isEvent
bool isEvent(const QDateTime &startdate, const QDateTime &enddate, const QString &summary)
Detect if event already exists.
Definition: konsolekalendar.cpp:435
KonsoleKalendar::~KonsoleKalendar
~KonsoleKalendar()
Destructor.
Definition: konsolekalendar.cpp:69
KonsoleKalendar::createCalendar
bool createCalendar()
Creates calendar file (If it doesn't exists)
Definition: konsolekalendar.cpp:136
KonsoleKalendar::KonsoleKalendar
KonsoleKalendar(KonsoleKalendarVariables *variables)
Constructs a KonsoleKalendar object from command line arguments.
Definition: konsolekalendar.cpp:64
KonsoleKalendar::deleteEvent
bool deleteEvent()
Delete event.
Definition: konsolekalendar.cpp:425
ExportTypeCSV
Export Comma-Separated Values.
Definition: konsolekalendarvariables.h:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:04 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KonsoleKalendar

Skip menu "KonsoleKalendar"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal