QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 }
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 QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugMsg(str)
Definition: qgslogger.h:38