1 /***************************************************************************
2   qgsafsdataitemguiprovider.cpp
3   --------------------------------------
4   Date                 : June 2019
5   Copyright            : (C) 2019 by Martin Dobias
6   Email                : wonder dot sk at gmail dot com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 
16 #include "qgsafsdataitemguiprovider.h"
17 #include "qgsafsdataitems.h"
18 #include "qgsafssourceselect.h"
19 #include "qgsmanageconnectionsdialog.h"
20 #include "qgsnewhttpconnection.h"
21 #include "qgsowsconnection.h"
22 
23 #include <QDesktopServices>
24 #include <QFileDialog>
25 #include <QMessageBox>
26 
27 
populateContextMenu(QgsDataItem * item,QMenu * menu,const QList<QgsDataItem * > &,QgsDataItemGuiContext)28 void QgsAfsDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
29 {
30   if ( QgsAfsRootItem *rootItem = qobject_cast< QgsAfsRootItem * >( item ) )
31   {
32     QAction *actionNew = new QAction( tr( "New Connection…" ), menu );
33     connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
34     menu->addAction( actionNew );
35 
36     QAction *actionSaveServers = new QAction( tr( "Save Connections…" ), this );
37     connect( actionSaveServers, &QAction::triggered, this, [] { saveConnections(); } );
38     menu->addAction( actionSaveServers );
39 
40     QAction *actionLoadServers = new QAction( tr( "Load Connections…" ), this );
41     connect( actionLoadServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
42     menu->addAction( actionLoadServers );
43   }
44   else if ( QgsAfsConnectionItem *connectionItem = qobject_cast< QgsAfsConnectionItem * >( item ) )
45   {
46     QAction *actionRefresh = new QAction( tr( "Refresh" ), menu );
47     connect( actionRefresh, &QAction::triggered, this, [connectionItem] { refreshConnection( connectionItem ); } );
48     menu->addAction( actionRefresh );
49 
50     menu->addSeparator();
51 
52     QAction *actionEdit = new QAction( tr( "Edit Connection…" ), menu );
53     connect( actionEdit, &QAction::triggered, this, [connectionItem] { editConnection( connectionItem ); } );
54     menu->addAction( actionEdit );
55 
56     QAction *actionDelete = new QAction( tr( "Delete Connection…" ), menu );
57     connect( actionDelete, &QAction::triggered, this, [connectionItem] { deleteConnection( connectionItem ); } );
58     menu->addAction( actionDelete );
59 
60     QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
61     connect( viewInfo, &QAction::triggered, this, [ = ]
62     {
63       QDesktopServices::openUrl( QUrl( connectionItem->url() ) );
64     } );
65     menu->addAction( viewInfo );
66   }
67   else if ( QgsAfsFolderItem *folderItem = qobject_cast< QgsAfsFolderItem * >( item ) )
68   {
69     QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
70     connect( viewInfo, &QAction::triggered, this, [ = ]
71     {
72       QDesktopServices::openUrl( QUrl( folderItem->path() ) );
73     } );
74     menu->addAction( viewInfo );
75   }
76   else if ( QgsAfsServiceItem *serviceItem = qobject_cast< QgsAfsServiceItem * >( item ) )
77   {
78     QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
79     connect( viewInfo, &QAction::triggered, this, [ = ]
80     {
81       QDesktopServices::openUrl( QUrl( serviceItem->path() ) );
82     } );
83     menu->addAction( viewInfo );
84   }
85   else if ( QgsAfsParentLayerItem *layerItem = qobject_cast< QgsAfsParentLayerItem * >( item ) )
86   {
87     QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
88     connect( viewInfo, &QAction::triggered, this, [ = ]
89     {
90       QDesktopServices::openUrl( QUrl( layerItem->path() ) );
91     } );
92     menu->addAction( viewInfo );
93   }
94   else if ( QgsAfsLayerItem *layerItem = qobject_cast< QgsAfsLayerItem * >( item ) )
95   {
96     QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
97     connect( viewInfo, &QAction::triggered, this, [ = ]
98     {
99       QDesktopServices::openUrl( QUrl( layerItem->path() ) );
100     } );
101     menu->addAction( viewInfo );
102     menu->addSeparator();
103   }
104 }
105 
newConnection(QgsDataItem * item)106 void QgsAfsDataItemGuiProvider::newConnection( QgsDataItem *item )
107 {
108   QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionOther, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), QString(), QgsNewHttpConnection::FlagShowHttpSettings );
109   nc.setWindowTitle( tr( "Create a New ArcGIS Feature Service Connection" ) );
110 
111   if ( nc.exec() )
112   {
113     item->refresh();
114   }
115 }
116 
editConnection(QgsDataItem * item)117 void QgsAfsDataItemGuiProvider::editConnection( QgsDataItem *item )
118 {
119   QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionOther, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), item->name(), QgsNewHttpConnection::FlagShowHttpSettings );
120   nc.setWindowTitle( tr( "Modify ArcGIS Feature Service Connection" ) );
121 
122   if ( nc.exec() )
123   {
124     // the parent should be updated
125     item->refresh();
126     if ( item->parent() )
127       item->parent()->refreshConnections();
128   }
129 }
130 
deleteConnection(QgsDataItem * item)131 void QgsAfsDataItemGuiProvider::deleteConnection( QgsDataItem *item )
132 {
133   if ( QMessageBox::question( nullptr, QObject::tr( "Delete Connection" ),
134                               QObject::tr( "Are you sure you want to delete the connection to %1?" ).arg( item->name() ),
135                               QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
136     return;
137 
138   QgsOwsConnection::deleteConnection( QStringLiteral( "arcgisfeatureserver" ), item->name() );
139 
140   // the parent should be updated
141   if ( item->parent() )
142     item->parent()->refreshConnections();
143 }
144 
refreshConnection(QgsDataItem * item)145 void QgsAfsDataItemGuiProvider::refreshConnection( QgsDataItem *item )
146 {
147   item->refresh();
148   // the parent should be updated
149   if ( item->parent() )
150     item->parent()->refreshConnections();
151 }
152 
saveConnections()153 void QgsAfsDataItemGuiProvider::saveConnections()
154 {
155   QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::ArcgisFeatureServer );
156   dlg.exec();
157 }
158 
loadConnections(QgsDataItem * item)159 void QgsAfsDataItemGuiProvider::loadConnections( QgsDataItem *item )
160 {
161   QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
162                      tr( "XML files (*.xml *.XML)" ) );
163   if ( fileName.isEmpty() )
164   {
165     return;
166   }
167 
168   QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::ArcgisFeatureServer, fileName );
169   if ( dlg.exec() == QDialog::Accepted )
170     item->refreshConnections();
171 }
172