QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  if ( !layers.empty() )
26  QgsMessageLog::logMessage( QObject::tr( "%n unavailable layer(s) found:", nullptr, layers.size() ) );
27 
28  for ( const QDomNode &layer : layers )
29  {
30  QgsMessageLog::logMessage( QObject::tr( " * %1" ).arg( dataSource( layer ) ) );
31  }
32 }
33 
35 {
36  const QString type = layerNode.toElement().attribute( QStringLiteral( "type" ) );
37 
38  if ( type.isNull() )
39  {
40  QgsDebugMsg( QStringLiteral( "cannot find ``type'' attribute" ) );
41 
42  return IS_BOGUS;
43  }
44 
45  if ( "raster" == type )
46  {
47  QgsDebugMsgLevel( QStringLiteral( "is a raster" ), 2 );
48 
49  return IS_RASTER;
50  }
51  else if ( "vector" == type )
52  {
53  QgsDebugMsgLevel( QStringLiteral( "is a vector" ), 2 );
54 
55  return IS_VECTOR;
56  }
57 
58  QgsDebugMsgLevel( "is unknown type " + type, 2 );
59 
60  return IS_BOGUS;
61 }
62 
63 QString QgsProjectBadLayerHandler::dataSource( const QDomNode &layerNode )
64 {
65  const QDomNode dataSourceNode = layerNode.namedItem( QStringLiteral( "datasource" ) );
66 
67  if ( dataSourceNode.isNull() )
68  {
69  QgsDebugMsg( QStringLiteral( "cannot find datasource node" ) );
70 
71  return QString();
72  }
73 
74  return dataSourceNode.toElement().text();
75 }
76 
78 {
79  // XXX but what about rasters that can be URLs? _Can_ they be URLs?
80 
81  switch ( dataType( layerNode ) )
82  {
83  case IS_VECTOR:
84  {
85  const QString ds = dataSource( layerNode );
86 
87  QgsDebugMsgLevel( "datasource is " + ds, 2 );
88 
89  if ( ds.contains( QLatin1String( "host=" ) ) )
90  {
91  return IS_URL;
92  }
93  else if ( ds.contains( QLatin1String( "dbname=" ) ) )
94  {
95  return IS_DATABASE;
96  }
97  // be default, then, this should be a file based layer data source
98  // XXX is this a reasonable assumption?
99 
100  return IS_FILE;
101  }
102 
103  case IS_RASTER: // rasters are currently only accessed as
104  // physical files
105  return IS_FILE;
106 
107  default:
108  QgsDebugMsg( QStringLiteral( "unknown ``type'' attribute" ) );
109  }
110 
111  return IS_Unknown;
112 }
113 
114 void QgsProjectBadLayerHandler::setDataSource( QDomNode &layerNode, const QString &dataSource )
115 {
116  const QDomNode dataSourceNode = layerNode.namedItem( QStringLiteral( "datasource" ) );
117  const QDomElement dataSourceElement = dataSourceNode.toElement();
118  QDomText dataSourceText = dataSourceElement.firstChild().toText();
119 
120  QgsDebugMsgLevel( "datasource changed from " + dataSourceText.data(), 2 );
121 
122  dataSourceText.setData( dataSource );
123 
124  QgsDebugMsgLevel( "to " + dataSourceText.data(), 2 );
125 }
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsProjectBadLayerHandler::DataType
DataType
file data representation
Definition: qgsprojectbadlayerhandler.h:63
QgsProjectBadLayerHandler::setDataSource
void setDataSource(QDomNode &layerNode, const QString &dataSource)
Set the datasource element to the new value.
Definition: qgsprojectbadlayerhandler.cpp:114
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsProjectBadLayerHandler::IS_VECTOR
@ IS_VECTOR
Vector data.
Definition: qgsprojectbadlayerhandler.h:65
QgsProjectBadLayerHandler::IS_FILE
@ IS_FILE
Saved in a file.
Definition: qgsprojectbadlayerhandler.h:73
QgsProjectBadLayerHandler::IS_Unknown
@ IS_Unknown
Unknown type.
Definition: qgsprojectbadlayerhandler.h:76
QgsProjectBadLayerHandler::dataType
DataType dataType(const QDomNode &layerNode)
Returns data type associated with the given QgsProject file Dom node.
Definition: qgsprojectbadlayerhandler.cpp:34
QgsProjectBadLayerHandler::ProviderType
ProviderType
the flavors for data storage
Definition: qgsprojectbadlayerhandler.h:71
qgsapplication.h
QgsProjectBadLayerHandler::handleBadLayers
virtual void handleBadLayers(const QList< QDomNode > &layers)
This method will be called whenever the project tries to load layers which cannot be accessed.
Definition: qgsprojectbadlayerhandler.cpp:23
qgsprojectbadlayerhandler.h
QgsProjectBadLayerHandler::IS_DATABASE
@ IS_DATABASE
Saved in a database.
Definition: qgsprojectbadlayerhandler.h:74
QgsMessageLog::logMessage
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).
Definition: qgsmessagelog.cpp:27
QgsProjectBadLayerHandler::dataSource
QString dataSource(const QDomNode &layerNode)
Returns the data source for the given layer.
Definition: qgsprojectbadlayerhandler.cpp:63
QgsProjectBadLayerHandler::IS_RASTER
@ IS_RASTER
Raster data.
Definition: qgsprojectbadlayerhandler.h:66
QgsProjectBadLayerHandler::IS_URL
@ IS_URL
Retrieved from a URL.
Definition: qgsprojectbadlayerhandler.h:75
QgsProjectBadLayerHandler::IS_BOGUS
@ IS_BOGUS
Bogus data.
Definition: qgsprojectbadlayerhandler.h:67
QgsProjectBadLayerHandler::providerType
ProviderType providerType(const QDomNode &layerNode)
Returns the physical storage type associated with the given layer.
Definition: qgsprojectbadlayerhandler.cpp:77
qgslogger.h
qgsmessagelog.h