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