File indexing completed on 2024-12-01 12:30:19

0001 /*
0002     SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #undef QT_NO_CAST_FROM_ASCII
0008 
0009 #include <QFile>
0010 #include <QTextStream>
0011 #include <QThread>
0012 
0013 //! [helper_declaration]
0014 #include <KAuth/ActionReply>
0015 #include <KAuth/HelperSupport>
0016 
0017 using namespace KAuth;
0018 
0019 class MyHelper : public QObject
0020 {
0021     Q_OBJECT
0022 public Q_SLOTS:
0023     ActionReply read(const QVariantMap &args);
0024     ActionReply write(const QVariantMap &args);
0025     ActionReply longaction(const QVariantMap &args);
0026 };
0027 //! [helper_declaration]
0028 
0029 //! [helper_read_action]
0030 ActionReply MyHelper::read(const QVariantMap &args)
0031 {
0032     ActionReply reply;
0033     QString filename = args["filename"].toString();
0034     QFile file(filename);
0035     if (!file.open(QIODevice::ReadOnly)) {
0036         reply = ActionReply::HelperErrorReply();
0037         reply.setErrorDescription(file.errorString());
0038         return reply;
0039     }
0040     QTextStream stream(&file);
0041     QString contents;
0042     stream >> contents;
0043     reply.addData("contents", contents);
0044     return reply;
0045 }
0046 //! [helper_read_action]
0047 
0048 ActionReply MyHelper::write(const QVariantMap &args)
0049 {
0050     Q_UNUSED(args)
0051     return ActionReply::SuccessReply();
0052 }
0053 
0054 //! [helper_longaction]
0055 ActionReply MyHelper::longaction(const QVariantMap &)
0056 {
0057     for (int i = 1; i <= 100; i++) {
0058         if (HelperSupport::isStopped()) {
0059             break;
0060         }
0061         HelperSupport::progressStep(i);
0062         QThread::usleep(250000);
0063     }
0064     return ActionReply::SuccessReply();
0065 }
0066 //! [helper_longaction]
0067 
0068 //! [helper_main]
0069 KAUTH_HELPER_MAIN("org.kde.kf5auth.example", MyHelper)
0070 //! [helper_main]
0071 
0072 #include "helper.moc"