File indexing completed on 2024-05-12 05:12:40

0001 /*
0002     Copyright (C) 2012  Kevin Krammer <krammer@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License along
0015     with this program; if not, write to the Free Software Foundation, Inc.,
0016     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0017 */
0018 
0019 #ifndef COMMANDFACTORY_H
0020 #define COMMANDFACTORY_H
0021 
0022 #include <QObject>
0023 
0024 #include <KLocalizedString>
0025 
0026 class AbstractCommand;
0027 struct CommandData;
0028 
0029 class QString;
0030 
0031 #define DEFINE_COMMAND(commandName, className, shortHelp)           \
0032     class className##Factory                            \
0033     {                                       \
0034     public:                                 \
0035         className##Factory();                           \
0036     };                                      \
0037     static className##Factory sFactory;                     \
0038     static AbstractCommand *className##Creator(QObject *parent)         \
0039     {                                       \
0040         return (new className(parent));                     \
0041     }                                       \
0042     className##Factory::className##Factory()                    \
0043     {                                       \
0044         CommandFactory::registerCommand(QLatin1String(commandName),     \
0045                                         ki18nc("@info:shell", shortHelp),   \
0046                                         &className##Creator);           \
0047     }                                       \
0048     QString className::name() const                     \
0049     {                                       \
0050         return (QLatin1String(commandName));                    \
0051     }
0052 
0053 class CommandFactory
0054 {
0055 public:
0056     explicit CommandFactory(const QStringList *parsedArgs);
0057     ~CommandFactory() = default;
0058 
0059     AbstractCommand *createCommand();
0060 
0061     typedef AbstractCommand *(*creatorFunction)(QObject *parent);
0062     static void registerCommand(const QString &name,
0063                                 const KLocalizedString &shortHelp,
0064                                 CommandFactory::creatorFunction creator);
0065 private:
0066     const QStringList *mParsedArgs;
0067 
0068 private:
0069     void checkAndHandleHelp();
0070     void printHelpAndExit(bool userRequestedHelp);
0071 };
0072 
0073 #endif // COMMANDFACTORY_H