Commit 28293c06 authored by coderus's avatar coderus

FilePicker GetFile and GetFileURL implementation

GetFiles not implemented until https://github.com/tmeshkova/mozilla-central/issues/12 not fixed
parent f34e96f4
......@@ -9,6 +9,8 @@
#include "nsIWidget.h"
#include "nsDirectoryServiceDefs.h"
#include "nsIEmbedLiteJSON.h"
#include "nsNetUtil.h"
#include "nsIURI.h"
//-----------------------------
......@@ -18,7 +20,6 @@ NS_IMPL_ISUPPORTS1(nsEmbedFilePicker, nsIFilePicker)
nsEmbedFilePicker::nsEmbedFilePicker()
{
mModalDepth = 0;
mService = do_GetService("@mozilla.org/embedlite-app-service;1");
/* member initializers and constructor code */
}
......@@ -29,10 +30,10 @@ nsEmbedFilePicker::~nsEmbedFilePicker()
}
NS_IMETHODIMP nsEmbedFilePicker::Init(nsIDOMWindow* parent, const nsAString& title, int16_t mode)
: mWin(parent)
, mModalDepth(0)
, mTitle(title)
{
mWin = parent;
mModalDepth = 0;
mTitle.Assign(title);
printf("nsEmbedFilePicker::Init IMPLEMENTED: win:%p, title:%s, mode:%i\n", parent, NS_ConvertUTF16toUTF8(title).get(), mode);
// return NS_ERROR_NOT_IMPLEMENTED;
return NS_OK;
......@@ -101,31 +102,53 @@ NS_IMETHODIMP nsEmbedFilePicker::SetDisplayDirectory(nsIFile* aDisplayDirectory)
NS_IMETHODIMP nsEmbedFilePicker::GetFile(nsIFile* *aFile)
{
printf("nsEmbedFilePicker::GetFile IMPLEMENTED\n");
// return NS_ERROR_NOT_IMPLEMENTED;
NS_ENSURE_ARG_POINTER(aFile);
*aFile = nullptr;
EmbedFilePickerResponse response = DoSendAsyncPrompt(0);
if (response.accepted) {
NS_ENSURE_ARG_POINTER(aFile);
nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
mFile.AssignLiteral("/home/user/test.deb");
file->InitWithNativePath(mFile);
*aFile = nullptr;
NS_ADDREF(*aFile = file);
nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
file->InitWithNativePath(NS_ConvertUTF16toUTF8(response.filePath));
return NS_OK;
NS_ADDREF(*aFile = file);
return NS_OK;
}
else {
return NS_ERROR_ABORT;
}
}
NS_IMETHODIMP nsEmbedFilePicker::GetFileURL(nsIURI* *aFileURL)
{
printf("nsEmbedFilePicker::GetFileURL NOT IMPLEMENTED\n");
return NS_ERROR_NOT_IMPLEMENTED;
*aFileURL = nullptr;
EmbedFilePickerResponse response = DoSendAsyncPrompt(0);
if (response.accepted) {
return NS_NewURI(aFileURL, NS_ConvertUTF16toUTF8(response.filePath));
}
else {
return NS_ERROR_ABORT;
}
}
NS_IMETHODIMP nsEmbedFilePicker::GetFiles(nsISimpleEnumerator* *aFiles)
{
printf("nsEmbedFilePicker::GetFileURL NOT IMPLEMENTED\n");
return NS_ERROR_NOT_IMPLEMENTED;
NS_ENSURE_ARG_POINTER(aFiles);
EmbedFilePickerResponse response = DoSendAsyncPrompt(2);
if (response.accepted) {
nsCOMArray<nsIFile> mFiles;
return NS_OK;
}
else {
return NS_ERROR_ABORT;
}
}
NS_IMETHODIMP nsEmbedFilePicker::GetAddToRecentDocs(bool* aAddToRecentDocs)
......@@ -153,7 +176,7 @@ NS_IMETHODIMP nsEmbedFilePicker::Open(nsIFilePickerShownCallback* aFilePickerSho
return NS_OK;
}
nsresult
EmbedFilePickerResponse
nsEmbedFilePicker::DoSendAsyncPrompt(int mode)
{
uint32_t winid;
......@@ -166,17 +189,22 @@ nsEmbedFilePicker::DoSendAsyncPrompt(int mode)
json->CreateObject(getter_AddRefs(root));
root->SetPropertyAsUint32(NS_LITERAL_STRING("winid"), winid);
root->SetPropertyAsUint32(NS_LITERAL_STRING("mode"), mode);
root->SetPropertyAsAString(NS_LITERAL_STRING("title"), mTitle);
json->CreateJSON(root, sendString);
mService->SendAsyncMessage(0, NS_LITERAL_STRING("embed:filepicker").get(), sendString.get());
mResponseMap[winid] = EmbedFilePickerResponse();
mService->SendAsyncMessage(winid, NS_LITERAL_STRING("embed:filepicker").get(), sendString.get());
mService->AddMessageListener("filepickerresponse", this);
nsresult rv(NS_OK);
mService->EnterSecureJSContext();
EmbedFilePickerResponse response;
nsCOMPtr<nsIDOMWindowUtils> utils = do_GetInterface(mWin);
NS_ENSURE_TRUE(utils, NS_ERROR_FAILURE);
NS_ENSURE_TRUE(utils, response);
nsCOMPtr<nsIDOMWindow> modalStateWin;
rv = utils->EnterModalStateWithWindow(getter_AddRefs(modalStateWin));
......@@ -193,24 +221,38 @@ nsEmbedFilePicker::DoSendAsyncPrompt(int mode)
}
}
mService->RemoveMessageListener("promptresponse", this);
std::map<uint32_t, EmbedFilePickerResponse>::iterator it = mResponseMap.find(winid);
if (it == mResponseMap.end()) {
return response;
}
rv = utils->LeaveModalStateWithWindow(modalStateWin);
mService->LeaveSecureJSContext();
return NS_OK;
response = it->second;
mResponseMap.erase(it);
return response;
}
NS_IMETHODIMP
nsEmbedFilePicker::OnMessageReceived(const char* messageName, const PRUnichar* message)
{
nsCOMPtr<nsIEmbedLiteJSON> json = do_GetService("@mozilla.org/embedlite-json;1");
printf(">>>>>>Func:%s::%d name:%s, msg:%s\n", __PRETTY_FUNCTION__, __LINE__, messageName, NS_ConvertUTF16toUTF8(message).get());
nsCOMPtr<nsIPropertyBag2> root;
NS_ENSURE_SUCCESS(json->ParseJSON(nsDependentString(message), getter_AddRefs(root)), NS_ERROR_FAILURE);
uint32_t winid = 0;
root->GetPropertyAsUint32(NS_LITERAL_STRING("winid"), &winid);
bool accepted;
root->GetPropertyAsBool(NS_LITERAL_STRING("accepted"), accepted);
std::map<uint32_t, EmbedFilePickerResponse>::iterator it = mResponseMap.find(winid);
if (it == mResponseMap.end()) {
mModalDepth--;
return NS_ERROR_FAILURE;
}
EmbedFilePickerResponse& response = it->second;
root->GetPropertyAsBool(NS_LITERAL_STRING("accepted"), &response.accepted);
root->GetPropertyAsAString(NS_LITERAL_STRING("items"), response.filePath);
mModalDepth--;
......
......@@ -10,9 +10,23 @@
#include "nsStringGlue.h"
#include "nsIFilePicker.h"
#include "nsIEmbedAppService.h"
#include "nsIDOMWindowUtils.h"
#include "nsCOMPtr.h"
#include <map>
#include <string>
class EmbedFilePickerResponse
{
public:
EmbedFilePickerResponse()
: accepted(false)
{}
virtual ~EmbedFilePickerResponse() {}
bool accepted;
nsString filePath;
};
class nsEmbedFilePicker : public nsIFilePicker, public nsIEmbedMessageListener
{
public:
......@@ -24,12 +38,12 @@ public:
private:
~nsEmbedFilePicker();
nsresult DoSendAsyncPrompt(int mode);
nsCString mFile;
EmbedFilePickerResponse DoSendAsyncPrompt(int mode);
int mModalDepth;
nsCOMPtr<nsIEmbedAppService> mService;
nsCOMPtr<nsIDOMWindow> mWin;
nsAString mTitle;
nsString mTitle;
std::map<uint32_t, EmbedFilePickerResponse> mResponseMap;
};
#define NS_EMBED_FILEPICKER_SERVICE_CID \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment