File indexing completed on 2024-04-28 05:50:42
0001 /* 0002 This source file is part of Konsole, a terminal emulator. 0003 0004 SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #ifndef KEYBOARDTRANSLATORREADER_H 0010 #define KEYBOARDTRANSLATORREADER_H 0011 0012 // Konsole 0013 #include "KeyboardTranslator.h" 0014 0015 class QIODevice; 0016 0017 namespace Konsole 0018 { 0019 /** 0020 * Parses the contents of a Keyboard Translator (.keytab) file and 0021 * returns the entries found in it. 0022 * 0023 * Usage example: 0024 * 0025 * @code 0026 * QFile source( "/path/to/keytab" ); 0027 * source.open( QIODevice::ReadOnly ); 0028 * 0029 * KeyboardTranslator* translator = new KeyboardTranslator( "name-of-translator" ); 0030 * 0031 * KeyboardTranslatorReader reader(source); 0032 * while ( reader.hasNextEntry() ) 0033 * translator->addEntry(reader.nextEntry()); 0034 * 0035 * source.close(); 0036 * 0037 * if ( !reader.parseError() ) 0038 * { 0039 * // parsing succeeded, do something with the translator 0040 * } 0041 * else 0042 * { 0043 * // parsing failed 0044 * } 0045 * @endcode 0046 */ 0047 class KeyboardTranslatorReader 0048 { 0049 public: 0050 /** Constructs a new reader which parses the given @p source */ 0051 explicit KeyboardTranslatorReader(QIODevice *source); 0052 ~KeyboardTranslatorReader(); 0053 0054 KeyboardTranslatorReader(const KeyboardTranslatorReader &) = delete; 0055 KeyboardTranslatorReader &operator=(const KeyboardTranslatorReader &) = delete; 0056 0057 /** 0058 * Returns the description text. 0059 * TODO: More documentation 0060 */ 0061 QString description() const; 0062 0063 /** Returns true if there is another entry in the source stream */ 0064 bool hasNextEntry() const; 0065 /** Returns the next entry found in the source stream */ 0066 KeyboardTranslator::Entry nextEntry(); 0067 0068 /** 0069 * Returns true if an error occurred whilst parsing the input or 0070 * false if no error occurred. 0071 */ 0072 bool parseError(); 0073 0074 /** 0075 * Parses a condition and result string for a translator entry 0076 * and produces a keyboard translator entry. 0077 * 0078 * The condition and result strings are in the same format as in 0079 */ 0080 static KeyboardTranslator::Entry createEntry(const QString &condition, const QString &result); 0081 0082 private: 0083 struct Token { 0084 enum Type { 0085 TitleKeyword, 0086 TitleText, 0087 KeyKeyword, 0088 KeySequence, 0089 Command, 0090 OutputText, 0091 }; 0092 Type type; 0093 QString text; 0094 }; 0095 QList<Token> tokenize(const QString &); 0096 void readNext(); 0097 bool decodeSequence(const QString &, 0098 int &keyCode, 0099 Qt::KeyboardModifiers &modifiers, 0100 Qt::KeyboardModifiers &modifierMask, 0101 KeyboardTranslator::States &flags, 0102 KeyboardTranslator::States &flagMask); 0103 0104 static bool parseAsModifier(const QString &item, Qt::KeyboardModifier &modifier); 0105 static bool parseAsStateFlag(const QString &item, KeyboardTranslator::State &flag); 0106 static bool parseAsKeyCode(const QString &item, int &keyCode); 0107 static bool parseAsCommand(const QString &text, KeyboardTranslator::Command &command); 0108 0109 QIODevice *_source; 0110 QString _description; 0111 KeyboardTranslator::Entry _nextEntry; 0112 bool _hasNext; 0113 }; 0114 0115 } 0116 0117 #endif