File indexing completed on 2024-05-19 05:11:08

0001 /******************************************************************************
0002  * konsolekalendaradd.cpp                                                     *
0003  *                                                                            *
0004  * KonsoleKalendar is a command line interface to KDE calendars               *
0005  * SPDX-FileCopyrightText: 2002-2004 Tuukka Pasanen <illuusio@mailcity.com>   *
0006  * SPDX-FileCopyrightText: 2003-2005 Allen Winter <winter@kde.org>            *
0007  *                                                                            *
0008  * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 *
0009  *                                                                            *
0010  ******************************************************************************/
0011 /**
0012  * @file konsolekalendaradd.cpp
0013  * Provides the KonsoleKalendarAdd class definition.
0014  * @author Tuukka Pasanen
0015  * @author Allen Winter
0016  */
0017 #include "konsolekalendaradd.h"
0018 
0019 #include <CalendarSupport/KCalPrefs>
0020 
0021 #include "konsolekalendar_debug.h"
0022 
0023 #include <KLocalizedString>
0024 
0025 #include <KCalendarCore/Event>
0026 #include <KCalendarCore/FileStorage>
0027 
0028 #include <Akonadi/Collection>
0029 #include <Akonadi/IncidenceChanger>
0030 
0031 #include <QElapsedTimer>
0032 #include <QEventLoop>
0033 #include <QObject>
0034 #include <QTimeZone>
0035 
0036 #include <QStandardPaths>
0037 #include <cstdlib>
0038 #include <iostream>
0039 
0040 using namespace KCalendarCore;
0041 using namespace std;
0042 
0043 KonsoleKalendarAdd::KonsoleKalendarAdd(KonsoleKalendarVariables *vars)
0044 {
0045     m_variables = vars;
0046 }
0047 
0048 KonsoleKalendarAdd::~KonsoleKalendarAdd() = default;
0049 
0050 /**
0051  * Adds event to Calendar
0052  */
0053 
0054 bool KonsoleKalendarAdd::addEvent()
0055 {
0056     bool status = true;
0057 
0058     qCDebug(KONSOLEKALENDAR_LOG) << "konsolekalendaradd.cpp::addEvent()";
0059 
0060     if (m_variables->isDryRun()) {
0061         cout << i18n("Insert Event <Dry Run>:").toLocal8Bit().data() << endl;
0062         printSpecs();
0063     } else {
0064         if (m_variables->isVerbose()) {
0065             cout << i18n("Insert Event <Verbose>:").toLocal8Bit().data() << endl;
0066             printSpecs();
0067         }
0068 
0069         Event::Ptr event = Event::Ptr(new Event());
0070 
0071         const auto timeZone = m_variables->getCalendar()->timeZone();
0072         event->setDtStart(m_variables->getStartDateTime().toTimeZone(timeZone));
0073         event->setDtEnd(m_variables->getEndDateTime().toTimeZone(timeZone));
0074         event->setSummary(m_variables->getSummary());
0075         event->setAllDay(m_variables->getFloating());
0076         event->setDescription(m_variables->getDescription());
0077         event->setLocation(m_variables->getLocation());
0078 
0079         Akonadi::CalendarBase::Ptr calendar = m_variables->getCalendar();
0080         QEventLoop loop;
0081         QObject::connect(calendar.data(), &Akonadi::CalendarBase::createFinished, &loop, &QEventLoop::quit);
0082         QElapsedTimer t;
0083         t.start();
0084         Q_ASSERT(calendar->incidence(event->uid()) == nullptr); // can't exist yet
0085         if (!m_variables->allowGui()) {
0086             Akonadi::IncidenceChanger *changer = calendar->incidenceChanger();
0087             changer->setShowDialogsOnError(false);
0088             Akonadi::Collection collection = m_variables->collectionId() != -1
0089                 ? Akonadi::Collection(m_variables->collectionId())
0090                 : Akonadi::Collection(CalendarSupport::KCalPrefs::instance()->defaultCalendarId());
0091 
0092             if (!collection.isValid()) {
0093                 cout << i18n("Calendar is invalid. Please specify one with --calendar").toLocal8Bit().data() << "\n";
0094             }
0095 
0096             changer->setDefaultCollection(collection);
0097             changer->setDestinationPolicy(Akonadi::IncidenceChanger::DestinationPolicyNeverAsk);
0098         }
0099         calendar->addEvent(event);
0100         loop.exec();
0101         qCDebug(KONSOLEKALENDAR_LOG) << "Creation took " << t.elapsed() << "ms.";
0102         status = calendar->incidence(event->uid()) != nullptr;
0103         if (status) {
0104             cout << i18n("Success: \"%1\" inserted", m_variables->getSummary()).toLocal8Bit().data() << endl;
0105         } else {
0106             cout << i18n("Failure: \"%1\" not inserted", m_variables->getSummary()).toLocal8Bit().data() << endl;
0107             status = false;
0108         }
0109     }
0110 
0111     qCDebug(KONSOLEKALENDAR_LOG) << "konsolekalendaradd.cpp::addEvent() | Done";
0112     return status;
0113 }
0114 
0115 bool KonsoleKalendarAdd::addImportedCalendar()
0116 {
0117     MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc()));
0118     FileStorage instore(cal, m_variables->getImportFile());
0119     if (!instore.load()) {
0120         qCDebug(KONSOLEKALENDAR_LOG) << "konsolekalendaradd.cpp::importCalendar() |"
0121                                      << "Can't import file:" << m_variables->getImportFile();
0122         return false;
0123     }
0124     Akonadi::CalendarBase::Ptr calendar = m_variables->getCalendar();
0125 
0126     if (!m_variables->allowGui()) {
0127         Akonadi::IncidenceChanger *changer = calendar->incidenceChanger();
0128         changer->setShowDialogsOnError(false);
0129         Akonadi::Collection collection = m_variables->collectionId() != -1 ? Akonadi::Collection(m_variables->collectionId())
0130                                                                            : Akonadi::Collection(CalendarSupport::KCalPrefs::instance()->defaultCalendarId());
0131 
0132         if (!collection.isValid()) {
0133             cout << i18n("Calendar is invalid. Please specify one with --calendar").toLocal8Bit().data() << "\n";
0134         }
0135 
0136         changer->setDefaultCollection(collection);
0137         changer->setDestinationPolicy(Akonadi::IncidenceChanger::DestinationPolicyNeverAsk);
0138     }
0139 
0140     QEventLoop loop;
0141     QObject::connect(calendar.data(), &Akonadi::CalendarBase::createFinished, &loop, &QEventLoop::quit);
0142     QElapsedTimer t;
0143     const auto rawEvents = cal->rawEvents();
0144     for (const auto &event : rawEvents) {
0145         if (calendar->incidence(event->uid()) != nullptr) {
0146             if (m_variables->isVerbose()) {
0147                 cout << i18n("Insert Event skipped, because UID \"%1\" is already known. <Verbose>", event->uid()).toLocal8Bit().data() << endl;
0148             } else {
0149                 qCInfo(KONSOLEKALENDAR_LOG) << "Event with UID " << event->uid() << "is already in calendar, skipping import of this Event.";
0150             }
0151             continue;
0152         }
0153         if (m_variables->isVerbose()) {
0154             cout << i18n("Add Event with UID \"%1\". <Verbose>", event->uid()).toLocal8Bit().data() << endl;
0155         }
0156         if (m_variables->isDryRun()) {
0157             continue;
0158         }
0159         t.start();
0160         calendar->addEvent(event);
0161         loop.exec();
0162         qCDebug(KONSOLEKALENDAR_LOG) << "Creation of event took " << t.elapsed() << "ms."
0163                                      << "status: " << (calendar->incidence(event->uid()) != nullptr);
0164         if (calendar->incidence(event->uid()) == nullptr) {
0165             return false;
0166         }
0167     }
0168     qCDebug(KONSOLEKALENDAR_LOG) << "konsolekalendaradd.cpp::importCalendar() |"
0169                                  << "Successfully imported file:" << m_variables->getImportFile();
0170     return true;
0171 }
0172 
0173 void KonsoleKalendarAdd::printSpecs()
0174 {
0175     cout << i18n("  What:  %1", m_variables->getSummary()).toLocal8Bit().data() << endl;
0176 
0177     cout << i18n("  Begin: %1", m_variables->getStartDateTime().toString(Qt::TextDate)).toLocal8Bit().data() << endl;
0178 
0179     cout << i18n("  End:   %1", m_variables->getEndDateTime().toString(Qt::TextDate)).toLocal8Bit().data() << endl;
0180 
0181     if (m_variables->getFloating() == true) {
0182         cout << i18n("  No Time Associated with Event").toLocal8Bit().data() << endl;
0183     }
0184 
0185     cout << i18n("  Desc:  %1", m_variables->getDescription()).toLocal8Bit().data() << endl;
0186 
0187     cout << i18n("  Location:  %1", m_variables->getLocation()).toLocal8Bit().data() << endl;
0188 }