FB::VariantList btlauncherAPI::stopRunning(const std::string& val) { FBLOG_INFO("stopRunning()", "START"); FBLOG_INFO("stopRunning()", val.c_str()); FB::VariantList list; bool foundIt = false; size_t procCount = 0; kinfo_proc *procList = NULL; GetBSDProcessList(&procList, &procCount); size_t i; for (i = 0; i < procCount; i++) { if (!strcmp(procList[i].kp_proc.p_comm, val.c_str())) { FBLOG_INFO("stopRunning()", val.c_str()); kill(procList[i].kp_proc.p_pid, SIGKILL); foundIt = true; } } free(procList); if (foundIt) { list.push_back("ok"); } else { list.push_back("could not open process"); } FBLOG_INFO("stopRunning()", "END"); return list; }
FB::variant jsonValueToVariant( Json::Value root ) { Json::Value def; if (root.isString()) return root.asString(); else if (root.isBool()) return root.asBool(); else if (root.isDouble()) return root.asDouble(); else if (root.isInt()) return root.asInt(); else if (root.isUInt()) return root.asUInt(); else if (root.isNull()) return FB::FBNull(); else if (root.isArray()) { FB::VariantList outList; for (size_t i = 0; i < root.size(); ++i) { outList.push_back(jsonValueToVariant(root.get(i, def))); } return outList; } else if (root.isObject()) { Json::Value::Members members = root.getMemberNames(); FB::VariantMap outMap; for (Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it) { outMap[*it] = jsonValueToVariant(root.get(*it, def)); } return outMap; } else { return FB::FBVoid(); } }
void DialogManagerX11::_showFolderDialog(FB::PluginWindow* win, bool multi, const PathCallback& cb) { FB::VariantList out; // Create the dialog. Do it the hard way so we can override the default // behavior which does not allow files and directories to be selected together. GtkWidget *dialog = gtk_dialog_new_with_buttons("Open", NULL, GTK_DIALOG_MODAL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); GtkWidget *action_area = gtk_dialog_get_action_area(GTK_DIALOG(dialog)); GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); // Some nicer formatting gtk_box_set_spacing(GTK_BOX(content_area), 2); gtk_container_set_border_width(GTK_CONTAINER(dialog), 5); gtk_container_set_border_width(GTK_CONTAINER(action_area), 5); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); // Create the file chooser widget GtkWidget *chooser = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN); if (multi) gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE); // And add it to the dialog gtk_container_add(GTK_CONTAINER(content_area), chooser); gtk_widget_show(chooser); // for the life of me I can't figure out how to get the filechooserwidget // to return an actual requested size. Going with the simple hard coded // size until that can be figured out. gtk_window_resize(GTK_WINDOW(dialog), 600, 400); // run the dialog gint result = gtk_dialog_run(GTK_DIALOG(dialog)); if (result == GTK_RESPONSE_ACCEPT) { char *buffer; std::string filename; GSList *filenames, *iterator; filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(chooser)); iterator = filenames; while (iterator) { buffer = (char *)g_slist_nth_data(iterator, 0); filename = std::string(buffer); g_free(buffer); // append a trailing slash if the name is a directory if (fs::is_directory(filename)) filename.push_back('/'); out.push_back(filename); iterator = g_slist_next(iterator); } g_slist_free(filenames); } gtk_widget_destroy(dialog); // call the callback with the results cb(out); }
FB::variant FB::JSFunction::call( const std::vector<variant>& args ) { FB::VariantList list; if (args.size() >= 1) { list.insert(list.end(), args.begin()+1, args.end()); } return exec(list); }
FB::VariantList FB::JSArray::Values() { FB::VariantList output; for (size_t i = 0; i < GetLength(); i++) { output.push_back((*this)[i]); } return output; }
long FBTestPluginAPI::countArrayLength(const FB::JSObjectPtr &jso) { if (!jso->HasProperty("getArray")) throw FB::invalid_arguments(); FB::VariantList array = jso->GetProperty("getArray").cast<FB::VariantList>(); long len = array.size();// array->GetProperty("length").convert_cast<long>(); return len; }
FB::VariantList HPCCSystemsGraphViewControlAPI::getPropertiesForItems(const std::vector<int> & items) { FB::VariantList retVal; for(std::vector<int>::const_iterator itr = items.begin(); itr != items.end(); ++itr) retVal.push_back(getProperties(*itr)); return retVal; }
FB::JSArray::JSArray(BrowserHostPtr host, const FB::VariantList& values) { m_obj = host->getDOMWindow()->createArray(); for (FB::VariantList::const_iterator it = values.begin(); it != values.end(); ++it) { push(*it); } RegisterMethods(); }
FB::VariantList FBTestPluginAPI::reverseArray(const std::vector<std::string>& arr) { FB::VariantList outArr; for (std::vector<std::string>::const_reverse_iterator it = arr.rbegin(); it != arr.rend(); it++) { outArr.push_back(*it); } return outArr; }
FB::VariantList HPCCSystemsGraphViewControlAPI::getVertices() { FB::VariantList items; std::vector<int> vertices; getPlugin()->GetVertices(vertices); for(std::vector<int>::const_iterator itr = vertices.begin(); itr != vertices.end(); ++itr) items.push_back(*itr); return items; }
FB::VariantList FBTestPluginAPI::getObjectValues(const FB::JSObjectPtr& arr) { FB::VariantList outArr; std::map<std::string, FB::variant> inMap; arr->GetObjectValues(arr, inMap); for (std::map<std::string, FB::variant>::iterator it = inMap.begin(); it != inMap.end(); it++) { outArr.push_back(it->second); } return outArr; }
FB::VariantList HPCCSystemsGraphViewControlAPI::getSelectionAsGlobalID() { FB::VariantList retVal; std::vector<std::string> selectedItems; getPlugin()->GetSelection(selectedItems); for(std::vector<std::string>::const_iterator itr = selectedItems.begin(); itr != selectedItems.end(); ++itr) retVal.push_back(*itr); return retVal; }
FB::VariantList HPCCSystemsGraphViewControlAPI::find(const std::string & text, boost::optional<bool> includeProperties) { assert(getPlugin()); FB::VariantList retVal; std::vector<int> foundItems; getPlugin()->Find(text, includeProperties ? *includeProperties : true, foundItems); for(std::vector<int>::const_iterator itr = foundItems.begin(); itr != foundItems.end(); ++itr) retVal.push_back(*itr); return retVal; }
FB::variant btlauncherAPI::runProgram(const std::string& program, const FB::JSObjectPtr& callback) { FBLOG_INFO("runProgram()", "START"); string exe = this->installPath; if (program == "SoShare") exe += SOSHARE_EXE_PATH; else if (program == "Torque") exe += TORQUE_EXE_PATH; else exe += BTLIVE_EXE_PATH; FBLOG_INFO("runProgram()", exe.c_str()); struct stat st; if (stat(exe.c_str(), &st) == -1 || !S_ISREG(st.st_mode)) { FBLOG_INFO("runProgram()", "stat check problem"); callback->InvokeAsync("", FB::variant_list_of(false)(0)); FBLOG_INFO("runProgram()", "END"); return 0; } FB::VariantList list = isRunning(program); if (list.empty()) { switch(fork()) { case -1: { perror("BTLauncher Run Program Fork"); FBLOG_INFO("runProgram()", "fork - failure"); break; } case 0: { FBLOG_INFO("runProgram()", "fork - child process"); FBLOG_INFO("runProgram()", exe.c_str()); execlp(exe.c_str(), exe.c_str(), NULL); FBLOG_INFO("runProgram()", "child process exit"); exit(1); } default: { break; } } } callback->InvokeAsync("", FB::variant_list_of(true)(1)); FBLOG_INFO("runProgram()", "END"); return 0; }
FB::VariantList btlauncherAPI::isRunning(const std::string& val) { FBLOG_INFO("isRunning()", "START"); FB::VariantList list; size_t procCount = 0; kinfo_proc *procList = NULL; GetBSDProcessList(&procList, &procCount); size_t i; for (i = 0; i < procCount; i++) { if (!strcmp(procList[i].kp_proc.p_comm, val.c_str())) { FBLOG_INFO("isRunning()", val.c_str()); list.push_back(procList[i].kp_proc.p_comm); } } free(procList); FBLOG_INFO("isRunning()", "END"); return list; }
FB::VariantList btlauncherAPI::stopRunning(const std::wstring& val) { FB::VariantList list; if (wcsstr(val.c_str(), _T(BT_HEXCODE)) || wcsstr(val.c_str(), _T(BTLIVE_CODE))) { HWND hWnd = FindWindow( val.c_str(), NULL ); DWORD pid; DWORD parent; parent = GetWindowThreadProcessId(hWnd, &pid); HANDLE pHandle = OpenProcess(PROCESS_TERMINATE, NULL, pid); if (! pHandle) { list.push_back("could not open process"); list.push_back(GetLastError()); } else { BOOL result = TerminateProcess(pHandle, 0); list.push_back("ok"); list.push_back(result); } } return list; }
FB::VariantMap UploadQueue::getStatusDict(bool include_filenames) { FB::VariantMap d; // whole-queue stats d["queue_name"] = name; d["current_queue_bytes_remaining"] = current_queue_bytes; d["total_queue_bytes"] = total_queue_bytes; d["total_queue_files"] = total_queue_files; d["current_queue_files_remaining"] = files_waiting; d["batches_remaining"] = ceil(static_cast<float>(files_waiting) / static_cast<float>(batch_size)); d["status"] = "Uploading"; // this-batch stats if (include_filenames) { FB::VariantList cf; for (std::set<std::wstring>::const_iterator it = current_upload_files.begin(); it != current_upload_files.end(); ++it) { cf.push_back(*it); } d["current_files"] = cf; } d["current_batch_bytes_total"] = current_batch_bytes; double current_batch_pct_complete = 0.0; if (current_upload_request) { HTTP::Status st = current_upload_request->getStatus(); FB::VariantMap status(st.asDict()); d.insert(status.begin(), status.end()); if (st.send_total) { current_batch_pct_complete = static_cast<double>(st.bytes_sent) / static_cast<double>(st.send_total); } } d["current_batch_pct_complete"] = current_batch_pct_complete; d["overall_progress_pct"] = static_cast<double>( (total_queue_bytes - (current_queue_bytes + current_batch_bytes)) + (current_batch_pct_complete * current_batch_bytes)) / static_cast<double>(total_queue_bytes); return d; }
FB::variant NPObjectAPI::Construct( const FB::VariantList& args ) { if (m_browser.expired()) return false; NpapiBrowserHostPtr browser(getHost()); if (!browser->isMainThread()) { return browser->CallOnMainThread(boost::bind((FB::ConstructType)&NPObjectAPI::Construct, this, args)); } if (is_JSAPI) { FB::JSAPIPtr tmp = inner.lock(); if (tmp) return tmp->Construct(args); else return false; } NPVariant retVal; // Convert the arguments to NPVariants boost::scoped_array<NPVariant> npargs(new NPVariant[args.size()]); for (unsigned int i = 0; i < args.size(); i++) { browser->getNPVariant(&npargs[i], args[i]); } bool res = false; // construct res = browser->Construct(obj, npargs.get(), args.size(), &retVal); // Free the NPVariants that we earlier allocated for (unsigned int i = 0; i < args.size(); i++) { browser->ReleaseVariantValue(&npargs[i]); } if (!res) { // If the method call failed, throw an exception throw script_error("constructor"); } else { FB::variant ret = browser->getVariant(&retVal); browser->ReleaseVariantValue(&retVal); // Always release the return value! return ret; } }
FB::VariantMap FBTestPluginAPI::getUserData() { FB::VariantMap map; map["Name"] = "Richard Bateman"; map["Location"] = "Somewhere in Utah"; map["EyeColor"] = "Hazel"; map["HairColor"] = "Brown"; FB::VariantList kids; kids.push_back("Caleb"); kids.push_back("Unknown"); kids.push_back("Ok, I only have one, but I'm filling space"); FB::VariantMap innerMap; innerMap["test"] = 12; innerMap["test2"] = true; innerMap["test3"] = 12.4; innerMap["test4"] = "asdf"; map["inner"] = innerMap; kids.push_back(innerMap); map["Kids"] = kids; return map; }
Json::Value variantToJsonValue(const FB::variant &val) { if (val.is_of_type<std::string>()) { return Json::Value(val.convert_cast<std::string>()); } else if (val.is_of_type<FB::VariantMap>()) { Json::Value retVal(Json::objectValue); FB::VariantMap map = val.cast<FB::VariantMap>(); for (FB::VariantMap::iterator it = map.begin(); it != map.end(); ++it) { retVal[it->first] = variantToJsonValue(it->second); } return retVal; } else if (val.is_of_type<FB::VariantList>()) { Json::Value retVal(Json::arrayValue); FB::VariantList list = val.cast<FB::VariantList>(); for (FB::VariantList::iterator it = list.begin(); it != list.end(); ++it) { retVal.append(variantToJsonValue(*it)); } return retVal; } else if (val.is_of_type<int>() || val.is_of_type<char>() || val.is_of_type<short>() || val.is_of_type<long>()) { return Json::Value((Json::Int)val.convert_cast<long>()); } else if (val.is_of_type<unsigned int>() || val.is_of_type<unsigned short>() || val.is_of_type<unsigned char>() || val.is_of_type<unsigned long>()) { return Json::Value((Json::UInt) val.convert_cast<unsigned long>()); } else if (val.is_of_type<double>() || val.is_of_type<float>()) { return Json::Value(val.convert_cast<double>()); } else if (val.is_of_type<bool>()) { return Json::Value(val.convert_cast<bool>()); } else { return Json::Value(Json::nullValue); } }
void IDispatchAPI::callMultipleFunctions( const std::string& name, const FB::VariantList& args, const std::vector<FB::JSObjectPtr>& direct, const std::vector<FB::JSObjectPtr>& ifaces ) { if (!isValid()) throw FB::script_error("Error calling handlers"); ActiveXBrowserHostPtr browser(getHost()); if (!browser->isMainThread()) { return browser->ScheduleOnMainThread(shared_from_this(), boost::bind(&IDispatchAPI::callMultipleFunctions, this, name, args, direct, ifaces)); } size_t argCount(args.size()); boost::scoped_array<CComVariant> comArgs(new CComVariant[argCount + 1]); boost::scoped_array<VARIANTARG> rawComArgs(new VARIANTARG[argCount + 1]); DISPPARAMS params; DISPID tid = DISPID_THIS; params.cArgs = args.size() + 1; params.cNamedArgs = 1; params.rgvarg = rawComArgs.get(); params.rgdispidNamedArgs = &tid; // Needed for IE9 for (size_t i = 0; i < argCount; i++) { browser->getComVariant(&comArgs[argCount - i], args[i]); // We copy w/out adding a ref so that comArgs will still clean up the values when it goes away rawComArgs[argCount - i] = comArgs[argCount - i]; } comArgs[0] = getIDispatch(); // Needed for IE 9 rawComArgs[0] = comArgs[0]; CComVariant result; CComExcepInfo exceptionInfo; std::vector<FB::JSObjectPtr>::const_iterator it(direct.begin()); std::vector<FB::JSObjectPtr>::const_iterator endit(direct.end()); for (it; it != endit; ++it) { IDispatchAPIPtr ptr(boost::static_pointer_cast<IDispatchAPI>(*it)); DISPID dispId = ptr->getIDForName(std::wstring(L"")); if (dispId == DISPID_UNKNOWN) { continue; } CComQIPtr<IDispatchEx> dispatchEx(ptr->getIDispatch()); HRESULT hr; if (!dispatchEx) { hr = getIDispatch()->Invoke(dispId, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, &result, &exceptionInfo, NULL); } else { hr = dispatchEx->InvokeEx(dispId, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, &result, &exceptionInfo, NULL); } } it = ifaces.begin(); endit = ifaces.end(); for (it; it != endit; ++it) { IDispatchAPIPtr ptr(boost::static_pointer_cast<IDispatchAPI>(*it)); DISPID dispId = getIDForName(FB::utf8_to_wstring(name)); if (dispId == DISPID_UNKNOWN) { continue; } CComQIPtr<IDispatchEx> dispatchEx(ptr->getIDispatch()); HRESULT hr; if (!dispatchEx) { hr = getIDispatch()->Invoke(dispId, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, &result, &exceptionInfo, NULL); } else { hr = dispatchEx->InvokeEx(dispId, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, &result, &exceptionInfo, NULL); } } }
FB::VariantList FBTestPluginAPI::getUserArray() { FB::VariantList map; map.push_back("Richard Bateman"); map.push_back("Somewhere in Utah"); map.push_back("Hazel"); map.push_back("Brown"); FB::VariantList kids; kids.push_back("Caleb"); kids.push_back("Unknown"); kids.push_back("Ok, I only have one, but I'm filling space"); FB::VariantMap innerMap; innerMap["test"] = 12; innerMap["test2"] = true; innerMap["test3"] = 12.4; innerMap["test4"] = "asdf"; map.push_back(innerMap); kids.push_back(innerMap); map.push_back(kids); return map; }
void NpapiBrowserHost::getNPVariant(NPVariant *dst, const FB::variant &var) { if (var.get_type() == typeid(FB::Npapi::NpapiNull)) { dst->type = NPVariantType_Null; } else if (var.get_type() == typeid(int) || var.get_type() == typeid(long) || var.get_type() == typeid(short) || var.get_type() == typeid(char) || var.get_type() == typeid(unsigned int) || var.get_type() == typeid(unsigned short) || var.get_type() == typeid(unsigned char)) { // Integer type dst->type = NPVariantType_Int32; dst->value.intValue = var.convert_cast<long>(); } else if (var.get_type() == typeid(double) || var.get_type() == typeid(float)) { dst->type = NPVariantType_Double; dst->value.doubleValue = var.convert_cast<double>(); } else if (var.get_type() == typeid(bool)) { dst->type = NPVariantType_Bool; dst->value.boolValue = var.convert_cast<bool>(); } else if (var.get_type() == typeid(std::string)) { std::string str = var.convert_cast<std::string>(); char *outStr = (char*)this->MemAlloc(str.size() + 1); memcpy(outStr, str.c_str(), str.size() + 1); dst->type = NPVariantType_String; dst->value.stringValue.UTF8Characters = outStr; dst->value.stringValue.UTF8Length = str.size(); } else if (var.get_type() == typeid(FB::VariantList)) { JSAPI_DOMNode outArr = this->getDOMWindow().createArray(); FB::VariantList inArr = var.cast<FB::VariantList>(); for (FB::VariantList::iterator it = inArr.begin(); it != inArr.end(); it++) { outArr.callMethod<void>("push", variant_list_of(*it)); } FB::AutoPtr<NPObjectAPI> api = dynamic_cast<NPObjectAPI*>(outArr.getJSObject().ptr()); if (api.ptr() != NULL) { dst->type = NPVariantType_Object; dst->value.objectValue = api->getNPObject(); this->RetainObject(dst->value.objectValue); } } else if (var.get_type() == typeid(FB::VariantMap)) { JSAPI_DOMNode out = this->getDOMWindow().createMap(); FB::VariantMap inMap = var.cast<FB::VariantMap>(); for (FB::VariantMap::iterator it = inMap.begin(); it != inMap.end(); it++) { out.setProperty(it->first, it->second); } FB::AutoPtr<NPObjectAPI> api = dynamic_cast<NPObjectAPI*>(out.getJSObject().ptr()); if (api.ptr() != NULL) { dst->type = NPVariantType_Object; dst->value.objectValue = api->getNPObject(); this->RetainObject(dst->value.objectValue); } } else if (var.get_type() == typeid(FB::JSOutObject)) { NPObject *outObj = NULL; FB::JSOutObject obj = var.cast<FB::JSOutObject>(); NPObjectAPI *tmpObj = dynamic_cast<NPObjectAPI *>(obj.ptr()); if (tmpObj == NULL) { outObj = NPJavascriptObject::NewObject(this, obj); } else { outObj = tmpObj->getNPObject(); this->RetainObject(outObj); } dst->type = NPVariantType_Object; dst->value.objectValue = outObj; } else if (var.get_type() == typeid(FB::JSObject)) { NPObject *outObj = NULL; FB::JSObject obj = var.cast<JSObject>(); NPObjectAPI *tmpObj = dynamic_cast<NPObjectAPI *>(obj.ptr()); if (tmpObj == NULL) { outObj = NPJavascriptObject::NewObject(this, obj); } else { outObj = tmpObj->getNPObject(); this->RetainObject(outObj); } dst->type = NPVariantType_Object; dst->value.objectValue = outObj; } // TODO: implement object types }