Commit 16a470ab authored by Jekyll Wu's avatar Jekyll Wu
Browse files

Split class KeyboardTranslatorManager into its own files

parent 25b8b535
......@@ -79,6 +79,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/tests/CTestCustom.cmake)
IncrementalSearchBar.cpp
KeyBindingEditor.cpp
KeyboardTranslator.cpp
KeyboardTranslatorManager.cpp
ManageProfilesDialog.cpp
ProcessInfo.cpp
Profile.cpp
......
......@@ -51,6 +51,7 @@
#include "ui_EditProfileDialog.h"
#include "KeyBindingEditor.h"
#include "KeyboardTranslator.h"
#include "KeyboardTranslatorManager.h"
#include "SessionManager.h"
#include "ShellCommand.h"
#include "TabTitleFormatAction.h"
......
......@@ -27,6 +27,7 @@
// Konsole
#include "KeyboardTranslator.h"
#include "KeyboardTranslatorManager.h"
#include "Screen.h"
#include "ScreenWindow.h"
......
......@@ -28,143 +28,15 @@
// Qt
#include <QtCore/QBuffer>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QTextStream>
#include <QtGui/QKeySequence>
// KDE
#include <KDebug>
#include <KLocalizedString>
#include <KStandardDirs>
using namespace Konsole;
const QByteArray KeyboardTranslatorManager::defaultTranslatorText(
"keyboard \"Fallback Key Translator\"\n"
"key Tab : \"\\t\""
);
KeyboardTranslatorManager::KeyboardTranslatorManager()
: _haveLoadedAll(false)
{
}
KeyboardTranslatorManager::~KeyboardTranslatorManager()
{
qDeleteAll(_translators);
}
QString KeyboardTranslatorManager::findTranslatorPath(const QString& name)
{
return KStandardDirs::locate("data", "konsole/" + name + ".keytab");
}
void KeyboardTranslatorManager::findTranslators()
{
QStringList list = KGlobal::dirs()->findAllResources("data",
"konsole/*.keytab",
KStandardDirs::NoDuplicates);
// add the name of each translator to the list and associated
// the name with a null pointer to indicate that the translator
// has not yet been loaded from disk
foreach ( const QString& translatorPath, list) {
QString name = QFileInfo(translatorPath).baseName();
if (!_translators.contains(name))
_translators.insert(name, 0);
}
_haveLoadedAll = true;
}
const KeyboardTranslator* KeyboardTranslatorManager::findTranslator(const QString& name)
{
if (name.isEmpty())
return defaultTranslator();
if (_translators.contains(name) && _translators[name] != 0)
return _translators[name];
KeyboardTranslator* translator = loadTranslator(name);
if (translator != 0)
_translators[name] = translator;
else if (!name.isEmpty())
kWarning() << "Unable to load translator" << name;
return translator;
}
bool KeyboardTranslatorManager::saveTranslator(const KeyboardTranslator* translator)
{
const QString path = KGlobal::dirs()->saveLocation("data", "konsole/") + translator->name()
+ ".keytab";
//kDebug() << "Saving translator to" << path;
QFile destination(path);
if (!destination.open(QIODevice::WriteOnly | QIODevice::Text)) {
kWarning() << "Unable to save keyboard translation:"
<< destination.errorString();
return false;
}
{
KeyboardTranslatorWriter writer(&destination);
writer.writeHeader(translator->description());
foreach ( const KeyboardTranslator::Entry& entry, translator->entries() )
writer.writeEntry(entry);
}
destination.close();
return true;
}
KeyboardTranslator* KeyboardTranslatorManager::loadTranslator(const QString& name)
{
const QString& path = findTranslatorPath(name);
QFile source(path);
if (name.isEmpty() || !source.open(QIODevice::ReadOnly | QIODevice::Text))
return 0;
return loadTranslator(&source, name);
}
const KeyboardTranslator* KeyboardTranslatorManager::defaultTranslator()
{
// Try to find the default.keytab file if it exists, otherwise
// fall back to the hard-coded one
const KeyboardTranslator* translator = findTranslator("default");
if (!translator) {
QBuffer textBuffer;
textBuffer.setData(defaultTranslatorText);
textBuffer.open(QIODevice::ReadOnly);
translator = loadTranslator(&textBuffer, "fallback");
}
return translator;
}
KeyboardTranslator* KeyboardTranslatorManager::loadTranslator(QIODevice* source, const QString& name)
{
KeyboardTranslator* translator = new KeyboardTranslator(name);
KeyboardTranslatorReader reader(source);
translator->setDescription(reader.description());
while (reader.hasNextEntry())
translator->addEntry(reader.nextEntry());
source->close();
if (!reader.parseError()) {
return translator;
} else {
delete translator;
return 0;
}
}
KeyboardTranslatorWriter::KeyboardTranslatorWriter(QIODevice* destination)
: _destination(destination)
{
......@@ -525,15 +397,6 @@ QList<KeyboardTranslatorReader::Token> KeyboardTranslatorReader::tokenize(const
return list;
}
QList<QString> KeyboardTranslatorManager::allTranslators()
{
if (!_haveLoadedAll) {
findTranslators();
}
return _translators.keys();
}
KeyboardTranslator::Entry::Entry()
: _keyCode(0)
, _modifiers(Qt::NoModifier)
......@@ -803,30 +666,3 @@ KeyboardTranslator::Entry KeyboardTranslator::findEntry(int keyCode, Qt::Keyboar
}
return Entry(); // entry not found
}
void KeyboardTranslatorManager::addTranslator(KeyboardTranslator* translator)
{
_translators.insert(translator->name(), translator);
if (!saveTranslator(translator))
kWarning() << "Unable to save translator" << translator->name()
<< "to disk.";
}
bool KeyboardTranslatorManager::deleteTranslator(const QString& name)
{
Q_ASSERT(_translators.contains(name));
// locate and delete
QString path = findTranslatorPath(name);
if (QFile::remove(path)) {
_translators.remove(name);
return true;
} else {
kWarning() << "Failed to remove translator - " << path;
return false;
}
}
K_GLOBAL_STATIC(KeyboardTranslatorManager , theKeyboardTranslatorManager)
KeyboardTranslatorManager* KeyboardTranslatorManager::instance()
{
return theKeyboardTranslatorManager;
}
......@@ -435,76 +435,6 @@ private:
QTextStream* _writer;
};
/**
* Manages the keyboard translations available for use by terminal sessions,
* see KeyboardTranslator.
*/
class KeyboardTranslatorManager
{
public:
/**
* Constructs a new KeyboardTranslatorManager and loads the list of
* available keyboard translations.
*
* The keyboard translations themselves are not loaded until they are
* first requested via a call to findTranslator()
*/
KeyboardTranslatorManager();
~KeyboardTranslatorManager();
/**
* Adds a new translator. If a translator with the same name
* already exists, it will be replaced by the new translator.
*
* TODO: More documentation.
*/
void addTranslator(KeyboardTranslator* translator);
/**
* Deletes a translator. Returns true on successful deletion or false otherwise.
*
* TODO: More documentation
*/
bool deleteTranslator(const QString& name);
/** Returns the default translator for Konsole. */
const KeyboardTranslator* defaultTranslator();
/**
* Returns the keyboard translator with the given name or 0 if no translator
* with that name exists.
*
* The first time that a translator with a particular name is requested,
* the on-disk .keyboard file is loaded and parsed.
*/
const KeyboardTranslator* findTranslator(const QString& name);
/**
* Returns a list of the names of available keyboard translators.
*
* The first time this is called, a search for available
* translators is started.
*/
QList<QString> allTranslators();
/** Returns the global KeyboardTranslatorManager instance. */
static KeyboardTranslatorManager* instance();
private:
static const QByteArray defaultTranslatorText;
void findTranslators(); // locate the available translators
KeyboardTranslator* loadTranslator(const QString& name); // loads the translator
// with the given name
KeyboardTranslator* loadTranslator(QIODevice* device, const QString& name);
bool saveTranslator(const KeyboardTranslator* translator);
QString findTranslatorPath(const QString& name);
QHash<QString, KeyboardTranslator*> _translators; // maps translator-name -> KeyboardTranslator
// instance
bool _haveLoadedAll;
};
inline int KeyboardTranslator::Entry::keyCode() const
{
return _keyCode;
......
/*
This source file is part of Konsole, a terminal emulator.
Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "KeyboardTranslatorManager.h"
// Qt
#include <QtCore/QBuffer>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
// KDE
#include <KDebug>
#include <KStandardDirs>
using namespace Konsole;
const QByteArray KeyboardTranslatorManager::defaultTranslatorText(
"keyboard \"Fallback Key Translator\"\n"
"key Tab : \"\\t\""
);
KeyboardTranslatorManager::KeyboardTranslatorManager()
: _haveLoadedAll(false)
{
}
KeyboardTranslatorManager::~KeyboardTranslatorManager()
{
qDeleteAll(_translators);
}
QString KeyboardTranslatorManager::findTranslatorPath(const QString& name)
{
return KStandardDirs::locate("data", "konsole/" + name + ".keytab");
}
void KeyboardTranslatorManager::findTranslators()
{
QStringList list = KGlobal::dirs()->findAllResources("data",
"konsole/*.keytab",
KStandardDirs::NoDuplicates);
// add the name of each translator to the list and associated
// the name with a null pointer to indicate that the translator
// has not yet been loaded from disk
foreach ( const QString& translatorPath, list) {
QString name = QFileInfo(translatorPath).baseName();
if (!_translators.contains(name))
_translators.insert(name, 0);
}
_haveLoadedAll = true;
}
const KeyboardTranslator* KeyboardTranslatorManager::findTranslator(const QString& name)
{
if (name.isEmpty())
return defaultTranslator();
if (_translators.contains(name) && _translators[name] != 0)
return _translators[name];
KeyboardTranslator* translator = loadTranslator(name);
if (translator != 0)
_translators[name] = translator;
else if (!name.isEmpty())
kWarning() << "Unable to load translator" << name;
return translator;
}
bool KeyboardTranslatorManager::saveTranslator(const KeyboardTranslator* translator)
{
const QString path = KGlobal::dirs()->saveLocation("data", "konsole/") + translator->name()
+ ".keytab";
//kDebug() << "Saving translator to" << path;
QFile destination(path);
if (!destination.open(QIODevice::WriteOnly | QIODevice::Text)) {
kWarning() << "Unable to save keyboard translation:"
<< destination.errorString();
return false;
}
{
KeyboardTranslatorWriter writer(&destination);
writer.writeHeader(translator->description());
foreach ( const KeyboardTranslator::Entry& entry, translator->entries() )
writer.writeEntry(entry);
}
destination.close();
return true;
}
KeyboardTranslator* KeyboardTranslatorManager::loadTranslator(const QString& name)
{
const QString& path = findTranslatorPath(name);
QFile source(path);
if (name.isEmpty() || !source.open(QIODevice::ReadOnly | QIODevice::Text))
return 0;
return loadTranslator(&source, name);
}
const KeyboardTranslator* KeyboardTranslatorManager::defaultTranslator()
{
// Try to find the default.keytab file if it exists, otherwise
// fall back to the hard-coded one
const KeyboardTranslator* translator = findTranslator("default");
if (!translator) {
QBuffer textBuffer;
textBuffer.setData(defaultTranslatorText);
textBuffer.open(QIODevice::ReadOnly);
translator = loadTranslator(&textBuffer, "fallback");
}
return translator;
}
KeyboardTranslator* KeyboardTranslatorManager::loadTranslator(QIODevice* source, const QString& name)
{
KeyboardTranslator* translator = new KeyboardTranslator(name);
KeyboardTranslatorReader reader(source);
translator->setDescription(reader.description());
while (reader.hasNextEntry())
translator->addEntry(reader.nextEntry());
source->close();
if (!reader.parseError()) {
return translator;
} else {
delete translator;
return 0;
}
}
QList<QString> KeyboardTranslatorManager::allTranslators()
{
if (!_haveLoadedAll) {
findTranslators();
}
return _translators.keys();
}
void KeyboardTranslatorManager::addTranslator(KeyboardTranslator* translator)
{
_translators.insert(translator->name(), translator);
if (!saveTranslator(translator))
kWarning() << "Unable to save translator" << translator->name()
<< "to disk.";
}
bool KeyboardTranslatorManager::deleteTranslator(const QString& name)
{
Q_ASSERT(_translators.contains(name));
// locate and delete
QString path = findTranslatorPath(name);
if (QFile::remove(path)) {
_translators.remove(name);
return true;
} else {
kWarning() << "Failed to remove translator - " << path;
return false;
}
}
K_GLOBAL_STATIC(KeyboardTranslatorManager , theKeyboardTranslatorManager)
KeyboardTranslatorManager* KeyboardTranslatorManager::instance()
{
return theKeyboardTranslatorManager;
}
/*
This source file is part of Konsole, a terminal emulator.
Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
#ifndef KEYBOARDTRANSLATOR_MANAGER_H
#define KEYBOARDTRANSLATOR_MANAGER_H
// Qt
#include <QtCore/QHash>
#include <QtCore/QList>
// Konsole
#include "konsole_export.h"
#include "KeyboardTranslator.h"
class QIODevice;
namespace Konsole
{
/**
* Manages the keyboard translations available for use by terminal sessions,
* see KeyboardTranslator.
*/
class KeyboardTranslatorManager
{
public:
/**
* Constructs a new KeyboardTranslatorManager and loads the list of
* available keyboard translations.
*
* The keyboard translations themselves are not loaded until they are
* first requested via a call to findTranslator()
*/
KeyboardTranslatorManager();
~KeyboardTranslatorManager();
/**
* Adds a new translator. If a translator with the same name
* already exists, it will be replaced by the new translator.
*
* TODO: More documentation.
*/
void addTranslator(KeyboardTranslator* translator);
/**
* Deletes a translator. Returns true on successful deletion or false otherwise.
*
* TODO: More documentation
*/
bool deleteTranslator(const QString& name);
/** Returns the default translator for Konsole. */
const KeyboardTranslator* defaultTranslator();
/**
* Returns the keyboard translator with the given name or 0 if no translator
* with that name exists.
*
* The first time that a translator with a particular name is requested,
* the on-disk .keyboard file is loaded and parsed.
*/
const KeyboardTranslator* findTranslator(const QString& name);
/**
* Returns a list of the names of available keyboard translators.
*
* The first time this is called, a search for available
* translators is started.
*/
QList<QString> allTranslators();
/** Returns the global KeyboardTranslatorManager instance. */
static KeyboardTranslatorManager* instance();
private:
static const QByteArray defaultTranslatorText;
void findTranslators(); // locate the available translators
KeyboardTranslator* loadTranslator(const QString& name); // loads the translator
// with the given name
KeyboardTranslator* loadTranslator(QIODevice* device, const QString& name);
bool saveTranslator(const KeyboardTranslator* translator);
QString findTranslatorPath(const QString& name);
QHash<QString, KeyboardTranslator*> _translators; // maps translator-name -> KeyboardTranslator
// instance
bool _haveLoadedAll;
};
}
#endif // KEYBOARDTRANSLATOR_MANAGER_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment