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  ***************************************************************************/
16 #include "qgsprojectbadlayerhandler.h"
17 #include "qgslogger.h"
18 #include "qgsmessagelog.h"
19 #include "qgsapplication.h"
20 
21 #include <QFileInfo>
22 
handleBadLayers(const QList<QDomNode> & layers)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 
dataType(const QDomNode & layerNode)33 QgsProjectBadLayerHandler::DataType QgsProjectBadLayerHandler::dataType( const QDomNode &layerNode )
34 {
35   const 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 
dataSource(const QDomNode & layerNode)62 QString QgsProjectBadLayerHandler::dataSource( const QDomNode &layerNode )
63 {
64   const 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 
providerType(const QDomNode & layerNode)76 QgsProjectBadLayerHandler::ProviderType QgsProjectBadLayerHandler::providerType( const QDomNode &layerNode )
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       const 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 
setDataSource(QDomNode & layerNode,const QString & dataSource)113 void QgsProjectBadLayerHandler::setDataSource( QDomNode &layerNode, const QString &dataSource )
114 {
115   const QDomNode dataSourceNode = layerNode.namedItem( QStringLiteral( "datasource" ) );
116   const 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 }
125