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