/*static*/ KValueRef Codec::ExtractZipAsync(const ValueList& args) { std::string zipFile = args.GetString(0); std::string directory = args.GetString(1); AutoPtr<AsyncJob> job = args.GetObject(2).cast<AsyncJob>(); KMethodRef callback = 0; if (args.size() > 3) { callback = args.GetMethod(3); } std::ifstream stream(UTF8ToSystem(zipFile).c_str(), std::ios::binary); Poco::Zip::Decompress decompressor(stream, directory); try { decompressor.decompressAllFiles(); } catch (std::exception& e) { Logger::Get("Codec")->Error("exception decompressing: %s", e.what()); throw ValueException::FromFormat("Exception during extraction: %s", e.what()); } stream.close(); if (!callback.isNull()) { ValueList args; args.push_back(Value::NewString(directory)); RunOnMainThread(callback, args, true); } return Value::Undefined; }
void UIBinding::Log(Logger::Level level, std::string& message) { if (level > Logger::LWARN) return; std::string methodName("warn"); if (level < Logger::LWARN) methodName = "error"; std::string origMethodName(methodName); origMethodName.append("_orig"); std::vector<AutoUserWindow>& openWindows = UIBinding::GetInstance()->GetOpenWindows(); for (size_t i = 0; i < openWindows.size(); i++) { KObjectRef domWindow = openWindows[i]->GetDOMWindow(); if (domWindow.isNull()) continue; KObjectRef console = domWindow->GetObject("console", 0); if (console.isNull()) continue; KMethodRef method = console->GetMethod(origMethodName.c_str(), 0); if (method.isNull()) method = console->GetMethod(methodName.c_str(), 0); RunOnMainThread(method, ValueList(Value::NewString(message)), false); } }
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); } }
AutoMenuItem UIBinding::__CreateCheckMenuItem(const ValueList& args) { args.VerifyException("createCheckMenuItem", "?s m|0"); std::string label = args.GetString(0, ""); KMethodRef eventListener = args.GetMethod(1, NULL); AutoMenuItem item = this->CreateCheckMenuItem(); if (!label.empty()) item->SetLabel(label); if (!eventListener.isNull()) item->AddEventListener(Event::CLICKED, eventListener); return item; }
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 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 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))); }
/*static*/ KValueRef Codec::CreateZipAsync(const ValueList& args) { std::string directory = args.GetString(0); std::string zipFile = args.GetString(1); AutoPtr<AsyncJob> job = args.GetObject(2).cast<AsyncJob>(); KMethodRef callback = 0; if (args.size() > 3) { callback = args.GetMethod(3); } Poco::Path path(directory); path.makeDirectory(); std::ofstream stream(UTF8ToSystem(zipFile).c_str(), std::ios::binary | std::ios::trunc); Poco::Zip::Compress compressor(stream, true); try { compressor.addRecursive(path); } catch (std::exception& e) { Logger::Get("Codec")->Error("exception compressing: %s", e.what()); throw ValueException::FromFormat("Exception during zip: %s", e.what()); } compressor.close(); stream.close(); if (!callback.isNull()) { ValueList args; args.push_back(Value::NewString(zipFile)); RunOnMainThread(callback, args, true); } return Value::Undefined; }
void KEventObject::RemoveEventListener(std::string& eventName, unsigned int listenerId, KMethodRef callback) { Poco::Mutex::ScopedLock lock(listenersMutex); std::vector<EventListener*>::iterator i = listeners.begin(); while (i != listeners.end()) { EventListener* listener = *i; if (listener->eventName == eventName && ((callback.isNull() && listenerId == 0) || (!callback.isNull() && callback->Equals(listener->callback)) || (listenerId != 0 && listenerId == listener->listenerId))) { i = listeners.erase(i); delete listener; } else { i++; } } }
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))); } }
void SnarlWin32::ShowNotification(std::string& title, std::string& description, std::string& iconURL, int timeout, KMethodRef callback) { SnarlInterface snarlInterface; std::wstring wideTitle = UTF8ToWide(title); std::wstring wideText = UTF8ToWide(description); std::wstring wideIconPath(L""); if (!iconURL.empty()) { std::string iconPath = URLUtils::URLToPath(iconURL); wideIconPath.append(UTF8ToWide(iconPath)); } HWND replyWindow = Win32Host::Win32Instance()->AddMessageHandler( &SnarlWin32::MessageHandler); long id = snarlInterface.snShowMessage(wideTitle, wideText, timeout, wideIconPath, replyWindow, SnarlWin32::snarlWindowMessage); if (!callback.isNull()) { SnarlWin32::snarlCallbacks[id] = callback; } }
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; }