1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
* DesktopServicesFeaturePlugin.cpp - implementation of DesktopServicesFeaturePlugin class
*
* Copyright (c) 2017-2019 Tobias Junghans <tobydox@veyon.io>
*
* This file is part of Veyon - http://veyon.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include <QDesktopServices>
#include <QInputDialog>
#include <QUrl>
#include "Computer.h"
#include "ComputerControlInterface.h"
#include "DesktopServicesConfigurationPage.h"
#include "DesktopServicesFeaturePlugin.h"
#include "VeyonMasterInterface.h"
#include "RunProgramDialog.h"
#include "PlatformCoreFunctions.h"
#include "PlatformUserFunctions.h"
DesktopServicesFeaturePlugin::DesktopServicesFeaturePlugin( QObject* parent ) :
QObject( parent ),
m_configuration(),
m_runProgramFeature( Feature::Action | Feature::AllComponents,
Feature::Uid( "da9ca56a-b2ad-4fff-8f8a-929b2927b442" ),
Feature::Uid(),
tr( "Run program" ), QString(),
tr( "Click this button to run a program on all computers." ),
QStringLiteral(":/desktopservices/preferences-desktop-launch-feedback.png") ),
m_openWebsiteFeature( Feature::Action | Feature::AllComponents,
Feature::Uid( "8a11a75d-b3db-48b6-b9cb-f8422ddd5b0c" ),
Feature::Uid(),
tr( "Open website" ), QString(),
tr( "Click this button to open a website on all computers." ),
QStringLiteral(":/desktopservices/internet-web-browser.png") ),
m_predefinedProgramsFeatures( predefinedPrograms() ),
m_predefinedWebsitesFeatures( predefinedWebsites() ),
m_features( FeatureList( { m_runProgramFeature, m_openWebsiteFeature } ) + m_predefinedProgramsFeatures + m_predefinedWebsitesFeatures )
{
}
bool DesktopServicesFeaturePlugin::startFeature( VeyonMasterInterface& master, const Feature& feature,
const ComputerControlInterfaceList& computerControlInterfaces )
{
if( m_features.contains( feature ) == false )
{
return false;
}
if( feature == m_runProgramFeature )
{
RunProgramDialog runProgramDialog( master.mainWindow() );
if( runProgramDialog.exec() == QDialog::Accepted &&
runProgramDialog.programs().isEmpty() == false )
{
sendFeatureMessage( FeatureMessage( feature.uid(), FeatureMessage::DefaultCommand ).
addArgument( ProgramsArgument, runProgramDialog.programs() ), computerControlInterfaces );
}
}
else if( feature == m_openWebsiteFeature )
{
const auto urlString = QInputDialog::getText( master.mainWindow(),
tr( "Open website" ),
tr( "Please enter the URL of the website to open:" ) );
if( urlString.isEmpty() == false )
{
sendFeatureMessage( FeatureMessage( feature.uid(), FeatureMessage::DefaultCommand ).
addArgument( WebsiteUrlArgument, urlString ), computerControlInterfaces );
}
}
else if( m_predefinedProgramsFeatures.contains( feature ) )
{
sendFeatureMessage( FeatureMessage( m_runProgramFeature.uid(), FeatureMessage::DefaultCommand ).
addArgument( ProgramsArgument, predefinedServicePath( feature.uid() ) ), computerControlInterfaces );
}
else if( m_predefinedWebsitesFeatures.contains( feature ) )
{
sendFeatureMessage( FeatureMessage( m_openWebsiteFeature.uid(), FeatureMessage::DefaultCommand ).
addArgument( WebsiteUrlArgument, predefinedServicePath( feature.uid() ) ), computerControlInterfaces );
}
return true;
}
bool DesktopServicesFeaturePlugin::stopFeature( VeyonMasterInterface& master, const Feature& feature,
const ComputerControlInterfaceList& computerControlInterfaces )
{
Q_UNUSED(master);
Q_UNUSED(feature);
Q_UNUSED(computerControlInterfaces);
return false;
}
bool DesktopServicesFeaturePlugin::handleFeatureMessage( VeyonMasterInterface& master, const FeatureMessage& message,
ComputerControlInterface::Pointer computerControlInterface )
{
Q_UNUSED(master);
Q_UNUSED(message);
Q_UNUSED(computerControlInterface);
return false;
}
bool DesktopServicesFeaturePlugin::handleFeatureMessage( VeyonServerInterface& server, const FeatureMessage& message )
{
Q_UNUSED(server);
if( message.featureUid() == m_runProgramFeature.uid() )
{
const auto programs = message.argument( ProgramsArgument ).toStringList();
for( const auto& program : programs )
{
runProgramAsUser( program );
}
}
else if( message.featureUid() == m_openWebsiteFeature.uid() )
{
openWebsite( message.argument( WebsiteUrlArgument ).toString() );
}
else
{
return false;
}
return true;
}
bool DesktopServicesFeaturePlugin::handleFeatureMessage( VeyonWorkerInterface& worker, const FeatureMessage& message )
{
Q_UNUSED(worker);
Q_UNUSED(message);
return false;
}
ConfigurationPage* DesktopServicesFeaturePlugin::createConfigurationPage()
{
return new DesktopServicesConfigurationPage( m_configuration );
}
void DesktopServicesFeaturePlugin::runProgramAsUser( const QString& commandLine )
{
qDebug() << "DesktopServicesFeaturePlugin::runProgramAsUser(): launching" << commandLine;
QString program;
QStringList parameters;
// parse command line format "C:\Program Files\..." -foo -bar
if( commandLine.startsWith( '"' ) && commandLine.count( '"' ) > 1 )
{
const auto commandLineSplit = commandLine.split( '"' );
program = commandLineSplit.value( 1 );
parameters = commandLine.mid( program.size() + 2 ).split( ' ' );
}
// parse command line format program.exe -foo -bar
else if( commandLine.contains( ' ' ) )
{
const auto commandLineSplit = commandLine.split( ' ' );
program = commandLineSplit.first();
parameters = commandLineSplit.mid( 1 );
}
else
{
// no arguments so use command line as program name
program = commandLine;
}
VeyonCore::platform().coreFunctions().runProgramAsUser( program, parameters,
VeyonCore::platform().userFunctions().currentUser(),
VeyonCore::platform().coreFunctions().activeDesktopName() );
}
bool DesktopServicesFeaturePlugin::openWebsite( const QString& urlString )
{
QUrl url( urlString, QUrl::TolerantMode );
if( url.scheme().isEmpty() )
{
url = QUrl( QStringLiteral("http://") + urlString, QUrl::TolerantMode );
}
if( url.isEmpty() || url.isValid() == false )
{
return false;
}
if( QDesktopServices::openUrl( url ) == false )
{
qWarning() << "DesktopServicesFeaturePlugin: could not open URL" << url
<< "via QDesktopServices - trying native generic URL handler";
runProgramAsUser( QStringLiteral("%1 %2").arg(
VeyonCore::platform().coreFunctions().genericUrlHandler(),
url.toString() ) );
}
return true;
}
FeatureList DesktopServicesFeaturePlugin::predefinedPrograms() const
{
FeatureList programFeatures;
const auto programs = m_configuration.predefinedPrograms();
if( programs.size() > 0 )
{
programFeatures.reserve( programs.size() + 1 );
for( const auto program : programs )
{
const auto programObject = DesktopServiceObject( program.toObject() );
programFeatures.append( Feature( Feature::Action | Feature::Master, programObject.uid(), m_runProgramFeature.uid(),
programObject.name(), QString(), tr("Run program \"%1\"").arg( programObject.name() ) ) );
}
auto primaryFeature = m_runProgramFeature;
primaryFeature.setParentUid( m_runProgramFeature.uid() );
primaryFeature.setDisplayName( tr("Custom program") );
programFeatures.append( primaryFeature );
}
return programFeatures;
}
FeatureList DesktopServicesFeaturePlugin::predefinedWebsites() const
{
FeatureList websiteFeatures;
const auto websites = m_configuration.predefinedWebsites();
if( websites.size() > 0 )
{
websiteFeatures.reserve( websites.size() + 1 );
for( const auto website : websites )
{
const auto websiteObject = DesktopServiceObject( website.toObject() );
websiteFeatures.append( Feature( Feature::Action | Feature::Master, websiteObject.uid(), m_openWebsiteFeature.uid(),
websiteObject.name(), QString(),
tr("Open website \"%1\"").arg( websiteObject.name() ) ) );
}
auto primaryFeature = m_openWebsiteFeature;
primaryFeature.setParentUid( m_openWebsiteFeature.uid() );
primaryFeature.setDisplayName( tr("Custom website") );
websiteFeatures.append( primaryFeature );
}
return websiteFeatures;
}
QString DesktopServicesFeaturePlugin::predefinedServicePath( Feature::Uid subFeatureUid ) const
{
for( const auto& services : { m_configuration.predefinedPrograms(), m_configuration.predefinedWebsites() } )
{
for( const auto service : services )
{
const auto serviceObject = DesktopServiceObject( service.toObject() );
if( serviceObject.uid() == subFeatureUid )
{
return serviceObject.path();
}
}
}
return QString();
}
|