| 1 | /* |
|---|---|
| 2 | SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #undef QT_NO_CAST_FROM_ASCII |
| 8 | |
| 9 | #include <QFile> |
| 10 | #include <QTextStream> |
| 11 | #include <QThread> |
| 12 | |
| 13 | //! [helper_declaration] |
| 14 | #include <KAuth/ActionReply> |
| 15 | #include <KAuth/HelperSupport> |
| 16 | |
| 17 | using namespace KAuth; |
| 18 | |
| 19 | class MyHelper : public QObject |
| 20 | { |
| 21 | Q_OBJECT |
| 22 | public Q_SLOTS: |
| 23 | ActionReply read(const QVariantMap &args); |
| 24 | ActionReply write(const QVariantMap &args); |
| 25 | ActionReply longaction(const QVariantMap &args); |
| 26 | }; |
| 27 | //! [helper_declaration] |
| 28 | |
| 29 | //! [helper_read_action] |
| 30 | ActionReply MyHelper::read(const QVariantMap &args) |
| 31 | { |
| 32 | ActionReply reply; |
| 33 | QString filename = args["filename"].toString(); |
| 34 | QFile file(filename); |
| 35 | if (!file.open(flags: QIODevice::ReadOnly)) { |
| 36 | reply = ActionReply::HelperErrorReply(); |
| 37 | reply.setErrorDescription(file.errorString()); |
| 38 | return reply; |
| 39 | } |
| 40 | QTextStream stream(&file); |
| 41 | QString contents; |
| 42 | stream >> contents; |
| 43 | reply.addData(key: "contents", value: contents); |
| 44 | return reply; |
| 45 | } |
| 46 | //! [helper_read_action] |
| 47 | |
| 48 | ActionReply MyHelper::write(const QVariantMap &args) |
| 49 | { |
| 50 | Q_UNUSED(args) |
| 51 | return ActionReply::SuccessReply(); |
| 52 | } |
| 53 | |
| 54 | //! [helper_longaction] |
| 55 | ActionReply MyHelper::longaction(const QVariantMap &) |
| 56 | { |
| 57 | for (int i = 1; i <= 100; i++) { |
| 58 | if (HelperSupport::isStopped()) { |
| 59 | break; |
| 60 | } |
| 61 | HelperSupport::progressStep(step: i); |
| 62 | QThread::usleep(250000); |
| 63 | } |
| 64 | return ActionReply::SuccessReply(); |
| 65 | } |
| 66 | //! [helper_longaction] |
| 67 | |
| 68 | //! [helper_main] |
| 69 | KAUTH_HELPER_MAIN("org.kde.kf6auth.example", MyHelper) |
| 70 | //! [helper_main] |
| 71 | |
| 72 | #include "helper.moc" |
| 73 |
