File indexing completed on 2024-05-19 05:48:45

0001 /* ========================================================================
0002  *    Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
0003  *
0004  *    This file is part of Zeit.
0005  *
0006  *    Zeit is free software: you can redistribute it and/or modify
0007  *    it under the terms of the GNU General Public License as published by
0008  *    the Free Software Foundation, either version 3 of the License, or
0009  *    (at your option) any later version.
0010  *
0011  *    Zeit is distributed in the hope that it will be useful,
0012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  *    GNU General Public License for more details.
0015  *
0016  *    You should have received a copy of the GNU General Public License
0017  *    along with Zeit.  If not, see <http://www.gnu.org/licenses/>.
0018  * ======================================================================== *
0019  *
0020  *    This file was modified to fit into the project Kcron.
0021  *    The same license terms apply.
0022  *
0023  * ======================================================================== */
0024 
0025 #include <QFile>
0026 
0027 #include "kcm_cron_helper_debug.h"
0028 
0029 #include "kcronhelper.h"
0030 
0031 ActionReply KcronHelper::save(const QVariantMap &args)
0032 {
0033     qCDebug(KCM_CRON_HELPER_LOG) << "running actions";
0034 
0035     QByteArray newCronData;
0036     {
0037         const QString source = args[QLatin1String("source")].toString();
0038         QFile sourceFile(source);
0039         if (!sourceFile.open(QIODevice::ReadOnly)) {
0040             qCWarning(KCM_CRON_HELPER_LOG) << "can't open source file for reading" << source << sourceFile.errorString();
0041             ActionReply reply = ActionReply::HelperErrorReply();
0042             reply.setErrorDescription(sourceFile.errorString());
0043             return reply;
0044         }
0045 
0046         newCronData = sourceFile.readAll();
0047     }
0048 
0049     {
0050         const QString destination = QStringLiteral("/etc/crontab");
0051         QFile destinationFile(destination);
0052         if (!destinationFile.open(QIODevice::WriteOnly)) {
0053             ActionReply reply = ActionReply::HelperErrorReply();
0054             qCWarning(KCM_CRON_HELPER_LOG) << "can't open destination file for writing" << destinationFile.errorString();
0055             reply.setErrorDescription(destinationFile.errorString());
0056             return reply;
0057         }
0058 
0059         if (destinationFile.write(newCronData) < 0) {
0060             ActionReply reply = ActionReply::HelperErrorReply();
0061             qCWarning(KCM_CRON_HELPER_LOG) << "writing to destination file failed" << destinationFile.errorString();
0062             reply.setErrorDescription(destinationFile.errorString());
0063         }
0064     }
0065 
0066     return ActionReply::SuccessReply();
0067 }
0068 
0069 KAUTH_HELPER_MAIN("local.kcron.crontab", KcronHelper)
0070 
0071 #include "moc_kcronhelper.cpp"