From 2e65e2ca1dda1b2fde587d98d56e4b9ea1c0d96f Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Thu, 26 Oct 2006 20:21:26 +0000 Subject: Registered a nsITransfer for opening documents after downloading. --- diff --git a/activities/web/webactivity.py b/activities/web/webactivity.py index 08d0af1..200bd39 100644 --- a/activities/web/webactivity.py +++ b/activities/web/webactivity.py @@ -20,6 +20,7 @@ import gtkmozembed import logging import _sugar +from sugar.activity import ActivityFactory from sugar.activity.Activity import Activity from sugar import env from sugar.graphics import style @@ -105,10 +106,10 @@ def start(): chandler = _sugar.get_browser_chandler() chandler.connect('handle-content', handle_content_cb) - logging.debug('handle-content connected.') def stop(): gtkmozembed.pop_startup() -def handle_content_cb(chandler, url, suggestedFileName, mimeType, tmpFileName): - logging.debug('File ' + tmpFileName + ' with MIMEType ' + mimeType + ' downloaded from ' + url) +def handle_content_cb(chandler, url, mimeType, tmpFileName): + activity = ActivityFactory.create("org.laptop.sugar.Xbook") + activity.execute("open_document", [tmpFileName]) diff --git a/lib/src/Makefile.am b/lib/src/Makefile.am index 6b88d3f..078c927 100644 --- a/lib/src/Makefile.am +++ b/lib/src/Makefile.am @@ -4,6 +4,7 @@ libsugarprivate_la_CPPFLAGS = \ -I$(MOZILLA_INCLUDE_DIR)/exthandler \ -I$(MOZILLA_INCLUDE_DIR)/mimetype \ -I$(MOZILLA_INCLUDE_DIR)/necko \ + -I$(MOZILLA_INCLUDE_DIR)/uriloader \ -DSHARE_DIR=\"$(pkgdatadir)\" noinst_LTLIBRARIES = libsugarprivate.la @@ -22,6 +23,8 @@ libsugarprivate_la_SOURCES = \ sugar-browser-chandler.c \ sugar-content-handler.h \ sugar-content-handler.cpp \ + SugarDownload.h \ + SugarDownload.cpp \ sugar-key-grabber.h \ sugar-key-grabber.c \ sugar-push-scroller.c \ diff --git a/lib/src/SugarDownload.cpp b/lib/src/SugarDownload.cpp new file mode 100644 index 0000000..b288dfe --- /dev/null +++ b/lib/src/SugarDownload.cpp @@ -0,0 +1,117 @@ +#include +#include +#include + +#include "sugar-browser-chandler.h" + +#include "SugarDownload.h" + +GSugarDownload::GSugarDownload() +{ +} + +GSugarDownload::~GSugarDownload() +{ +} + +NS_IMPL_ISUPPORTS3 (GSugarDownload, + nsIWebProgressListener, + nsIWebProgressListener2, + nsITransfer) + +NS_IMETHODIMP +GSugarDownload::Init (nsIURI *aSource, + nsIURI *aTarget, + const nsAString &aDisplayName, + nsIMIMEInfo *aMIMEInfo, + PRTime aStartTime, + nsILocalFile *aTempFile, + nsICancelable *aCancelable) +{ + FILE *file = fopen("/home/tomeu/file.txt","a+"); + fprintf(file,"%s\n","GSugarDownload::Init"); + fclose(file); + + mSource = aSource; + mTarget = aTarget; + mMIMEInfo = aMIMEInfo; + mTempFile = aTempFile; + + return NS_OK; +} + +NS_IMETHODIMP +GSugarDownload::OnStateChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, + PRUint32 aStateFlags, nsresult aStatus) +{ + nsCString url; + nsCString mimeType; + nsCString tmpFileName; + + if ((((aStateFlags & STATE_IS_REQUEST) && + (aStateFlags & STATE_IS_NETWORK) && + (aStateFlags & STATE_STOP)) || + aStateFlags == STATE_STOP) && + NS_SUCCEEDED (aStatus)) { + + mMIMEInfo->GetMIMEType(mimeType); + mSource->GetSpec(url); + mTempFile->GetNativeLeafName(tmpFileName); + + // FIXME: Hack. Mozilla adds a .part to the file name. Must exist a better/simpler way. + // FIXME: Also creates a nice memory leak. + char *tmpFileName_striped = (char*)malloc(strlen(tmpFileName.get())); + strncpy(tmpFileName_striped, tmpFileName.get(), strlen(tmpFileName.get()) - 5); + + SugarBrowserChandler *browser_chandler = sugar_get_browser_chandler(); + sugar_browser_chandler_handle_content(browser_chandler, + url.get(), + mimeType.get(), + tmpFileName_striped); + } + + return NS_OK; +} + +NS_IMETHODIMP +GSugarDownload::OnProgressChange (nsIWebProgress *aWebProgress, + nsIRequest *aRequest, + PRInt32 aCurSelfProgress, + PRInt32 aMaxSelfProgress, + PRInt32 aCurTotalProgress, + PRInt32 aMaxTotalProgress) +{ + return OnProgressChange64 (aWebProgress, aRequest, + aCurSelfProgress, aMaxSelfProgress, + aCurTotalProgress, aMaxTotalProgress); +} + +NS_IMETHODIMP +GSugarDownload::OnProgressChange64 (nsIWebProgress *aWebProgress, + nsIRequest *aRequest, + PRInt64 aCurSelfProgress, + PRInt64 aMaxSelfProgress, + PRInt64 aCurTotalProgress, + PRInt64 aMaxTotalProgress) +{ + return NS_OK; +} + +NS_IMETHODIMP +GSugarDownload::OnLocationChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location) +{ + return NS_OK; +} + +NS_IMETHODIMP +GSugarDownload::OnStatusChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, + nsresult aStatus, const PRUnichar *aMessage) +{ + return NS_OK; +} + +NS_IMETHODIMP +GSugarDownload::OnSecurityChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state) +{ + return NS_OK; +} diff --git a/lib/src/SugarDownload.h b/lib/src/SugarDownload.h new file mode 100644 index 0000000..468428c --- /dev/null +++ b/lib/src/SugarDownload.h @@ -0,0 +1,38 @@ +#ifndef SugarDownload_h__ +#define SugarDownload_h__ + +#include +#include +#include +#include +#include +#include +#include + +#define G_SUGARDOWNLOAD_CID \ +{ /* b1813bbe-6518-11db-967e-00e08161165f */ \ + 0xb1813bbe, \ + 0x6518, \ + 0x11db, \ + {0x96, 0x7e, 0x0, 0xe0, 0x81, 0x61, 0x16, 0x5f} \ +} + +class GSugarDownload : public nsITransfer +{ +public: + GSugarDownload(); + virtual ~GSugarDownload(); + + NS_DECL_ISUPPORTS + NS_DECL_NSIWEBPROGRESSLISTENER + NS_DECL_NSIWEBPROGRESSLISTENER2 + NS_DECL_NSITRANSFER + +protected: + nsIURI *mSource; + nsIURI *mTarget; + nsIMIMEInfo *mMIMEInfo; + nsILocalFile *mTempFile; +}; + +#endif // SugarDownload_h__ diff --git a/lib/src/sugar-browser-chandler.c b/lib/src/sugar-browser-chandler.c index 608c1da..930b19f 100644 --- a/lib/src/sugar-browser-chandler.c +++ b/lib/src/sugar-browser-chandler.c @@ -28,9 +28,8 @@ sugar_browser_chandler_class_init(SugarBrowserChandlerClass *browser_chandler_cl G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (SugarBrowserChandlerClass, handle_content), NULL, NULL, - sugar_marshal_VOID__STRING_STRING_STRING_STRING, - G_TYPE_NONE, 4, - G_TYPE_STRING, + sugar_marshal_VOID__STRING_STRING_STRING, + G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); @@ -48,7 +47,6 @@ sugar_get_browser_chandler() void sugar_browser_chandler_handle_content (SugarBrowserChandler *browser_chandler, const char *url, - const char *suggested_file_name, const char *mime_type, const char *tmp_file_name) { @@ -56,7 +54,6 @@ sugar_browser_chandler_handle_content (SugarBrowserChandler *browser_chandler, signals[HANDLE_CONTENT], 0 /* details */, url, - suggested_file_name, mime_type, tmp_file_name); } diff --git a/lib/src/sugar-browser-chandler.h b/lib/src/sugar-browser-chandler.h index 10f8e2a..cccd983 100644 --- a/lib/src/sugar-browser-chandler.h +++ b/lib/src/sugar-browser-chandler.h @@ -31,7 +31,6 @@ GType sugar_browser_chandler_get_type (void); SugarBrowserChandler *sugar_get_browser_chandler (void); void sugar_browser_chandler_handle_content (SugarBrowserChandler *chandler, const char *url, - const char *suggested_file_name, const char *mime_type, const char *tmp_file_name); diff --git a/lib/src/sugar-browser.cpp b/lib/src/sugar-browser.cpp index 0b014c9..0cbfd29 100644 --- a/lib/src/sugar-browser.cpp +++ b/lib/src/sugar-browser.cpp @@ -19,6 +19,7 @@ #include "sugar-browser.h" #include "sugar-content-handler.h" +#include "SugarDownload.h" #include #include @@ -35,6 +36,7 @@ #include NS_GENERIC_FACTORY_CONSTRUCTOR(GSugarContentHandler) +NS_GENERIC_FACTORY_CONSTRUCTOR(GSugarDownload) enum { PROP_0, @@ -52,6 +54,12 @@ static const nsModuleComponentInfo sSugarComponents[] = { G_SUGARCONTENTHANDLER_CID, NS_IHELPERAPPLAUNCHERDLG_CONTRACTID, GSugarContentHandlerConstructor + }, + { + "Sugar Download", + G_SUGARDOWNLOAD_CID, + NS_TRANSFER_CONTRACTID, + GSugarDownloadConstructor } }; @@ -94,31 +102,34 @@ sugar_browser_startup(void) NS_GetComponentManager (getter_AddRefs (componentManager)); NS_ENSURE_TRUE (componentManager, FALSE); - nsCOMPtr componentFactory; - rv = NS_NewGenericFactory(getter_AddRefs(componentFactory), - &(sSugarComponents[0])); - if (NS_FAILED(rv) || !componentFactory) { - g_warning ("Failed to make a factory for %s\n", sSugarComponents[0].mDescription); - return FALSE; - } - - rv = componentRegistrar->RegisterFactory(sSugarComponents[0].mCID, - sSugarComponents[0].mDescription, - sSugarComponents[0].mContractID, - componentFactory); - if (NS_FAILED(rv)) { - g_warning ("Failed to register factory for %s\n", sSugarComponents[0].mDescription); - return FALSE; - } + for (guint i = 0; i < G_N_ELEMENTS(sSugarComponents); i++) { + + nsCOMPtr componentFactory; + rv = NS_NewGenericFactory(getter_AddRefs(componentFactory), + &(sSugarComponents[i])); + if (NS_FAILED(rv) || !componentFactory) { + g_warning ("Failed to make a factory for %s\n", sSugarComponents[i].mDescription); + return FALSE; + } - if (sSugarComponents[0].mRegisterSelfProc) { - rv = sSugarComponents[0].mRegisterSelfProc(componentManager, nsnull, - nsnull, nsnull, - &sSugarComponents[0]); + rv = componentRegistrar->RegisterFactory(sSugarComponents[i].mCID, + sSugarComponents[i].mDescription, + sSugarComponents[i].mContractID, + componentFactory); if (NS_FAILED(rv)) { - g_warning ("Failed to register-self for %s\n", sSugarComponents[0].mDescription); + g_warning ("Failed to register factory for %s\n", sSugarComponents[i].mDescription); return FALSE; } + + if (sSugarComponents[i].mRegisterSelfProc) { + rv = sSugarComponents[i].mRegisterSelfProc(componentManager, nsnull, + nsnull, nsnull, + &sSugarComponents[i]); + if (NS_FAILED(rv)) { + g_warning ("Failed to register-self for %s\n", sSugarComponents[i].mDescription); + return FALSE; + } + } } return TRUE; diff --git a/lib/src/sugar-content-handler.cpp b/lib/src/sugar-content-handler.cpp index 0597c48..44aae59 100644 --- a/lib/src/sugar-content-handler.cpp +++ b/lib/src/sugar-content-handler.cpp @@ -7,6 +7,7 @@ #include #include "sugar-browser-chandler.h" +#include "SugarDownload.h" #include "sugar-content-handler.h" @@ -27,42 +28,10 @@ GSugarContentHandler::Show (nsIHelperAppLauncher *aLauncher, nsISupports *aContext, PRUint32 aReason) { - SugarBrowserChandler *browser_chandler; - nsresult rv; - nsCString url; - nsCString mimeType; - nsString suggested_file_name_utf16; - nsCString suggested_file_name; - nsCString tmp_file_name; - - NS_ENSURE_TRUE (aLauncher, NS_ERROR_FAILURE); - - nsCOMPtr MIMEInfo; - aLauncher->GetMIMEInfo (getter_AddRefs(MIMEInfo)); - NS_ENSURE_TRUE (MIMEInfo, NS_ERROR_FAILURE); - - rv = MIMEInfo->GetMIMEType (mimeType); - - nsCOMPtr uri; - aLauncher->GetSource (getter_AddRefs(uri)); - NS_ENSURE_TRUE (uri, NS_ERROR_FAILURE); - - uri->GetSpec (url); - - aLauncher->GetSuggestedFileName (suggested_file_name_utf16); - NS_UTF16ToCString (suggested_file_name_utf16, - NS_CSTRING_ENCODING_UTF8, suggested_file_name); - nsCOMPtr tmp_file; aLauncher->GetTargetFile(getter_AddRefs(tmp_file)); - tmp_file->GetNativeLeafName (tmp_file_name); - - browser_chandler = sugar_get_browser_chandler(); - sugar_browser_chandler_handle_content(browser_chandler, - url.get(), - suggested_file_name.get(), - mimeType.get(), - tmp_file_name.get()); + + aLauncher->SaveToDisk (tmp_file, PR_FALSE); return NS_OK; } @@ -74,7 +43,6 @@ NS_IMETHODIMP GSugarContentHandler::PromptForSaveToFile( const PRUnichar *aSuggestedFileExtension, nsILocalFile **_retval) { - return NS_OK; } diff --git a/lib/src/sugar-marshal.list b/lib/src/sugar-marshal.list index ba41075..5a0120f 100644 --- a/lib/src/sugar-marshal.list +++ b/lib/src/sugar-marshal.list @@ -1,3 +1,3 @@ VOID:OBJECT,STRING,LONG,LONG VOID:OBJECT,LONG -VOID:STRING,STRING,STRING,STRING +VOID:STRING,STRING,STRING -- cgit v0.9.1