File indexing completed on 2024-04-28 15:28:43

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 #include "filedialog_binding.h"
0023 
0024 #include <QStringList>
0025 #include <QFileDialog>
0026 #include <QDebug>
0027 
0028 #include <kjs/object.h>
0029 
0030 #include "static_binding.h"
0031 
0032 using namespace KJSEmbed;
0033 
0034 KJS::JSValue *callGetExistingDirectory(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0035 {
0036     QWidget *parent = KJSEmbed::extractObject<QWidget>(exec, args, 0, nullptr);
0037     QString caption = KJSEmbed::extractVariant<QString>(exec, args, 1, QString());
0038     QString dir = KJSEmbed::extractVariant<QString>(exec, args, 2, QString());
0039     QFileDialog::Options options = (QFileDialog::Options)KJSEmbed::extractVariant<uint>(exec, args, 3, QFileDialog::ShowDirsOnly);
0040 
0041     return KJS::jsString(QFileDialog::getExistingDirectory(parent, caption, dir, options));
0042 }
0043 
0044 KJS::JSValue *callGetOpenFileName(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0045 {
0046     QWidget *parent = KJSEmbed::extractObject<QWidget>(exec, args, 0, nullptr);
0047     QString caption = KJSEmbed::extractVariant<QString>(exec, args, 1, "");
0048     QString dir = KJSEmbed::extractVariant<QString>(exec, args, 2, "");
0049     QString filter = KJSEmbed::extractVariant<QString>(exec, args, 3, "");
0050 //    QString *selectedFilter = KJSEmbed::extractVariant<QString>(exec, args, 4, 0);
0051     QFileDialog::Options options = (QFileDialog::Options)KJSEmbed::extractVariant<uint>(exec, args, 4, 0);
0052 
0053     return KJS::jsString(QFileDialog::getOpenFileName(parent, caption, dir, filter, nullptr, options));
0054 }
0055 
0056 KJS::JSValue *callGetOpenFileNames(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0057 {
0058     QWidget *parent = KJSEmbed::extractObject<QWidget>(exec, args, 0, nullptr);
0059     QString caption = KJSEmbed::extractVariant<QString>(exec, args, 1, QString());
0060     QString dir = KJSEmbed::extractVariant<QString>(exec, args, 2, QString());
0061     QString filter = KJSEmbed::extractVariant<QString>(exec, args, 3, QString());
0062 //    QString *selectedFilter = KJSEmbed::extractVariant<QString>(exec, args, 4, 0);
0063     QFileDialog::Options options = (QFileDialog::Options)KJSEmbed::extractVariant<uint>(exec, args, 4, 0);
0064 
0065     QStringList fileNames =  QFileDialog::getOpenFileNames(parent, caption, dir, filter, nullptr, options);
0066 
0067     return convertToValue(exec, fileNames);
0068 }
0069 
0070 KJS::JSValue *callGetSaveFileName(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0071 {
0072     QWidget *parent = KJSEmbed::extractObject<QWidget>(exec, args, 0, nullptr);
0073     QString caption = KJSEmbed::extractVariant<QString>(exec, args, 1, QString());
0074     QString dir = KJSEmbed::extractVariant<QString>(exec, args, 2, QString());
0075     QString filter = KJSEmbed::extractVariant<QString>(exec, args, 3, QString());
0076 //    QString *selectedFilter = KJSEmbed::extractVariant<QString>(exec, args, 4, 0);
0077     QFileDialog::Options options = (QFileDialog::Options)KJSEmbed::extractVariant<uint>(exec, args, 4, 0);
0078 
0079     return KJS::jsString(QFileDialog::getSaveFileName(parent, caption, dir, filter, nullptr, options));
0080 }
0081 const Method FileDialog::FileDialogMethods[] = {
0082     {"getExistingDirectory", 1, KJS::DontDelete | KJS::ReadOnly, &callGetExistingDirectory },
0083     {"getOpenFileName", 1, KJS::DontDelete | KJS::ReadOnly, &callGetOpenFileName },
0084     {"getOpenFileNames", 1, KJS::DontDelete | KJS::ReadOnly, &callGetOpenFileNames },
0085     {"getSaveFileName", 0, KJS::DontDelete | KJS::ReadOnly, &callGetSaveFileName },
0086     {nullptr, 0, 0, nullptr }
0087 };