QGIS API Documentation 3.99.0-Master (d270888f95f)
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 ***************************************************************************/
17
18#include "qgsapplication.h"
19#include "qgsdatasourceuri.h"
20#include "qgslogger.h"
21#include "qgsmessagelog.h"
22
23#include <QFileInfo>
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
28void QgsProjectBadLayerHandler::handleBadLayers( const QList<QDomNode> &layers )
29{
30 if ( !layers.empty() )
31 QgsMessageLog::logMessage( QObject::tr( "%n unavailable layer(s) found:", nullptr, layers.size() ) );
32
33 for ( const QDomNode &layer : layers )
34 {
35 QgsMessageLog::logMessage( QObject::tr( " * %1" ).arg( QgsDataSourceUri::removePassword( dataSource( layer ), true ) ) );
36 }
37}
38
40{
41 const QString type = layerNode.toElement().attribute( u"type"_s );
42
43 if ( type.isNull() )
44 {
45 QgsDebugError( u"cannot find ``type'' attribute"_s );
46
47 return IS_BOGUS;
48 }
49
50 if ( "raster" == type )
51 {
52 QgsDebugMsgLevel( u"is a raster"_s, 2 );
53
54 return IS_RASTER;
55 }
56 else if ( "vector" == type )
57 {
58 QgsDebugMsgLevel( u"is a vector"_s, 2 );
59
60 return IS_VECTOR;
61 }
62
63 QgsDebugMsgLevel( "is unknown type " + type, 2 );
64
65 return IS_BOGUS;
66}
67
68QString QgsProjectBadLayerHandler::dataSource( const QDomNode &layerNode )
69{
70 const QDomNode dataSourceNode = layerNode.namedItem( u"datasource"_s );
71
72 if ( dataSourceNode.isNull() )
73 {
74 QgsDebugError( u"cannot find datasource node"_s );
75
76 return QString();
77 }
78
79 return dataSourceNode.toElement().text();
80}
81
83{
84 // XXX but what about rasters that can be URLs? _Can_ they be URLs?
85
86 switch ( dataType( layerNode ) )
87 {
88 case IS_VECTOR:
89 {
90 const QString ds = dataSource( layerNode );
91
92 QgsDebugMsgLevel( "datasource is " + ds, 2 );
93
94 if ( ds.contains( "host="_L1 ) )
95 {
96 return IS_URL;
97 }
98 else if ( ds.contains( "dbname="_L1 ) )
99 {
100 return IS_DATABASE;
101 }
102 // be default, then, this should be a file based layer data source
103 // XXX is this a reasonable assumption?
104
105 return IS_FILE;
106 }
107
108 case IS_RASTER: // rasters are currently only accessed as
109 // physical files
110 return IS_FILE;
111
112 default:
113 QgsDebugError( u"unknown ``type'' attribute"_s );
114 }
115
116 return IS_Unknown;
117}
118
119void QgsProjectBadLayerHandler::setDataSource( QDomNode &layerNode, const QString &dataSource )
120{
121 const QDomNode dataSourceNode = layerNode.namedItem( u"datasource"_s );
122 const QDomElement dataSourceElement = dataSourceNode.toElement();
123 QDomText dataSourceText = dataSourceElement.firstChild().toText();
124
125 QgsDebugMsgLevel( "datasource changed from " + dataSourceText.data(), 2 );
126
127 dataSourceText.setData( dataSource );
128
129 QgsDebugMsgLevel( "to " + dataSourceText.data(), 2 );
130}
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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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:63
#define QgsDebugError(str)
Definition qgslogger.h:59