File indexing completed on 2024-05-12 05:46:52

0001 /*
0002  *   Copyright (C) 2016 Ivan Čukić <ivan.cukic(at)kde.org>
0003  *
0004  *   This library is free software; you can redistribute it and/or
0005  *   modify it under the terms of the GNU Lesser General Public
0006  *   License as published by the Free Software Foundation; either
0007  *   version 2.1 of the License, or (at your option) version 3, or any
0008  *   later version accepted by the membership of KDE e.V. (or its
0009  *   successor approved by the membership of KDE e.V.), which shall
0010  *   act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  *   This library is distributed in the hope that it will be useful,
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  *   Lesser General Public License for more details.
0016  *
0017  *   You should have received a copy of the GNU Lesser General Public
0018  *   License along with this library.
0019  *   If not, see <http://www.gnu.org/licenses/>.
0020  */
0021 
0022 #ifndef KACTIVITIES_UTILS_H
0023 #define KACTIVITIES_UTILS_H
0024 
0025 QTextStream out(stdout);
0026 
0027 class StringListView {
0028 public:
0029     StringListView(const QStringList &list, int start, int end = -1)
0030         : m_list(list)
0031         , m_start(start)
0032         , m_size((end == -1 ? list.count() : end) - start)
0033     {
0034 
0035     }
0036 
0037     const QString &operator() (int index) const
0038     {
0039         return m_list[m_start + index];
0040     }
0041 
0042     int count() const
0043     {
0044         return m_size;
0045     }
0046 
0047 private:
0048     const QStringList &m_list;
0049     int m_start;
0050     int m_size;
0051 
0052 };
0053 
0054 KActivities::Controller *controller = nullptr;
0055 
0056 class Flags {
0057 public:
0058     Flags()
0059       : bare(false)
0060       , color(true)
0061     {
0062     }
0063 
0064     bool bare;
0065     bool color;
0066 
0067 } flags;
0068 
0069 QString toDashes(const QString &command)
0070 {
0071     QString result(command);
0072 
0073     for (int i = 0; i < result.size() - 1; ++i) {
0074         if (result[i].isLower() &&
0075             result[i+1].isUpper()) {
0076             result[i+1] = result[i+1].toLower();
0077             result.insert(i+1, QStringLiteral("-"));
0078         }
0079     }
0080 
0081     return result;
0082 }
0083 
0084 void printActivity(const QString &id)
0085 {
0086     if (flags.bare) {
0087         out << id << "\n";
0088 
0089     } else {
0090         using namespace KActivities;
0091         Info info(id);
0092 
0093         out
0094             << (
0095                 info.id() == controller->currentActivity() ? "[CURRENT] " :
0096                 info.state() == Info::Running    ? "[RUNNING] " :
0097                 info.state() == Info::Stopped    ? "[STOPPED] " :
0098                 info.state() == Info::Starting   ? "[STARTING]" :
0099                 info.state() == Info::Stopping   ? "[STOPPING]" :
0100                                                    "unknown   "
0101             )
0102             << info.id()
0103             << " "
0104             << info.name()
0105             << " ("
0106             << info.icon()
0107             << ")\n"
0108              ;
0109 
0110         if (info.id() == controller->currentActivity()
0111             && info.state() != Info::Running) {
0112             qWarning()
0113                  << "Activity is the current one, but its state is"
0114                  << (
0115                     info.state() == Info::Running  ? "running"  :
0116                     info.state() == Info::Stopped  ? "stopped"  :
0117                     info.state() == Info::Starting ? "starting" :
0118                     info.state() == Info::Stopping ? "stopping" :
0119                                                      "unknown   "
0120                  );
0121         }
0122     }
0123 }
0124 
0125 template <typename T>
0126 T awaitFuture(const QFuture<T> &future)
0127 {
0128     while (!future.isFinished()) {
0129         QCoreApplication::processEvents();
0130     }
0131 
0132     return future.result();
0133 }
0134 
0135 void awaitFuture(const QFuture<void> &future)
0136 {
0137     while (!future.isFinished()) {
0138         QCoreApplication::processEvents();
0139     }
0140 }
0141 
0142 void switchToActivity(const QString &id)
0143 {
0144     auto result = awaitFuture(controller->setCurrentActivity(id));
0145 
0146     if (!flags.bare) {
0147         if (result) {
0148             qDebug() << "Current activity is" << id;
0149         } else {
0150             qDebug() << "Failed to change the activity";
0151         }
0152     }
0153 }
0154 
0155 
0156 #define DEFINE_COMMAND(Command, MinArgCount)                                   \
0157     struct Command##_command {                                                 \
0158         const StringListView &args;                                            \
0159         Command##_command(const StringListView &args)                          \
0160             : args(args)                                                       \
0161         {                                                                      \
0162             if (args.count() < MinArgCount + 1) {                              \
0163                 qFatal("not enough arguments for " #Command);                  \
0164             }                                                                  \
0165         }                                                                      \
0166                                                                                \
0167         int operator()();                                                      \
0168     };                                                                         \
0169                                                                                \
0170     int Command##_command::operator()()
0171 
0172 #endif