Skip to content

Commit c53c858

Browse files
committed
grass plugin: avoid garbled japanese/cyrillic chars in the tools' GUI (fix #4547, #3164)
Thanks Minoru Akagi for patches!
1 parent ef6b3c4 commit c53c858

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

src/plugins/grass/qgsgrassmodule.cpp

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,61 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions(
353353
return;
354354
}
355355

356+
QByteArray baDesc = process.readAllStandardOutput();
357+
358+
// GRASS commands usually output text in system default encoding.
359+
// Let's use the System codec whether Qt doesn't recognize the encoding
360+
// of the interface description (see http://hub.qgis.org/issues/4547)
361+
QTextCodec *codec = 0;
362+
363+
QgsDebugMsg( "trying to get encoding name from XML interface description..." );
364+
365+
// XXX: getting the encoding using a regular expression works
366+
// until GRASS will use UTF-16 or UTF-32.
367+
// TODO: We should check the correct encoding by using the BOM (Byte
368+
// Order Mark) from the beginning of the data.
369+
QString xmlDeclaration = QString::fromUtf8( baDesc ).section( '>', 0, 0, QString::SectionIncludeTrailingSep );
370+
QRegExp reg( "<\\?xml\\s+.*encoding\\s*=\\s*(['\"])([A-Za-z][-a-zA-Z0-9_.]*)\\1\\s*\\?>" );
371+
if ( reg.indexIn( xmlDeclaration ) != -1 )
372+
{
373+
QByteArray enc = reg.cap( 2 ).toLocal8Bit();
374+
QgsDebugMsg( QString( "found encoding name '%1'" ).arg( enc ) );
375+
376+
codec = QTextCodec::codecForName( enc );
377+
if ( !codec )
378+
{
379+
QgsDebugMsg( "unrecognized encoding name. Let's use 'System' codec" );
380+
codec = QTextCodec::codecForName( "System" );
381+
Q_ASSERT( codec );
382+
}
383+
}
384+
else
385+
{
386+
QgsDebugMsg( "unable to get encoding name from XML content. Will let Qt detects encoding!" );
387+
}
388+
389+
bool ok = false;
356390
QDomDocument gDoc( "task" );
357391
QString err;
358392
int line, column;
359-
if ( !gDoc.setContent( &process, false, &err, &line, &column ) )
393+
394+
if ( codec )
395+
{
396+
QgsDebugMsg( QString( "parsing XML interface description using '%1' codec..." ).arg( codec->name() ) );
397+
ok = gDoc.setContent( codec->toUnicode( baDesc ), false, &err, &line, &column );
398+
if ( !ok )
399+
{
400+
QgsDebugMsg( "parse FAILED using '%1' codec. Will let Qt detects encoding" );
401+
codec = 0;
402+
}
403+
}
404+
405+
if ( !codec )
406+
{
407+
ok = gDoc.setContent( baDesc, false, &err, &line, &column );
408+
}
409+
410+
if ( !ok )
360411
{
361412
QString errmsg = tr( "Cannot read module description (%1):" ).arg( mXName )
362413
+ tr( "\n%1\nat line %2 column %3" ).arg( err ).arg( line ).arg( column );
@@ -401,7 +452,7 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions(
401452
mypInnerFrameLayout->addWidget( &mAdvancedFrame );
402453
mypInnerFrameLayout->addStretch( 1 );
403454

404-
// Hide advanced and set butto next
455+
// Hide advanced and set button next
405456
switchAdvanced();
406457

407458
QVBoxLayout *mypSimpleLayout = new QVBoxLayout( mypSimpleFrame );
@@ -1555,9 +1606,9 @@ void QgsGrassModule::readStdout()
15551606
mProcess.setReadChannel( QProcess::StandardOutput );
15561607
while ( mProcess.canReadLine() )
15571608
{
1558-
//line = QString::fromLocal8Bit( mProcess.readLineStdout().ascii() );
15591609
QByteArray ba = mProcess.readLine();
1560-
line = QString::fromUtf8( ba ).replace( '\n', "" );
1610+
//line = QString::fromUtf8( ba ).replace( '\n', "" );
1611+
line = QString::fromLocal8Bit( ba ).replace( '\n', "" );
15611612
//QgsDebugMsg(QString("line: '%1'").arg(line));
15621613

15631614
// GRASS_INFO_PERCENT is catched here only because of bugs in GRASS,
@@ -1578,21 +1629,19 @@ void QgsGrassModule::readStderr()
15781629
{
15791630
QgsDebugMsg( "called." );
15801631

1581-
mProcess.setReadChannel( QProcess::StandardError );
1582-
15831632
QString line;
15841633
QRegExp rxpercent( "GRASS_INFO_PERCENT: (\\d+)" );
15851634
QRegExp rxmessage( "GRASS_INFO_MESSAGE\\(\\d+,\\d+\\): (.*)" );
15861635
QRegExp rxwarning( "GRASS_INFO_WARNING\\(\\d+,\\d+\\): (.*)" );
15871636
QRegExp rxerror( "GRASS_INFO_ERROR\\(\\d+,\\d+\\): (.*)" );
15881637
QRegExp rxend( "GRASS_INFO_END\\(\\d+,\\d+\\)" );
15891638

1590-
1639+
mProcess.setReadChannel( QProcess::StandardError );
15911640
while ( mProcess.canReadLine() )
15921641
{
1593-
//line = QString::fromLocal8Bit( mProcess.readLineStderr().ascii() );
15941642
QByteArray ba = mProcess.readLine();
1595-
line = QString::fromUtf8( ba ).replace( '\n', "" );
1643+
//line = QString::fromUtf8( ba ).replace( '\n', "" );
1644+
line = QString::fromLocal8Bit( ba ).replace( '\n', "" );
15961645
//QgsDebugMsg(QString("line: '%1'").arg(line));
15971646

15981647
if ( rxpercent.indexIn( line ) != -1 )

0 commit comments

Comments
 (0)