41 #include <ksystemtimezone.h>
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>
52 #include <QtCore/QDateTime>
53 #include <QtCore/QFile>
54 #include <QtCore/QTextStream>
61 using namespace KCalCore;
66 m_variables = variables;
77 kDebug() <<
"konsolecalendar.cpp::importCalendar() | importing now!";
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 );
91 QObject::connect(job, SIGNAL(result(KJob*)), &loop, SLOT(quit()));
95 if (job->error() != 0)
98 Akonadi::Collection::List collections = job->collections();
100 if (collections.isEmpty()) {
101 cout << i18n(
"There are no calendars available.").toLocal8Bit().data() << endl;
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(
"- ");
110 bool readOnly = !( collection.rights() & Akonadi::Collection::CanCreateItem ||
111 collection.rights() & Akonadi::Collection::CanChangeItem ||
112 collection.rights() & Akonadi::Collection::CanDeleteItem );
114 QString readOnlyString = readOnly ? i18n(
"(Read only)") + QLatin1Char(
' ') : QString();
116 cout << colId.toLocal8Bit().data() << readOnlyString.toLocal8Bit().constData() << collection.displayName().toLocal8Bit().data() << endl;
124 bool KonsoleKalendar::createAkonadiResource(
const QString &icalFileName)
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);
130 QObject::connect(job, SIGNAL(result(KJob*)), &loop, SLOT(quit()));
133 return job->error() == 0;
140 const QString filename = m_variables->getCalendarFile();
142 if ( m_variables->isDryRun() ) {
143 cout << i18n(
"Create Calendar <Dry Run>: %1", filename ).toLocal8Bit().data()
146 kDebug() <<
"konsolekalendar.cpp::createCalendar() |"
147 <<
"Creating calendar file: "
148 << filename.toLocal8Bit().data();
150 if ( m_variables->isVerbose() ) {
151 cout << i18n(
"Create Calendar <Verbose>: %1", filename ).toLocal8Bit().data()
155 status = createAkonadiResource(filename);
167 const KDateTime::Spec timeSpec = m_variables->getCalendar()->timeSpec();
168 Akonadi::CalendarBase::Ptr calendar = m_variables->getCalendar();
170 if ( m_variables->isDryRun() ) {
171 cout << i18n(
"View Events <Dry Run>:" ).toLocal8Bit().data()
175 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
176 <<
"open export file";
178 if ( m_variables->isExportFile() ) {
179 f.setFileName( m_variables->getExportFile() );
180 if ( !f.open( QIODevice::WriteOnly ) ) {
182 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
183 <<
"unable to open export file"
184 << m_variables->getExportFile();
187 f.open( stdout, QIODevice::WriteOnly );
191 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
192 <<
"opened successful";
194 if ( m_variables->isVerbose() ) {
195 cout << i18n(
"View Event <Verbose>:" ).toLocal8Bit().data()
200 QTextStream ts( &f );
205 if ( m_variables->getAll() ) {
206 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
207 <<
"view all events sorted list";
209 Event::List sortedList = calendar->events( EventSortStartDate );
210 kDebug() <<
"Found" << sortedList.count() <<
"events";
211 if ( !sortedList.isEmpty() ) {
214 foreach(
const KCalCore::Event::Ptr &event, sortedList ) {
215 status &= printEvent( &ts, event, event->dtStart().date() );
218 }
else if ( m_variables->isUID() ) {
219 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
220 <<
"view events by uid list";
222 event = calendar->event( m_variables->getUID() );
225 status = printEvent ( &ts, event, event->dtStart().date() );
227 }
else if ( m_variables->isNext() ) {
228 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
229 <<
"Show next activity in calendar";
231 QDateTime datetime = m_variables->getStartDateTime();
232 datetime = datetime.addDays( 720 );
235 for ( dt = m_variables->getStartDateTime().date();
236 dt <= datetime.date();
237 dt = dt.addDays( 1 ) ) {
238 Event::List events = calendar->events( dt, timeSpec,
240 SortDirectionAscending );
241 kDebug() <<
"2-Found" << events.count() <<
"events on date" << dt;
243 if ( !events.isEmpty() ) {
244 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
245 <<
"Got the next event";
246 printEvent( &ts, events.first(), dt );
251 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
252 <<
"view raw events within date range list";
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,
260 SortDirectionAscending );
261 kDebug() <<
"3-Found" << events.count() <<
"events on date: " << dt;
262 status = printEventList( &ts, &events, dt );
266 QDate firstdate, lastdate;
267 if ( m_variables->getAll() ) {
268 kDebug() <<
"konsolekalendar.cpp::showInstance() |"
269 <<
"HTML view all events sorted list";
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() ) {
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;
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();
291 KCalUtils::HTMLExportSettings htmlSettings( QLatin1String(
"Konsolekalendar") );
294 htmlSettings.setCreditName( QLatin1String(
"KonsoleKalendar") );
295 htmlSettings.setCreditURL(
296 QLatin1String(
"http://pim.kde.org/components/konsolekalendar.php") );
298 htmlSettings.setExcludePrivate(
true );
299 htmlSettings.setExcludeConfidential(
true );
301 htmlSettings.setEventView(
false );
302 htmlSettings.setMonthView(
false );
304 title = i18n(
"Events:" );
305 htmlSettings.setMonthView(
true );
307 if ( firstdate == lastdate ) {
308 title = i18n(
"Events: %1",
309 firstdate.toString( Qt::TextDate ) );
311 title = i18n(
"Events: %1 - %2",
312 firstdate.toString( Qt::TextDate ),
313 lastdate.toString( Qt::TextDate ) );
315 htmlSettings.setEventView(
true );
317 htmlSettings.setEventTitle( title );
318 htmlSettings.setEventAttendees(
true );
324 htmlSettings.setTodoListTitle( title );
325 htmlSettings.setTodoView(
false );
330 htmlSettings.setDateStart( QDateTime( firstdate ) );
331 htmlSettings.setDateEnd( QDateTime( lastdate ) ) ;
333 KCalUtils::HtmlExport *exp =
new KCalUtils::HtmlExport( calendar.data(), &htmlSettings );
334 status = exp->save( &ts );
343 bool KonsoleKalendar::printEventList( QTextStream *ts,
344 Event::List *eventList, QDate date )
348 kDebug() << eventList->count();
349 if ( !eventList->isEmpty() ) {
350 Event::Ptr singleEvent;
351 Event::List::ConstIterator it;
353 for ( it = eventList->constBegin();
354 it != eventList->constEnd() && status !=
false;
358 status = printEvent( ts, singleEvent, date );
365 bool KonsoleKalendar::printEvent( QTextStream *ts,
const Event::Ptr &event, QDate dt )
372 switch ( m_variables->getExportType() ) {
375 kDebug() <<
"konsolekalendar.cpp::printEvent() |"
381 kDebug() <<
"konsolekalendar.cpp::printEvent() |"
382 <<
"TEXT-SHORT export";
383 if ( dt.daysTo( m_saveDate ) ) {
395 kDebug() <<
"konsolekalendar.cpp::printEvent() |"
406 kDebug() <<
"konsolecalendar.cpp::addEvent() |"
409 kDebug() <<
"konsolecalendar.cpp::addEvent() |"
410 <<
"Adding Event now!";
417 kDebug() <<
"konsolecalendar.cpp::changeEvent() |"
418 <<
"Create Changing";
420 kDebug() <<
"konsolecalendar.cpp::changeEvent() |"
421 <<
"Changing Event now!";
427 kDebug() <<
"konsolecalendar.cpp::deleteEvent() |"
428 <<
"Create Deleting";
430 kDebug() <<
"konsolecalendar.cpp::deleteEvent() |"
431 <<
"Deleting Event now!";
436 const QDateTime &enddate,
const QString &summary )
441 Event::List::ConstIterator it;
445 KDateTime::Spec timeSpec = m_variables->getCalendar()->timeSpec();
446 Event::List eventList( m_variables->getCalendar()->
447 rawEventsForDate( startdate.date(), timeSpec,
449 SortDirectionAscending ) );
450 for ( it = eventList.constBegin(); it != eventList.constEnd(); ++it ) {
452 if ( event->dtEnd().toTimeSpec( timeSpec ).dateTime() == enddate &&
453 event->summary() == summary ) {
461 void KonsoleKalendar::printSpecs()
463 cout << i18n(
" What: %1",
464 m_variables->getSummary() ).toLocal8Bit().data()
467 cout << i18n(
" Begin: %1",
468 m_variables->getStartDateTime().toString( Qt::TextDate ) ).toLocal8Bit().data()
471 cout << i18n(
" End: %1",
472 m_variables->getEndDateTime().toString( Qt::TextDate ) ).toLocal8Bit().data()
475 if ( m_variables->getFloating() == true ) {
476 cout << i18n(
" No Time Associated with Event" ).toLocal8Bit().data()
480 cout << i18n(
" Desc: %1",
481 m_variables->getDescription() ).toLocal8Bit().data()
484 cout << i18n(
" Location: %1",
485 m_variables->getLocation() ).toLocal8Bit().data()
Export HTML for the time span on month boundaries.
bool changeEvent()
Modify the Event.
Class to manage the Event insertion capability.
bool changeEvent()
Change event.
bool printCalendarList()
Prints the available calendars.
Provides the KonsoleKalendarAdd class definition.
Class to manage the Export functionality.
bool exportAsTxtShort(QTextStream *ts, const KCalCore::Event::Ptr &event, const QDate &date, bool sameday)
Export the Event in Short Text Mode.
bool exportAsTxt(QTextStream *ts, const KCalCore::Event::Ptr &event, const QDate &date)
Export the Event in Text Mode.
This class provides all the variables for the program.
Class to manage the Event removal capability.
bool deleteEvent()
Delete the Event.
Provides the KonsoleKalendar class definition.
Provides the KonsoleKalendarChange class definition.
Provides the KonsoleKalendarDelete class definition.
bool addImportedCalendar()
Imports calendar file to current Calendar.
bool addEvent()
Add the Event.
bool addEvent()
Add event to calendar.
Class to manage the Event modification capability.
Provides the KonsoleKalendarExports class definition.
bool showInstance()
Visualize what we need.
Export HTML for the specified time span.
bool importCalendar()
Imports calendar file.
bool exportAsCSV(QTextStream *ts, const KCalCore::Event::Ptr &event, const QDate &date)
Export the Event in Comma-Separated Values (CSV) Mode.
bool isEvent(const QDateTime &startdate, const QDateTime &enddate, const QString &summary)
Detect if event already exists.
~KonsoleKalendar()
Destructor.
bool createCalendar()
Creates calendar file (If it doesn't exists)
KonsoleKalendar(KonsoleKalendarVariables *variables)
Constructs a KonsoleKalendar object from command line arguments.
bool deleteEvent()
Delete event.
Export Comma-Separated Values.