Registered a nsITransfer for opening documents after downloading.

This commit is contained in:
Tomeu Vizoso 2006-10-26 22:21:26 +02:00
parent 5ff6ba45d4
commit 2e65e2ca1d
9 changed files with 202 additions and 68 deletions

View File

@ -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])

View File

@ -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 \

117
lib/src/SugarDownload.cpp Normal file
View File

@ -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;
}

38
lib/src/SugarDownload.h Normal file
View File

@ -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__

View File

@ -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);
}

View File

@ -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);

View File

@ -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,32 +102,35 @@ sugar_browser_startup(void)
NS_GetComponentManager (getter_AddRefs (componentManager));
NS_ENSURE_TRUE (componentManager, FALSE);
for (guint i = 0; i < G_N_ELEMENTS(sSugarComponents); i++) {
nsCOMPtr<nsIGenericFactory> componentFactory;
rv = NS_NewGenericFactory(getter_AddRefs(componentFactory),
&(sSugarComponents[0]));
&(sSugarComponents[i]));
if (NS_FAILED(rv) || !componentFactory) {
g_warning ("Failed to make a factory for %s\n", sSugarComponents[0].mDescription);
g_warning ("Failed to make a factory for %s\n", sSugarComponents[i].mDescription);
return FALSE;
}
rv = componentRegistrar->RegisterFactory(sSugarComponents[0].mCID,
sSugarComponents[0].mDescription,
sSugarComponents[0].mContractID,
rv = componentRegistrar->RegisterFactory(sSugarComponents[i].mCID,
sSugarComponents[i].mDescription,
sSugarComponents[i].mContractID,
componentFactory);
if (NS_FAILED(rv)) {
g_warning ("Failed to register factory for %s\n", sSugarComponents[0].mDescription);
g_warning ("Failed to register factory for %s\n", sSugarComponents[i].mDescription);
return FALSE;
}
if (sSugarComponents[0].mRegisterSelfProc) {
rv = sSugarComponents[0].mRegisterSelfProc(componentManager, nsnull,
if (sSugarComponents[i].mRegisterSelfProc) {
rv = sSugarComponents[i].mRegisterSelfProc(componentManager, nsnull,
nsnull, nsnull,
&sSugarComponents[0]);
&sSugarComponents[i]);
if (NS_FAILED(rv)) {
g_warning ("Failed to register-self for %s\n", sSugarComponents[0].mDescription);
g_warning ("Failed to register-self for %s\n", sSugarComponents[i].mDescription);
return FALSE;
}
}
}
return TRUE;
}

View File

@ -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;
}

View File

@ -1,3 +1,3 @@
VOID:OBJECT,STRING,LONG,LONG
VOID:OBJECT,LONG
VOID:STRING,STRING,STRING,STRING
VOID:STRING,STRING,STRING