src/scripttools/debugging/qscriptdebuggerconsolecommandmanager.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qscriptdebuggerconsolecommandmanager_p.h"
       
    43 #include "qscriptdebuggerconsolecommand_p.h"
       
    44 #include "qscriptdebuggerconsolecommandgroupdata_p.h"
       
    45 
       
    46 #include <QtCore/qlist.h>
       
    47 #include <QtCore/qstringlist.h>
       
    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(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("addCommand(): nameless command ignored");
       
   120         return;
       
   121     }
       
   122     if (command->group().isEmpty()) {
       
   123         qWarning("addCommand(): groupless command '%s' ignored",
       
   124                  qPrintable(command->name()));
       
   125         return;
       
   126     }
       
   127     if (findCommand(command->name()) != 0) {
       
   128         qWarning("addCommand(): duplicate command '%s' (group '%s') ignored",
       
   129                  qPrintable(command->name()), qPrintable(command->group()));
       
   130         return;
       
   131     }
       
   132     if (!d->groups.contains(command->group())) {
       
   133         qWarning("addCommand(): group '%s' for command '%s' is unknown!",
       
   134                  qPrintable(command->group()), qPrintable(command->name()));
       
   135     }
       
   136     d->commands.append(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("addCommandGroup(): nameless group ignored");
       
   148         return;
       
   149     }
       
   150     if (d->groups.contains(name)) {
       
   151         qWarning("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(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(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(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(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(cmd->name());
       
   233 //        names += cmd->aliases();
       
   234         for (int j = 0; j < names.size(); ++j) {
       
   235             const QString &name = names.at(j);
       
   236             if ((name.length() > prefix.length()) && name.startsWith(prefix))
       
   237                 result.append(name);
       
   238         }
       
   239     }
       
   240     qStableSort(result);
       
   241     return result;
       
   242 }
       
   243 
       
   244 QT_END_NAMESPACE