Example #1
0
	/**
	 * Return a File object.
	 */
	void PhoneGapFile::actionGetFileMetadata(JSONMessage& message)
	{
		String callbackID = message.getParam("PhoneGapCallBackId");

		String fullPath = message.getArgsField("fullPath");

		char sizeBuf[64];
		sprintf(sizeBuf, "%i", FileGetSize(fullPath));

		String file = emitFile(
			FileGetName(fullPath),
			fullPath,
			FileGetMimeType(fullPath),
			FileGetDate(fullPath),
			sizeBuf);

		// Note that _castDate also casts a File object.
		callSuccess(
			callbackID,
			file,
			"window.localFileSystem._castDate");
	}
Example #2
0
	void PhoneGapFile::actionReadAsDataURL(JSONMessage& message)
	{
		String callbackID = message.getParam("PhoneGapCallBackId");

		String fullPath = message.getArgsField("fileName");

		String content;
		int result = FileRead(fullPath, content);
		if (result < 0)
		{
			callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR);
			return;
		}

		String base64URL = "\"data:";
		base64URL += FileGetMimeType(fullPath);
		base64URL += ";base64,";
		base64URL += JSONMessage::base64Encode(content.c_str());
		base64URL += "\"";

		// Send back the file content.
		callSuccess(callbackID, base64URL);
	}
Example #3
0
	/**
	 * Event handler for capture events
	 * @param event the event struct.
	 */
	void PhoneGapCapture::customEvent(const MAEvent &event)
	{
		char pathBuffer[1024];
		char messageBuffer[1024];

		if (event.type == EVENT_TYPE_CAPTURE)
		{
			MACaptureEventData eventData = event.captureData;

			switch (eventData.type)
			{
				case MA_CAPTURE_EVENT_TYPE_VIDEO:
					// Videos are already stored, we need the filepath
					maCaptureGetVideoPath(
						eventData.handle,
						pathBuffer,
						sizeof(pathBuffer));
					sprintf(
						messageBuffer,
						"[{\"fullPath\":\"%s\",\"name\":\"%s\"}]",
						pathBuffer,
						FileNameFromPath(pathBuffer));
					mMessageHandler->callSuccess(
						mCaptureCallBack,
						PHONEGAP_CALLBACK_STATUS_OK,
						messageBuffer,
						false);
					maCaptureDestroyData(eventData.handle);
					break;

				case MA_CAPTURE_EVENT_TYPE_IMAGE:
				{
					char deviceOS[64];
					String extension;

					maGetSystemProperty(
						"mosync.device.OS",
						deviceOS,
						sizeof(deviceOS));

					// File format is different on different platforms.
					// TODO: What about WP?
					if (strcmp(deviceOS, "iPhone OS") == 0)
					{
						extension = "png";
					}
					else if(strcmp(deviceOS, "Android") == 0)
					{
						extension = "jpg";
					}
					else
					{
						// TODO: What to use as default?
						extension = "image";
					}

					// Images need to be stored. We use maLocalTime to
					// get a unique number for the filename.
					String localPath = (mMessageHandler->getFileUtil())->getAppPath();
					sprintf(
						pathBuffer,
						"%simg%d.%s",
						localPath.c_str(),
						maLocalTime(),
						extension.c_str());
					int result = maCaptureWriteImage(
						eventData.handle,
						pathBuffer,
						sizeof(pathBuffer));

					// TODO: What should we do if FileGetSize fails?
					// Return an error?
					// Set size to zero for now.
					int size = FileGetSize(pathBuffer);
					if (size < 0)
					{
						size = 0;
					}
					char sizeBuf[64];
					sprintf(sizeBuf, "%i", size);

					sprintf(
						messageBuffer,
						"[{\"fullPath\":\"%s\",\"name\":\"%s\",\"lastModifiedDate\":\"%s\",\"size\":\"%s\",\"type\":\"%s\"}]",
						pathBuffer,
						FileNameFromPath(pathBuffer),
						FileGetDate(pathBuffer).c_str(),
						sizeBuf,
						FileGetMimeType(pathBuffer).c_str());

					if (result == MA_CAPTURE_RES_OK)
					{
						mMessageHandler->callSuccess(
							mCaptureCallBack,
							PHONEGAP_CALLBACK_STATUS_OK,
							messageBuffer,
							false);
					}
					else
					{
						mMessageHandler->callError(
							mCaptureCallBack,
							PHONEGAP_CALLBACK_STATUS_ERROR,
							"{\"code\":\"CAPTURE_INTERNAL_ERR\"}",
							false);
					}

					// Free capture data.
					maCaptureDestroyData(eventData.handle);

					break;
				}

				case MA_CAPTURE_EVENT_TYPE_CANCEL:
					mMessageHandler->callError(
						mCaptureCallBack,
						PHONEGAP_CALLBACK_STATUS_ERROR,
						"{\"code\":\"CAPTURE_NO_MEDIA_FILES\"}",
						false);
					break;
			} // switch
		}
	}