1 /***************************************************************************
2 qgsamsdataitemguiprovider.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 "qgsamsdataitemguiprovider.h"
17 #include "qgsamsdataitems.h"
18 #include "qgsmanageconnectionsdialog.h"
19 #include "qgsnewhttpconnection.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 QgsAmsDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
28 {
29 if ( QgsAmsRootItem *rootItem = qobject_cast< QgsAmsRootItem * >( 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…" ), this );
36 connect( actionSaveServers, &QAction::triggered, this, [] { saveConnections(); } );
37 menu->addAction( actionSaveServers );
38
39 QAction *actionLoadServers = new QAction( tr( "Load Connections…" ), this );
40 connect( actionLoadServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
41 menu->addAction( actionLoadServers );
42 }
43 if ( QgsAmsConnectionItem *connectionItem = qobject_cast< QgsAmsConnectionItem * >( 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( "Delete 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 ( QgsAmsFolderItem *folderItem = qobject_cast< QgsAmsFolderItem * >( 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 ( QgsAmsServiceItem *serviceItem = qobject_cast< QgsAmsServiceItem * >( 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 ( QgsAmsLayerItem *layerItem = qobject_cast< QgsAmsLayerItem * >( item ) )
85 {
86 QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
87 connect( viewInfo, &QAction::triggered, this, [ = ]
88 {
89 QDesktopServices::openUrl( QUrl( layerItem->path() ) );
90 } );
91 menu->addAction( viewInfo );
92 menu->addSeparator();
93 }
94 }
95
newConnection(QgsDataItem * item)96 void QgsAmsDataItemGuiProvider::newConnection( QgsDataItem *item )
97 {
98 QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionOther, QStringLiteral( "qgis/connections-arcgismapserver/" ), QString(), QgsNewHttpConnection::FlagShowHttpSettings );
99 nc.setWindowTitle( tr( "Create a New ArcGIS Map Service Connection" ) );
100
101 if ( nc.exec() )
102 {
103 item->refreshConnections();
104 }
105 }
106
editConnection(QgsDataItem * item)107 void QgsAmsDataItemGuiProvider::editConnection( QgsDataItem *item )
108 {
109 QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionOther, QStringLiteral( "qgis/connections-arcgismapserver/" ), item->name(), QgsNewHttpConnection::FlagShowHttpSettings );
110 nc.setWindowTitle( tr( "Modify ArcGIS Map Service Connection" ) );
111
112 if ( nc.exec() )
113 {
114 item->refresh();
115 if ( item->parent() )
116 item->parent()->refreshConnections();
117 }
118 }
119
deleteConnection(QgsDataItem * item)120 void QgsAmsDataItemGuiProvider::deleteConnection( QgsDataItem *item )
121 {
122 if ( QMessageBox::question( nullptr, QObject::tr( "Delete Connection" ),
123 QObject::tr( "Are you sure you want to delete the connection to %1?" ).arg( item->name() ),
124 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
125 return;
126
127 QgsOwsConnection::deleteConnection( QStringLiteral( "arcgismapserver" ), item->name() );
128 if ( item->parent() )
129 item->parent()->refreshConnections();
130 }
131
refreshConnection(QgsDataItem * item)132 void QgsAmsDataItemGuiProvider::refreshConnection( QgsDataItem *item )
133 {
134 item->refresh();
135 // the parent should be updated
136 if ( item->parent() )
137 item->parent()->refreshConnections();
138 }
139
saveConnections()140 void QgsAmsDataItemGuiProvider::saveConnections()
141 {
142 QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::ArcgisMapServer );
143 dlg.exec();
144 }
145
loadConnections(QgsDataItem * item)146 void QgsAmsDataItemGuiProvider::loadConnections( QgsDataItem *item )
147 {
148 QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
149 tr( "XML files (*.xml *.XML)" ) );
150 if ( fileName.isEmpty() )
151 {
152 return;
153 }
154
155 QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::ArcgisMapServer, fileName );
156 if ( dlg.exec() == QDialog::Accepted )
157 item->refreshConnections();
158 }
159