@@ -262,6 +262,11 @@ bool QgsGrassObject::operator==( const QgsGrassObject& other ) const
262
262
&& mName == other.mName && mType == other.mType ;
263
263
}
264
264
265
+ QgsGrass::QgsGrass ()
266
+ : mMapsetSearchPathWatcher( 0 )
267
+ {
268
+ }
269
+
265
270
QString QgsGrass::pathSeparator ()
266
271
{
267
272
#ifdef Q_OS_WIN
@@ -316,8 +321,8 @@ bool QgsGrass::init( void )
316
321
lock ();
317
322
QgsDebugMsg ( " do init" );
318
323
319
- // Is it active mode ?
320
324
active = false ;
325
+ // Is it active mode ?
321
326
if ( getenv ( " GISRC" ) )
322
327
{
323
328
G_TRY
@@ -352,6 +357,12 @@ bool QgsGrass::init( void )
352
357
return false ;
353
358
}
354
359
360
+ if ( active )
361
+ {
362
+ QgsGrass::instance ()->loadMapsetSearchPath (); // must be after G_no_gisinit()
363
+ QgsGrass::instance ()->setMapsetSearchPathWatcher ();
364
+ }
365
+
355
366
// I think that mask should not be used in QGIS as it can only confuses people,
356
367
// anyway, I don't think anybody is using MASK
357
368
// TODO7: Rast_suppress_masking (see G_suppress_masking() macro above) needs MAPSET
@@ -545,6 +556,11 @@ QString QgsGrass::getDefaultMapset()
545
556
return defaultMapset;
546
557
}
547
558
559
+ QString QgsGrass::getDefaultMapsetPath ()
560
+ {
561
+ return getDefaultLocationPath () + " /" + defaultMapset;
562
+ }
563
+
548
564
void QgsGrass::setLocation ( QString gisdbase, QString location )
549
565
{
550
566
QgsDebugMsg ( QString ( " gisdbase = %1 location = %2" ).arg ( gisdbase ).arg ( location ) );
@@ -601,6 +617,109 @@ void QgsGrass::setMapset( QgsGrassObject grassObject )
601
617
setMapset ( grassObject.gisdbase (), grassObject.location (), grassObject.mapset () );
602
618
}
603
619
620
+ bool QgsGrass::isMapsetInSearchPath ( QString mapset )
621
+ {
622
+ return mMapsetSearchPath .contains ( mapset );
623
+ }
624
+
625
+ void QgsGrass::loadMapsetSearchPath ()
626
+ {
627
+ QgsDebugMsg ( " entered" );
628
+ // do not lock, it is called from locked function
629
+ QStringList oldMapsetSearchPath = mMapsetSearchPath ;
630
+ mMapsetSearchPath .clear ();
631
+ if ( !activeMode () )
632
+ {
633
+ QgsDebugMsg ( " not active" );
634
+ emit mapsetSearchPathChanged ();
635
+ return ;
636
+ }
637
+ G_TRY
638
+ {
639
+ QgsGrass::setMapset ( getDefaultGisdbase (), getDefaultLocation (), getDefaultMapset () );
640
+ const char *mapset = 0 ;
641
+ #if GRASS_VERSION_MAJOR >= 7
642
+ G_reset_mapsets ();
643
+ for ( int i = 0 ; ( mapset = G_get_mapset_name ( i ) ); i++ )
644
+ #else
645
+ int result = G_reset_mapsets ();
646
+ Q_UNUSED ( result );
647
+ for ( int i = 0 ; ( mapset = G__mapset_name ( i ) ); i++ )
648
+ #endif
649
+ {
650
+ QgsDebugMsg ( QString ( " mapset = %1" ).arg ( mapset ) );
651
+ if ( G_is_mapset_in_search_path ( mapset ) )
652
+ {
653
+ mMapsetSearchPath << mapset;
654
+ }
655
+ }
656
+ }
657
+ G_CATCH ( QgsGrass::Exception &e )
658
+ {
659
+ QgsDebugMsg ( " cannot load mapset search path: " + QString ( e.what () ) );
660
+ }
661
+ QgsDebugMsg ( " mMapsetSearchPath = " + mMapsetSearchPath .join ( " ," ) );
662
+ if ( mMapsetSearchPath != oldMapsetSearchPath )
663
+ {
664
+ emit mapsetSearchPathChanged ();
665
+ }
666
+ }
667
+
668
+ void QgsGrass::setMapsetSearchPathWatcher ()
669
+ {
670
+ QgsDebugMsg ( " etered" );
671
+ if ( mMapsetSearchPathWatcher )
672
+ {
673
+ delete mMapsetSearchPathWatcher ;
674
+ mMapsetSearchPathWatcher = 0 ;
675
+ }
676
+ if ( !activeMode () )
677
+ {
678
+ return ;
679
+ }
680
+ mMapsetSearchPathWatcher = new QFileSystemWatcher ( this );
681
+
682
+ QString searchFilePath = getDefaultMapsetPath () + " /SEARCH_PATH" ;
683
+
684
+ if ( QFileInfo ( searchFilePath ).exists () )
685
+ {
686
+ QgsDebugMsg ( " add watcher on SEARCH_PATH file " + searchFilePath );
687
+ mMapsetSearchPathWatcher ->addPath ( searchFilePath );
688
+ connect ( mMapsetSearchPathWatcher , SIGNAL ( fileChanged ( const QString & ) ), SLOT ( onSearchPathFileChanged ( const QString & ) ) );
689
+ }
690
+ else
691
+ {
692
+ QgsDebugMsg ( " add watcher on mapset " + getDefaultMapsetPath () );
693
+ mMapsetSearchPathWatcher ->addPath ( getDefaultMapsetPath () );
694
+ connect ( mMapsetSearchPathWatcher , SIGNAL ( directoryChanged ( const QString & ) ), SLOT ( onSearchPathFileChanged ( const QString & ) ) );
695
+ }
696
+ }
697
+
698
+ void QgsGrass::onSearchPathFileChanged ( const QString & path )
699
+ {
700
+ QgsDebugMsg ( " path = " + path );
701
+ QString searchFilePath = getDefaultMapsetPath () + " /SEARCH_PATH" ;
702
+ if ( path == searchFilePath )
703
+ {
704
+ // changed or removed
705
+ loadMapsetSearchPath ();
706
+ if ( !QFileInfo ( searchFilePath ).exists () ) // removed
707
+ {
708
+ // reset watcher to mapset
709
+ setMapsetSearchPathWatcher ();
710
+ }
711
+ }
712
+ else
713
+ {
714
+ // mapset directory changed
715
+ if ( QFileInfo ( searchFilePath ).exists () ) // search path file added
716
+ {
717
+ loadMapsetSearchPath ();
718
+ setMapsetSearchPathWatcher ();
719
+ }
720
+ }
721
+ }
722
+
604
723
jmp_buf QgsGrass::jumper;
605
724
606
725
bool QgsGrass::mNonInitializable = false ;
@@ -860,6 +979,9 @@ QString QgsGrass::openMapset( const QString& gisdbase,
860
979
861
980
active = true ;
862
981
982
+ QgsGrass::instance ()->loadMapsetSearchPath ();
983
+ QgsGrass::instance ()->setMapsetSearchPathWatcher ();
984
+
863
985
// closeMapset() added at the beginning
864
986
#if 0
865
987
#ifndef Q_OS_WIN
@@ -936,6 +1058,7 @@ QString QgsGrass::closeMapset()
936
1058
}
937
1059
}
938
1060
1061
+ QgsGrass::instance ()->setMapsetSearchPathWatcher (); // unset watcher
939
1062
emit QgsGrass::instance ()->mapsetChanged ();
940
1063
return QString::null;
941
1064
}
0 commit comments