KValueRef Script::Evaluate(const char *mimeType, const char *name, const char *code, KObjectRef scope) { KObjectRef evaluator = this->FindEvaluatorWithMethod("canEvaluate", mimeType); if (!evaluator.isNull()) { KMethodRef evaluate = evaluator->GetMethod("evaluate"); if (!evaluate.isNull()) { ValueList args; args.push_back(Value::NewString(mimeType)); args.push_back(Value::NewString(name)); args.push_back(Value::NewString(code)); args.push_back(Value::NewObject(scope)); return evaluate->Call(args); } else { throw ValueException::FromFormat( "Error evaluating: No \"evaluate\" method found on evaluator for mimeType: \"%s\"", mimeType); } } else { throw ValueException::FromFormat("Error evaluating: No evaluator found for mimeType: \"%s\"", mimeType); } }
void Win32UserWindow::OpenSaveAsDialog(KMethodRef callback, std::string& title, std::string& path, std::string& defaultName, std::vector<std::string>& types, std::string& typesDescription) { KListRef results = SelectFile(true, false, title, path, defaultName, types, typesDescription); callback->Call(ValueList(Value::NewList(results))); }
void Win32UserWindow::OpenFileChooserDialog(KMethodRef callback, bool multiple, std::string& title, std::string& path, std::string& defaultName, std::vector<std::string>& types, std::string& typesDescription) { KListRef results = this->SelectFile( false, multiple, title, path, defaultName, types, typesDescription); callback->Call(ValueList(Value::NewList(results))); }
void Win32UserWindow::OpenFolderChooserDialog( KMethodRef callback, bool multiple, std::string& title, std::string& path, std::string& defaultName) { KListRef results = SelectDirectory(multiple, title, path, defaultName); callback->Call(ValueList(Value::NewList(results))); }
void TrayClickedCallback(GtkStatusIcon *status_icon, gpointer data) { TrayItemGtk* item = static_cast<TrayItemGtk*>(data); KMethodRef cb = item->GetCallback(); if (cb.isNull()) return; try { ValueList args; cb->Call(args); } catch (ValueException& e) { Logger* logger = Logger::Get("UI.TrayItemGtk"); SharedString ss = e.DisplayString(); logger->Error("Tray icon callback failed: %s", ss->c_str()); } }
KObjectRef Script::FindEvaluatorWithMethod(const char *method, const char *arg) { ValueList args; args.push_back(Value::NewString(arg)); for (size_t i = 0; i < evaluators->Size(); i++) { KMethodRef finder = evaluators->At(i)->ToObject()->GetMethod(method); if (!finder.isNull()) { KValueRef result = finder->Call(args); if (result->IsBool() && result->ToBool()) { return evaluators->At(i)->ToObject(); } } } return 0; }
void Network::_GetHostByAddress(const ValueList& args, KValueRef result) { if (args.at(0)->IsObject()) { KObjectRef obj = args.at(0)->ToObject(); AutoPtr<IPAddress> b = obj.cast<IPAddress>(); if (!b.isNull()) { // in this case, they've passed us an IPAddressBinding // object, which we can just retrieve the ipaddress // instance and resolving using it Poco::Net::IPAddress addr(b->GetAddress()->toString()); AutoPtr<Host> binding = new Host(addr); if (binding->IsInvalid()) { throw ValueException::FromString("Could not resolve address"); } result->SetObject(binding); return; } else { KMethodRef toStringMethod = obj->GetMethod("toString"); if (toStringMethod.isNull()) throw ValueException::FromString("Unknown object passed"); result->SetObject(GetHostBinding(toStringMethod->Call()->ToString())); return; } } else if (args.at(0)->IsString()) { // in this case, they just passed in a string so resolve as normal result->SetObject(GetHostBinding(args.GetString(0))); } }
AutoPtr<PreprocessData> Script::Preprocess(const char *url, KObjectRef scope) { KObjectRef evaluator = this->FindEvaluatorWithMethod("canPreprocess", url); if (!evaluator.isNull()) { KMethodRef preprocess = evaluator->GetMethod("preprocess"); if (!preprocess.isNull()) { ValueList args; args.push_back(Value::NewString(url)); args.push_back(Value::NewObject(scope)); KValueRef result = preprocess->Call(args); if (result->IsObject()) { KObjectRef object = result->ToObject(); AutoPtr<PreprocessData> data = new PreprocessData(); if (object->HasProperty("data")) { KValueRef objectData = object->Get("data"); if (objectData->IsObject()) { BlobRef blobData = objectData->ToObject().cast<Blob>(); if (!blobData.isNull()) { data->data = blobData; } } else if (objectData->IsString()) { data->data = new Blob(objectData->ToString(), strlen(objectData->ToString())); } } else { throw ValueException::FromString("Preprocessor didn't return any data"); } if (object->HasProperty("mimeType")) { data->mimeType = object->Get("mimeType")->ToString(); } else { throw ValueException::FromString("Preprocessor didn't return a mimeType"); } return data; } } else { throw ValueException::FromFormat( "Error preprocessing: No \"preprocess\" method found on evaluator for url: \"%s\"", url); } } else { throw ValueException::FromFormat("Error preprocessing: No evaluator found for url: \"%s\"", url); } return 0; }