File indexing completed on 2025-02-02 05:23:18

0001 /*
0002     SPDX-FileCopyrightText: 2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QCoreApplication>
0008 #include <QDebug>
0009 #include <QTimer>
0010 
0011 #include <PlasmaActivities/Controller>
0012 
0013 #include "utils.h"
0014 
0015 // Output modifiers
0016 
0017 DEFINE_COMMAND(bare, 0)
0018 {
0019     flags.bare = true;
0020     return 0;
0021 }
0022 
0023 DEFINE_COMMAND(noBare, 0)
0024 {
0025     flags.bare = false;
0026     return 0;
0027 }
0028 
0029 DEFINE_COMMAND(color, 0)
0030 {
0031     flags.color = true;
0032     return 0;
0033 }
0034 
0035 DEFINE_COMMAND(noColor, 0)
0036 {
0037     flags.color = false;
0038     return 0;
0039 }
0040 
0041 // Activity management
0042 
0043 DEFINE_COMMAND(createActivity, 1)
0044 {
0045     auto result = awaitFuture(controller->addActivity(args(1)));
0046 
0047     qDebug().noquote() << result;
0048 
0049     return 1;
0050 }
0051 
0052 DEFINE_COMMAND(removeActivity, 1)
0053 {
0054     awaitFuture(controller->removeActivity(args(1)));
0055 
0056     return 1;
0057 }
0058 
0059 DEFINE_COMMAND(startActivity, 1)
0060 {
0061     awaitFuture(controller->startActivity(args(1)));
0062 
0063     return 1;
0064 }
0065 
0066 DEFINE_COMMAND(stopActivity, 1)
0067 {
0068     awaitFuture(controller->stopActivity(args(1)));
0069 
0070     return 1;
0071 }
0072 
0073 DEFINE_COMMAND(listActivities, 0)
0074 {
0075     for (const auto &activity : controller->activities()) {
0076         printActivity(activity);
0077     }
0078 
0079     return 0;
0080 }
0081 
0082 DEFINE_COMMAND(currentActivity, 0)
0083 {
0084     printActivity(controller->currentActivity());
0085 
0086     return 0;
0087 }
0088 
0089 DEFINE_COMMAND(setActivityProperty, 3)
0090 {
0091     const auto what = args(1);
0092     const auto id = args(2);
0093     const auto value = args(3);
0094 
0095     // clang-format off
0096     awaitFuture(
0097         what == QLatin1String("name")        ? controller->setActivityName(id, value) :
0098         what == QLatin1String("description") ? controller->setActivityDescription(id, value) :
0099         what == QLatin1String("icon")        ? controller->setActivityIcon(id, value) :
0100                                 QFuture<void>()
0101         );
0102     // clang-format on
0103 
0104     return 3;
0105 }
0106 
0107 DEFINE_COMMAND(activityProperty, 2)
0108 {
0109     const auto what = args(1);
0110     const auto id = args(2);
0111 
0112     KActivities::Info info(id);
0113     // clang-format off
0114     out << (
0115         what == QLatin1String("name")        ? info.name() :
0116         what == QLatin1String("description") ? info.description() :
0117         what == QLatin1String("icon")        ? info.icon() :
0118                                 QString()
0119         ) << "\n";
0120     // clang-format on
0121     return 2;
0122 }
0123 
0124 // Activity switching
0125 
0126 DEFINE_COMMAND(setCurrentActivity, 1)
0127 {
0128     switchToActivity(args(1));
0129 
0130     return 1;
0131 }
0132 
0133 DEFINE_COMMAND(nextActivity, 0)
0134 {
0135     controller->nextActivity();
0136     return 0;
0137 }
0138 
0139 DEFINE_COMMAND(previousActivity, 0)
0140 {
0141     controller->previousActivity();
0142     return 0;
0143 }
0144 
0145 void printHelp()
0146 {
0147     if (!flags.bare) {
0148         qDebug() << "\nModifiers (applied only to trailing commands):"
0149                  << "\n    --bare, --no-bare        - show minimal info vs show everything"
0150                  << "\n    --color, --no-color      - make the output pretty"
0151 
0152                  << "\n\nCommands:"
0153                  << "\n    --list-activities        - lists all activities"
0154                  << "\n    --create-activity Name   - creates a new activity with the specified name"
0155                  << "\n    --remove-activity ID     - removes the activity with the specified id"
0156                  << "\n    --start-activity ID      - starts the specified activity"
0157                  << "\n    --stop-activity ID       - stops the specified activity"
0158 
0159                  << "\n    --current-activity       - show the current activity"
0160                  << "\n    --set-current-activity   - sets the current activity"
0161                  << "\n    --next-activity          - switches to the next activity (in list-activities order)"
0162                  << "\n    --previous-activity      - switches to the previous activity (in list-activities order)"
0163 
0164                  << "\n    --activity-property What ID"
0165                  << "\n                             - gets activity name, icon or description"
0166                  << "\n    --set-activity-property What ID Value"
0167                  << "\n                             - changes activity name, icon or description";
0168 
0169     } else {
0170         qDebug() << "\n--bare"
0171                  << "\n--no-bare"
0172                  << "\n--color"
0173                  << "\n--no-color"
0174                  << "\n--list-activities"
0175                  << "\n--create-activity NAME"
0176                  << "\n--remove-activity ID"
0177 
0178                  << "\n--current-activity"
0179                  << "\n--set-current-activity"
0180                  << "\n--next-activity"
0181                  << "\n--previous-activity";
0182     }
0183 }
0184 
0185 int main(int argc, char *argv[])
0186 {
0187     QCoreApplication app(argc, argv);
0188 
0189     QTimer::singleShot(0, &app, [] {
0190         const auto args = QCoreApplication::arguments();
0191 
0192         controller = new KActivities::Controller();
0193 
0194         while (controller->serviceStatus() != KActivities::Controller::Running) {
0195             QCoreApplication::processEvents();
0196         }
0197 
0198 // clang-format off
0199         #define MATCH_COMMAND(Command)                                         \
0200             else if (args[argId] == QLatin1String("--") + toDashes(QStringLiteral(#Command))) \
0201             {                                                                  \
0202                 argId += 1 + Command##_command({ args, argId })();             \
0203             }
0204         // clang-format on
0205         if (args.count() <= 1) {
0206             printHelp();
0207 
0208         } else {
0209             for (int argId = 1; argId < args.count();) {
0210                 if (args[argId] == QLatin1String("--help")) {
0211                     printHelp();
0212                     argId++;
0213                 }
0214 
0215                 MATCH_COMMAND(bare)
0216                 MATCH_COMMAND(noBare)
0217                 MATCH_COMMAND(color)
0218                 MATCH_COMMAND(noColor)
0219 
0220                 MATCH_COMMAND(listActivities)
0221 
0222                 MATCH_COMMAND(currentActivity)
0223                 MATCH_COMMAND(setCurrentActivity)
0224                 MATCH_COMMAND(activityProperty)
0225                 MATCH_COMMAND(setActivityProperty)
0226                 MATCH_COMMAND(nextActivity)
0227                 MATCH_COMMAND(previousActivity)
0228 
0229                 MATCH_COMMAND(createActivity)
0230                 MATCH_COMMAND(removeActivity)
0231                 MATCH_COMMAND(startActivity)
0232                 MATCH_COMMAND(stopActivity)
0233 
0234                 else
0235                 {
0236                     qDebug() << "Skipping unknown argument" << args[argId];
0237                     argId++;
0238                 }
0239             }
0240         }
0241 
0242         delete controller;
0243 
0244         QCoreApplication::quit();
0245     });
0246 
0247     return app.exec();
0248 }