• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
ColorSchemeManager.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Konsole, a terminal emulator.
3 
4  Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 */
21 
22 // Own
23 #include "ColorSchemeManager.h"
24 
25 // Qt
26 #include <QtCore/QIODevice>
27 #include <QtCore/QFileInfo>
28 #include <QtCore/QFile>
29 
30 // KDE
31 #include <KStandardDirs>
32 #include <KGlobal>
33 #include <KConfig>
34 #include <KLocalizedString>
35 #include <KDebug>
36 
37 using namespace Konsole;
38 
47 class KDE3ColorSchemeReader
48 {
49 public:
54  explicit KDE3ColorSchemeReader(QIODevice* device);
55 
63  ColorScheme* read();
64 
65 private:
66  // reads a line from the file specifying a color palette entry
67  // format is: color [index] [red] [green] [blue] [transparent] [bold]
68  bool readColorLine(const QString& line , ColorScheme* scheme);
69  bool readTitleLine(const QString& line , ColorScheme* scheme);
70 
71  QIODevice* _device;
72 };
73 
74 KDE3ColorSchemeReader::KDE3ColorSchemeReader(QIODevice* device) :
75  _device(device)
76 {
77 }
78 ColorScheme* KDE3ColorSchemeReader::read()
79 {
80  Q_ASSERT(_device->openMode() == QIODevice::ReadOnly ||
81  _device->openMode() == QIODevice::ReadWrite);
82 
83  ColorScheme* scheme = new ColorScheme();
84 
85  QRegExp comment("#.*$");
86  while (!_device->atEnd()) {
87  QString line(_device->readLine());
88  line.remove(comment);
89  line = line.simplified();
90 
91  if (line.isEmpty())
92  continue;
93 
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;
100  } else {
101  kWarning() << "KDE 3 color scheme contains an unsupported feature, '" <<
102  line << "'";
103  }
104  }
105 
106  return scheme;
107 }
108 bool KDE3ColorSchemeReader::readColorLine(const QString& line, ColorScheme* scheme)
109 {
110  QStringList list = line.split(QChar(' '));
111 
112  if (list.count() != 7)
113  return false;
114  if (list.first() != "color")
115  return false;
116 
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();
123 
124  const int MAX_COLOR_VALUE = 255;
125 
126  if ((index < 0 || index >= TABLE_COLORS)
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))
132  return false;
133 
134  ColorEntry entry;
135  entry.color = QColor(red, green, blue);
136  entry.fontWeight = (bold != 0) ? ColorEntry::Bold : ColorEntry::UseCurrentFormat;
137 
138  scheme->setColorTableEntry(index, entry);
139  return true;
140 }
141 bool KDE3ColorSchemeReader::readTitleLine(const QString& line, ColorScheme* scheme)
142 {
143  if (!line.startsWith(QLatin1String("title")))
144  return false;
145 
146  int spacePos = line.indexOf(' ');
147  if (spacePos == -1)
148  return false;
149 
150  QString description = line.mid(spacePos + 1);
151 
152  scheme->setDescription(i18n(description.toUtf8()));
153  return true;
154 }
155 
156 ColorSchemeManager::ColorSchemeManager()
157  : _haveLoadedAll(false)
158 {
159 #if defined(Q_WS_X11)
160  // Allow looking up colors in the X11 color database
161  QColor::setAllowX11ColorNames(true);
162 #endif
163 }
164 
165 ColorSchemeManager::~ColorSchemeManager()
166 {
167  qDeleteAll(_colorSchemes);
168 }
169 
170 K_GLOBAL_STATIC(ColorSchemeManager , theColorSchemeManager)
171 
172 ColorSchemeManager* ColorSchemeManager::instance()
173 {
174  return theColorSchemeManager;
175 }
176 
177 void ColorSchemeManager::loadAllColorSchemes()
178 {
179  int success = 0;
180  int failed = 0;
181 
182  QStringList nativeColorSchemes = listColorSchemes();
183  foreach(const QString& colorScheme, nativeColorSchemes) {
184  if (loadColorScheme(colorScheme))
185  success++;
186  else
187  failed++;
188  }
189 
190  QStringList kde3ColorSchemes = listKDE3ColorSchemes();
191  foreach(const QString& colorScheme, kde3ColorSchemes) {
192  if (loadKDE3ColorScheme(colorScheme))
193  success++;
194  else
195  failed++;
196  }
197 
198  if (failed > 0)
199  kWarning() << "failed to load " << failed << " color schemes.";
200 
201  _haveLoadedAll = true;
202 }
203 
204 QList<const ColorScheme*> ColorSchemeManager::allColorSchemes()
205 {
206  if (!_haveLoadedAll) {
207  loadAllColorSchemes();
208  }
209 
210  return _colorSchemes.values();
211 }
212 
213 bool ColorSchemeManager::loadColorScheme(const QString& filePath)
214 {
215  if (!filePath.endsWith(QLatin1String(".colorscheme")) || !QFile::exists(filePath))
216  return false;
217 
218  QFileInfo info(filePath);
219 
220  KConfig config(filePath , KConfig::NoGlobals);
221  ColorScheme* scheme = new ColorScheme();
222  scheme->setName(info.baseName());
223  scheme->read(config);
224 
225  if (scheme->name().isEmpty()) {
226  kWarning() << "Color scheme in" << filePath << "does not have a valid name and was not loaded.";
227  delete scheme;
228  return false;
229  }
230 
231  if (!_colorSchemes.contains(info.baseName())) {
232  _colorSchemes.insert(scheme->name(), scheme);
233  } else {
234  kDebug() << "color scheme with name" << scheme->name() << "has already been" <<
235  "found, ignoring.";
236 
237  delete scheme;
238  }
239 
240  return true;
241 }
242 
243 bool ColorSchemeManager::loadKDE3ColorScheme(const QString& filePath)
244 {
245  QFile file(filePath);
246  if (!filePath.endsWith(QLatin1String(".schema")) || !file.open(QIODevice::ReadOnly))
247  return false;
248 
249  KDE3ColorSchemeReader reader(&file);
250  ColorScheme* scheme = reader.read();
251  scheme->setName(QFileInfo(file).baseName());
252  file.close();
253 
254  if (scheme->name().isEmpty()) {
255  kWarning() << "color scheme name is not valid.";
256  delete scheme;
257  return false;
258  }
259 
260  QFileInfo info(filePath);
261 
262  if (!_colorSchemes.contains(info.baseName())) {
263  addColorScheme(scheme);
264  } else {
265  kWarning() << "color scheme with name" << scheme->name() << "has already been" <<
266  "found, ignoring.";
267  delete scheme;
268  }
269 
270  return true;
271 }
272 
273 QStringList ColorSchemeManager::listColorSchemes()
274 {
275  return KGlobal::dirs()->findAllResources("data",
276  "konsole/*.colorscheme",
277  KStandardDirs::NoDuplicates);
278 }
279 
280 QStringList ColorSchemeManager::listKDE3ColorSchemes()
281 {
282  return KGlobal::dirs()->findAllResources("data",
283  "konsole/*.schema",
284  KStandardDirs::NoDuplicates);
285 }
286 
287 const ColorScheme ColorSchemeManager::_defaultColorScheme;
288 const ColorScheme* ColorSchemeManager::defaultColorScheme() const
289 {
290  return &_defaultColorScheme;
291 }
292 
293 void ColorSchemeManager::addColorScheme(ColorScheme* scheme)
294 {
295  // remove existing colorscheme with the same name
296  if (_colorSchemes.contains(scheme->name())) {
297  delete _colorSchemes[scheme->name()];
298  _colorSchemes.remove(scheme->name());
299  }
300 
301  _colorSchemes.insert(scheme->name(), scheme);
302 
303  // save changes to disk
304  QString path = KGlobal::dirs()->saveLocation("data", "konsole/") + scheme->name() + ".colorscheme";
305  KConfig config(path , KConfig::NoGlobals);
306 
307  scheme->write(config);
308 }
309 
310 bool ColorSchemeManager::deleteColorScheme(const QString& name)
311 {
312  Q_ASSERT(_colorSchemes.contains(name));
313 
314  // look up the path and delete
315  QString path = findColorSchemePath(name);
316  if (QFile::remove(path)) {
317  delete _colorSchemes[name];
318  _colorSchemes.remove(name);
319  return true;
320  } else {
321  kWarning() << "Failed to remove color scheme -" << path;
322  return false;
323  }
324 }
325 
326 const ColorScheme* ColorSchemeManager::findColorScheme(const QString& name)
327 {
328  if (name.isEmpty())
329  return defaultColorScheme();
330 
331  // A fix to prevent infinite loops if users puts / in ColorScheme name
332  // Konsole will create a sub-folder in that case (bko 315086)
333  // More code will have to go in to prevent the users from doing that.
334  if (name.contains("/")) {
335  kWarning() << name << " has an invalid character / in the name ... skipping";
336  return defaultColorScheme();
337  }
338 
339  if (_colorSchemes.contains(name)) {
340  return _colorSchemes[name];
341  } else {
342  // look for this color scheme
343  QString path = findColorSchemePath(name);
344  if (!path.isEmpty() && loadColorScheme(path)) {
345  return findColorScheme(name);
346  } else {
347  if (!path.isEmpty() && loadKDE3ColorScheme(path))
348  return findColorScheme(name);
349  }
350 
351  kWarning() << "Could not find color scheme - " << name;
352 
353  return 0;
354  }
355 }
356 
357 QString ColorSchemeManager::findColorSchemePath(const QString& name) const
358 {
359  QString path = KStandardDirs::locate("data", "konsole/" + name + ".colorscheme");
360 
361  if (!path.isEmpty())
362  return path;
363 
364  path = KStandardDirs::locate("data", "konsole/" + name + ".schema");
365 
366  return path;
367 }
368 
QIODevice
TABLE_COLORS
#define TABLE_COLORS
Definition: CharacterColor.h:117
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
Konsole::ColorSchemeManager::ColorSchemeManager
ColorSchemeManager()
Constructs a new ColorSchemeManager and loads the list of available color schemes.
Definition: ColorSchemeManager.cpp:156
QFile::remove
bool remove()
QChar
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QString::simplified
QString simplified() const
Konsole::ColorSchemeManager::deleteColorScheme
bool deleteColorScheme(const QString &name)
Deletes a color scheme.
Definition: ColorSchemeManager.cpp:310
QFile::exists
bool exists() const
QString::remove
QString & remove(int position, int n)
QFile
Konsole::ColorScheme::write
void write(KConfig &config) const
Writes the color scheme to the specified configuration source.
Definition: ColorScheme.cpp:322
ColorSchemeManager.h
Konsole::ColorEntry::Bold
Always draw text in this color with a bold weight.
Definition: CharacterColor.h:46
QRegExp
Konsole::ColorSchemeManager::allColorSchemes
QList< const ColorScheme * > allColorSchemes()
Returns a list of the all the available color schemes.
Definition: ColorSchemeManager.cpp:204
Konsole::ColorSchemeManager
Manages the color schemes available for use by terminal displays.
Definition: ColorSchemeManager.h:38
QList::count
int count(const T &value) const
Konsole::ColorEntry
An entry in a terminal display's color palette.
Definition: CharacterColor.h:40
Konsole::ColorScheme::setColorTableEntry
void setColorTableEntry(int index, const ColorEntry &entry)
Sets a single entry within the color palette.
Definition: ColorScheme.cpp:180
QString::isEmpty
bool isEmpty() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Konsole::ColorSchemeManager::addColorScheme
void addColorScheme(ColorScheme *scheme)
Adds a new color scheme to the manager.
Definition: ColorSchemeManager.cpp:293
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QList::first
T & first()
QString
QList
QColor
QStringList
Konsole::ColorScheme::setDescription
void setDescription(const QString &description)
Sets the descriptive name of the color scheme.
Definition: ColorScheme.cpp:162
Konsole::ColorSchemeManager::~ColorSchemeManager
~ColorSchemeManager()
Destroys the ColorSchemeManager and saves any modified color schemes to disk.
Definition: ColorSchemeManager.cpp:165
QFileInfo
Konsole::ColorScheme::setName
void setName(const QString &name)
Sets the name of the color scheme.
Definition: ColorScheme.cpp:171
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QColor::setAllowX11ColorNames
void setAllowX11ColorNames(bool enabled)
Konsole::ColorSchemeManager::findColorScheme
const ColorScheme * findColorScheme(const QString &name)
Returns the color scheme with the given name or 0 if no scheme with that name exists.
Definition: ColorSchemeManager.cpp:326
QString::mid
QString mid(int position, int n) const
QLatin1String
Konsole::ColorScheme::name
QString name() const
Returns the name of the color scheme.
Definition: ColorScheme.cpp:175
Konsole::ColorEntry::color
QColor color
The color value of this entry for display.
Definition: CharacterColor.h:80
Konsole::ColorEntry::UseCurrentFormat
Use the current font weight set by the terminal application.
Definition: CharacterColor.h:53
Konsole::ColorEntry::fontWeight
FontWeight fontWeight
Specifies the font weight to use when drawing text with this color.
Definition: CharacterColor.h:86
Konsole::ColorSchemeManager::defaultColorScheme
const ColorScheme * defaultColorScheme() const
Returns the default color scheme for Konsole.
Definition: ColorSchemeManager.cpp:288
Konsole::ColorScheme
Represents a color scheme for a terminal display.
Definition: ColorScheme.h:72
Konsole::ColorScheme::read
void read(const KConfig &config)
Reads the color scheme from the specified configuration source.
Definition: ColorScheme.cpp:289
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal