void WorkerBinding::CreateWorker(const ValueList& args, SharedValue result)
	{
		if (args.size()!=2)
		{
			throw ValueException::FromString("invalid argument specified");
		}
		
		bool is_function = args.at(1)->ToBool();
		
		std::string code;
		Logger *logger = Logger::Get("Worker");
		
		if (is_function)
		{
			// convert the function to a string block 
			code =  "(";
			code += args.at(0)->ToString();
			code += ")()";
		}
		else 
		{
			// this is a path -- probably should verify that this is relative and not an absolute URL to remote
			SharedKMethod appURLToPath = global->GetNS("App.appURLToPath")->ToMethod();
			ValueList a;
			a.push_back(args.at(0));
			SharedValue result = appURLToPath->Call(a);
			const char *path = result->ToString();
			
			logger->Debug("worker file path = %s",path);
			
			std::ios::openmode flags = std::ios::in;
			Poco::FileInputStream *fis = new Poco::FileInputStream(path,flags);
			std::stringstream ostr;
			char buf[8096];
			int count = 0;
			while(!fis->eof())
			{
				fis->read((char*)&buf,8095);
				std::streamsize len = fis->gcount();
				if (len>0)
				{
					buf[len]='\0';
					count+=len;
					ostr << buf;
				}
				else break;
			}
			fis->close();
			code = std::string(ostr.str());
		}
		
		logger->Debug("Worker script code = %s", code.c_str());
		
		SharedKObject worker = new Worker(host,global,code);
		result->SetObject(worker);
	}
Example #2
0
void UserWindow::_CreateWindow(const ValueList& args, SharedValue result)
{
	//TODO: wrap in sharedptr
	WindowConfig *config = NULL;

	if (args.size() > 0 && args.at(0)->IsObject())
	{
		SharedKObject props = SharedKObject(new StaticBoundObject());
		config = new WindowConfig();
		props = args.at(0)->ToObject();
		config->UseProperties(props);
	}
	else if (args.size() > 0 && args.at(0)->IsString())
	{
		// String might match a url spec
		std::string url = args.at(0)->ToString();
		WindowConfig* matchedConfig = AppConfig::Instance()->GetWindowByURL(url);

		url = AppConfig::Instance()->InsertAppIDIntoURL(url);
		config = new WindowConfig(matchedConfig, url);
	}
	else
	{
		config = new WindowConfig();
	}

	SharedUserWindow new_window = this->binding->CreateWindow(config, shared_this);
	result->SetObject(new_window);
}
	void DatabaseModule::Open(const ValueList& args, SharedValue result)
	{
		DatabaseBinding *db = new DatabaseBinding(host);
		db->Open(args,result);
		SharedKObject kdb = db;
		result->SetObject(kdb);
	}
	void HttpServerRequest::Read(const ValueList& args, SharedValue result)
	{
		std::istream &in = request.stream();
		if (in.eof() || in.fail())
		{
			result->SetNull();
			return;
		}
		int max_size = 8096;
		if (args.size()==1)
		{
			max_size = args.at(0)->ToInt();
		}
		char *buf = new char[max_size];
		in.read(buf,max_size);
		std::streamsize count = in.gcount();
		if (count == 0)
		{
			result->SetNull();
		}
		else
		{
			result->SetObject(new Blob(buf,count));
		}
		delete [] buf;
	}
Example #5
0
	void Menu::_AddCheckItem(const ValueList& args, SharedValue result)
	{
		UIBinding* binding = UIBinding::GetInstance();
		AutoMenuItem newItem = binding->__CreateCheckMenuItem(args);
		this->AppendItem(newItem);
		result->SetObject(newItem);
	}
	void AppBinding::CreateProperties(const ValueList& args, SharedValue result)
	{
		AutoPtr<PropertiesBinding> properties = new PropertiesBinding();
		result->SetObject(properties);
		
		if (args.size() > 0 && args.at(0)->IsObject())
		{
			SharedKObject p = args.at(0)->ToObject();
			SharedStringList names = p->GetPropertyNames();
			for (size_t i = 0; i < names->size(); i++)
			{
				SharedValue value = p->Get(names->at(i));
				ValueList setterArgs;
				setterArgs.push_back(Value::NewString(names->at(i)));
				setterArgs.push_back(value);
				PropertiesBinding::Type type;
				
				if (value->IsList()) type = PropertiesBinding::List;
				else if (value->IsInt()) type = PropertiesBinding::Int;
				else if (value->IsDouble()) type = PropertiesBinding::Double;
				else if (value->IsBool()) type = PropertiesBinding::Bool;
				else type = PropertiesBinding::String;
				
				properties->Setter(setterArgs, type);
			}
		}
	}
Example #7
0
	void NetworkBinding::GetProxy(const ValueList& args, SharedValue result)
	{
		if(proxy)
		{
			result->SetObject(this->proxy);
		}
	}
Example #8
0
	void AppBinding::LoadProperties(const ValueList& args, SharedValue result)
	{
		if (args.size() >= 1 && args.at(0)->IsString()) {
			std::string file_path = args.at(1)->ToString();
			SharedBoundObject properties = new PropertiesBinding(file_path);
			result->SetObject(properties);
		}
	}
	void MediaBinding::_CreateSound(const ValueList& args, SharedValue result)
	{
		if (args.size()!=1)
			throw ValueException::FromString("createSound takes 1 parameter");

		std::string path(args.at(0)->ToString());
		result->SetObject(this->CreateSound(path));
	}
Example #10
0
	void NetworkBinding::_GetByHost(std::string hostname, SharedValue result)
	{
		SharedPtr<HostBinding> binding = new HostBinding(hostname);
		if (binding->IsInvalid())
		{
			throw ValueException::FromString("Could not resolve address");
		}
		result->SetObject(binding);
	}
Example #11
0
	void NetworkBinding::CreateIPAddress(const ValueList& args, SharedValue result)
	{
		SharedPtr<IPAddressBinding> binding = new IPAddressBinding(args.at(0)->ToString());
		if (binding->IsInvalid())
		{
			throw ValueException::FromString("Invalid address");
		}
		result->SetObject(binding);
	}
	void MenuItem::_AddSeparatorItem(const ValueList& args, SharedValue result)
	{
		UIBinding* binding = UIBinding::GetInstance();
		AutoMenuItem newItem = binding->__CreateSeparatorMenuItem(args);
		this->EnsureHasSubmenu();
		this->submenu->AppendItem(newItem);

		result->SetObject(newItem);
	}
Example #13
0
	void NetworkBinding::CreateHTTPServer(const ValueList& args, SharedValue result)
	{
		HTTPServerBinding* http = new HTTPServerBinding(host);
		SharedKObject obj = http->GetSelf()->ToObject();
		// we hold the reference to this until we're done with it
		// which happense when the binding impl calls remove
		this->bindings.push_back(obj);
		result->SetObject(obj);
	}
Example #14
0
	void Menu::_AddItem(const ValueList& args, SharedValue result)
	{
		args.VerifyException("addItem", "?s m|0 s|0");
		UIBinding* binding = UIBinding::GetInstance();

		// Create a menu item object and add it to this item's submenu
		AutoMenuItem newItem = binding->__CreateMenuItem(args);
		this->AppendItem(newItem);
		result->SetObject(newItem);
	}
	void UIBinding::_AddTray(const ValueList& args, SharedValue result)
	{
		args.VerifyException("createTrayIcon", "s,?m");
		std::string iconURL = args.GetString(0);

		SharedKMethod cb = args.GetMethod(1, NULL);
		AutoTrayItem item = this->AddTray(iconURL, cb);
		this->trayItems.push_back(item);
		result->SetObject(item);
	}
	void MenuItem::_AddCheckItem(const ValueList& args, SharedValue result)
	{
		UIBinding* binding = UIBinding::GetInstance();

		// Create a menu item object
		AutoMenuItem newItem = binding->__CreateCheckMenuItem(args);
		this->EnsureHasSubmenu();
		this->submenu->AppendItem(newItem);

		result->SetObject(newItem);
	}
	void MenuItem::_GetSubmenu(const ValueList& args, SharedValue result)
	{
		if (this->submenu.isNull())
		{
			result->SetNull();
		}
		else
		{
			result->SetObject(this->submenu);
		}
	}
	void UIBinding::_GetMenu(const ValueList& args, SharedValue result)
	{
		AutoMenu menu = this->GetMenu();
		if (menu.isNull())
		{
			result->SetNull();
		}
		else
		{
			result->SetObject(menu);
		}
	}
Example #19
0
	void FileStream::Read(const ValueList& args, SharedValue result)
	{
		if(!this->stream)
		{
			Logger* logger = Logger::Get("Filesystem.FileStream");
			logger->Error("Error in read. FileStream must be opened before calling read");
			throw ValueException::FromString("FileStream must be opened before calling read");
		}

		try
		{
			std::stringstream ostr;

			Poco::FileInputStream* fis = dynamic_cast<Poco::FileInputStream*>(this->stream);
			if(!fis)
			{
				Logger* logger = Logger::Get("Filesystem.FileStream");
				logger->Error("Error in read. FileInputStream is null");
				throw ValueException::FromString("FileStream must be opened for reading before calling read");
			}
			
			char buf[4096];
			int count = 0;

			while(!fis->eof())
			{
				fis->read((char*)&buf,4095);
				std::streamsize len = fis->gcount();
				if (len>0)
				{
					buf[len]='\0';
					count+=len;
					ostr << buf;
				}
				else break;
			}

			result->SetObject(new Blob(ostr.str().c_str(),count));
		}
		catch (Poco::Exception& exc)
		{
			Logger* logger = Logger::Get("Filesystem.FileStream");
			logger->Error("Error in read. Exception: %s",exc.displayText().c_str());
			throw ValueException::FromString(exc.displayText());
		}
	}
Example #20
0
	void NetworkBinding::GetHostByAddress(const ValueList& args, SharedValue result)
	{
		if (args.at(0)->IsObject())
		{
			SharedKObject obj = args.at(0)->ToObject();
			SharedPtr<IPAddressBinding> b = obj.cast<IPAddressBinding>();
			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
				IPAddress addr(b->GetAddress()->toString());
				SharedPtr<HostBinding> binding = new HostBinding(addr);
				if (binding->IsInvalid())
				{
					throw ValueException::FromString("Could not resolve address");
				}
				result->SetObject(binding);
				return;
			}
			else
			{
				SharedValue bo = obj->Get("toString");
				if (bo->IsMethod())
				{
					SharedKMethod m = bo->ToMethod();
					ValueList args;
					SharedValue tostr = m->Call(args);
					this->_GetByHost(tostr->ToString(),result);
					return;
				}
				throw ValueException::FromString("Unknown object passed");
			}
		}
		else if (args.at(0)->IsString())
		{
			// in this case, they just passed in a string so resolve as
			// normal
			this->_GetByHost(args.at(0)->ToString(),result);
		}
	}
Example #21
0
	void Blob::Concat(const ValueList& args, SharedValue result)
	{
		std::vector<AutoBlob> blobs;
		for (size_t i = 0; i < args.size(); i++)
		{
			if (args.at(i)->IsObject())
			{
				AutoBlob blob = args.GetObject(i).cast<Blob>();
				if (!blob.isNull())
				{
					blobs.push_back(blob);
				}
			}
			else if (args.at(i)->IsString())
			{
				blobs.push_back(new Blob(args.GetString(i)));
			}
		}
		
		AutoBlob newBlob = this->Concat(blobs);
		result->SetObject(newBlob);
	}
Example #22
0
	void FileStream::ReadLine(const ValueList& args, SharedValue result)
	{
		if(! this->stream)
		{
			Logger* logger = Logger::Get("Filesystem.FileStream");
			logger->Error("Error in readLine. FileStream must be opened before calling read");
			throw ValueException::FromString("FileStream must be opened before calling readLine");
		}

		try
		{
			Poco::FileInputStream* fis = dynamic_cast<Poco::FileInputStream*>(this->stream);
			if(!fis)
			{
				Logger* logger = Logger::Get("Filesystem.FileStream");
				logger->Error("Error in readLine. FileInputStream is null");
				throw ValueException::FromString("FileStream must be opened for reading before calling readLine");
			}

			if(fis->eof())
			{
				// close the file
				result->SetNull();
			}
			else
			{
				std::string line;
				std::getline(*fis, line);
				result->SetObject(new Blob((std::string)line));
			}
		}
		catch (Poco::Exception& exc)
		{
			Logger* logger = Logger::Get("Filesystem.FileStream");
			logger->Error("Error in readLine. Exception: %s",exc.displayText().c_str());
			throw ValueException::FromString(exc.displayText());
		}
	}
Example #23
0
	void NetworkBinding::CreateTCPSocket(const ValueList& args, SharedValue result)
	{
		//TODO: check for args
		SharedPtr<TCPSocketBinding> tcp = new TCPSocketBinding(host, args.at(0)->ToString(), args.at(1)->ToInt());
		result->SetObject(tcp);
	}
Example #24
0
	void NetworkBinding::CreateIRCClient(const ValueList& args, SharedValue result)
	{
		SharedPtr<IRCClientBinding> irc = new IRCClientBinding(host);
		result->SetObject(irc);
	}
Example #25
0
	void Event::_GetTarget(const ValueList&, SharedValue result)
	{
		result->SetObject(this->target);
	}
	void UIBinding::_GetContextMenu(const ValueList& args, SharedValue result)
	{
		AutoMenu menu = this->GetContextMenu();
		result->SetObject(menu);
	}
Example #27
0
	void Menu::_GetItemAt(const ValueList& args, SharedValue result)
	{
		args.VerifyException("getItemAt", "i");
		AutoMenuItem item = this->GetItemAt(args.GetInt(0));
		result->SetObject(item);
	}
	void UIBinding::_CreateSeparatorMenuItem(const ValueList& args, SharedValue result)
	{
		result->SetObject(__CreateSeparatorMenuItem(args));
	}
	void UIBinding::_CreateMenu(const ValueList& args, SharedValue result)
	{
		result->SetObject(__CreateMenu(args));
	}
	void UIBinding::_GetMainWindow(const ValueList& args, SharedValue result)
	{
		result->SetObject(this->mainWindow);
	}