Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
GISBASE and PATH init moved to qgis_grass library
git-svn-id: http://svn.osgeo.org/qgis/trunk@4789 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Feb 1, 2006
1 parent c5222ad commit 97e6eda
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/providers/grass/Makefile.am
Expand Up @@ -4,6 +4,8 @@

INCLUDES = -I../../core -I../../gui

DEFS=-DGRASS_BASE=\"$(GRASS_BASE)\"

lib_LTLIBRARIES = libqgisgrass.la

plugindir = ${pkglibdir}
Expand Down
164 changes: 126 additions & 38 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -21,63 +21,151 @@
#include "q3process.h"
#include "qfile.h"
#include "qfileinfo.h"
#include "qfiledialog.h"
#include "qdir.h"
#include "qtextstream.h"
#include "qsettings.h"

//#include "qgsapplication.h"
#include <QCoreApplication>
#include "qgsgrass.h"

extern "C" {
#include <unistd.h>
}

void QgsGrass::init( void ) {
if ( !initialized ) {
// Is it active mode ?
if ( getenv ("GISRC") ) {
active = true;
// Store default values
defaultGisdbase = G_gisdbase();
defaultLocation = G_location();
defaultMapset = G_mapset();
} else {
active = false;
}

// Don't use GISRC file and read/write GRASS variables (from location G_VAR_GISRC) to memory only.
G_set_gisrc_mode ( G_GISRC_MODE_MEMORY );
void QgsGrass::init( void )
{
if ( initialized ) return;

QSettings settings("QuantumGIS", "qgis");

// Is it active mode ?
if ( getenv ("GISRC") ) {
active = true;
// Store default values
defaultGisdbase = G_gisdbase();
defaultLocation = G_location();
defaultMapset = G_mapset();
} else {
active = false;
}

// Don't use GISRC file and read/write GRASS variables (from location G_VAR_GISRC) to memory only.
G_set_gisrc_mode ( G_GISRC_MODE_MEMORY );

// Init GRASS libraries (required)
G_no_gisinit(); // Doesn't check write permissions for mapset compare to G_gisinit("libgrass++");

// Init GRASS libraries (required)
G_no_gisinit(); // Doesn't check write permissions for mapset compare to G_gisinit("libgrass++");
// Set error function
G_set_error_routine ( &error_routine );

// Set error function
G_set_error_routine ( &error_routine );
// Set program name
G_set_program_name ("QGIS");


// Require GISBASE to be set. This should point to the location of
// the GRASS installation. The GRASS libraries use it to know
// where to look for things.

// Look first to see if GISBASE env var is already set.
// This is set when QGIS is run from within GRASS
// or when set explicitly by the user.
// This value should always take precedence.
QString gisBase = getenv("GISBASE");
std::cerr << "gisBase = " << gisBase.toLocal8Bit().data() << std::endl;
#ifdef QGISDEBUG
qDebug( "%s:%d GRASS gisBase from GISBASE env var is: %s", __FILE__, __LINE__, (const char*)gisBase );
#endif
if ( !isValidGrassBaseDir(gisBase) ) {
// Look for gisbase in QSettings
gisBase = settings.readEntry("/GRASS/gisbase", "");
#ifdef QGISDEBUG
qDebug( "%s:%d GRASS gisBase from QSettings is: %s", __FILE__, __LINE__, (const char*)gisBase );
#endif
}

// Set program name
G_set_program_name ("QGIS");
if ( !isValidGrassBaseDir(gisBase) ) {
// Use the location specified --with-grass during configure
gisBase = GRASS_BASE;
#ifdef QGISDEBUG
qDebug( "%s:%d GRASS gisBase from configure is: %s", __FILE__, __LINE__, (const char*)gisBase );
#endif
}

while ( !isValidGrassBaseDir(gisBase) ) {
// Keep asking user for GISBASE until we get a valid one
//QMessageBox::warning( 0, "Warning", "QGIS can't find your GRASS installation,\nGRASS data "
// "cannot be used.\nPlease select your GISBASE.\nGISBASE is full path to the\n"
// "directory where GRASS is installed." );
// XXX Need to subclass this and add explantory message above to left side
gisBase = QFileDialog::getExistingDirectory(
0, "Choose GISBASE ...", gisBase);
if (gisBase == QString::null)
{
// User pressed cancel. No GRASS for you!
return;
}
}

// Add path to GRASS modules
// TODO: do that portable
#ifdef QGISDEBUG
qDebug( "%s:%d Valid GRASS gisBase is: %s", __FILE__, __LINE__, (const char*)gisBase );
#endif
QString gisBaseEnv = "GISBASE=" + gisBase;
/* _Correct_ putenv() implementation is not making copy! */
char *gisBaseEnvChar = new char[gisBaseEnv.length()+1];
strcpy ( gisBaseEnvChar, const_cast<char *>(gisBaseEnv.ascii()) );
putenv( gisBaseEnvChar );
settings.writeEntry("/GRASS/gisbase", gisBase);

// Add path to GRASS modules
#ifdef WIN32
QString sep = ";";
QString sep = ";";
#else
QString sep = ":";
QString sep = ":";

#endif
QString path = "PATH=" + gisBase + "/bin";
path.append ( sep + gisBase + "/scripts" );

// On windows the GRASS libraries are in
// QgsApplication::prefixPath(), we have to add them
// to PATH to enable running of GRASS modules
// and database drivers
#ifdef WIN32
// It seems that QgsApplication::prefixPath()
// is not initialized at this point
path.append ( sep + QCoreApplication::applicationDirPath() );
#endif
QString gisBase = getenv("GISBASE");
QString path = "PATH=" + gisBase + "/bin";
path.append ( sep + gisBase + "/scripts" );
QString p = getenv ("PATH");
path.append ( sep + p );

QString p = getenv ("PATH");
path.append ( sep + p );
#ifdef QGISDEBUG
std::cerr << "set PATH: " << path.toLocal8Bit().data() << std::endl;
#endif
char *pathEnvChar = new char[path.length()+1];
strcpy ( pathEnvChar, const_cast<char *>(path.ascii()) );
putenv( pathEnvChar );

#ifdef QGISDEBUG
std::cerr << "set PATH: " << path.toLocal8Bit().data() << std::endl;
#endif
char *pathEnvChar = new char[path.length()+1];
strcpy ( pathEnvChar, const_cast<char *>(path.ascii()) );
putenv( pathEnvChar );
initialized = 1;
}

initialized = 1;
}
/*
* Check if given directory contains a GRASS installation
*/
bool QgsGrass::isValidGrassBaseDir(QString const gisBase)
{
if ( gisBase.isEmpty() )
{
return FALSE;
}

QFileInfo gbi ( gisBase + "/etc/element_list" );
if ( gbi.exists() ) {
return TRUE;
} else {
return FALSE;
}
}

bool QgsGrass::activeMode( void )
Expand Down
6 changes: 5 additions & 1 deletion src/providers/grass/qgsgrass.h
Expand Up @@ -86,10 +86,14 @@ class QgsGrass {
*/
static QString closeMapset ();

//! Check if given directory contains a GRASS installation
static bool isValidGrassBaseDir(QString const gisBase);

static void init (void);

private:
static int initialized; // Set to 1 after initialization
static bool active; // is active mode
static void init (void);
static QString defaultGisdbase;
static QString defaultLocation;
static QString defaultMapset;
Expand Down

0 comments on commit 97e6eda

Please sign in to comment.