QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgsalgorithmurlopener.cpp"
21#include "qgis.h"
22
23#include <QUrl>
24#include <QDesktopServices>
25
27
28QString QgsOpenUrlAlgorithm::name() const
29{
30 return QStringLiteral( "openurl" );
31}
32
33QString QgsOpenUrlAlgorithm::displayName() const
34{
35 return tr( "Open file or URL" );
36}
37
38QString QgsOpenUrlAlgorithm::shortDescription() const
39{
40 return tr( "Opens files in their default associated application, or URLs in the user's default web browser." );
41}
42
43QStringList QgsOpenUrlAlgorithm::tags() const
44{
45 return tr( "open,url,internet,url,fetch,get,request,https,pdf,file" ).split( ',' );
46}
47
48QString QgsOpenUrlAlgorithm::group() const
49{
50 return tr( "File tools" );
51}
52
53QString QgsOpenUrlAlgorithm::groupId() const
54{
55 return QStringLiteral( "filetools" );
56}
57
58QString QgsOpenUrlAlgorithm::shortHelpString() const
59{
60 return tr( "This algorithm opens files in their default associated application, or URLs in the user's default web browser." );
61}
62
63QgsOpenUrlAlgorithm *QgsOpenUrlAlgorithm::createInstance() const
64{
65 return new QgsOpenUrlAlgorithm();
66}
67
68void QgsOpenUrlAlgorithm::initAlgorithm( const QVariantMap & )
69{
70 addParameter( new QgsProcessingParameterString( QStringLiteral( "URL" ), tr( "URL or file path" ), QVariant(), false, false ) );
71 addOutput( new QgsProcessingOutputBoolean( QStringLiteral( "SUCCESS" ), QObject::tr( "Successfully performed opening file or URL" ) ) );
72}
73
74QVariantMap QgsOpenUrlAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75{
76 const QString url = parameterAsString( parameters, QStringLiteral( "URL" ), context );
77 if ( url.isEmpty() )
78 throw QgsProcessingException( tr( "No URL or file path specified" ) );
79 const QUrl qurl = QUrl::fromUserInput( url );
80
81 const bool result = QDesktopServices::openUrl( qurl );
82
83 if ( result )
84 feedback->pushInfo( QObject::tr( "Successfully opened %1" ).arg( url ) );
85 else
86 feedback->reportError( QObject::tr( "Failed opening %1" ).arg( url ) );
87
88 QVariantMap outputs;
89 outputs.insert( QStringLiteral( "SUCCESS" ), result );
90 return outputs;
91}
92
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.