1 /***************************************************************************
2 qgsarcgisrestdataitemguiprovider.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 "qgsarcgisrestdataitemguiprovider.h"
17 #include "qgsarcgisrestdataitems.h"
18 #include "qgsmanageconnectionsdialog.h"
19 #include "qgsnewarcgisrestconnection.h"
20 #include "qgsowsconnection.h"
21
22 #include <QDesktopServices>
23 #include <QFileDialog>
24 #include <QMessageBox>
25
26
populateContextMenu(QgsDataItem * item,QMenu * menu,const QList<QgsDataItem * > &,QgsDataItemGuiContext)27 void QgsArcGisRestDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
28 {
29 if ( QgsArcGisRestRootItem *rootItem = qobject_cast< QgsArcGisRestRootItem * >( item ) )
30 {
31 QAction *actionNew = new QAction( tr( "New Connection…" ), menu );
32 connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
33 menu->addAction( actionNew );
34
35 QAction *actionSaveServers = new QAction( tr( "Save Connections…" ), menu );
36 connect( actionSaveServers, &QAction::triggered, this, [] { saveConnections(); } );
37 menu->addAction( actionSaveServers );
38
39 QAction *actionLoadServers = new QAction( tr( "Load Connections…" ), menu );
40 connect( actionLoadServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
41 menu->addAction( actionLoadServers );
42 }
43 else if ( QgsArcGisRestConnectionItem *connectionItem = qobject_cast< QgsArcGisRestConnectionItem * >( item ) )
44 {
45 QAction *actionRefresh = new QAction( tr( "Refresh" ), menu );
46 connect( actionRefresh, &QAction::triggered, this, [connectionItem] { refreshConnection( connectionItem ); } );
47 menu->addAction( actionRefresh );
48
49 menu->addSeparator();
50
51 QAction *actionEdit = new QAction( tr( "Edit Connection…" ), menu );
52 connect( actionEdit, &QAction::triggered, this, [connectionItem] { editConnection( connectionItem ); } );
53 menu->addAction( actionEdit );
54
55 QAction *actionDelete = new QAction( tr( "Remove Connection…" ), menu );
56 connect( actionDelete, &QAction::triggered, this, [connectionItem] { deleteConnection( connectionItem ); } );
57 menu->addAction( actionDelete );
58
59 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
60 connect( viewInfo, &QAction::triggered, this, [ = ]
61 {
62 QDesktopServices::openUrl( QUrl( connectionItem->url() ) );
63 } );
64 menu->addAction( viewInfo );
65 }
66 else if ( QgsArcGisRestFolderItem *folderItem = qobject_cast< QgsArcGisRestFolderItem * >( item ) )
67 {
68 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
69 connect( viewInfo, &QAction::triggered, this, [ = ]
70 {
71 QDesktopServices::openUrl( QUrl( folderItem->path() ) );
72 } );
73 menu->addAction( viewInfo );
74 }
75 else if ( QgsArcGisFeatureServiceItem *serviceItem = qobject_cast< QgsArcGisFeatureServiceItem * >( item ) )
76 {
77 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
78 connect( viewInfo, &QAction::triggered, this, [ = ]
79 {
80 QDesktopServices::openUrl( QUrl( serviceItem->path() ) );
81 } );
82 menu->addAction( viewInfo );
83 }
84 else if ( QgsArcGisMapServiceItem *serviceItem = qobject_cast< QgsArcGisMapServiceItem * >( item ) )
85 {
86 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
87 connect( viewInfo, &QAction::triggered, this, [ = ]
88 {
89 QDesktopServices::openUrl( QUrl( serviceItem->path() ) );
90 } );
91 menu->addAction( viewInfo );
92 }
93 else if ( QgsArcGisRestParentLayerItem *layerItem = qobject_cast< QgsArcGisRestParentLayerItem * >( item ) )
94 {
95 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
96 connect( viewInfo, &QAction::triggered, this, [ = ]
97 {
98 QDesktopServices::openUrl( QUrl( layerItem->path() ) );
99 } );
100 menu->addAction( viewInfo );
101 }
102 else if ( QgsArcGisFeatureServiceLayerItem *layerItem = qobject_cast< QgsArcGisFeatureServiceLayerItem * >( item ) )
103 {
104 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
105 connect( viewInfo, &QAction::triggered, this, [ = ]
106 {
107 QDesktopServices::openUrl( QUrl( layerItem->path() ) );
108 } );
109 menu->addAction( viewInfo );
110 menu->addSeparator();
111 }
112 else if ( QgsArcGisMapServiceLayerItem *layerItem = qobject_cast< QgsArcGisMapServiceLayerItem * >( item ) )
113 {
114 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
115 connect( viewInfo, &QAction::triggered, this, [ = ]
116 {
117 QDesktopServices::openUrl( QUrl( layerItem->path() ) );
118 } );
119 menu->addAction( viewInfo );
120 menu->addSeparator();
121 }
122 }
123
newConnection(QgsDataItem * item)124 void QgsArcGisRestDataItemGuiProvider::newConnection( QgsDataItem *item )
125 {
126 QgsNewArcGisRestConnectionDialog nc( nullptr, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), QString() );
127 nc.setWindowTitle( tr( "Create a New ArcGIS REST Server Connection" ) );
128
129 if ( nc.exec() )
130 {
131 item->refresh();
132 }
133 }
134
editConnection(QgsDataItem * item)135 void QgsArcGisRestDataItemGuiProvider::editConnection( QgsDataItem *item )
136 {
137 QgsNewArcGisRestConnectionDialog nc( nullptr, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), item->name() );
138 nc.setWindowTitle( tr( "Modify ArcGIS REST Server Connection" ) );
139
140 if ( nc.exec() )
141 {
142 // the parent should be updated
143 item->refresh();
144 if ( item->parent() )
145 item->parent()->refreshConnections();
146 }
147 }
148
deleteConnection(QgsDataItem * item)149 void QgsArcGisRestDataItemGuiProvider::deleteConnection( QgsDataItem *item )
150 {
151 if ( QMessageBox::question( nullptr, QObject::tr( "Remove Connection" ),
152 QObject::tr( "Are you sure you want to remove the connection to %1?" ).arg( item->name() ),
153 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
154 return;
155
156 QgsOwsConnection::deleteConnection( QStringLiteral( "arcgisfeatureserver" ), item->name() );
157
158 // the parent should be updated
159 if ( item->parent() )
160 item->parent()->refreshConnections();
161 }
162
refreshConnection(QgsDataItem * item)163 void QgsArcGisRestDataItemGuiProvider::refreshConnection( QgsDataItem *item )
164 {
165 item->refresh();
166 // the parent should be updated
167 if ( item->parent() )
168 item->parent()->refreshConnections();
169 }
170
saveConnections()171 void QgsArcGisRestDataItemGuiProvider::saveConnections()
172 {
173 QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::ArcgisFeatureServer );
174 dlg.exec();
175 }
176
loadConnections(QgsDataItem * item)177 void QgsArcGisRestDataItemGuiProvider::loadConnections( QgsDataItem *item )
178 {
179 const QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
180 tr( "XML files (*.xml *.XML)" ) );
181 if ( fileName.isEmpty() )
182 {
183 return;
184 }
185
186 QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::ArcgisFeatureServer, fileName );
187 if ( dlg.exec() == QDialog::Accepted )
188 item->refreshConnections();
189 }
190