File indexing completed on 2024-05-12 05:21:23

0001 /*
0002   This file is part of KOrganizer.
0003 
0004   SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
0005   SPDX-FileCopyrightText: 2000, 2001, 2003 Cornelius Schumacher <schumacher@kde.org>
0006   SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
0007 
0008   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0009 */
0010 
0011 #include "koapp.h"
0012 #include "actionmanager.h"
0013 #include "calendarview.h"
0014 #include "korganizer-version.h"
0015 #include "korganizer.h"
0016 
0017 #include <KCalendarCore/CalFormat>
0018 
0019 #include "korganizer_debug.h"
0020 #include "korganizer_options.h"
0021 
0022 #include <QCommandLineParser>
0023 
0024 #include <Akonadi/ItemFetchJob>
0025 #include <Akonadi/ItemFetchScope>
0026 
0027 #include <KMessageBox>
0028 
0029 KOrganizerApp::KOrganizerApp(int &argc, char **argv[])
0030     : KontactInterface::PimUniqueApplication(argc, argv)
0031 {
0032     const QString prodId = QStringLiteral("-//K Desktop Environment//NONSGML KOrganizer %1//EN");
0033     KCalendarCore::CalFormat::setApplication(QStringLiteral("KOrganizer"), prodId.arg(QStringLiteral(KORGANIZER_VERSION)));
0034 }
0035 
0036 KOrganizerApp::~KOrganizerApp() = default;
0037 
0038 int KOrganizerApp::activate(const QStringList &args, const QString &workingDir)
0039 {
0040     Q_UNUSED(workingDir)
0041     static bool first = true;
0042     if (isSessionRestored() && first) {
0043         KOrg::MainWindow *korg = ActionManager::findInstance(QUrl());
0044         if (korg) {
0045             korg->view()->updateView();
0046         }
0047         first = false;
0048         return 0;
0049     }
0050     first = false;
0051 
0052     QCommandLineParser parser;
0053     korganizer_options(&parser);
0054     parser.process(args);
0055 
0056     if (parser.isSet(QStringLiteral("view"))) {
0057         processCalendar(QUrl(), false);
0058         const auto url = QUrl{parser.value(QStringLiteral("view"))};
0059         auto fetchJob = new Akonadi::ItemFetchJob(Akonadi::Item::fromUrl(url), this);
0060         fetchJob->fetchScope().fetchFullPayload();
0061         connect(fetchJob, &Akonadi::ItemFetchJob::result, this, [](KJob *job) {
0062             if (job->error()) {
0063                 KMessageBox::detailedError(nullptr, i18n("Failed to retrieve incidence from Akonadi"), job->errorText());
0064                 return;
0065             }
0066             auto fetchJob = static_cast<Akonadi::ItemFetchJob *>(job);
0067             if (fetchJob->count() != 1) {
0068                 KMessageBox::error(nullptr, i18n("Failed to retrieve incidence from Akonadi: requested incidence doesn't exist."));
0069                 return;
0070             }
0071             KOrg::MainWindow *korg = ActionManager::findInstance(QUrl());
0072             korg->actionManager()->view()->showIncidence(fetchJob->items().first());
0073         });
0074         return 0;
0075     }
0076 
0077     // No filenames given => all other args are meaningless, show main Window
0078     if (parser.positionalArguments().isEmpty()) {
0079         processCalendar(QUrl());
0080         return 0;
0081     }
0082 
0083     // If filenames were given as arguments, load them as calendars, one per window.
0084     // Import, merge, or ask => we need the resource calendar window anyway.
0085     processCalendar(QUrl());
0086     KOrg::MainWindow *korg = ActionManager::findInstance(QUrl());
0087     if (!korg) {
0088         qCCritical(KORGANIZER_LOG) << "Unable to find default calendar resources view.";
0089         return -1;
0090     }
0091     // Check for import, merge or ask
0092     if (parser.isSet(QStringLiteral("import"))) {
0093         const auto lst = parser.positionalArguments();
0094         for (const QString &url : lst) {
0095             korg->actionManager()->importURL(QUrl::fromUserInput(url), false);
0096         }
0097     } else if (parser.isSet(QStringLiteral("merge"))) {
0098         const auto lst = parser.positionalArguments();
0099         for (const QString &url : lst) {
0100             korg->actionManager()->importURL(QUrl::fromUserInput(url), true);
0101         }
0102     } else {
0103         const auto lst = parser.positionalArguments();
0104         for (const QString &url : lst) {
0105             korg->actionManager()->importCalendar(QUrl::fromUserInput(url));
0106         }
0107     }
0108 
0109     return 0;
0110 }
0111 
0112 void KOrganizerApp::processCalendar(const QUrl &url, bool show)
0113 {
0114     KOrg::MainWindow *korg = ActionManager::findInstance(url);
0115     if (!korg) {
0116         bool hasDocument = !url.isEmpty();
0117         korg = new KOrganizer();
0118         korg->init(hasDocument);
0119         if (show) {
0120             korg->topLevelWidget()->show();
0121         }
0122 
0123         qCDebug(KORGANIZER_LOG) << "calendar URL" << url.url();
0124 
0125         if (hasDocument) {
0126             korg->openURL(url);
0127         } else {
0128             //      korg->view()->updateView();
0129         }
0130     } else if (show) {
0131         korg->topLevelWidget()->show();
0132     }
0133 }
0134 
0135 #include "moc_koapp.cpp"