KJS::Value KstBindKst::loadScript(KJS::ExecState *exec, const KJS::List& args) {
  if (args.size() != 1) {
    KJS::Object eobj = KJS::Error::create(exec, KJS::SyntaxError);
    exec->setException(eobj);
    return KJS::Undefined();
  }

  if (args[0].type() != KJS::StringType) {
    KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
    exec->setException(eobj);
    return KJS::Undefined();
  }

  QString fn = args[0].toString(exec).qstring();
  if (!QFile::exists(fn)) { // One day make this support KIO FIXME
    return KJS::Boolean(false);
  }

  if (_ext->part()->runFile(fn)) {
    // FIXME: add to the script registry
  } else {
    KJS::Completion c = _ext->part()->completion();
    if (!c.isNull()) {
      QString err = c.toString(_ext->part()->globalExec()).qstring();
      KstDebug::self()->log(i18n("Error running script %1: %2").arg(fn).arg(err), KstDebug::Error);
    } else {
      KstDebug::self()->log(i18n("Unknown error running script %1.").arg(fn), KstDebug::Error);
    }
    return KJS::Boolean(false);
  }

  return KJS::Boolean(true);
}
Example #2
0
void KstJS::loadScript() {
  QString fn = KFileDialog::getOpenFileName("::<kstfiledir>",
      i18n("*.js|JavaScript (*.js)\n*|All Files"),
      app(), i18n("Open Script"));

  if (!fn.isEmpty()) {
    if (_jsPart->runFile(fn)) {
      if (!_scripts.contains(fn)) {
        _scripts.append(fn);
      }
    } else {
      KJS::Completion c = _jsPart->completion();
      QString err = c.toString(_jsPart->globalExec()).qstring();
      KMessageBox::error(app(), i18n("Error running script %1: %2").arg(fn).arg(err));
    }
  }
}