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