00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (C) 2010 Dan Leinir Turthra Jensen <admin@leinir.dk> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 #include "scriptingasset.h" 00021 00022 #include "scriptingengine.h" 00023 00024 #include <QtCore/QFile> 00025 #include <QtCore/QMimeData> 00026 00027 REGISTER_OBJECTTYPE( GluonEngine, ScriptingAsset ); 00028 00029 namespace GluonEngine 00030 { 00031 class ScriptingAsset::Private 00032 { 00033 public: 00034 Private() {}; 00035 00036 QString script; 00037 }; 00038 } 00039 00040 using namespace GluonEngine; 00041 00042 ScriptingAsset::ScriptingAsset( QObject* parent ) 00043 : Asset( parent ) 00044 , d( new Private ) 00045 { 00046 } 00047 00048 ScriptingAsset::~ScriptingAsset() 00049 { 00050 ScriptingEngine::instance()->unregisterAsset( this ); 00051 delete d; 00052 } 00053 00054 const QStringList 00055 ScriptingAsset::supportedMimeTypes() const 00056 { 00057 QStringList mime; 00058 00059 mime << "application/javascript"; 00060 mime << "text/plain"; 00061 00062 return mime; 00063 } 00064 00065 void 00066 ScriptingAsset::setFile( const QUrl& newFile ) 00067 { 00068 DEBUG_FUNC_NAME 00069 00070 ScriptingEngine::instance()->unregisterAsset( this ); 00071 00072 QFile script( newFile.path() ); 00073 if( script.open( QIODevice::ReadOnly ) ) 00074 { 00075 d->script = script.readAll(); 00076 mimeData()->setText( d->script ); 00077 } 00078 // Don't attempt to do anything if the script is empty 00079 if( d->script.isEmpty() ) 00080 return; 00081 00082 QScriptSyntaxCheckResult result = ScriptingEngine::instance()->registerAsset( this ); 00083 if( result.state() != QScriptSyntaxCheckResult::Valid ) 00084 debug( tr( "Script error %1 (%2,%3): %4" ).arg( fullyQualifiedName() ).arg( result.errorLineNumber() ).arg( result.errorColumnNumber() ).arg( result.errorMessage() ) ); 00085 00086 GluonEngine::Asset::setFile( newFile ); 00087 } 00088 00089 const QList<AssetTemplate*> 00090 ScriptingAsset::templates() 00091 { 00092 QList<AssetTemplate*> templates; 00093 templates.append( new AssetTemplate( "Scripted Logic", "scripting_template.js", "scripting", this ) ); 00094 return templates; 00095 } 00096 00097 QString 00098 ScriptingAsset::className() const 00099 { 00100 return ScriptingEngine::instance()->className( this ); 00101 } 00102 00103 Q_EXPORT_PLUGIN2( gluon_component_scripting, GluonEngine::ScriptingAsset ) 00104 00105 #include "scriptingasset.moc"