Commit d02e66c4 authored by Tatiana Meshkova's avatar Tatiana Meshkova

Added alert service to embedlite components

parent c5b1e8e7
......@@ -9,3 +9,11 @@ contract @mozilla.org/network/protocol/about;1?what=embedlite {59f3da9a-6c88-11e
# LoginManagerPrompter.js
component {72de694e-6c88-11e2-a4ee-6b515bdf0cb7} LoginManagerPrompter.js
contract @mozilla.org/login-manager/prompter;1 {72de694e-6c88-11e2-a4ee-6b515bdf0cb7}
# DownloadManagerUI.js
component {93db15b1-b408-453e-9a2b-6619e168324a} DownloadManagerUI.js
contract @mozilla.org/download-manager-ui;1 {93db15b1-b408-453e-9a2b-6619e168324a}
# HelperAppDialog.js
component {e9d277a0-268a-4ec2-bb8c-10fdf3e44611} HelperAppDialog.js
contract @mozilla.org/helperapplauncherdialog;1 {e9d277a0-268a-4ec2-bb8c-10fdf3e44611}
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Ci = Components.interfaces;
const Cr = Components.results;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
// -----------------------------------------------------------------------
// Download Manager UI
// -----------------------------------------------------------------------
function DownloadManagerUI() { }
DownloadManagerUI.prototype = {
classID: Components.ID("{93db15b1-b408-453e-9a2b-6619e168324a}"),
show: function show(aWindowContext, aDownload, aReason, aUsePrivateUI) {
if (!aReason)
aReason = Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED;
this._getBrowserApp().selectOrOpenTab("about:downloads");
},
get visible() {
let browserApp = this._getBrowserApp();
let downloadsTab = browserApp.getTabWithURL("about:downloads");
return (downloadsTab && downloadsTab == browserApp.selectedTab) ? true : false;
},
getAttention: function getAttention() {
if (this._getBrowserApp().getTabWithURL("about:downloads"))
return;
throw Cr.NS_ERROR_UNEXPECTED;
},
_getBrowserApp: function getBrowserApp() {
try {
return Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
} catch (e) {
throw Cr.NS_ERROR_FAILURE;
}
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI])
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DownloadManagerUI]);
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;
const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
// -----------------------------------------------------------------------
// HelperApp Launcher Dialog
// -----------------------------------------------------------------------
function HelperAppLauncherDialog() { }
HelperAppLauncherDialog.prototype = {
classID: Components.ID("{e9d277a0-268a-4ec2-bb8c-10fdf3e44611}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]),
show: function hald_show(aLauncher, aContext, aReason) {
// Check to see if we can open this file or not
if (aLauncher.MIMEInfo.hasDefaultHandler) {
aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useSystemDefault;
aLauncher.launchWithApplication(null, false);
} else {
let wasClicked = false;
let listener = {
observe: function(aSubject, aTopic, aData) {
if (aTopic == "alertclickcallback") {
wasClicked = true;
let win = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser");
if (win)
win.BrowserUI.showPanel("downloads-container");
aLauncher.saveToDisk(null, false);
} else {
if (!wasClicked)
aLauncher.cancel(Cr.NS_BINDING_ABORTED);
}
}
};
this._notify(aLauncher, listener);
}
},
promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) {
let file = null;
let prefs = Services.prefs;
if (!aForcePrompt) {
// Check to see if the user wishes to auto save to the default download
// folder without prompting. Note that preference might not be set.
let autodownload = true;
try {
autodownload = prefs.getBoolPref(PREF_BD_USEDOWNLOADDIR);
} catch (e) { }
if (autodownload) {
// Retrieve the user's default download directory
let dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
let defaultFolder = dnldMgr.userDownloadsDirectory;
try {
file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt);
}
catch (e) {
}
// Check to make sure we have a valid directory, otherwise, prompt
if (file)
return file;
}
}
// Use file picker to show dialog.
let picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
let windowTitle = "";
let parent = aContext.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
picker.init(parent, windowTitle, Ci.nsIFilePicker.modeSave);
picker.defaultString = aDefaultFile;
if (aSuggestedFileExt) {
// aSuggestedFileExtension includes the period, so strip it
picker.defaultExtension = aSuggestedFileExt.substring(1);
}
else {
try {
picker.defaultExtension = aLauncher.MIMEInfo.primaryExtension;
}
catch (e) { }
}
var wildCardExtension = "*";
if (aSuggestedFileExt) {
wildCardExtension += aSuggestedFileExt;
picker.appendFilter(aLauncher.MIMEInfo.description, wildCardExtension);
}
picker.appendFilters(Ci.nsIFilePicker.filterAll);
// Default to lastDir if it is valid, otherwise use the user's default
// downloads directory. userDownloadsDirectory should always return a
// valid directory, so we can safely default to it.
var dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
picker.displayDirectory = dnldMgr.userDownloadsDirectory;
// The last directory preference may not exist, which will throw.
try {
let lastDir = prefs.getComplexValue("browser.download.lastDir", Ci.nsILocalFile);
if (isUsableDirectory(lastDir))
picker.displayDirectory = lastDir;
}
catch (e) { }
if (picker.show() == Ci.nsIFilePicker.returnCancel) {
// null result means user cancelled.
return null;
}
// Be sure to save the directory the user chose through the Save As...
// dialog as the new browser.download.dir since the old one
// didn't exist.
file = picker.file;
if (file) {
try {
// Remove the file so that it's not there when we ensure non-existence later;
// this is safe because for the file to exist, the user would have had to
// confirm that he wanted the file overwritten.
if (file.exists())
file.remove(false);
}
catch (e) { }
var newDir = file.parent.QueryInterface(Ci.nsILocalFile);
prefs.setComplexValue("browser.download.lastDir", Ci.nsILocalFile, newDir);
file = this.validateLeafName(newDir, file.leafName, null);
}
return file;
},
validateLeafName: function hald_validateLeafName(aLocalFile, aLeafName, aFileExt) {
if (!(aLocalFile && this.isUsableDirectory(aLocalFile)))
return null;
// Remove any leading periods, since we don't want to save hidden files
// automatically.
aLeafName = aLeafName.replace(/^\.+/, "");
if (aLeafName == "")
aLeafName = "unnamed" + (aFileExt ? "." + aFileExt : "");
aLocalFile.append(aLeafName);
this.makeFileUnique(aLocalFile);
return aLocalFile;
},
makeFileUnique: function hald_makeFileUnique(aLocalFile) {
try {
// Note - this code is identical to that in
// toolkit/content/contentAreaUtils.js.
// If you are updating this code, update that code too! We can't share code
// here since this is called in a js component.
var collisionCount = 0;
while (aLocalFile.exists()) {
collisionCount++;
if (collisionCount == 1) {
// Append "(2)" before the last dot in (or at the end of) the filename
// special case .ext.gz etc files so we don't wind up with .tar(2).gz
if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i))
aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&");
else
aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&");
}
else {
// replace the last (n) in the filename with (n+1)
aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")");
}
}
aLocalFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
}
catch (e) {
dump("*** exception in validateLeafName: " + e + "\n");
if (e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED)
throw e;
if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) {
aLocalFile.append("unnamed");
if (aLocalFile.exists())
aLocalFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
}
}
},
isUsableDirectory: function hald_isUsableDirectory(aDirectory) {
return aDirectory.exists() && aDirectory.isDirectory() && aDirectory.isWritable();
},
_notify: function hald_notify(aLauncher, aCallback) {
let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
let notifier = Cc[aCallback ? "@mozilla.org/alerts-service;1" : "@mozilla.org/toaster-alerts-service;1"].getService(Ci.nsIAlertsService);
notifier.showAlertNotification(URI_GENERIC_ICON_DOWNLOAD,
bundle.GetStringFromName("alertDownloads"),
bundle.GetStringFromName("alertCantOpenDownload"),
true, "", aCallback, "downloadopen-fail");
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]);
......@@ -3,6 +3,8 @@ jscomps_manifest_DATA = \
AboutRedirector.js \
AlertsService.js \
LoginManagerPrompter.js \
HelperAppDialog.js \
DownloadManagerUI.js \
$(NULL)
INCLUDES = $(DEPS_CFLAGS)
......@@ -18,10 +18,12 @@
#include "nsILoginManager.h"
#include "nsIFormHistory.h"
#include "nsWidgetsCID.h"
#include "nsAlertsService.h"
using namespace mozilla::embedlite;
NS_GENERIC_FACTORY_CONSTRUCTOR(EmbedPromptFactory)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsEmbedAlertsService)
EmbedPromptRegister::EmbedPromptRegister()
{
......@@ -76,10 +78,27 @@ EmbedPromptRegister::Init()
true);
}
// Init LoginManager
nsCOMPtr<nsILoginManager> pwdmgr = do_GetService("@mozilla.org/login-manager;1");
// Init FormHistory
nsCOMPtr<nsIFormHistory2> formHistory = do_GetService("@mozilla.org/satchel/form-history");
f = new mozilla::embedlite::GenericFactory(nsEmbedAlertsServiceConstructor);
if (!f) {
NS_WARNING("Unable to create factory for component");
return NS_ERROR_FAILURE;
}
oldFactory = do_GetClassObject("@mozilla.org/alerts-service;1");
if (oldFactory) {
nsCID* cid = NULL;
rv = cr->ContractIDToCID("@mozilla.org/alerts-service;1", &cid);
if (!NS_FAILED(rv)) {
rv = cr->UnregisterFactory(*cid, oldFactory.get());
NS_Free(cid);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
}
}
nsCID alertsCID = NS_EMBED_ALERTS_SERVICE_CID;
rv = cr->RegisterFactory(fpickerCID, "EmbedLite Alerts Service",
"@mozilla.org/alerts-service;1", f);
return NS_OK;
}
......
......@@ -6,6 +6,7 @@ libprompt_la_SOURCES = \
EmbedPromptService.cpp \
GenericFactory.cpp \
nsEmbedChildModule.cpp \
nsAlertsService.cpp \
$(NULL)
libprompt_la_CPPFLAGS = \
......
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsXULAppAPI.h"
#include "nsAlertsService.h"
#include "nsStringGlue.h"
#include "nsISupportsArray.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsIServiceManager.h"
#include "nsIDOMWindow.h"
#include "nsIWindowWatcher.h"
#include "nsToolkitCompsCID.h"
using namespace mozilla;
NS_IMPL_THREADSAFE_ISUPPORTS2(nsEmbedAlertsService, nsIAlertsService, nsIAlertsProgressListener)
nsEmbedAlertsService::nsEmbedAlertsService()
{
printf(">>>>>>Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
}
nsEmbedAlertsService::~nsEmbedAlertsService()
{
printf(">>>>>>Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
}
bool nsEmbedAlertsService::ShouldShowAlert()
{
bool result = true;
printf(">>>>>>Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
return result;
}
NS_IMETHODIMP nsEmbedAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle,
const nsAString & aAlertText, bool aAlertTextClickable,
const nsAString & aAlertCookie,
nsIObserver * aAlertListener,
const nsAString & aAlertName)
{
printf(">>>>>>Func:%s::%d image:%s, title:%s, text:%s, clickable:%i, cookie:%s, listener:%p, name:%s\n", __PRETTY_FUNCTION__, __LINE__,
NS_ConvertUTF16toUTF8(aImageUrl).get(),
NS_ConvertUTF16toUTF8(aAlertTitle).get(),
NS_ConvertUTF16toUTF8(aAlertText).get(),
aAlertTextClickable,
NS_ConvertUTF16toUTF8(aAlertCookie).get(),
aAlertListener,
NS_ConvertUTF16toUTF8(aAlertName).get()
);
// Do not display the alert. Instead call alertfinished and get out.
if (aAlertListener)
aAlertListener->Observe(NULL, "alertfinished", PromiseFlatString(aAlertCookie).get());
return NS_OK;
}
NS_IMETHODIMP nsEmbedAlertsService::OnProgress(const nsAString & aAlertName,
int64_t aProgress,
int64_t aProgressMax,
const nsAString & aAlertText)
{
printf(">>>>>>Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
return NS_OK;
}
NS_IMETHODIMP nsEmbedAlertsService::OnCancel(const nsAString & aAlertName)
{
printf(">>>>>>Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
return NS_OK;
}
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsEmbedAlertsService_h__
#define nsEmbedAlertsService_h__
#include "nsIAlertsService.h"
#include "nsCOMPtr.h"
class nsEmbedAlertsService : public nsIAlertsService,
public nsIAlertsProgressListener
{
public:
NS_DECL_NSIALERTSPROGRESSLISTENER
NS_DECL_NSIALERTSSERVICE
NS_DECL_ISUPPORTS
nsEmbedAlertsService();
virtual ~nsEmbedAlertsService();
protected:
bool ShouldShowAlert();
};
#define NS_EMBED_ALERTS_SERVICE_CID \
{ 0xa6d8ca00, \
0x896b, \
0x11e2, \
{ 0x8f, 0x33, 0xb7, 0xe2, 0x65, 0x26, 0x98 }}
#endif /* nsEmbedAlertsService_h__ */
......@@ -33,8 +33,6 @@ EmbedWidgetFactoryRegister::~EmbedWidgetFactoryRegister()
NS_IMPL_ISUPPORTS1(EmbedWidgetFactoryRegister, nsSupportsWeakReference)
NS_DEFINE_NAMED_CID(NS_FILEPICKER_CID);
nsresult
EmbedWidgetFactoryRegister::Init()
{
......
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