@@ -353,10 +353,61 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions(
353
353
return ;
354
354
}
355
355
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 ;
356
390
QDomDocument gDoc ( " task" );
357
391
QString err;
358
392
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 )
360
411
{
361
412
QString errmsg = tr ( " Cannot read module description (%1):" ).arg ( mXName )
362
413
+ tr ( " \n %1\n at line %2 column %3" ).arg ( err ).arg ( line ).arg ( column );
@@ -401,7 +452,7 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions(
401
452
mypInnerFrameLayout->addWidget ( &mAdvancedFrame );
402
453
mypInnerFrameLayout->addStretch ( 1 );
403
454
404
- // Hide advanced and set butto next
455
+ // Hide advanced and set button next
405
456
switchAdvanced ();
406
457
407
458
QVBoxLayout *mypSimpleLayout = new QVBoxLayout ( mypSimpleFrame );
@@ -1555,9 +1606,9 @@ void QgsGrassModule::readStdout()
1555
1606
mProcess .setReadChannel ( QProcess::StandardOutput );
1556
1607
while ( mProcess .canReadLine () )
1557
1608
{
1558
- // line = QString::fromLocal8Bit( mProcess.readLineStdout().ascii() );
1559
1609
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 ' , " " );
1561
1612
// QgsDebugMsg(QString("line: '%1'").arg(line));
1562
1613
1563
1614
// GRASS_INFO_PERCENT is catched here only because of bugs in GRASS,
@@ -1578,21 +1629,19 @@ void QgsGrassModule::readStderr()
1578
1629
{
1579
1630
QgsDebugMsg ( " called." );
1580
1631
1581
- mProcess .setReadChannel ( QProcess::StandardError );
1582
-
1583
1632
QString line;
1584
1633
QRegExp rxpercent ( " GRASS_INFO_PERCENT: (\\ d+)" );
1585
1634
QRegExp rxmessage ( " GRASS_INFO_MESSAGE\\ (\\ d+,\\ d+\\ ): (.*)" );
1586
1635
QRegExp rxwarning ( " GRASS_INFO_WARNING\\ (\\ d+,\\ d+\\ ): (.*)" );
1587
1636
QRegExp rxerror ( " GRASS_INFO_ERROR\\ (\\ d+,\\ d+\\ ): (.*)" );
1588
1637
QRegExp rxend ( " GRASS_INFO_END\\ (\\ d+,\\ d+\\ )" );
1589
1638
1590
-
1639
+ mProcess . setReadChannel ( QProcess::StandardError );
1591
1640
while ( mProcess .canReadLine () )
1592
1641
{
1593
- // line = QString::fromLocal8Bit( mProcess.readLineStderr().ascii() );
1594
1642
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 ' , " " );
1596
1645
// QgsDebugMsg(QString("line: '%1'").arg(line));
1597
1646
1598
1647
if ( rxpercent.indexIn ( line ) != -1 )
0 commit comments