00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "savable.h"
00022
00023 #include <core/gluonobject.h>
00024 #include <core/debughelper.h>
00025
00026 #include <QtCore/QFile>
00027 #include <QtCore/QTextStream>
00028 #include <QtCore/QDir>
00029 #include <QtCore/QVariant>
00030
00031 using namespace GluonEngine;
00032
00033 bool
00034 Savable::saveToFile( GluonCore::GluonObject* object )
00035 {
00036 DEBUG_BLOCK
00037 if( !object )
00038 {
00039 DEBUG_TEXT( "Asset was NULL!" );
00040 return false;
00041 }
00042
00043 Savable* savableObject = dynamic_cast<Savable*>( object );
00044 if( !savableObject )
00045 {
00046 DEBUG_TEXT( "Attempted to save an object which does not inherit Savable!" );
00047 return false;
00048 }
00049
00050 if( !savableObject->savableDirty )
00051 {
00052 DEBUG_TEXT( "Trying to save an un-dirty object. This is technically possible, but makes little sense. So - let's not and say we did, shall we?" );
00053 return true;
00054 }
00055
00056
00057 if( object->property( "file" ).value<QUrl>().isEmpty() )
00058 object->setProperty( "file", QVariant::fromValue<QUrl>( QUrl( QString( "Scenes/%1.gdl" ).arg( object->fullyQualifiedFileName() ) ) ) );
00059
00060
00061 if( !QDir::current().exists( "Scenes" ) )
00062 QDir::current().mkdir( "Scenes" );
00063
00064
00065 QFile* savableFile = new QFile( object->property( "file" ).value<QUrl>().toLocalFile() );
00066 if( !savableFile->open( QIODevice::WriteOnly ) )
00067 {
00068 DEBUG_TEXT( QString( "Could not write to file %1" ).arg( object->property( "file" ).value<QUrl>().toString() ) )
00069 return false;
00070 }
00071
00072 QTextStream fileWriter( savableFile );
00073 fileWriter << savableObject->contentsToGDL();
00074 savableFile->close();
00075
00076 delete savableFile;
00077
00078
00079 if( savableObject )
00080 savableObject->savableDirty = false;
00081 return true;
00082 }