QGIS API Documentation  3.0.2-Girona (307d082)
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 : matthias@opengis.ch
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 bad layers dismissed:" ).arg( layers.size() ) );
26  Q_FOREACH ( const QDomNode &layer, layers )
27  {
28  QgsApplication::messageLog()->logMessage( QObject::tr( " * %1" ).arg( dataSource( layer ) ) );
29  }
30 }
31 
33 {
34  QString type = layerNode.toElement().attribute( QStringLiteral( "type" ) );
35 
36  if ( type.isNull() )
37  {
38  QgsDebugMsg( "cannot find ``type'' attribute" );
39 
40  return IS_BOGUS;
41  }
42 
43  if ( "raster" == type )
44  {
45  QgsDebugMsg( "is a raster" );
46 
47  return IS_RASTER;
48  }
49  else if ( "vector" == type )
50  {
51  QgsDebugMsg( "is a vector" );
52 
53  return IS_VECTOR;
54  }
55 
56  QgsDebugMsg( "is unknown type " + type );
57 
58  return IS_BOGUS;
59 }
60 
61 QString QgsProjectBadLayerHandler::dataSource( const QDomNode &layerNode )
62 {
63  QDomNode dataSourceNode = layerNode.namedItem( QStringLiteral( "datasource" ) );
64 
65  if ( dataSourceNode.isNull() )
66  {
67  QgsDebugMsg( "cannot find datasource node" );
68 
69  return QString();
70  }
71 
72  return dataSourceNode.toElement().text();
73 }
74 
76 {
77  // XXX but what about rasters that can be URLs? _Can_ they be URLs?
78 
79  switch ( dataType( layerNode ) )
80  {
81  case IS_VECTOR:
82  {
83  QString ds = dataSource( layerNode );
84 
85  QgsDebugMsg( "datasource is " + ds );
86 
87  if ( ds.contains( QLatin1String( "host=" ) ) )
88  {
89  return IS_URL;
90  }
91  else if ( ds.contains( QLatin1String( "dbname=" ) ) )
92  {
93  return IS_DATABASE;
94  }
95  // be default, then, this should be a file based layer data source
96  // XXX is this a reasonable assumption?
97 
98  return IS_FILE;
99  }
100 
101  case IS_RASTER: // rasters are currently only accessed as
102  // physical files
103  return IS_FILE;
104 
105  default:
106  QgsDebugMsg( "unknown ``type'' attribute" );
107  }
108 
109  return IS_Unknown;
110 }
111 
112 void QgsProjectBadLayerHandler::setDataSource( QDomNode &layerNode, const QString &dataSource )
113 {
114  QDomNode dataSourceNode = layerNode.namedItem( QStringLiteral( "datasource" ) );
115  QDomElement dataSourceElement = dataSourceNode.toElement();
116  QDomText dataSourceText = dataSourceElement.firstChild().toText();
117 
118  QgsDebugMsg( "datasource changed from " + dataSourceText.data() );
119 
120  dataSourceText.setData( dataSource );
121 
122  QgsDebugMsg( "to " + dataSourceText.data() );
123 }
DataType
file data representation
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void setDataSource(QDomNode &layerNode, const QString &dataSource)
Set the datasource element to the new value.
ProviderType
the flavors for data storage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
static QgsMessageLog * messageLog()
Returns the application&#39;s message log.
DataType dataType(const QDomNode &layerNode)
Returns data type associated with the given QgsProject file Dom node.
QString dataSource(const QDomNode &layerNode)
Return the data source for the given layer.
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)
Return the physical storage type associated with the given layer.