QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmurlopener.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmurlopener.cpp
3 ---------------------
4 begin : August 2024
5 copyright : (C) 2024 by Dave Signer
6 email : david at opengis dot ch
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
19
20#include "qgis.h"
22
23#include <QDesktopServices>
24#include <QString>
25#include <QUrl>
26
27#include "moc_qgsalgorithmurlopener.cpp"
28
29using namespace Qt::StringLiterals;
30
32
33QString QgsOpenUrlAlgorithm::name() const
34{
35 return u"openurl"_s;
36}
37
38QString QgsOpenUrlAlgorithm::displayName() const
39{
40 return tr( "Open file or URL" );
41}
42
43QString QgsOpenUrlAlgorithm::shortDescription() const
44{
45 return tr( "Opens files in their default associated application, or URLs in the user's default web browser." );
46}
47
48QStringList QgsOpenUrlAlgorithm::tags() const
49{
50 return tr( "open,url,internet,url,fetch,get,request,https,pdf,file" ).split( ',' );
51}
52
53QString QgsOpenUrlAlgorithm::group() const
54{
55 return tr( "File tools" );
56}
57
58QString QgsOpenUrlAlgorithm::groupId() const
59{
60 return u"filetools"_s;
61}
62
63QString QgsOpenUrlAlgorithm::shortHelpString() const
64{
65 return tr( "This algorithm opens files in their default associated application, or URLs in the user's default web browser." );
66}
67
68QgsOpenUrlAlgorithm *QgsOpenUrlAlgorithm::createInstance() const
69{
70 return new QgsOpenUrlAlgorithm();
71}
72
73void QgsOpenUrlAlgorithm::initAlgorithm( const QVariantMap & )
74{
75 addParameter( new QgsProcessingParameterString( u"URL"_s, tr( "URL or file path" ), QVariant(), false, false ) );
76 addOutput( new QgsProcessingOutputBoolean( u"SUCCESS"_s, QObject::tr( "Successfully performed opening file or URL" ) ) );
77}
78
79QVariantMap QgsOpenUrlAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
80{
81 const QString url = parameterAsString( parameters, u"URL"_s, context );
82 if ( url.isEmpty() )
83 throw QgsProcessingException( tr( "No URL or file path specified" ) );
84 const QUrl qurl = QUrl::fromUserInput( url );
85
86 const bool result = QDesktopServices::openUrl( qurl );
87
88 if ( result )
89 feedback->pushInfo( QObject::tr( "Successfully opened %1" ).arg( url ) );
90 else
91 feedback->reportError( QObject::tr( "Failed opening %1" ).arg( url ) );
92
93 QVariantMap outputs;
94 outputs.insert( u"SUCCESS"_s, result );
95 return outputs;
96}
97
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean output for processing algorithms.
A string parameter for processing algorithms.