1 /***************************************************************************
2 qgsdatacollectionitem.cpp
3 -------------------
4 begin : 2011-04-01
5 copyright : (C) 2011 Radim Blazek
6 email : radim dot blazek at gmail dot com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "qgsdatacollectionitem.h"
19 #include "qgsapplication.h"
20 #include "qgsdataitemproviderregistry.h"
21 #include "qgsprovidermetadata.h"
22 #include "qgsproviderregistry.h"
23 #include "qgsabstractdatabaseproviderconnection.h"
24 #include <QRegularExpression>
25
QgsDataCollectionItem(QgsDataItem * parent,const QString & name,const QString & path,const QString & providerKey)26 QgsDataCollectionItem::QgsDataCollectionItem( QgsDataItem *parent,
27 const QString &name,
28 const QString &path,
29 const QString &providerKey )
30 : QgsDataItem( Qgis::BrowserItemType::Collection, parent, name, path, providerKey )
31 {
32 mCapabilities = Qgis::BrowserItemCapability::Fertile;
33 mIconName = QStringLiteral( "/mIconDbSchema.svg" );
34 }
35
~QgsDataCollectionItem()36 QgsDataCollectionItem::~QgsDataCollectionItem()
37 {
38 QgsDebugMsgLevel( "mName = " + mName + " mPath = " + mPath, 2 );
39
40 // Do not delete children, children are deleted by QObject parent
41 #if 0
42 const auto constMChildren = mChildren;
43 for ( QgsDataItem *i : constMChildren )
44 {
45 QgsDebugMsgLevel( QStringLiteral( "delete child = 0x%0" ).arg( static_cast<qlonglong>( i ), 8, 16, QLatin1Char( '0' ) ), 2 );
46 delete i;
47 }
48 #endif
49 }
50
iconDataCollection()51 QIcon QgsDataCollectionItem::iconDataCollection()
52 {
53 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconDbSchema.svg" ) );
54 }
55
openDirIcon(const QColor & fillColor,const QColor & strokeColor)56 QIcon QgsDataCollectionItem::openDirIcon( const QColor &fillColor, const QColor &strokeColor )
57 {
58 return fillColor.isValid() || strokeColor.isValid()
59 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpenParams.svg" ), fillColor, strokeColor )
60 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpen.svg" ) );
61 }
62
homeDirIcon(const QColor & fillColor,const QColor & strokeColor)63 QIcon QgsDataCollectionItem::homeDirIcon( const QColor &fillColor, const QColor &strokeColor )
64 {
65 return fillColor.isValid() || strokeColor.isValid()
66 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHomeParams.svg" ), fillColor, strokeColor )
67 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHome.svg" ) );
68 }
69
databaseConnection() const70 QgsAbstractDatabaseProviderConnection *QgsDataCollectionItem::databaseConnection() const
71 {
72 const QString dataProviderKey { QgsApplication::dataItemProviderRegistry()->dataProviderKey( providerKey() ) };
73 QgsProviderMetadata *md { QgsProviderRegistry::instance()->providerMetadata( dataProviderKey ) };
74
75 if ( ! md )
76 {
77 return nullptr;
78 }
79
80 const QString connectionName { name() };
81
82 try
83 {
84 // First try to retrieve the connection by name if this is a stored connection
85 if ( md->findConnection( connectionName ) )
86 {
87 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionName ) );
88 }
89
90 // If that fails, try to create a connection from the path, in case this is a
91 // filesystem-based DB (gpkg or spatialite)
92 // The name is useless, we need to get the file path from the data item path
93 const QString databaseFilePath { path().remove( QRegularExpression( R"re([\aZ]{2,}://)re" ) ) };
94
95 if ( QFile::exists( databaseFilePath ) )
96 {
97 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( databaseFilePath, {} ) );
98 }
99 }
100 catch ( QgsProviderConnectionException & )
101 {
102 // This is expected and it is not an error in case the provider does not implement
103 // the connections API
104 }
105 return nullptr;
106 }
107
iconDir(const QColor & fillColor,const QColor & strokeColor)108 QIcon QgsDataCollectionItem::iconDir( const QColor &fillColor, const QColor &strokeColor )
109 {
110 return fillColor.isValid() || strokeColor.isValid()
111 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderParams.svg" ), fillColor, strokeColor )
112 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolder.svg" ) );
113 }
114
115
116
117