File indexing completed on 2024-05-12 05:13:34

0001 /*
0002   SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
0003   SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0004 
0005   SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "incidencedefaults.h"
0009 #include "incidencedialog.h"
0010 #include "korganizereditorconfig.h"
0011 
0012 #include <Akonadi/Item>
0013 #include <CalendarSupport/KCalPrefs>
0014 #include <akonadi/calendarsettings.h>
0015 
0016 #include <KCalendarCore/Event>
0017 #include <KCalendarCore/Journal>
0018 #include <KCalendarCore/Todo>
0019 
0020 #include <QApplication>
0021 #include <QCommandLineOption>
0022 #include <QCommandLineParser>
0023 
0024 #include <iostream>
0025 
0026 using namespace IncidenceEditorNG;
0027 
0028 int main(int argc, char **argv)
0029 {
0030     QApplication app(argc, argv);
0031     QCoreApplication::setApplicationName(QStringLiteral("IncidenceEditorNGApp"));
0032     QCoreApplication::setApplicationVersion(QStringLiteral("0.1"));
0033 
0034     QCommandLineParser parser;
0035     parser.addHelpOption();
0036     parser.addVersionOption();
0037     parser.addOption(QCommandLineOption(QStringLiteral("new-event"), QStringLiteral("Creates a new event")));
0038     parser.addOption(QCommandLineOption(QStringLiteral("new-todo"), QStringLiteral("Creates a new todo")));
0039     parser.addOption(QCommandLineOption(QStringLiteral("new-journal"), QStringLiteral("Creates a new journal")));
0040     parser.addOption(QCommandLineOption(QStringLiteral("item"),
0041                                         QStringLiteral("Loads an existing item, or returns without doing anything "
0042                                                        "when the item is not an event or todo."),
0043                                         QStringLiteral("id")));
0044     parser.process(app);
0045 
0046     Akonadi::Item item(-1);
0047 
0048     IncidenceDefaults defaults;
0049     // Set the full emails manually here, to avoid that we get dependencies on
0050     // KCalPrefs all over the place.
0051     defaults.setFullEmails(CalendarSupport::KCalPrefs::instance()->fullEmails());
0052     // NOTE: At some point this should be generalized. That is, we now use the
0053     //       freebusy url as a hack, but this assumes that the user has only one
0054     //       groupware account. Which doesn't have to be the case necessarily.
0055     //       This method should somehow depend on the calendar selected to which
0056     //       the incidence is added.
0057     if (CalendarSupport::KCalPrefs::instance()->useGroupwareCommunication()) {
0058         defaults.setGroupWareDomain(QUrl(Akonadi::CalendarSettings::self()->freeBusyRetrieveUrl()).host());
0059     }
0060 
0061     if (parser.isSet(QStringLiteral("new-event"))) {
0062         std::cout << "Creating new event..." << std::endl;
0063         KCalendarCore::Event::Ptr event(new KCalendarCore::Event);
0064         defaults.setDefaults(event);
0065         item.setPayload<KCalendarCore::Event::Ptr>(event);
0066     } else if (parser.isSet(QStringLiteral("new-todo"))) {
0067         std::cout << "Creating new todo..." << std::endl;
0068         KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo);
0069         defaults.setDefaults(todo);
0070         item.setPayload<KCalendarCore::Todo::Ptr>(todo);
0071     } else if (parser.isSet(QStringLiteral("new-journal"))) {
0072         std::cout << "Creating new journal..." << std::endl;
0073         KCalendarCore::Journal::Ptr journal(new KCalendarCore::Journal);
0074         defaults.setDefaults(journal);
0075         item.setPayload<KCalendarCore::Journal::Ptr>(journal);
0076     } else if (parser.isSet(QStringLiteral("item"))) {
0077         bool ok = false;
0078         qint64 id = parser.value(QStringLiteral("item")).toLongLong(&ok);
0079         if (!ok) {
0080             std::cerr << "Invalid akonadi item id given." << std::endl;
0081             return 1;
0082         }
0083 
0084         item.setId(id);
0085         std::cout << "Trying to load Akonadi Item " << QString::number(id).toLatin1().data();
0086         std::cout << "..." << std::endl;
0087     } else {
0088         std::cerr << "Invalid usage." << std::endl << std::endl;
0089         return 1;
0090     }
0091 
0092     EditorConfig::setEditorConfig(new KOrganizerEditorConfig);
0093 
0094     auto dialog = new IncidenceDialog();
0095 
0096     Akonadi::Collection collection(CalendarSupport::KCalPrefs::instance()->defaultCalendarId());
0097 
0098     if (collection.isValid()) {
0099         dialog->selectCollection(collection);
0100     }
0101 
0102     dialog->load(item); // The dialog will show up once the item is loaded.
0103 
0104     return app.exec();
0105 }