| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtSCriptTools module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qscriptdebuggerconsolecommandmanager_p.h" |
| 41 | #include "qscriptdebuggerconsolecommand_p.h" |
| 42 | #include "qscriptdebuggerconsolecommandgroupdata_p.h" |
| 43 | |
| 44 | #include <QtCore/qlist.h> |
| 45 | #include <QtCore/qstringlist.h> |
| 46 | |
| 47 | #include <algorithm> |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | /*! |
| 52 | \since 4.5 |
| 53 | \class QScriptDebuggerConsoleCommandManager |
| 54 | \internal |
| 55 | |
| 56 | \brief The QScriptDebuggerConsoleCommandManager manages a collection of console commands. |
| 57 | |
| 58 | */ |
| 59 | |
| 60 | class QScriptDebuggerConsoleCommandManagerPrivate |
| 61 | { |
| 62 | Q_DECLARE_PUBLIC(QScriptDebuggerConsoleCommandManager) |
| 63 | public: |
| 64 | QScriptDebuggerConsoleCommandManagerPrivate(); |
| 65 | ~QScriptDebuggerConsoleCommandManagerPrivate(); |
| 66 | |
| 67 | QList<QScriptDebuggerConsoleCommand*> commands; |
| 68 | QMap<QString, QScriptDebuggerConsoleCommandGroupData> groups; |
| 69 | |
| 70 | QScriptDebuggerConsoleCommandManager *q_ptr; |
| 71 | }; |
| 72 | |
| 73 | QScriptDebuggerConsoleCommandManagerPrivate::QScriptDebuggerConsoleCommandManagerPrivate() |
| 74 | { |
| 75 | groups[QLatin1String("breakpoints" )] = |
| 76 | QScriptDebuggerConsoleCommandGroupData(QLatin1String("Making program stop at certain points" ), |
| 77 | QLatin1String("" )); |
| 78 | groups[QLatin1String("files" )] = |
| 79 | QScriptDebuggerConsoleCommandGroupData(QLatin1String("Examining files" ), |
| 80 | QLatin1String("" )); |
| 81 | groups[QLatin1String("stack" )] = |
| 82 | QScriptDebuggerConsoleCommandGroupData(QLatin1String("Examining the stack" ), |
| 83 | QLatin1String("" )); |
| 84 | groups[QLatin1String("running" )] = |
| 85 | QScriptDebuggerConsoleCommandGroupData(QLatin1String("Running the program" ), |
| 86 | QLatin1String("" )); |
| 87 | groups[QLatin1String("status" )] = |
| 88 | QScriptDebuggerConsoleCommandGroupData(QLatin1String("Status inquiries" ), |
| 89 | QLatin1String("" )); |
| 90 | groups[QLatin1String("void" )] = |
| 91 | QScriptDebuggerConsoleCommandGroupData(QLatin1String("No such group" ), |
| 92 | QLatin1String("It's a secret to everyone" )); |
| 93 | } |
| 94 | |
| 95 | QScriptDebuggerConsoleCommandManagerPrivate::~QScriptDebuggerConsoleCommandManagerPrivate() |
| 96 | { |
| 97 | qDeleteAll(c: commands); |
| 98 | } |
| 99 | |
| 100 | QScriptDebuggerConsoleCommandManager::QScriptDebuggerConsoleCommandManager() |
| 101 | : d_ptr(new QScriptDebuggerConsoleCommandManagerPrivate) |
| 102 | { |
| 103 | d_ptr->q_ptr = this; |
| 104 | } |
| 105 | |
| 106 | QScriptDebuggerConsoleCommandManager::~QScriptDebuggerConsoleCommandManager() |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | /*! |
| 111 | Adds the given \a command. |
| 112 | The manager takes ownership of the command. |
| 113 | */ |
| 114 | void QScriptDebuggerConsoleCommandManager::addCommand(QScriptDebuggerConsoleCommand *command) |
| 115 | { |
| 116 | Q_D(QScriptDebuggerConsoleCommandManager); |
| 117 | Q_ASSERT(command != 0); |
| 118 | if (command->name().isEmpty()) { |
| 119 | qWarning(msg: "addCommand(): nameless command ignored" ); |
| 120 | return; |
| 121 | } |
| 122 | if (command->group().isEmpty()) { |
| 123 | qWarning(msg: "addCommand(): groupless command '%s' ignored" , |
| 124 | qPrintable(command->name())); |
| 125 | return; |
| 126 | } |
| 127 | if (findCommand(name: command->name()) != 0) { |
| 128 | qWarning(msg: "addCommand(): duplicate command '%s' (group '%s') ignored" , |
| 129 | qPrintable(command->name()), qPrintable(command->group())); |
| 130 | return; |
| 131 | } |
| 132 | if (!d->groups.contains(akey: command->group())) { |
| 133 | qWarning(msg: "addCommand(): group '%s' for command '%s' is unknown!" , |
| 134 | qPrintable(command->group()), qPrintable(command->name())); |
| 135 | } |
| 136 | d->commands.append(t: command); |
| 137 | } |
| 138 | |
| 139 | /*! |
| 140 | Registers a command group with the given \a name and \a data. |
| 141 | */ |
| 142 | void QScriptDebuggerConsoleCommandManager::addCommandGroup( |
| 143 | const QString &name, const QScriptDebuggerConsoleCommandGroupData &data) |
| 144 | { |
| 145 | Q_D(QScriptDebuggerConsoleCommandManager); |
| 146 | if (name.isEmpty()) { |
| 147 | qWarning(msg: "addCommandGroup(): nameless group ignored" ); |
| 148 | return; |
| 149 | } |
| 150 | if (d->groups.contains(akey: name)) { |
| 151 | qWarning(msg: "addCommandGroup(): group '%s' already defined" , |
| 152 | qPrintable(name)); |
| 153 | return; |
| 154 | } |
| 155 | d->groups[name] = data; |
| 156 | } |
| 157 | |
| 158 | /*! |
| 159 | Returns the command with the given \a name if one exists, otherwise |
| 160 | returns 0. |
| 161 | */ |
| 162 | QScriptDebuggerConsoleCommand *QScriptDebuggerConsoleCommandManager::findCommand(const QString &name) const |
| 163 | { |
| 164 | Q_D(const QScriptDebuggerConsoleCommandManager); |
| 165 | for (int i = 0; i < d->commands.size(); ++i) { |
| 166 | QScriptDebuggerConsoleCommand *cmd = d->commands.at(i); |
| 167 | if (cmd->name() == name) |
| 168 | return cmd; |
| 169 | else if (cmd->aliases().contains(str: name)) |
| 170 | return cmd; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /*! |
| 176 | Returns the commands organized into groups. |
| 177 | */ |
| 178 | QMap<QString, QList<QScriptDebuggerConsoleCommand*> > QScriptDebuggerConsoleCommandManager::commands() const |
| 179 | { |
| 180 | Q_D(const QScriptDebuggerConsoleCommandManager); |
| 181 | QMap<QString, QList<QScriptDebuggerConsoleCommand*> > result; |
| 182 | for (int i = 0; i < d->commands.size(); ++i) { |
| 183 | QScriptDebuggerConsoleCommand *cmd = d->commands.at(i); |
| 184 | result[cmd->group()].append(t: cmd); |
| 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | /*! |
| 190 | Returns commands in the group of the given \a name. |
| 191 | */ |
| 192 | QScriptDebuggerConsoleCommandList QScriptDebuggerConsoleCommandManager::commandsInGroup(const QString &name) const |
| 193 | { |
| 194 | Q_D(const QScriptDebuggerConsoleCommandManager); |
| 195 | QScriptDebuggerConsoleCommandList result; |
| 196 | for (int i = 0; i < d->commands.size(); ++i) { |
| 197 | QScriptDebuggerConsoleCommand *cmd = d->commands.at(i); |
| 198 | if (cmd->group() == name) |
| 199 | result.append(t: cmd); |
| 200 | } |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | /*! |
| 205 | Returns data associated with the group of the given \a name. |
| 206 | */ |
| 207 | QScriptDebuggerConsoleCommandGroupData QScriptDebuggerConsoleCommandManager::commandGroupData(const QString &name) const |
| 208 | { |
| 209 | Q_D(const QScriptDebuggerConsoleCommandManager); |
| 210 | return d->groups.value(akey: name); |
| 211 | } |
| 212 | |
| 213 | /*! |
| 214 | Returns all command groups. |
| 215 | */ |
| 216 | QMap<QString, QScriptDebuggerConsoleCommandGroupData> QScriptDebuggerConsoleCommandManager::commandGroups() const |
| 217 | { |
| 218 | Q_D(const QScriptDebuggerConsoleCommandManager); |
| 219 | return d->groups; |
| 220 | } |
| 221 | |
| 222 | /*! |
| 223 | Returns the possible completions for the given \a prefix. |
| 224 | */ |
| 225 | QStringList QScriptDebuggerConsoleCommandManager::completions(const QString &prefix) const |
| 226 | { |
| 227 | Q_D(const QScriptDebuggerConsoleCommandManager); |
| 228 | QStringList result; |
| 229 | for (int i = 0; i < d->commands.size(); ++i) { |
| 230 | QScriptDebuggerConsoleCommand *cmd = d->commands.at(i); |
| 231 | QStringList names; |
| 232 | names.append(t: cmd->name()); |
| 233 | // names += cmd->aliases(); |
| 234 | for (int j = 0; j < names.size(); ++j) { |
| 235 | const QString &name = names.at(i: j); |
| 236 | if ((name.length() > prefix.length()) && name.startsWith(s: prefix)) |
| 237 | result.append(t: name); |
| 238 | } |
| 239 | } |
| 240 | std::stable_sort(first: result.begin(), last: result.end()); |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | QT_END_NAMESPACE |
| 245 | |