Index: src/core/qgsmaplayer.h =================================================================== --- src/core/qgsmaplayer.h (revision 8652) +++ src/core/qgsmaplayer.h (working copy) @@ -201,7 +201,7 @@ * @see also loadNamedStyle (); */ virtual QString loadDefaultStyle ( bool & theResultFlag ); - + /** Retrieve a named style for this layer if one * exists (either as a .qml file on disk or as a * record in the users style table in their personal qgis.db) @@ -217,6 +217,8 @@ */ virtual QString loadNamedStyle ( const QString theURI , bool & theResultFlag ); + virtual bool loadNamedStyleFromDb ( const QString db, const QString theURI , QString &qml ); + /** Save the properties of this layer as the default style * (either as a .qml file on disk or as a * record in the users style table in their personal qgis.db) Index: src/core/qgsmaplayer.cpp =================================================================== --- src/core/qgsmaplayer.cpp (revision 8652) +++ src/core/qgsmaplayer.cpp (working copy) @@ -420,37 +420,20 @@ return loadNamedStyle ( key, theResultFlag ); } -QString QgsMapLayer::loadNamedStyle ( const QString theURI , bool & theResultFlag ) +bool QgsMapLayer::loadNamedStyleFromDb(const QString db, const QString theURI, QString &qml) { - theResultFlag = false; + bool theResultFlag = false; - QDomDocument myDocument ( "qgis" ); - - // location of problem associated with errorMsg - int line, column; - QString myErrorMessage; - - QFile myFile ( theURI ); - if ( myFile.open(QFile::ReadOnly ) ) - { - // read file - theResultFlag = myDocument.setContent ( &myFile, &myErrorMessage, &line, &column ); - if(!theResultFlag) - myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column); - myFile.close(); - } - else - { // read from database sqlite3 *myDatabase; sqlite3_stmt *myPreparedStatement; const char *myTail; int myResult; - myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase); - if (myResult) + myResult = sqlite3_open(db.toUtf8().data(), &myDatabase); + if (!myResult) { - return tr("could not open user database"); + return false; } QString mySql = "select qml from tbl_styles where style=?"; @@ -462,21 +445,51 @@ if( sqlite3_bind_text(myPreparedStatement, 1, param.data(), param.length(), SQLITE_STATIC)==SQLITE_OK && sqlite3_step(myPreparedStatement)==SQLITE_ROW ) { - QString qml = QString::fromUtf8( (char *)sqlite3_column_text(myPreparedStatement, 0) ); - theResultFlag = myDocument.setContent ( qml, &myErrorMessage, &line, &column ); - if(!theResultFlag) - myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column); + qml = QString::fromUtf8( (char *)sqlite3_column_text(myPreparedStatement, 0) ); + theResultFlag = true; } } - else - { - theResultFlag = false; - myErrorMessage = tr("style %1 not found in database").arg(theURI); - } sqlite3_finalize(myPreparedStatement); sqlite3_close(myDatabase); + + return theResultFlag; +} + +QString QgsMapLayer::loadNamedStyle ( const QString theURI , bool & theResultFlag ) +{ + theResultFlag = false; + + QDomDocument myDocument ( "qgis" ); + + // location of problem associated with errorMsg + int line, column; + QString myErrorMessage; + + QFile myFile ( theURI ); + if ( myFile.open(QFile::ReadOnly ) ) + { + // read file + theResultFlag = myDocument.setContent ( &myFile, &myErrorMessage, &line, &column ); + if(!theResultFlag) + myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column); + myFile.close(); } + else + { + QString qml; + if( loadNamedStyleFromDb( theURI, QgsApplication::qgisUserDbFilePath(), qml ) || + loadNamedStyleFromDb( theURI, QgsApplication::qgisMasterDbFilePath(), qml ) ) + { + theResultFlag = myDocument.setContent ( qml, &myErrorMessage, &line, &column ); + if(!theResultFlag) + myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column); + } + else + { + myErrorMessage = tr("style not found in databases"); + } + } if(!theResultFlag) return myErrorMessage;