Serialize Download Datasource -> XML in profile directory, some initial command support (launch, reveal, download file) in the UI & at datasource level.

NOT PART OF BUILD.
This commit is contained in:
ben%netscape.com 2001-11-16 00:23:01 +00:00
Родитель d8934e6880
Коммит 68cd7ad7bf
8 изменённых файлов: 296 добавлений и 34 удалений

Просмотреть файл

@ -55,8 +55,8 @@ interface nsIDownloadManager : nsISupports {
%{C++
#define NS_IDOWNLOADMANAGER_CONTRACTID "@mozilla.org/downloadmgr;1"
#define NS_IDOWNLOADMANAGER_CLASSNAME "Mozilla Download Manager"
#define NS_DOWNLOADMANAGER_CONTRACTID "@mozilla.org/download-manager;1"
#define NS_DOWNLOADMANAGER_CLASSNAME "Mozilla Download Manager"
// {EDB0490E-1DD1-11B2-83B8-DBF8D85906A6}
#define NS_DOWNLOADMANAGER_CID \
{ 0xedb0490e, 0x1dd1, 0x11b2, { 0x83, 0xb8, 0xdb, 0xf8, 0xd8, 0x59, 0x06, 0xa6 } }

Просмотреть файл

@ -36,11 +36,27 @@
*
* ***** END LICENSE BLOCK ***** */
var NC_NS = "http://home.netscape.com/NC-rdf#";
var gDownloadView = null;
var gDownloadViewChildren = null;
var gRDFService = null;
var gNC_File = null;
function NODE_ID(aElement)
{
return aElement.getAttribute("ref") || aElement.id;
}
function Startup()
{
const rdfSvcContractID = "@mozilla.org/rdf/rdf-service;1";
const rdfSvcIID = Components.interfaces.nsIRDFService;
gRDFService = Components.classes[rdfSvcContractID].getService(rdfSvcIID);
gNC_File = gRDFService.GetResource(NC_NS + "File");
gDownloadView = document.getElementById("downloadView");
gDownloadViewChildren = document.getElementById("downloadViewChildren");
@ -51,6 +67,31 @@ function Startup()
gDownloadView.controllers.appendController(downloadViewController);
}
function Test()
{
// Test code:
var uriContractID = "@mozilla.org/network/standard-url;1";
var uriIID = Components.interfaces.nsIURI;
var uri = Components.classes[uriContractID].createInstance(uriIID);
uri.spec = "http://www.silverstone.net.nz/mozilla/fsmoz.png";
var lfContractID = "@mozilla.org/file/local;1";
var lfIID = Components.interfaces.nsILocalFile;
var lf = Components.classes[lfContractID].createInstance(lfIID);
lf.initWithPath("C:\\GOATS\\goat.png");
var dlmgrContractID = "@mozilla.org/download-manager;1";
var dlmgrIID = Components.interfaces.nsIDownloadManager;
var dlmgr = Components.classes[dlmgrContractID].getService(dlmgrIID);
var ds = dlmgr.QueryInterface(Components.interfaces.nsIRDFDataSource);
gDownloadView.database.AddDataSource(ds);
gDownloadView.builder.rebuild();
dlmgr.addItem("Some File", uri, lf, null, null);
gDownloadView.builder.rebuild();
dump("*** goat\n");
}
function Shutdown()
{
@ -92,10 +133,15 @@ var downloadViewController = {
var selectionCount = gDownloadView.selectedItems.length;
switch (aCommand) {
case "cmd_downloadFile":
downloadFile();
return true;
case "cmd_properties":
case "cmd_openfile":
case "cmd_showinshell":
if (selectionCount != 1)
return false;
var file = getFileForItem(gDownloadView.selectedItems[0]);
return file.exists();
case "cmd_properties":
return selectionCount == 1;
case "cmd_pause":
case "cmd_delete":
@ -109,6 +155,7 @@ var downloadViewController = {
doCommand: function dVC_doCommand (aCommand)
{
dump("*** command = " + aCommand + "\n");
var selection = gDownloadView.selectedItems;
switch (aCommand) {
case "cmd_downloadFile":
dump("*** show a dialog that lets a user specify a URL to download\n");
@ -118,9 +165,13 @@ var downloadViewController = {
break;
case "cmd_openfile":
dump("*** launch the file for the selected item\n");
var file = getFileForItem(selection[0]);
file.launch();
break;
case "cmd_showinshell":
dump("*** show the containing folder for the selected item\n");
var file = getFileForItem(selection[0]);
file.reveal();
break;
case "cmd_pause":
dump("*** pause the transfer for the selected item\n");
@ -155,3 +206,61 @@ var downloadViewController = {
}
};
function downloadFile()
{
var bundle = document.getElementById("downloadBundle");
// Select a file to download
const promptContractID = "@mozilla.org/embedcomp/prompt-service;1";
const promptIID = Components.interfaces.nsIPromptService;
var promptSvc = Components.classes[promptContractID].getService(promptIID);
var downloadFileTitle = bundle.getString("downloadFileTitle");
var downloadFileMsg = bundle.getString("downloadFileMsg");
var rv = { value: "" };
var accept = promptSvc.prompt(window, downloadFileTitle, downloadFileMsg,
rv, null, { });
if (accept && rv.value != "") {
// Now select a location to save it to
const fpContractID = "@mozilla.org/filepicker;1";
const fpIID = Components.interfaces.nsIFilePicker;
var fp = Components.classes[fpContractID].getService(fpIID);
// XXX-todo: make this file picker use the user's download folder
var title = bundle.getString("chooseDestinationTitle");
fp.init(window, title, fpIID.modeSave);
fp.appendFilters(fpIID.filterAll);
if (fp.show() == fpIID.returnOK) {
var uriContractID = "@mozilla.org/network/standard-url;1";
var uriIID = Components.interfaces.nsIURI;
var uri = Components.classes[uriContractID].createInstance(uriIID);
uri.spec = rv.value;
var dlmgrContractID = "@mozilla.org/download-manager;1";
var dlmgrIID = Components.interfaces.nsIDownloadManager;
var dlmgr = Components.classes[dlmgrContractID].getService(dlmgrIID);
dlmgr.addItem(fp.file.leafName, uri, fp.file, null, null);
}
}
}
function getFileForItem(aElement)
{
var itemResource = gRDFService.GetResource(NODE_ID(aElement));
var fileResource = gDownloadView.database.GetTarget(itemResource, gNC_File, true);
fileResource = fileResource.QueryInterface(Components.interfaces.nsIRDFResource);
return createLocalFile(fileResource.Value);
}
function createLocalFile(aFilePath)
{
var lfContractID = "@mozilla.org/file/local;1";
var lfIID = Components.interfaces.nsILocalFile;
var lf = Components.classes[lfContractID].createInstance(lfIID);
dump("*** aPath = " + aFilePath + "\n");
lf.initWithPath(aFilePath);
return lf;
}

Просмотреть файл

@ -50,6 +50,9 @@
<script type="application/x-javascript" src="chrome://global/content/treePopups.js"></script>
<script type="application/x-javascript" src="chrome://communicator/content/downloadmanager/downloadmanager.js"></script>
<stringbundle id="downloadBundle"
src="chrome://communicator/locale/downloadmanager/downloadmanager.properties"/>
<commands id="commands">
<commandset id="commandUpdate_Downloads"
@ -115,6 +118,8 @@
<toolbarseparator/>
<toolbarbutton id="btn_openfile" command="cmd_openfile"/>
<toolbarbutton id="btn_showinshell" command="cmd_showinshell"/>
<toolbarseparator/>
<toolbarbutton label="Test" oncommand="Test();"/>
</toolbar>
<tree id="downloadView" ref="NC:DownloadsRoot"
@ -167,8 +172,8 @@
rdf:type="rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#type">
<treerow>
<treecell class="treecell-indent"
label="rdf:http://home.netscape.com/NC-rdf#Name"
src="rdf:http://home.netscape.com/NC-rdf#Icon"
label="rdf:http://home.netscape.com/NC-rdf#File"
src="moz-icon:rdf:http://home.netscape.com/NC-rdf#File"
rdf:type="rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#type"/>
<treecell>
<progressmeter class="tree-progressmeter" value="rdf:http://home.netscape.com/NC-rdf#Progress"/>

Просмотреть файл

Просмотреть файл

@ -1,3 +1,4 @@
en-US.jar:
locale/en-US/communicator/downloadmanager/downloadmanager.ent
locale/en-US/communicator/downloadmanager/downloadmanager.properties

Просмотреть файл

@ -26,7 +26,8 @@ REQUIRES = xpcom \
rdf \
uriloader \
necko \
webBrowser_core \
intl \
webbrowserpersist \
$(NULL)
CPP_OBJS= \

Просмотреть файл

@ -37,22 +37,37 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsRDFCID.h"
#include "nsDownloadManager.h"
#include "nsIServiceManager.h"
#include "nsIWebProgress.h"
#include "nsDownloadManager.h"
#include "nsIStringBundle.h"
#include "nsIRDFLiteral.h"
#include "nsIRDFXMLSerializer.h"
#include "nsIRDFXMLSource.h"
#include "rdf.h"
#include "nsRDFCID.h"
#include "nsCRT.h"
#include "nsString.h"
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
#include "nsAppDirectoryServiceDefs.h"
#include "nsFileSpec.h"
#include "nsFileStream.h"
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
#define PROFILE_DOWNLOAD_FILE "downloads.rdf"
#define NSDOWNLOADMANAGER_PROPERTIES_URI "chrome://communicator/locale/downloadmanager/downloadmanager.properties"
nsIRDFResource* gNC_DownloadsRoot;
nsIRDFResource* gNC_File;
nsIRDFResource* gNC_URL;
nsIRDFResource* gNC_Name;
nsIRDFResource* gNC_Progress;
NS_IMPL_ISUPPORTS1(nsDownloadManager, nsIDownloadManager)
nsIRDFService* gRDFService;
NS_IMPL_ISUPPORTS3(nsDownloadManager, nsIDownloadManager, nsIRDFDataSource, nsIRDFRemoteDataSource)
nsDownloadManager::nsDownloadManager()
{
@ -61,9 +76,15 @@ nsDownloadManager::nsDownloadManager()
nsDownloadManager::~nsDownloadManager()
{
mRDFService->UnregisterDataSource(this);
gRDFService->UnregisterDataSource(this);
NS_IF_RELEASE(gNC_DownloadsRoot);
NS_IF_RELEASE(gNC_File);
NS_IF_RELEASE(gNC_URL);
NS_IF_RELEASE(gNC_Name);
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
gRDFService = nsnull;
}
nsresult
@ -71,18 +92,23 @@ nsDownloadManager::Init()
{
nsresult rv;
mRDFService = do_GetService("@mozilla.org/rdf/rdf-service;1", &rv);
rv = nsServiceManager::GetService(kRDFServiceCID, NS_GET_IID(nsIRDFService),
(nsISupports**) &gRDFService);
if (NS_FAILED(rv)) return rv;
mRDFContainerUtils = do_GetService("@mozilla.org/rdf/container-utils;1", &rv);
if (NS_FAILED(rv)) return rv;
mRDFService->GetResource("NC:Downloads", &gNC_DownloadsRoot);
gRDFService->GetResource("NC:DownloadsRoot", &gNC_DownloadsRoot);
gRDFService->GetResource(NC_NAMESPACE_URI "File", &gNC_File);
gRDFService->GetResource(NC_NAMESPACE_URI "URL", &gNC_URL);
gRDFService->GetResource(NC_NAMESPACE_URI "Name", &gNC_Name);
gRDFService->GetResource(NC_NAMESPACE_URI "Progress", &gNC_Progress);
mInner = do_GetService(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "in-memory-datasource", &rv);
if (NS_FAILED(rv)) return rv;
return mRDFService->RegisterDataSource(this, PR_FALSE);
return gRDFService->RegisterDataSource(this, PR_FALSE);
}
NS_IMETHODIMP
@ -105,11 +131,11 @@ nsDownloadManager::AddItem(const PRUnichar* aDisplayName, nsIURI* aSourceURI,
nsCOMPtr<nsIRDFContainer> downloads;
GetDownloadsContainer(getter_AddRefs(downloads));
nsXPIDLCString spec;
aSourceURI->GetSpec(getter_Copies(spec));
nsXPIDLCString filePath;
aLocalFile->GetPath(getter_Copies(filePath));
nsCOMPtr<nsIRDFResource> downloadItem;
mRDFService->GetResource(spec, getter_AddRefs(downloadItem));
gRDFService->GetResource(filePath, getter_AddRefs(downloadItem));
PRInt32 itemIndex;
downloads->IndexOf(downloadItem, &itemIndex);
@ -121,24 +147,31 @@ nsDownloadManager::AddItem(const PRUnichar* aDisplayName, nsIURI* aSourceURI,
downloads->AppendElement(downloadItem);
// NC:Name
nsAutoString displayName; displayName.Assign(aDisplayName);
if (displayName.IsEmpty()) {
nsXPIDLString unicodeDisplayName;
aLocalFile->GetUnicodeLeafName(getter_Copies(unicodeDisplayName));
displayName.Assign(unicodeDisplayName);
}
nsCOMPtr<nsIRDFLiteral> nameLiteral;
mRDFService->GetLiteral(aDisplayName, getter_AddRefs(nameLiteral));
gRDFService->GetLiteral(displayName.get(), getter_AddRefs(nameLiteral));
Assert(downloadItem, gNC_Name, nameLiteral, PR_TRUE);
// NC:URL
nsXPIDLCString spec;
aSourceURI->GetSpec(getter_Copies(spec));
nsCOMPtr<nsIRDFResource> urlResource;
mRDFService->GetResource(spec, getter_AddRefs(urlResource));
gRDFService->GetResource(spec, getter_AddRefs(urlResource));
Assert(downloadItem, gNC_URL, urlResource, PR_TRUE);
// NC:File
nsXPIDLCString filePath;
aLocalFile->GetPath(getter_Copies(filePath));
nsCOMPtr<nsIRDFResource> fileResource;
mRDFService->GetResource(filePath, getter_AddRefs(fileResource));
gRDFService->GetResource(filePath, getter_AddRefs(fileResource));
Assert(downloadItem, gNC_File, fileResource, PR_TRUE);
return NS_OK;
return Flush();
}
nsresult
@ -324,6 +357,65 @@ nsDownloadManager::DoCommand(nsISupportsArray* aSources,
return mInner->DoCommand(aSources, aCommand, aArguments);
}
////////////////////////////////////////////////////////////////////////
// nsIRDFRemoteDataSource
NS_IMETHODIMP
nsDownloadManager::GetLoaded(PRBool* aResult)
{
*aResult = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsDownloadManager::Init(const char* aURI)
{
return NS_OK;
}
NS_IMETHODIMP
nsDownloadManager::Refresh(PRBool aBlocking)
{
return NS_OK;
}
NS_IMETHODIMP
nsDownloadManager::Flush()
{
nsresult rv;
// Locate datasource file
nsCOMPtr<nsIProperties> fileLocator(do_GetService("@mozilla.org/file/directory_service;1"));
nsCOMPtr<nsIFile> profileDir;
rv = fileLocator->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile), getter_AddRefs(profileDir));
if (NS_FAILED(rv)) return rv;
rv = profileDir->Append(PROFILE_DOWNLOAD_FILE);
if (NS_FAILED(rv)) return rv;
nsXPIDLCString fileURL;
profileDir->GetURL(getter_Copies(fileURL));
nsAutoString fileAS; fileAS.AssignWithConversion(fileURL);
nsFileURL url(fileURL, PR_TRUE);
nsFileSpec path(url);
nsOutputFileStream out(path);
if (!out.is_open())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIRDFXMLSerializer> serializer(do_CreateInstance("@mozilla.org/rdf/xml-serializer;1", &rv));
if (NS_FAILED(rv)) return rv;
rv = serializer->Init(mInner);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIRDFXMLSource> source(do_QueryInterface(serializer, &rv));
if (NS_FAILED(rv)) return rv;
return source->Serialize(out.GetIStream());
}
///////////////////////////////////////////////////////////////////////////////
//
DownloadItem::DownloadItem()
@ -332,6 +424,41 @@ DownloadItem::DownloadItem()
DownloadItem::~DownloadItem()
{
UpdateProgressInfo();
}
nsresult
DownloadItem::UpdateProgressInfo()
{
nsresult rv;
nsCOMPtr<nsIStringBundleService> sbs(do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIStringBundle> bundle;
rv = sbs->CreateBundle(NSDOWNLOADMANAGER_PROPERTIES_URI, getter_AddRefs(bundle));
if (NS_FAILED(rv)) return rv;
nsAutoString key; key.AssignWithConversion("progressFormat");
nsAutoString curTotalProgressStr; curTotalProgressStr.AppendInt(mCurTotalProgress);
nsAutoString maxTotalProgressStr; maxTotalProgressStr.AppendInt(mMaxTotalProgress);
const PRUnichar* formatStrings[2] = { curTotalProgressStr.get(), maxTotalProgressStr.get() };
nsXPIDLString progressString;
rv = bundle->FormatStringFromName(key.get(), formatStrings, 2, getter_Copies(progressString));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIRDFLiteral> progressLiteral;
rv = gRDFService->GetLiteral(progressString, getter_AddRefs(progressLiteral));
if (NS_FAILED(rv)) return rv;
rv = mDataSource->Assert(mDownloadItem, gNC_Progress, progressLiteral, PR_TRUE);
if (NS_FAILED(rv)) return rv;
// Store Unformatted Elapsed Time
return rv;
}
nsresult
@ -353,21 +480,28 @@ DownloadItem::Init(nsIRDFResource* aDownloadItem,
rv = mWebBrowserPersist->SetProgressListener(this);
if (NS_FAILED(rv)) return rv;
return mWebBrowserPersist->SaveURI(aURI, aPostData, aFile);
rv = mWebBrowserPersist->SaveURI(aURI, aPostData, aFile);
if (NS_FAILED(rv)) return rv;
return rv;
}
///////////////////////////////////////////////////////////////////////////////
// nsIWebProgressListener
NS_IMETHODIMP
DownloadItem::OnProgressChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRInt32 aCurSelfProgress,
PRInt32 aMaxSelfProgress,
PRInt32 aCurTotalProgress,
PRInt32 aMaxTotalProgress)
nsIRequest *aRequest,
PRInt32 aCurSelfProgress,
PRInt32 aMaxSelfProgress,
PRInt32 aCurTotalProgress,
PRInt32 aMaxTotalProgress)
{
return NS_ERROR_NOT_IMPLEMENTED;
mCurTotalProgress = aCurTotalProgress;
mMaxTotalProgress = aMaxTotalProgress;
return NS_OK;
}
NS_IMETHODIMP
@ -390,6 +524,14 @@ DownloadItem::OnStatusChange(nsIWebProgress *aWebProgress,
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
DownloadItem::OnStateChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest, PRInt32 aStateFlags,
PRUint32 aStatus)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
DownloadItem::OnSecurityChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest, PRInt32 state)

Просмотреть файл

@ -54,8 +54,7 @@
class nsDownloadManager : public nsIDownloadManager,
public nsIRDFDataSource,
public nsIRDFRemoteDataSource,
public nsIWebProgressListener
public nsIRDFRemoteDataSource
{
public:
NS_DECL_NSIRDFDATASOURCE
@ -93,11 +92,16 @@ public:
nsIRDFDataSource* aDataSource,
nsIURI* aURI, nsIInputStream* aPostData, nsILocalFile* aFile);
protected:
nsresult UpdateProgressInfo();
protected:
nsCOMPtr<nsIWebBrowserPersist> mWebBrowserPersist;
nsCOMPtr<nsIRequestObserver> mRequestObserver;
nsIRDFResource* mDownloadItem;
nsIRDFDataSource* mDataSource;
PRInt32 mCurTotalProgress;
PRInt32 mMaxTotalProgress;
};
#endif