Esempio n. 1
0
	void Bytes::_Split(const ValueList& args, KValueRef result)
	{
		// This method now follows the spec located at:
		// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/split
		// Except support for regular expressions
		args.VerifyException("Bytes.split", "?s,i");

		KListRef list = new StaticBoundList();
		result->SetList(list);

		std::string target = "";
		if (this->size > 0)
		{
			target = this->buffer;
		}
		else
		{
			list->Append(Value::NewString(target));
			return;
		}

		if (args.size() <= 0)
		{
			list->Append(Value::NewString(target));
			return;
		}

		std::string separator = args.GetString(0);

		int limit = args.GetInt(1, INT_MAX);

		// We could use Poco's tokenizer here, but it doesn't split strings
		// like "abc,def,," -> ['abc', 'def', '', ''] correctly. It produces
		// ['abc', 'def', ''] which is a different behavior than the JS split.
		// So we roll our own for now -- it's not very efficient right now, but
		// it should be correct.
		size_t next = target.find(separator);
		while (target.size() > 0 && next != std::string::npos)
		{
			std::string token;
			if (separator.size() == 0)
			{
				token = target.substr(0, 1);
			}
			else
			{
				token = target.substr(0, next);
			}
			target = target.substr(next + 1);
			next = target.find(separator);

			if ((int) list->Size() >= limit)
				return;

			list->Append(Value::NewString(token));
		}

		if ((int) list->Size() < limit && separator.size() != 0)
			list->Append(Value::NewString(target));
	}
Esempio n. 2
0
 KListRef APIBinding::ManifestToKList(vector<pair<string, string> >& manifest)
 {
     KListRef list = new StaticBoundList();
     vector<pair<string, string> >::iterator i = manifest.begin();
     while (i != manifest.end())
     {
         KListRef entry = new StaticBoundList();
         entry->Append(Value::NewString(i->first));
         entry->Append(Value::NewString(i->second));
         list->Append(Value::NewList(entry));
         *i++;
     }
     return list;
 }
KListRef Win32UserWindow::SelectDirectory(
	bool multiple,
	std::string& title,
	std::string& path,
	std::string& defaultName)
{
	KListRef results = new StaticBoundList();

	BROWSEINFO bi = { 0 };
	std::wstring titleW = UTF8ToWide(title);
	bi.lpszTitle = titleW.c_str();
	bi.hwndOwner = this->windowHandle;
	bi.ulFlags = BIF_RETURNONLYFSDIRS;
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);

	if (pidl != 0)
	{
		wchar_t in_path[MAX_PATH];
		if (SHGetPathFromIDList(pidl, in_path))
		{
			std::wstring inPathW = in_path;
			std::string inPath = WideToUTF8(inPathW);
			results->Append(Value::NewString(inPath));
		}

		IMalloc * imalloc = 0;
		if (SUCCEEDED(SHGetMalloc(&imalloc)))
		{
			imalloc->Free(pidl);
			imalloc->Release();
		}
	}
	return results;
}
Esempio n. 4
0
	void UIBinding::_GetOpenWindows(const ValueList& args, KValueRef result)
	{
		KListRef list = new StaticBoundList();
		std::vector<AutoUserWindow>::iterator w = openWindows.begin();
		while (w != openWindows.end()) {
			list->Append(Value::NewObject(*w++));
		}
		result->SetList(list);
	}
Esempio n. 5
0
 KListRef APIBinding::DependencyVectorToKList(std::vector<SharedDependency>& deps)
 {
     KListRef dependencyList = new StaticBoundList();
     std::vector<SharedDependency>::iterator i = deps.begin();
     while (i != deps.end())
     {
         KValueRef dValue = Value::NewObject(new DependencyBinding(*i++));
         dependencyList->Append(dValue);
     }
     return dependencyList;
 }
Esempio n. 6
0
void HostBinding::GetAliases(const ValueList& args, KValueRef result)
{
    KListRef list = new StaticBoundList();
    std::vector<std::string> aliases = this->host.aliases();
    std::vector<std::string>::iterator iter = aliases.begin();
    while (iter!=aliases.end())
    {
        std::string alias = (*iter++);
        list->Append(Value::NewString(alias));
    }
    result->SetList(list);
}
Esempio n. 7
0
	void AppBinding::GetArguments(const ValueList& args, KValueRef result)
	{
		static KListRef argList(0);
		if (argList.isNull())
		{
			// Skip the first argument which is the filename to the executable
			argList = new StaticBoundList();
			for (int i = 1; i < host->GetCommandLineArgCount(); i++)
				argList->Append(Value::NewString(host->GetCommandLineArg(i)));
		}

		result->SetList(argList);
	}
Esempio n. 8
0
void HostBinding::GetAddresses(const ValueList& args, KValueRef result)
{
    KListRef list = new StaticBoundList();
    std::vector<IPAddress> addresses = this->host.addresses();
    std::vector<IPAddress>::iterator iter = addresses.begin();
    while (iter!=addresses.end())
    {
        IPAddress address = (*iter++);
        KObjectRef obj = new IPAddressBinding(address);
        KValueRef addr = Value::NewObject(obj);
        list->Append(addr);
    }
    result->SetList(list);
}
Esempio n. 9
0
	void AppBinding::GetArguments(const ValueList& args, KValueRef result)
	{
		static KListRef argList(0);
		if (argList.isNull())
		{
			// Skip the first argument which is the filename of the executable.
			std::vector<std::string>& args = host->GetApplication()->GetArguments();
			argList = new StaticBoundList();
			for (size_t i = 1; i < args.size(); i++)
				argList->Append(Value::NewString(args.at(i)));
		}

		result->SetList(argList);
	}
Esempio n. 10
0
static void GetInterfaceList()
{
    if (!interfaceList.isNull())
        return;

    interfaceList = new StaticBoundList();
    std::vector<NetworkInterface> list = NetworkInterface::list();
    for (size_t i = 0; i < list.size(); i++)
    {
        NetworkInterface& interface = list[i];
        interfaceList->Append(Value::NewObject(new Interface(interface)));

        if (!interface.address().isLoopback() &&
            interface.address().isIPv4Compatible())
            firstIPv4Address = interface.address().toString();
    }
}
Esempio n. 11
0
    KListRef APIBinding::ComponentVectorToKList(
        vector<SharedComponent>& components,
        KComponentType filter)
    {
        KListRef componentList = new StaticBoundList();
        vector<SharedComponent>::iterator i = components.begin();
        while (i != components.end())
        {
            SharedComponent c = *i++;
            if (filter == UNKNOWN || filter == c->type)
            {
                KValueRef cValue = Value::NewObject(new ComponentBinding(c));
                componentList->Append(cValue);
            }
        }

        return componentList;
    }
	void PropertiesBinding::GetList(const ValueList& args, KValueRef result)
	{
		KValueRef stringValue = Value::Null;
		GetString(args, stringValue);

		if (!stringValue->IsNull())
		{
			KListRef list = new StaticBoundList();
			std::string string = stringValue->ToString();
			Poco::StringTokenizer t(string, ",", Poco::StringTokenizer::TOK_TRIM);
			for (size_t i = 0; i < t.count(); i++)
			{
				KValueRef token = Value::NewString(t[i].c_str());
				list->Append(token);
			}

			KListRef list2 = list;
			result->SetList(list2);
		}
	}
	void FilesystemBinding::GetRootDirectories(const ValueList& args, KValueRef result)
	{
		try
		{
			Poco::Path path;
			std::vector<std::string> roots;
			path.listRoots(roots);

			KListRef rootList = new StaticBoundList();
			for(size_t i = 0; i < roots.size(); i++)
			{
				ti::File* file = new ti::File(roots.at(i));
				KValueRef value = Value::NewObject((KObjectRef) file);
				rootList->Append(value);
			}

			KListRef list = rootList;
			result->SetList(list);
		}
		catch (Poco::Exception& exc)
		{
			throw ValueException::FromString(exc.displayText());
		}
	}
Esempio n. 14
0
KListRef Win32UserWindow::SelectFile(
 	bool saveDialog,
	bool multiple,
	std::string& title,
	std::string& path,
	std::string& defaultName,
	std::vector<std::string>& types,
	std::string& typesDescription)
{
	std::wstring filter;
	std::wstring typesDescriptionW = UTF8ToWide(typesDescription);
	if (types.size() > 0)
	{
		//"All\0*.*\0Test\0*.TXT\0";
		filter.append(typesDescriptionW);
		filter.push_back(L'\0');
		for (int i = 0; i < types.size(); i++)
		{
			std::string type = types.at(i);
			std::wstring typeW = UTF8ToWide(type);
			//multiple filters: "*.TXT;*.DOC;*.BAK"
			size_t found = type.find("*.");
			if (found != 0)
			{
				filter.append(L"*.");
			}
			filter.append(typeW);
			filter.append(L";");
		}
		filter.push_back(L'\0');
	}

	OPENFILENAME ofn;
	wchar_t filenameW[1024];
	wcscpy(filenameW, UTF8ToWide(defaultName).c_str());
	
	// init OPENFILE
	std::wstring pathW = UTF8ToWide(path);
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = this->windowHandle;
	ofn.lpstrFile = filenameW;
	if (wcslen(filenameW) == 0)
	{
		ofn.lpstrFile[0] = L'\0';
	}
	ofn.nMaxFile = 1024;
	ofn.lpstrFilter = (LPWSTR) (filter.size() == 0 ? NULL : filter.c_str());
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = (LPWSTR) (pathW.length() == 0 ? NULL : pathW.c_str());
	ofn.Flags = OFN_EXPLORER;

	if (!title.empty())
	{
		std::wstring titleW = UTF8ToWide(title);
		ofn.lpstrTitle = titleW.c_str();
	}

	if (!saveDialog)
	{
		ofn.Flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
	}

	if (multiple)
	{
		ofn.Flags |= OFN_ALLOWMULTISELECT;
	}

	KListRef results = new StaticBoundList();
	// display the open dialog box
	BOOL result;

	if (saveDialog)
	{
		result = ::GetSaveFileName(&ofn);
	}
	else
	{
		result = ::GetOpenFileName(&ofn);
	}

	if (result)
	{
		// if the user selected multiple files, ofn.lpstrFile is a NULL-separated list of filenames
		// if the user only selected one file, ofn.lpstrFile is a normal string

		std::vector<std::string> tokens;
		ParseStringNullSeparated(ofn.lpstrFile, tokens);

		if (tokens.size() == 1)
		{
			results->Append(Value::NewString(tokens.at(0)));
		}
		else if (tokens.size() > 1)
		{
			std::string directory(tokens.at(0));
			for (int i = 1; i < tokens.size(); i++)
			{
				std::string n;
				n.append(directory.c_str());
				n.append("\\");
				n.append(tokens.at(i).c_str());
				results->Append(Value::NewString(n));
			}
		}
	}
	else
	{
		DWORD error = CommDlgExtendedError();
		std::string errorMessage = Win32Utils::QuickFormatMessage(error);
		Logger::Get("UI.Win32UserWindow")->Error("Error while opening files: %s", errorMessage.c_str());
		/*
		printf("Error when opening files: %d\n", error);
		switch(error)
		{
			case CDERR_DIALOGFAILURE: printf("CDERR_DIALOGFAILURE\n"); break;
			case CDERR_FINDRESFAILURE: printf("CDERR_FINDRESFAILURE\n"); break;
			case CDERR_NOHINSTANCE: printf("CDERR_NOHINSTANCE\n"); break;
			case CDERR_INITIALIZATION: printf("CDERR_INITIALIZATION\n"); break;
			case CDERR_NOHOOK: printf("CDERR_NOHOOK\n"); break;
			case CDERR_LOCKRESFAILURE: printf("CDERR_LOCKRESFAILURE\n"); break;
			case CDERR_NOTEMPLATE: printf("CDERR_NOTEMPLATE\n"); break;
			case CDERR_LOADRESFAILURE: printf("CDERR_LOADRESFAILURE\n"); break;
			case CDERR_STRUCTSIZE: printf("CDERR_STRUCTSIZE\n"); break;
			case CDERR_LOADSTRFAILURE: printf("CDERR_LOADSTRFAILURE\n"); break;
			case FNERR_BUFFERTOOSMALL: printf("FNERR_BUFFERTOOSMALL\n"); break;
			case CDERR_MEMALLOCFAILURE: printf("CDERR_MEMALLOCFAILURE\n"); break;
			case FNERR_INVALIDFILENAME: printf("FNERR_INVALIDFILENAME\n"); break;
			case CDERR_MEMLOCKFAILURE: printf("CDERR_MEMLOCKFAILURE\n"); break;
			case FNERR_SUBCLASSFAILURE: printf("FNERR_SUBCLASSFAILURE\n"); break;
		}*/
	}
	return results;
}
KListRef Win32UserWindow::SelectFile(bool saveDialog, bool multiple, std::string& title,
	std::string& path, std::string& defaultName, std::vector<std::string>& types,
	std::string& typesDescription)
{
	std::wstring filter;
	std::wstring typesDescriptionW = ::UTF8ToWide(typesDescription);
	if (types.size() > 0)
	{
		//"All\0*.*\0Test\0*.TXT\0";
		if (typesDescription.size() == 0)
		{
			// Reasonable default?
			typesDescriptionW = L"Selected Files";
		}
		filter.append(typesDescriptionW);
		filter.push_back(L'\0');
		
		for (int i = 0; i < types.size(); i++)
		{
			std::string type = types.at(i);
			std::wstring typeW = ::UTF8ToWide(type);
			//multiple filters: "*.TXT;*.DOC;*.BAK"
			size_t found = type.find("*.");
			if (found != 0)
			{
				filter.append(L"*.");
			}
			filter.append(typeW);
			filter.append(L";");
		}
		filter.push_back(L'\0');
	}

	OPENFILENAME ofn;

	std::wstring pathW = ::UTF8ToWide(path);
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = this->windowHandle;

	// Windows may not null-terminate the string it puts here, so we zero it.
	wchar_t filenameW[MAX_FILE_DIALOG_STRING];
	ZeroMemory(&filenameW, MAX_FILE_DIALOG_STRING * sizeof(wchar_t));
	wcscpy(filenameW, ::UTF8ToWide(defaultName).c_str());
	ofn.lpstrFile = filenameW;

	ofn.nMaxFile = MAX_FILE_DIALOG_STRING;
	ofn.lpstrFilter = (LPWSTR) (filter.size() == 0 ? 0 : filter.c_str());
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = 0;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = (LPWSTR) (pathW.length() == 0 ? 0 : pathW.c_str());
	ofn.Flags = OFN_EXPLORER;

	std::wstring titleW;
	if (!title.empty())
	{
		titleW = ::UTF8ToWide(title);
		ofn.lpstrTitle = titleW.c_str();
	}

	if (!saveDialog)
	{
		ofn.Flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
	}

	if (multiple)
	{
		ofn.Flags |= OFN_ALLOWMULTISELECT;
	}

	
	BOOL result;
	if (saveDialog)
	{
		result = ::GetSaveFileName(&ofn);
	}
	else
	{
		result = ::GetOpenFileName(&ofn);
	}

	// A zero-return value here indicates either an error or that the user
	// cancelled the action (CommDlgExtendedError returns 0). We should
	// return a helpful exception if it's an error.
	if (!result)
	{
		DWORD code = CommDlgExtendedError();
		if (code == 0)
			return new StaticBoundList();

		throw ValueException::FromFormat(
			"File dialog action failed with error code: %i", code);
	}

	// From:  http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx
	// If multiple files have been selected there will be two '\0' characters
	// at the end of this array of characters, so if we enabled multiple file
	// selected, just check for that second '\0'.
	KListRef results = new StaticBoundList();
	if (multiple && ofn.lpstrFile[ofn.nFileOffset - 1] == L'\0')
	{
		std::vector<std::wstring> files;
		ParseMultipleSelectedFiles(&ofn, files);
		for (size_t i = 0; i < files.size(); i++)
		{
			results->Append(Value::NewString(
				::WideToUTF8(files[i])));
		}
	}
	else
	{
		results->Append(Value::NewString(::WideToUTF8(
			ofn.lpstrFile)));
	}

	return results;
}