Skip to content
Browse files

Added: charset auto-detection for Windows

  • Loading branch information...
1 parent dcd4513 commit d158d38186b1d84044aeda34acd4bf02554750b9 mezomish committed Jul 27, 2009
Showing with 55 additions and 34 deletions.
  1. +38 −0 src/Document.cpp
  2. +1 −0 src/Document.h
  3. +7 −0 src/JuffApp.cpp
  4. +2 −0 src/JuffApp.h
  5. +7 −33 src/SciDoc.cpp
  6. +0 −1 src/SciDoc.h
  7. BIN win32/cygwin1.dll
  8. BIN win32/enca.exe
View
38 src/Document.cpp
@@ -19,10 +19,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "Document.h"
#include "CommandStorage.h"
+#include "JuffApp.h"
#include "Log.h"
#include "Functions.h"
#include <QtCore/QFileInfo>
+#include <QtCore/QProcess>
#include <QtGui/QAbstractButton>
#include <QtGui/QAction>
#include <QtGui/QMessageBox>
@@ -144,6 +146,42 @@ void Document::checkLastModified() {
}
}
+/**
+* mapCharset()
+*
+* Maps charset from enca's format to Qt's charset name.
+*/
+QString mapCharset(const QString& encaName) {
+ if ( encaName == "windows-1251" ) {
+ return "Windows-1251";
+ }
+ else if ( encaName == "IBM866" ) {
+ return "IBM 866";
+ }
+ else {
+ return encaName;
+ }
+}
+
+QString Document::guessCharset() const {
+ QStringList params;
+ params << "-m" << fileName();
+ if ( !JuffApp::language().isEmpty() ) {
+ params << "-L" << JuffApp::language().left(2);
+ }
+
+ QProcess enca;
+ enca.start("enca", params);
+ enca.waitForFinished();
+ QString output = QString(enca.readAllStandardOutput()).simplified();
+ if ( !output.isEmpty() ) {
+ QString codecName = mapCharset(output);
+ return codecName;
+ }
+ else {
+ return "";
+ }
+}
//} // namespace Data
} // namespace Juff
View
1 src/Document.h
@@ -101,6 +101,7 @@ protected slots:
protected:
QString type_;
+ QString guessCharset() const;
private:
QString fileName_;
View
7 src/JuffApp.cpp
@@ -25,6 +25,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <errno.h>
#endif // Q_OS_UNIX
+QString JuffApp::language_ = QString();
+
JuffApp::JuffApp(int& argc, char** argv) : QApplication(argc, argv), sent_(false) {
int sock = -1;
gui_ = 0;
@@ -79,6 +81,10 @@ JuffApp::~JuffApp() {
Settings::write();
}
+QString JuffApp::language() {
+ return language_;
+}
+
bool JuffApp::sentFilesToExistingInstance() {
return sent_;
}
@@ -88,6 +94,7 @@ void JuffApp::init(int& argc, char** argv) {
// translator
QString lng = QLocale::system().name();
+ language_ = lng;
if ( translator_.load("juffed_" + lng, AppInfo::translationPath()) ) {
if ( !translator_.isEmpty() ) {
installTranslator(&translator_);
View
2 src/JuffApp.h
@@ -38,6 +38,7 @@ Q_OBJECT
virtual ~JuffApp();
bool sentFilesToExistingInstance();
+ static QString language();
private:
void init(int& argc, char** argv);
@@ -51,6 +52,7 @@ Q_OBJECT
QTranslator translator_;
SocketListener* listener_;
bool sent_;
+ static QString language_;
};
#endif
View
40 src/SciDoc.cpp
@@ -36,7 +36,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <QtCore/QDir>
#include <QtCore/QFile>
-#include <QtCore/QProcess>
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
#include <QtGui/QMenu>
@@ -550,43 +549,18 @@ void SciDoc::reload() {
}
}
-/**
-* mapCharset()
-*
-* Maps charset from enca's format to Qt's charset name.
-*/
-QString mapCharset(const QString& encaName) {
- if ( encaName == "windows-1251" ) {
- return "Windows-1251";
- }
- else if ( encaName == "IBM866" ) {
- return "IBM 866";
- }
- else {
- return encaName;
- }
-}
-
-void SciDoc::guessCharset() {
- QProcess enca;
- enca.start("enca", QStringList() << "-m" << fileName());
- enca.waitForFinished();
- QString output = QString(enca.readAllStandardOutput()).simplified();
- if ( !output.isEmpty() ) {
- QString codecName = mapCharset(output).toLocal8Bit();
+void SciDoc::readDoc() {
+ QString text;
+ QFile file(fileName());
+ if ( file.open(QIODevice::ReadOnly) ) {
+
+ QString codecName = guessCharset();
QTextCodec* codec = QTextCodec::codecForName(codecName.toAscii());
if ( codec ) {
docInt_->codec_ = codec;
docInt_->charsetName_ = codecName;
}
- }
-}
-
-void SciDoc::readDoc() {
- QString text;
- QFile file(fileName());
- if ( file.open(QIODevice::ReadOnly) ) {
- guessCharset();
+
QTextStream ts(&file);
ts.setCodec(docInt_->codec_);
docInt_->edit1_->setText(ts.readAll());
View
1 src/SciDoc.h
@@ -118,7 +118,6 @@ private slots:
void onLinesCountChanged();
private:
- void guessCharset();
void readDoc();
void loadAutocompletionAPI(const QString& lexName, QsciLexer* lexer);
void stripTrailingSpaces();
View
BIN win32/cygwin1.dll
Binary file not shown.
View
BIN win32/enca.exe
Binary file not shown.

0 comments on commit d158d38

Please sign in to comment.
Something went wrong with that request. Please try again.