From 3d874d9e0d089780b40c7b5abc35d0b546249b34 Mon Sep 17 00:00:00 2001 From: execjosh Date: Tue, 26 Mar 2013 19:23:02 +0900 Subject: [PATCH] Make `Terminal` emit a signal upon encoding change See #11234 https://github.com/ariya/phantomjs/pull/11234 Spin off from #11168 https://github.com/ariya/phantomjs/pull/11168 --- src/terminal.cpp | 26 +++++++++++++++++++++++++- src/terminal.h | 7 ++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/terminal.cpp b/src/terminal.cpp index e9b531c9..ec399dce 100644 --- a/src/terminal.cpp +++ b/src/terminal.cpp @@ -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 diff --git a/src/terminal.h b/src/terminal.h index d07ad595..a12738a1 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -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;