00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "fileio.h"
00023 #include <QtCore/QFile>
00024 #include "kjseglobal.h"
00025 #include <QtCore/QTemporaryFile>
00026 #include <QtCore/QDebug>
00027
00028 using namespace KJSEmbed;
00029
00030 FileIOBinding::FileIOBinding( KJS::ExecState *exec, QFile *value )
00031 : ObjectBinding(exec, "File", value )
00032 {
00033 StaticBinding::publish( exec, this, FileIO::methods() );
00034 StaticBinding::publish( exec, this, ObjectFactory::methods() );
00035 StaticBinding::publish( exec, this, VariantFactory::methods() );
00036 }
00037
00038 START_OBJECT_METHOD( callFileOpen, QFile )
00039 result = KJS::jsBoolean( object->open( (QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)));
00040 END_OBJECT_METHOD
00041
00042 START_OBJECT_METHOD( callFileClose, QFile )
00043 object->close();
00044 END_OBJECT_METHOD
00045
00046 START_OBJECT_METHOD( callFileReadLine, QFile )
00047 result = KJS::jsString( object->readLine().data() );
00048 END_OBJECT_METHOD
00049
00050 START_OBJECT_METHOD( callFileReadAll, QFile )
00051 result = KJS::jsString( object->readAll().data() );
00052 END_OBJECT_METHOD
00053
00054 START_OBJECT_METHOD( callFileWriteLine, QFile )
00055 result = KJS::jsNumber( (long int)object->write(KJSEmbed::extractQByteArray(exec, args, 0) + '\n') );
00056 END_OBJECT_METHOD
00057
00058 START_OBJECT_METHOD( callFileAtEnd, QFile )
00059 result = KJS::jsBoolean(object->atEnd());
00060 END_OBJECT_METHOD
00061
00062 START_STATIC_OBJECT_METHOD( callOpenFile )
00063 QFile *file = new QFile( KJSEmbed::extractQString( exec, args, 0) );
00064 if( file->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)))
00065 {
00066 return new KJSEmbed::FileIOBinding( exec, file );
00067 }
00068 delete file;
00069 KJS::throwError(exec, KJS::TypeError, i18n("Could not open file '%1'", KJSEmbed::extractQString( exec, args, 0)));
00070 return KJS::jsNull();
00071 END_STATIC_OBJECT_METHOD
00072
00073 START_STATIC_OBJECT_METHOD( callRemoveFile )
00074 return KJS::jsBoolean( QFile::remove(KJSEmbed::extractQString( exec, args, 0) ));
00075 END_STATIC_OBJECT_METHOD
00076
00077 START_STATIC_OBJECT_METHOD( callCopyFile )
00078 return KJS::jsBoolean( QFile::copy(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ));
00079 END_STATIC_OBJECT_METHOD
00080
00081 START_STATIC_OBJECT_METHOD( callMoveFile )
00082 if( QFile::copy(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ) )
00083 return KJS::jsBoolean( QFile::remove(KJSEmbed::extractQString( exec, args, 0) ) );
00084 return KJS::jsBoolean(false);
00085 END_STATIC_OBJECT_METHOD
00086
00087 START_STATIC_OBJECT_METHOD( callLinkFile )
00088 return KJS::jsBoolean( QFile::link(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ));
00089 END_STATIC_OBJECT_METHOD
00090
00091 START_STATIC_OBJECT_METHOD( callExistsFile )
00092 return KJS::jsBoolean( QFile::exists(KJSEmbed::extractQString( exec, args, 0) ));
00093 END_STATIC_OBJECT_METHOD
00094
00095 START_STATIC_OBJECT_METHOD( callTempFile )
00096 QTemporaryFile *file = new QTemporaryFile( KJSEmbed::extractQString( exec, args, 0) );
00097 file->setAutoRemove( KJSEmbed::extractBool(exec, args, 1));
00098
00099 if( file->open() )
00100 {
00101 return new KJSEmbed::FileIOBinding( exec, file );
00102 }
00103 delete file;
00104 KJS::throwError(exec, KJS::GeneralError, i18n("Could not create temporary file."));
00105 END_STATIC_OBJECT_METHOD
00106
00107 START_STATIC_METHOD_LUT( FileIO )
00108 {"openfile", 2, KJS::DontDelete|KJS::ReadOnly, &callOpenFile },
00109 {"remove", 1, KJS::DontDelete|KJS::ReadOnly, &callRemoveFile },
00110 {"copy", 2, KJS::DontDelete|KJS::ReadOnly, &callCopyFile },
00111 {"move", 2, KJS::DontDelete|KJS::ReadOnly, &callMoveFile },
00112 {"link", 2, KJS::DontDelete|KJS::ReadOnly, &callLinkFile },
00113 {"exists", 2, KJS::DontDelete|KJS::ReadOnly, &callExistsFile },
00114 {"tempfile", 2, KJS::DontDelete|KJS::ReadOnly, &callTempFile }
00115 END_METHOD_LUT
00116
00117 START_ENUM_LUT( FileIO )
00118 {"ReadOnly", QIODevice::ReadOnly },
00119 {"WriteOnly", QIODevice::WriteOnly },
00120 {"ReadWrite", QIODevice::ReadWrite }
00121 END_ENUM_LUT
00122
00123 START_METHOD_LUT( FileIO )
00124 {"open", 1, KJS::DontDelete|KJS::ReadOnly, &callFileOpen },
00125 {"close", 0, KJS::DontDelete|KJS::ReadOnly, &callFileClose },
00126 {"readln", 0, KJS::DontDelete|KJS::ReadOnly, &callFileReadLine },
00127 {"readAll", 0, KJS::DontDelete|KJS::ReadOnly, &callFileReadAll },
00128 {"writeln", 1, KJS::DontDelete|KJS::ReadOnly, &callFileWriteLine },
00129 {"atEnd", 0, KJS::DontDelete|KJS::ReadOnly, &callFileAtEnd }
00130 END_METHOD_LUT
00131
00132 START_CTOR( FileIO, File, 1 )
00133 return new KJSEmbed::FileIOBinding( exec, new QFile( KJSEmbed::extractQString(exec,args,0 ) ) );
00134 END_CTOR
00135
00136