File indexing completed on 2024-05-05 05:53:45

0001 /*
0002     This source file is part of Konsole, a terminal emulator.
0003 
0004     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0005     SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef COLORSCHEMEMANAGER_H
0011 #define COLORSCHEMEMANAGER_H
0012 
0013 // Qt
0014 #include <QHash>
0015 #include <QStringList>
0016 
0017 // Konsole
0018 #include "ColorScheme.h"
0019 
0020 namespace Konsole
0021 {
0022 /**
0023  * Manages the color schemes available for use by terminal displays.
0024  * See ColorScheme
0025  */
0026 class ColorSchemeManager
0027 {
0028 public:
0029     /**
0030      * Constructs a new ColorSchemeManager and loads the list
0031      * of available color schemes.
0032      *
0033      * The color schemes themselves are not loaded until they are first
0034      * requested via a call to findColorScheme()
0035      */
0036     ColorSchemeManager();
0037 
0038     /**
0039      * Returns the default color scheme for Konsole
0040      */
0041     const std::shared_ptr<const ColorScheme> &defaultColorScheme() const;
0042 
0043     /**
0044      * Returns the color scheme with the given name or 0 if no
0045      * scheme with that name exists.  If @p name is empty, the
0046      * default color scheme is returned.
0047      *
0048      * The first time that a color scheme with a particular name is
0049      * requested, the configuration information is loaded from disk.
0050      */
0051     std::shared_ptr<const ColorScheme> findColorScheme(const QString &name);
0052 
0053     /**
0054      * Adds a new color scheme to the manager.  If @p scheme has the same name as
0055      * an existing color scheme, it replaces the existing scheme.
0056      */
0057     void addColorScheme(const std::shared_ptr<ColorScheme> &scheme);
0058 
0059     /**
0060      * Deletes a color scheme.  Returns true on successful deletion or false otherwise.
0061      */
0062     bool deleteColorScheme(const QString &name);
0063 
0064     /**
0065      * Returns a list of the all the available color schemes.
0066      * This may be slow when first called because all of the color
0067      * scheme resources on disk must be located, read and parsed.
0068      *
0069      * Subsequent calls will be inexpensive.
0070      */
0071     QList<std::shared_ptr<const ColorScheme>> allColorSchemes();
0072 
0073     /** Returns the global color scheme manager instance. */
0074     static ColorSchemeManager *instance();
0075 
0076     /**
0077      * Returns @c true if a colorscheme with @p name exists both under
0078      * the user's home dir location, and a system-wide location
0079      */
0080     bool canResetColorScheme(const QString &name);
0081 
0082     /**
0083      * Returns @c true if a colorscheme with @p name exists under the
0084      * user's home dir location, and hence can be deleted
0085      */
0086     bool isColorSchemeDeletable(const QString &name);
0087 
0088     // unloads a color scheme by it's file path (doesn't delete!)
0089     bool unloadColorScheme(const QString &filePath);
0090 
0091     // loads a color scheme from a KDE 4+ .colorscheme file
0092     std::shared_ptr<const ColorScheme> loadColorScheme(const QString &filePath);
0093 
0094     // @returns the scheme name of a given file or a null string if the file is
0095     //   no theme
0096     static QString colorSchemeNameFromPath(const QString &path);
0097 
0098 private:
0099     // returns a list of paths of color schemes in the KDE 4+ .colorscheme file format
0100     QStringList listColorSchemes();
0101     // finds the path of a color scheme
0102     QString findColorSchemePath(const QString &name) const;
0103     // @returns whether a path is a valid color scheme name
0104     static bool pathIsColorScheme(const QString &path);
0105 
0106     QHash<QString, std::weak_ptr<const ColorScheme>> _colorSchemes;
0107 
0108     Q_DISABLE_COPY(ColorSchemeManager)
0109 };
0110 }
0111 
0112 #endif // COLORSCHEMEMANAGER_H