File indexing completed on 2024-04-21 09:36:46

0001 /*
0002     KT icons implementation.
0003     --------------------------------------------------------------------
0004     SPDX-FileCopyrightText: 1999 Gary Meyer <gary@meyer.net>
0005     --------------------------------------------------------------------
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "kcmCron.h"
0010 
0011 #include <KAboutData>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <KPluginFactory>
0015 #include <KStandardShortcut>
0016 #include <QVBoxLayout>
0017 
0018 #include "crontabWidget.h"
0019 
0020 #include "ctInitializationError.h"
0021 #include "ctcron.h"
0022 #include "cthost.h"
0023 #include "cttask.h"
0024 #include "kcm_cron_debug.h"
0025 
0026 K_PLUGIN_CLASS_WITH_JSON(KCMCron, "kcm_cron.json")
0027 
0028 KCMCron::KCMCron(QObject *parent)
0029     : KCModule(parent)
0030 {
0031     // Initialize document.
0032     CTInitializationError ctInitializationError;
0033     mCtHost = new CTHost(findCrontabBinary(), ctInitializationError);
0034     if (ctInitializationError.hasErrorMessage()) {
0035         KMessageBox::error(widget(),
0036                            i18n("The following error occurred while initializing KCron:"
0037                                 "\n\n%1\n\nKCron will now exit.\n",
0038                                 ctInitializationError.errorMessage()));
0039     }
0040 
0041     mCrontabWidget = new CrontabWidget(widget(), mCtHost);
0042 
0043     qCDebug(KCM_CRON_LOG) << "Crontab Widget initialized";
0044 
0045     connect(mCrontabWidget->tasksWidget(), &TasksWidget::taskModified, this, &KCModule::setNeedsSave);
0046     connect(mCrontabWidget->variablesWidget(), &VariablesWidget::variableModified, this, &KCModule::setNeedsSave);
0047 
0048     // Initialize view.
0049     auto layout = new QVBoxLayout(widget());
0050 
0051     layout->addWidget(mCrontabWidget);
0052 
0053     init();
0054 }
0055 
0056 QString KCMCron::findCrontabBinary()
0057 {
0058     return QStringLiteral(CRONTAB_BINARY);
0059 }
0060 
0061 KCMCron::~KCMCron()
0062 {
0063     delete mCrontabWidget;
0064     delete mCtHost;
0065 }
0066 
0067 void KCMCron::load()
0068 {
0069     qCDebug(KCM_CRON_LOG) << "Calling load";
0070 
0071     mCtHost->cancel();
0072 }
0073 
0074 void KCMCron::save()
0075 {
0076     qCDebug(KCM_CRON_LOG) << "Saving crontab...";
0077 
0078     CTSaveStatus saveStatus = mCtHost->save(mCrontabWidget);
0079     if (saveStatus.isError()) {
0080         KMessageBox::detailedError(widget(), saveStatus.errorMessage(), saveStatus.detailErrorMessage());
0081     }
0082     qCDebug(KCM_CRON_LOG) << "saved ct host";
0083 }
0084 
0085 void KCMCron::defaults()
0086 {
0087     qCDebug(KCM_CRON_LOG) << "Loading defaults";
0088 
0089     mCtHost->cancel();
0090 }
0091 
0092 bool KCMCron::init()
0093 {
0094     // Display greeting screen.
0095     // If there currently are no scheduled tasks...
0096     int taskCount = 0;
0097     for (CTCron *ctCron : std::as_const(mCtHost->mCrons)) {
0098         taskCount += ctCron->tasks().count();
0099     }
0100 
0101     if (taskCount == 0) {
0102         // TODO Add this as a passive popup/message/something else
0103         KMessageBox::information(widget(),
0104                                  i18n("You can use this application to schedule programs to run in the background.\nTo schedule a new task now, click on "
0105                                       "the Tasks folder and select Edit/New from the menu."),
0106                                  i18n("Welcome to the Task Scheduler"),
0107                                  QStringLiteral("welcome"));
0108     }
0109 
0110     return true;
0111 }
0112 
0113 CTHost *KCMCron::ctHost() const
0114 {
0115     return mCtHost;
0116 }
0117 
0118 #include "kcmCron.moc"
0119 
0120 #include "moc_kcmCron.cpp"