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