Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTomeu Vizoso <tomeu@tomeuvizoso.net>2006-10-26 20:21:26 (GMT)
committer Tomeu Vizoso <tomeu@tomeuvizoso.net>2006-10-26 20:21:26 (GMT)
commit2e65e2ca1dda1b2fde587d98d56e4b9ea1c0d96f (patch)
tree68044ef3303003515e3e7b65699b75c9f78857f1 /lib
parent5ff6ba45d42335deb517a7f0629da75b432e4a16 (diff)
Registered a nsITransfer for opening documents after downloading.
Diffstat (limited to 'lib')
-rw-r--r--lib/src/Makefile.am3
-rw-r--r--lib/src/SugarDownload.cpp117
-rw-r--r--lib/src/SugarDownload.h38
-rw-r--r--lib/src/sugar-browser-chandler.c7
-rw-r--r--lib/src/sugar-browser-chandler.h1
-rw-r--r--lib/src/sugar-browser.cpp53
-rw-r--r--lib/src/sugar-content-handler.cpp38
-rw-r--r--lib/src/sugar-marshal.list2
8 files changed, 196 insertions, 63 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <nsStringAPI.h>
+
+#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 <nsCOMPtr.h>
+#include <nsIInterfaceRequestor.h>
+#include <nsITransfer.h>
+#include <nsIWebProgressListener.h>
+#include <nsIMIMEInfo.h>
+#include <nsIURL.h>
+#include <nsILocalFile.h>
+
+#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 <gtkmozembed_internal.h>
#include <nsCOMPtr.h>
@@ -35,6 +36,7 @@
#include <nsIComponentManager.h>
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<nsIGenericFactory> 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<nsIGenericFactory> 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 <nsIFile.h>
#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<nsIMIMEInfo> MIMEInfo;
- aLauncher->GetMIMEInfo (getter_AddRefs(MIMEInfo));
- NS_ENSURE_TRUE (MIMEInfo, NS_ERROR_FAILURE);
-
- rv = MIMEInfo->GetMIMEType (mimeType);
-
- nsCOMPtr<nsIURI> 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<nsIFile> 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