• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

kjsembed

  • sources
  • kde-4.12
  • kdelibs
  • kjsembed
  • kjsembed
fileio.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3  Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4  Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
5  Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 #include "fileio.h"
23 #include <QtCore/QFile>
24 #include "kjseglobal.h"
25 #include <QtCore/QTemporaryFile>
26 #include <QtCore/QDebug>
27 
28 using namespace KJSEmbed;
29 
30 FileIOBinding::FileIOBinding( KJS::ExecState *exec, QFile *value )
31  : ObjectBinding(exec, "File", value )
32 {
33  StaticBinding::publish( exec, this, FileIO::methods() );
34  StaticBinding::publish( exec, this, ObjectFactory::methods() );
35  StaticBinding::publish( exec, this, VariantFactory::methods() );
36 }
37 
38 START_OBJECT_METHOD( callFileOpen, QFile )
39  result = KJS::jsBoolean( object->open( (QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)));
40 END_OBJECT_METHOD
41 
42 START_OBJECT_METHOD( callFileClose, QFile )
43  object->close();
44 END_OBJECT_METHOD
45 
46 START_OBJECT_METHOD( callFileReadLine, QFile )
47  result = KJS::jsString( object->readLine().data() );
48 END_OBJECT_METHOD
49 
50 START_OBJECT_METHOD( callFileReadAll, QFile )
51  result = KJS::jsString( object->readAll().data() );
52 END_OBJECT_METHOD
53 
54 START_OBJECT_METHOD( callFileWriteLine, QFile )
55  result = KJS::jsNumber( (long int)object->write(KJSEmbed::extractQByteArray(exec, args, 0) + '\n') );
56 END_OBJECT_METHOD
57 
58 START_OBJECT_METHOD( callFileAtEnd, QFile )
59  result = KJS::jsBoolean(object->atEnd());
60 END_OBJECT_METHOD
61 
62 START_STATIC_OBJECT_METHOD( callOpenFile )
63  QFile *file = new QFile( KJSEmbed::extractQString( exec, args, 0) );
64  if( file->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)))
65  {
66  return new KJSEmbed::FileIOBinding( exec, file );
67  }
68  delete file;
69  KJS::throwError(exec, KJS::TypeError, i18n("Could not open file '%1'", KJSEmbed::extractQString( exec, args, 0)));
70  return KJS::jsNull();
71 END_STATIC_OBJECT_METHOD
72 
73 START_STATIC_OBJECT_METHOD( callRemoveFile )
74  return KJS::jsBoolean( QFile::remove(KJSEmbed::extractQString( exec, args, 0) ));
75 END_STATIC_OBJECT_METHOD
76 
77 START_STATIC_OBJECT_METHOD( callCopyFile )
78  return KJS::jsBoolean( QFile::copy(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ));
79 END_STATIC_OBJECT_METHOD
80 
81 START_STATIC_OBJECT_METHOD( callMoveFile )
82  if( QFile::copy(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ) )
83  return KJS::jsBoolean( QFile::remove(KJSEmbed::extractQString( exec, args, 0) ) );
84  return KJS::jsBoolean(false);
85 END_STATIC_OBJECT_METHOD
86 
87 START_STATIC_OBJECT_METHOD( callLinkFile )
88  return KJS::jsBoolean( QFile::link(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ));
89 END_STATIC_OBJECT_METHOD
90 
91 START_STATIC_OBJECT_METHOD( callExistsFile )
92  return KJS::jsBoolean( QFile::exists(KJSEmbed::extractQString( exec, args, 0) ));
93 END_STATIC_OBJECT_METHOD
94 
95 START_STATIC_OBJECT_METHOD( callTempFile )
96  QTemporaryFile *file = new QTemporaryFile( KJSEmbed::extractQString( exec, args, 0) );
97  file->setAutoRemove( KJSEmbed::extractBool(exec, args, 1));
98 
99  if( file->open() )
100  {
101  return new KJSEmbed::FileIOBinding( exec, file );
102  }
103  delete file;
104  KJS::throwError(exec, KJS::GeneralError, i18n("Could not create temporary file."));
105 END_STATIC_OBJECT_METHOD
106 
107 START_STATIC_METHOD_LUT( FileIO )
108  {"openfile", 2, KJS::DontDelete|KJS::ReadOnly, &callOpenFile },
109  {"remove", 1, KJS::DontDelete|KJS::ReadOnly, &callRemoveFile },
110  {"copy", 2, KJS::DontDelete|KJS::ReadOnly, &callCopyFile },
111  {"move", 2, KJS::DontDelete|KJS::ReadOnly, &callMoveFile },
112  {"link", 2, KJS::DontDelete|KJS::ReadOnly, &callLinkFile },
113  {"exists", 2, KJS::DontDelete|KJS::ReadOnly, &callExistsFile },
114  {"tempfile", 2, KJS::DontDelete|KJS::ReadOnly, &callTempFile }
115 END_METHOD_LUT
116 
117 START_ENUM_LUT( FileIO )
118  {"ReadOnly", QIODevice::ReadOnly },
119  {"WriteOnly", QIODevice::WriteOnly },
120  {"ReadWrite", QIODevice::ReadWrite }
121 END_ENUM_LUT
122 
123 START_METHOD_LUT( FileIO )
124  {"open", 1, KJS::DontDelete|KJS::ReadOnly, &callFileOpen },
125  {"close", 0, KJS::DontDelete|KJS::ReadOnly, &callFileClose },
126  {"readln", 0, KJS::DontDelete|KJS::ReadOnly, &callFileReadLine },
127  {"readAll", 0, KJS::DontDelete|KJS::ReadOnly, &callFileReadAll },
128  {"writeln", 1, KJS::DontDelete|KJS::ReadOnly, &callFileWriteLine },
129  {"atEnd", 0, KJS::DontDelete|KJS::ReadOnly, &callFileAtEnd }
130 END_METHOD_LUT
131 
132 START_CTOR( FileIO, File, 1 )
133  return new KJSEmbed::FileIOBinding( exec, new QFile( KJSEmbed::extractQString(exec,args,0 ) ) );
134 END_CTOR
135 
136 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
START_METHOD_LUT
#define START_METHOD_LUT(TYPE)
Definition: binding_support.h:127
KJSEmbed::StaticBinding::publish
static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
Publishes an array of Methods to an object.
Definition: static_binding.cpp:60
remove
object remove(key)
KJSEmbed::FileIOBinding
Definition: fileio.h:35
KJSEmbed::extractBool
bool KJSEMBED_EXPORT extractBool(KJS::ExecState *exec, const KJS::List &args, int idx, bool defaultValue=false)
Extracts a bool from an argument list.
Definition: binding_support.cpp:149
END_CTOR
#define END_CTOR
Definition: binding_support.h:166
KJSEmbed::extractInt
int KJSEMBED_EXPORT extractInt(KJS::ExecState *exec, const KJS::List &args, int idx, int defaultValue=0)
Extracts an integer from an argument list.
Definition: binding_support.cpp:72
START_STATIC_METHOD_LUT
END_STATIC_OBJECT_METHOD START_STATIC_METHOD_LUT(FileIO)
Definition: fileio.cpp:107
DomDocumentNS::data
QString data
Definition: dom.cpp:436
KJSEmbed::ObjectBinding
Definition: object_binding.h:88
START_CTOR
#define START_CTOR(TYPE, JSNAME, ARGS)
Definition: binding_support.h:157
END_OBJECT_METHOD
#define END_OBJECT_METHOD
End a variant method started by START_OBJECT_METHOD.
Definition: object_binding.h:57
KJSEmbed::ObjectFactory::methods
static const Method * methods()
Definition: object_binding.h:85
START_OBJECT_METHOD
#define START_OBJECT_METHOD(METHODNAME, TYPE)
A simple pointer syle method.
Definition: object_binding.h:40
END_METHOD_LUT
#define END_METHOD_LUT
Definition: binding_support.h:135
file
END_OBJECT_METHOD QFile * file
Definition: fileio.cpp:63
KJSEmbed::extractQByteArray
QByteArray KJSEMBED_EXPORT extractQByteArray(KJS::ExecState *exec, const KJS::List &args, int idx, const QByteArray &defaultValue=QByteArray())
Extracts a QByteArray from an argument list.
Definition: binding_support.cpp:50
KJSEmbed::FileIOBinding::FileIOBinding
FileIOBinding(KJS::ExecState *exec, QFile *value)
Definition: fileio.cpp:30
if
if(file->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)))
Definition: fileio.cpp:64
fileio.h
END_STATIC_OBJECT_METHOD
#define END_STATIC_OBJECT_METHOD
Definition: object_binding.h:75
KJSEmbed::VariantFactory::methods
static const Method * methods()
Definition: variant_binding.h:251
kjseglobal.h
KJS::jsString
KJS::JSCell * jsString(const QString &s)
Definition: kjseglobal.h:73
result
result
Definition: fileio.cpp:39
setAutoRemove
file setAutoRemove(KJSEmbed::extractBool(exec, args, 1))
close
END_OBJECT_METHOD object close()
value
QVariant value
Definition: settings.cpp:35
KJSEmbed::extractQString
QString KJSEMBED_EXPORT extractQString(KJS::ExecState *exec, const KJS::List &args, int idx, const QString defaultValue=QString())
Extracts a QString from an argument list.
Definition: binding_support.cpp:34
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
START_STATIC_OBJECT_METHOD
#define START_STATIC_OBJECT_METHOD(METHODNAME)
Definition: object_binding.h:67
START_ENUM_LUT
#define START_ENUM_LUT(TYPE)
Definition: binding_support.h:139
END_ENUM_LUT
#define END_ENUM_LUT
Definition: binding_support.h:143
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal