File indexing completed on 2025-02-02 05:23:18
0001 /* 0002 SPDX-FileCopyrightText: 2016 Ivan Čukić <ivan.cukic(at)kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef KACTIVITIES_UTILS_H 0008 #define KACTIVITIES_UTILS_H 0009 0010 QTextStream out(stdout); 0011 0012 class StringListView 0013 { 0014 public: 0015 StringListView(const QStringList &list, int start, int end = -1) 0016 : m_list(list) 0017 , m_start(start) 0018 , m_size((end == -1 ? list.count() : end) - start) 0019 { 0020 } 0021 0022 const QString &operator()(int index) const 0023 { 0024 return m_list[m_start + index]; 0025 } 0026 0027 int count() const 0028 { 0029 return m_size; 0030 } 0031 0032 private: 0033 const QStringList &m_list; 0034 int m_start; 0035 int m_size; 0036 }; 0037 0038 KActivities::Controller *controller = nullptr; 0039 0040 class Flags 0041 { 0042 public: 0043 Flags() 0044 : bare(false) 0045 , color(true) 0046 { 0047 } 0048 0049 bool bare; 0050 bool color; 0051 0052 } flags; 0053 0054 QString toDashes(const QString &command) 0055 { 0056 QString result(command); 0057 0058 for (int i = 0; i < result.size() - 1; ++i) { 0059 if (result[i].isLower() && result[i + 1].isUpper()) { 0060 result[i + 1] = result[i + 1].toLower(); 0061 result.insert(i + 1, QStringLiteral("-")); 0062 } 0063 } 0064 0065 return result; 0066 } 0067 0068 void printActivity(const QString &id) 0069 { 0070 // clang-format off 0071 if (flags.bare) { 0072 out << id << "\n"; 0073 0074 } else { 0075 using namespace KActivities; 0076 Info info(id); 0077 0078 out 0079 << ( 0080 info.id() == controller->currentActivity() ? "[CURRENT] " : 0081 info.state() == Info::Running ? "[RUNNING] " : 0082 info.state() == Info::Stopped ? "[STOPPED] " : 0083 info.state() == Info::Starting ? "[STARTING]" : 0084 info.state() == Info::Stopping ? "[STOPPING]" : 0085 "unknown " 0086 ) 0087 << info.id() 0088 << " " 0089 << info.name() 0090 << " (" 0091 << info.icon() 0092 << ")\n" 0093 ; 0094 0095 if (info.id() == controller->currentActivity() 0096 && info.state() != Info::Running) { 0097 qWarning() 0098 << "Activity is the current one, but its state is" 0099 << ( 0100 info.state() == Info::Running ? "running" : 0101 info.state() == Info::Stopped ? "stopped" : 0102 info.state() == Info::Starting ? "starting" : 0103 info.state() == Info::Stopping ? "stopping" : 0104 "unknown " 0105 ); 0106 } 0107 } 0108 // clang-format on 0109 } 0110 0111 template<typename T> 0112 T awaitFuture(const QFuture<T> &future) 0113 { 0114 while (!future.isFinished()) { 0115 QCoreApplication::processEvents(); 0116 } 0117 0118 return future.result(); 0119 } 0120 0121 void awaitFuture(const QFuture<void> &future) 0122 { 0123 while (!future.isFinished()) { 0124 QCoreApplication::processEvents(); 0125 } 0126 } 0127 0128 void switchToActivity(const QString &id) 0129 { 0130 auto result = awaitFuture(controller->setCurrentActivity(id)); 0131 0132 if (!flags.bare) { 0133 if (result) { 0134 qDebug() << "Current activity is" << id; 0135 } else { 0136 qDebug() << "Failed to change the activity"; 0137 } 0138 } 0139 } 0140 0141 // clang-format off 0142 #define DEFINE_COMMAND(Command, MinArgCount) \ 0143 struct Command##_command { \ 0144 const StringListView &args; \ 0145 Command##_command(const StringListView &args) \ 0146 : args(args) \ 0147 { \ 0148 if (args.count() < MinArgCount + 1) { \ 0149 qFatal("not enough arguments for " #Command); \ 0150 } \ 0151 } \ 0152 \ 0153 int operator()(); \ 0154 }; \ 0155 \ 0156 int Command##_command::operator()() 0157 0158 #endif 0159 // clang-format on