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 KEYBOARDTRANSLATOR_MANAGER_H
0010 #define KEYBOARDTRANSLATOR_MANAGER_H
0011 
0012 // Qt
0013 #include <QHash>
0014 #include <QStringList>
0015 
0016 // Konsole
0017 #include "KeyboardTranslator.h"
0018 
0019 class QIODevice;
0020 
0021 namespace Konsole
0022 {
0023 /**
0024  * Manages the keyboard translations available for use by terminal sessions,
0025  * see KeyboardTranslator.
0026  */
0027 class KeyboardTranslatorManager
0028 {
0029 public:
0030     /**
0031      * Constructs a new KeyboardTranslatorManager and loads the list of
0032      * available keyboard translations.
0033      *
0034      * The keyboard translations themselves are not loaded until they are
0035      * first requested via a call to findTranslator()
0036      */
0037     KeyboardTranslatorManager();
0038     ~KeyboardTranslatorManager();
0039 
0040     KeyboardTranslatorManager(const KeyboardTranslatorManager &) = delete;
0041     KeyboardTranslatorManager &operator=(const KeyboardTranslatorManager &) = delete;
0042 
0043     /**
0044      * Adds a new translator.  If a translator with the same name
0045      * already exists, it will be replaced by the new translator.
0046      *
0047      * TODO: More documentation.
0048      */
0049     void addTranslator(KeyboardTranslator *translator);
0050 
0051     /**
0052      * Deletes a translator.  Returns true on successful deletion or false otherwise.
0053      *
0054      * TODO: More documentation
0055      */
0056     bool deleteTranslator(const QString &name);
0057 
0058     /**
0059      * Checks whether a translator can be deleted or not (by checking if
0060      * the directory containing the .keytab file is writable, because one
0061      * can still delete a file owned by a different user if the directory
0062      * containing it is writable for the current user).
0063      */
0064     bool isTranslatorDeletable(const QString &name) const;
0065 
0066     /**
0067      * Checks whether a translator can be reset to its default values.
0068      * This is only applicable for translators that exist in two different
0069      * locations:
0070      *  - system-wide location which is read-only for the user (typically
0071      *    /usr/share/konsole/ on Linux)
0072      *  - writable user-specific location under the user's home directory
0073      *    (typically ~/.local/share/konsole on Linux)
0074      *
0075      * Resetting here basically means it deletes the translator from the
0076      * location under the user's home directory, then "reloads" it from
0077      * the system-wide location.
0078      */
0079     bool isTranslatorResettable(const QString &name) const;
0080 
0081     /** Returns the default translator for Konsole. */
0082     const KeyboardTranslator *defaultTranslator();
0083 
0084     /**
0085      * Returns the keyboard translator with the given name or 0 if no translator
0086      * with that name exists.
0087      *
0088      * The first time that a translator with a particular name is requested,
0089      * the on-disk .keytab file is loaded and parsed.
0090      */
0091     const KeyboardTranslator *findTranslator(const QString &name);
0092     /**
0093      * Returns a list of the names of available keyboard translators.
0094      *
0095      * The first time this is called, a search for available
0096      * translators is started.
0097      */
0098     const QStringList allTranslators();
0099 
0100     /** Returns the global KeyboardTranslatorManager instance. */
0101     static KeyboardTranslatorManager *instance();
0102 
0103     /** Returns the translator path */
0104     const QString findTranslatorPath(const QString &name) const;
0105 
0106 private:
0107     void findTranslators(); // locate all available translators
0108 
0109     // loads the translator with the given name
0110     KeyboardTranslator *loadTranslator(const QString &name);
0111     KeyboardTranslator *loadTranslator(QIODevice *source, const QString &name);
0112 
0113     bool saveTranslator(const KeyboardTranslator *translator);
0114 
0115     bool _haveLoadedAll;
0116 
0117     const KeyboardTranslator *_fallbackTranslator;
0118     QHash<QString, KeyboardTranslator *> _translators;
0119 };
0120 }
0121 
0122 #endif // KEYBOARDTRANSLATOR_MANAGER_H