execjosh 2013-03-26 19:23:02 +09:00 committed by Ariya Hidayat
parent c07a2efa33
commit 3d874d9e0d
2 changed files with 31 additions and 2 deletions

View File

@ -54,9 +54,33 @@ QString Terminal::getEncoding() const
return m_encoding.getName();
}
void Terminal::setEncoding(const QString &encoding)
bool Terminal::setEncoding(const QString &encoding)
{
// Since there can be multiple names for the same codec (i.e., "utf8" and
// "utf-8"), we need to get the codec in the system first and use its
// canonical name
QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
if ((QTextCodec *)NULL == codec) {
return false;
}
// Check whether encoding actually needs to be changed
const QString encodingBeforeUpdate(m_encoding.getName());
if (0 == encodingBeforeUpdate.compare(QString(codec->name()), Qt::CaseInsensitive)) {
return false;
}
m_encoding.setEncoding(encoding);
// Emit the signal only if the encoding actually was changed
const QString encodingAfterUpdate(m_encoding.getName());
if (0 == encodingBeforeUpdate.compare(encodingAfterUpdate, Qt::CaseInsensitive)) {
return false;
}
emit encodingChanged(encoding);
return true;
}
void Terminal::cout(const QString &string, const bool newline) const

View File

@ -39,15 +39,20 @@
class Terminal: public QObject
{
Q_OBJECT
public:
static Terminal *instance();
QString getEncoding() const;
void setEncoding(const QString &encoding);
bool setEncoding(const QString &encoding);
void cout(const QString &string, const bool newline = true) const;
void cerr(const QString &string, const bool newline = true) const;
signals:
void encodingChanged(const QString &encoding);
private:
void output(std::ostream &out, const QString &string, const bool newline) const;