26 #include <QtCore/QIODevice>
27 #include <QtCore/QFileInfo>
28 #include <QtCore/QFile>
31 #include <KStandardDirs>
34 #include <KLocalizedString>
37 using namespace Konsole;
47 class KDE3ColorSchemeReader
54 explicit KDE3ColorSchemeReader(QIODevice* device);
68 bool readColorLine(
const QString& line ,
ColorScheme* scheme);
69 bool readTitleLine(
const QString& line ,
ColorScheme* scheme);
74 KDE3ColorSchemeReader::KDE3ColorSchemeReader(QIODevice* device) :
80 Q_ASSERT(_device->openMode() == QIODevice::ReadOnly ||
81 _device->openMode() == QIODevice::ReadWrite);
85 QRegExp comment(
"#.*$");
86 while (!_device->atEnd()) {
87 QString line(_device->readLine());
89 line = line.simplified();
94 if (line.startsWith(QLatin1String(
"color"))) {
95 if (!readColorLine(line, scheme))
96 kWarning() <<
"Failed to read KDE 3 color scheme line" << line;
97 }
else if (line.startsWith(QLatin1String(
"title"))) {
98 if (!readTitleLine(line, scheme))
99 kWarning() <<
"Failed to read KDE 3 color scheme title line" << line;
101 kWarning() <<
"KDE 3 color scheme contains an unsupported feature, '" <<
108 bool KDE3ColorSchemeReader::readColorLine(
const QString& line,
ColorScheme* scheme)
110 QStringList list = line.split(QChar(
' '));
112 if (list.count() != 7)
114 if (list.first() !=
"color")
117 int index = list[1].toInt();
118 int red = list[2].toInt();
119 int green = list[3].toInt();
120 int blue = list[4].toInt();
121 int transparent = list[5].toInt();
122 int bold = list[6].toInt();
124 const int MAX_COLOR_VALUE = 255;
127 || (red < 0 || red > MAX_COLOR_VALUE)
128 || (blue < 0 || blue > MAX_COLOR_VALUE)
129 || (green < 0 || green > MAX_COLOR_VALUE)
130 || (transparent != 0 && transparent != 1)
131 || (bold != 0 && bold != 1))
135 entry.
color = QColor(red, green, blue);
141 bool KDE3ColorSchemeReader::readTitleLine(
const QString& line,
ColorScheme* scheme)
143 if (!line.startsWith(QLatin1String(
"title")))
146 int spacePos = line.indexOf(
' ');
150 QString description = line.mid(spacePos + 1);
157 : _haveLoadedAll(false)
159 #if defined(Q_WS_X11)
161 QColor::setAllowX11ColorNames(
true);
167 qDeleteAll(_colorSchemes);
174 return theColorSchemeManager;
177 void ColorSchemeManager::loadAllColorSchemes()
182 QStringList nativeColorSchemes = listColorSchemes();
183 foreach(
const QString& colorScheme, nativeColorSchemes) {
184 if (loadColorScheme(colorScheme))
190 QStringList kde3ColorSchemes = listKDE3ColorSchemes();
191 foreach(
const QString& colorScheme, kde3ColorSchemes) {
192 if (loadKDE3ColorScheme(colorScheme))
199 kWarning() <<
"failed to load " << failed <<
" color schemes.";
201 _haveLoadedAll =
true;
206 if (!_haveLoadedAll) {
207 loadAllColorSchemes();
210 return _colorSchemes.values();
213 bool ColorSchemeManager::loadColorScheme(
const QString& filePath)
215 if (!filePath.endsWith(QLatin1String(
".colorscheme")) || !QFile::exists(filePath))
218 QFileInfo info(filePath);
220 KConfig config(filePath , KConfig::NoGlobals);
222 scheme->
setName(info.baseName());
223 scheme->
read(config);
225 if (scheme->
name().isEmpty()) {
226 kWarning() <<
"Color scheme in" << filePath <<
"does not have a valid name and was not loaded.";
231 if (!_colorSchemes.contains(info.baseName())) {
232 _colorSchemes.insert(scheme->
name(), scheme);
234 kDebug() <<
"color scheme with name" << scheme->
name() <<
"has already been" <<
243 bool ColorSchemeManager::loadKDE3ColorScheme(
const QString& filePath)
245 QFile file(filePath);
246 if (!filePath.endsWith(QLatin1String(
".schema")) || !file.open(QIODevice::ReadOnly))
249 KDE3ColorSchemeReader reader(&file);
251 scheme->
setName(QFileInfo(file).baseName());
254 if (scheme->
name().isEmpty()) {
255 kWarning() <<
"color scheme name is not valid.";
260 QFileInfo info(filePath);
262 if (!_colorSchemes.contains(info.baseName())) {
265 kWarning() <<
"color scheme with name" << scheme->
name() <<
"has already been" <<
273 QStringList ColorSchemeManager::listColorSchemes()
275 return KGlobal::dirs()->findAllResources(
"data",
276 "konsole/*.colorscheme",
277 KStandardDirs::NoDuplicates);
280 QStringList ColorSchemeManager::listKDE3ColorSchemes()
282 return KGlobal::dirs()->findAllResources(
"data",
284 KStandardDirs::NoDuplicates);
287 const ColorScheme ColorSchemeManager::_defaultColorScheme;
290 return &_defaultColorScheme;
296 if (_colorSchemes.contains(scheme->
name())) {
297 delete _colorSchemes[scheme->
name()];
298 _colorSchemes.remove(scheme->
name());
301 _colorSchemes.insert(scheme->
name(), scheme);
304 QString path = KGlobal::dirs()->saveLocation(
"data",
"konsole/") + scheme->
name() +
".colorscheme";
305 KConfig config(path , KConfig::NoGlobals);
307 scheme->
write(config);
312 Q_ASSERT(_colorSchemes.contains(name));
315 QString path = findColorSchemePath(name);
316 if (QFile::remove(path)) {
317 delete _colorSchemes[name];
318 _colorSchemes.remove(name);
321 kWarning() <<
"Failed to remove color scheme -" << path;
334 if (name.contains(
"/")) {
335 kWarning() << name <<
" has an invalid character / in the name ... skipping";
339 if (_colorSchemes.contains(name)) {
340 return _colorSchemes[name];
343 QString path = findColorSchemePath(name);
344 if (!path.isEmpty() && loadColorScheme(path)) {
347 if (!path.isEmpty() && loadKDE3ColorScheme(path))
351 kWarning() <<
"Could not find color scheme - " << name;
357 QString ColorSchemeManager::findColorSchemePath(
const QString& name)
const
359 QString path = KStandardDirs::locate(
"data",
"konsole/" + name +
".colorscheme");
364 path = KStandardDirs::locate(
"data",
"konsole/" + name +
".schema");
ColorSchemeManager()
Constructs a new ColorSchemeManager and loads the list of available color schemes.
bool deleteColorScheme(const QString &name)
Deletes a color scheme.
void write(KConfig &config) const
Writes the color scheme to the specified configuration source.
Always draw text in this color with a bold weight.
QList< const ColorScheme * > allColorSchemes()
Returns a list of the all the available color schemes.
Manages the color schemes available for use by terminal displays.
An entry in a terminal display's color palette.
void setColorTableEntry(int index, const ColorEntry &entry)
Sets a single entry within the color palette.
void addColorScheme(ColorScheme *scheme)
Adds a new color scheme to the manager.
void setDescription(const QString &description)
Sets the descriptive name of the color scheme.
~ColorSchemeManager()
Destroys the ColorSchemeManager and saves any modified color schemes to disk.
void setName(const QString &name)
Sets the name of the color scheme.
const ColorScheme * findColorScheme(const QString &name)
Returns the color scheme with the given name or 0 if no scheme with that name exists.
QString name() const
Returns the name of the color scheme.
QColor color
The color value of this entry for display.
Use the current font weight set by the terminal application.
FontWeight fontWeight
Specifies the font weight to use when drawing text with this color.
const ColorScheme * defaultColorScheme() const
Returns the default color scheme for Konsole.
Represents a color scheme for a terminal display.
void read(const KConfig &config)
Reads the color scheme from the specified configuration source.