QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsprojectbadlayerhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectbadlayerhandler.cpp - QgsProjectBadLayerHandler
3
4 ---------------------
5 begin : 22.10.2016
6 copyright : (C) 2016 by Matthias Kuhn
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#include "qgsdatasourceuri.h"
18#include "qgslogger.h"
19#include "qgsmessagelog.h"
20#include "qgsapplication.h"
21
22#include <QFileInfo>
23
24void QgsProjectBadLayerHandler::handleBadLayers( const QList<QDomNode> &layers )
25{
26 if ( !layers.empty() )
27 QgsMessageLog::logMessage( QObject::tr( "%n unavailable layer(s) found:", nullptr, layers.size() ) );
28
29 for ( const QDomNode &layer : layers )
30 {
31 QgsMessageLog::logMessage( QObject::tr( " * %1" ).arg( QgsDataSourceUri::removePassword( dataSource( layer ), true ) ) );
32 }
33}
34
36{
37 const QString type = layerNode.toElement().attribute( QStringLiteral( "type" ) );
38
39 if ( type.isNull() )
40 {
41 QgsDebugError( QStringLiteral( "cannot find ``type'' attribute" ) );
42
43 return IS_BOGUS;
44 }
45
46 if ( "raster" == type )
47 {
48 QgsDebugMsgLevel( QStringLiteral( "is a raster" ), 2 );
49
50 return IS_RASTER;
51 }
52 else if ( "vector" == type )
53 {
54 QgsDebugMsgLevel( QStringLiteral( "is a vector" ), 2 );
55
56 return IS_VECTOR;
57 }
58
59 QgsDebugMsgLevel( "is unknown type " + type, 2 );
60
61 return IS_BOGUS;
62}
63
64QString QgsProjectBadLayerHandler::dataSource( const QDomNode &layerNode )
65{
66 const QDomNode dataSourceNode = layerNode.namedItem( QStringLiteral( "datasource" ) );
67
68 if ( dataSourceNode.isNull() )
69 {
70 QgsDebugError( QStringLiteral( "cannot find datasource node" ) );
71
72 return QString();
73 }
74
75 return dataSourceNode.toElement().text();
76}
77
79{
80 // XXX but what about rasters that can be URLs? _Can_ they be URLs?
81
82 switch ( dataType( layerNode ) )
83 {
84 case IS_VECTOR:
85 {
86 const QString ds = dataSource( layerNode );
87
88 QgsDebugMsgLevel( "datasource is " + ds, 2 );
89
90 if ( ds.contains( QLatin1String( "host=" ) ) )
91 {
92 return IS_URL;
93 }
94 else if ( ds.contains( QLatin1String( "dbname=" ) ) )
95 {
96 return IS_DATABASE;
97 }
98 // be default, then, this should be a file based layer data source
99 // XXX is this a reasonable assumption?
100
101 return IS_FILE;
102 }
103
104 case IS_RASTER: // rasters are currently only accessed as
105 // physical files
106 return IS_FILE;
107
108 default:
109 QgsDebugError( QStringLiteral( "unknown ``type'' attribute" ) );
110 }
111
112 return IS_Unknown;
113}
114
115void QgsProjectBadLayerHandler::setDataSource( QDomNode &layerNode, const QString &dataSource )
116{
117 const QDomNode dataSourceNode = layerNode.namedItem( QStringLiteral( "datasource" ) );
118 const QDomElement dataSourceElement = dataSourceNode.toElement();
119 QDomText dataSourceText = dataSourceElement.firstChild().toText();
120
121 QgsDebugMsgLevel( "datasource changed from " + dataSourceText.data(), 2 );
122
123 dataSourceText.setData( dataSource );
124
125 QgsDebugMsgLevel( "to " + dataSourceText.data(), 2 );
126}
static QString removePassword(const QString &aUri, bool hide=false)
Removes the password element from a URI.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void setDataSource(QDomNode &layerNode, const QString &dataSource)
Set the datasource element to the new value.
ProviderType
the flavors for data storage
virtual void handleBadLayers(const QList< QDomNode > &layers)
This method will be called whenever the project tries to load layers which cannot be accessed.
ProviderType providerType(const QDomNode &layerNode)
Returns the physical storage type associated with the given layer.
QString dataSource(const QDomNode &layerNode)
Returns the data source for the given layer.
DataType dataType(const QDomNode &layerNode)
Returns data type associated with the given QgsProject file Dom node.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38