#include "config.h"
#include "FileInputType.h"
#include "Event.h"
#include "File.h"
#include "FileList.h"
#include "FileSystem.h"
#include "FormDataList.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "LocalizedStrings.h"
#include "RenderFileUploadControl.h"
#include <wtf/PassOwnPtr.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
using namespace HTMLNames;
inline FileInputType::FileInputType(HTMLInputElement* element)
: BaseButtonInputType(element)
, m_fileList(FileList::create())
{
}
PassOwnPtr<InputType> FileInputType::create(HTMLInputElement* element)
{
return adoptPtr(new FileInputType(element));
}
const AtomicString& FileInputType::formControlType() const
{
return InputTypeNames::file();
}
bool FileInputType::appendFormData(FormDataList& encoding, bool multipart) const
{
FileList* fileList = element()->files();
unsigned numFiles = fileList->length();
if (!multipart) {
for (unsigned i = 0; i < numFiles; ++i)
encoding.appendData(element()->name(), fileList->item(i)->fileName());
return true;
}
if (!numFiles) {
encoding.appendBlob(element()->name(), File::create(""));
return true;
}
for (unsigned i = 0; i < numFiles; ++i)
encoding.appendBlob(element()->name(), fileList->item(i));
return true;
}
bool FileInputType::valueMissing(const String& value) const
{
return value.isEmpty();
}
String FileInputType::valueMissingText() const
{
return element()->multiple() ? validationMessageValueMissingForMultipleFileText() : validationMessageValueMissingForFileText();
}
void FileInputType::handleDOMActivateEvent(Event* event)
{
if (element()->disabled() || !element()->renderer())
return;
toRenderFileUploadControl(element()->renderer())->click();
event->setDefaultHandled();
}
RenderObject* FileInputType::createRenderer(RenderArena* arena, RenderStyle*) const
{
return new (arena) RenderFileUploadControl(element());
}
bool FileInputType::canSetStringValue() const
{
return false;
}
bool FileInputType::canChangeFromAnotherType() const
{
return false;
}
FileList* FileInputType::files()
{
return m_fileList.get();
}
bool FileInputType::canSetValue(const String& value)
{
return value.isEmpty();
}
bool FileInputType::getTypeSpecificValue(String& value)
{
if (m_fileList->isEmpty()) {
value = String();
return true;
}
value = "C:\\fakepath\\" + m_fileList->item(0)->fileName();
return true;
}
bool FileInputType::storesValueSeparateFromAttribute()
{
return true;
}
void FileInputType::setFileList(const Vector<String>& paths)
{
m_fileList->clear();
size_t size = paths.size();
#if ENABLE(DIRECTORY_UPLOAD)
if (size && element()->fastHasAttribute(webkitdirectoryAttr)) {
String rootPath = directoryName(paths[0]);
for (size_t i = 1; i < size; i++) {
while (!paths[i].startsWith(rootPath))
rootPath = directoryName(rootPath);
}
rootPath = directoryName(rootPath);
ASSERT(rootPath.length());
for (size_t i = 0; i < size; i++) {
String relativePath = paths[i].substring(1 + rootPath.length()).replace('\\', '/');
m_fileList->append(File::create(relativePath, paths[i]));
}
return;
}
#endif
for (size_t i = 0; i < size; i++)
m_fileList->append(File::create(paths[i]));
}
bool FileInputType::isFileUpload() const
{
return true;
}
}