Added handling of Gecko.LauncherDialog.Download (#1548)

* Added handling of Gecko.LauncherDialog.Download
Code lifted from https://stackoverflow.com/questions/27368791/how-to-handle-downloading-in-geckofx-29
This code addition addresses https://github.com/mRemoteNG/mRemoteNG/issues/1400
* updated changelog for #1400
* refractored launcher dialog method
This commit is contained in:
david-sway
2019-08-30 09:15:45 -04:00
committed by Faryan Rezagholi
parent 11199eabf3
commit 125330b695
2 changed files with 38 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]
### Added
- #1512: Added option to close panel from right click menu
- #1400: Added file download handling to HTTP(S) connections using Gecko
- #826: Allow selecting RDP version to use when connecting
### Changed
- #1468: Improved mRemoteNG startup time

View File

@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Windows.Forms;
using Gecko;
using mRemoteNG.Tools;
@@ -66,6 +67,7 @@ namespace mRemoteNG.Connection.Protocol.Http
if (GeckoBrowser != null)
{
GeckoBrowser.DocumentTitleChanged += geckoBrowser_DocumentTitleChanged;
LauncherDialog.Download += geckoBrowser_LauncherDialog_Download;
GeckoBrowser.NSSError += CertEvent.GeckoBrowser_NSSError;
}
else
@@ -267,6 +269,40 @@ namespace mRemoteNG.Connection.Protocol.Http
}
}
private void geckoBrowser_LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e)
{
var objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1");
using (var tmp = new nsAString(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\mremoteng.download"))
{
objTarget.InitWithPath(tmp);
}
//Save file dialog
var saveFileDialog = new SaveFileDialog
{
Filter = "All files (*.*)|*.*",
FilterIndex = 2,
RestoreDirectory = true,
FileName = e.Filename
};
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
var source = IOService.CreateNsIUri(e.Url);
var dest = IOService.CreateNsIUri(new Uri(saveFileDialog.FileName).AbsoluteUri);
var t = (nsAStringBase)new nsAString(Path.GetFileName(saveFileDialog.FileName));
var persist = Xpcom.CreateInstance<nsIWebBrowserPersist>("@mozilla.org/embedding/browser/nsWebBrowserPersist;1");
var nst = Xpcom.CreateInstance<nsITransfer>("@mozilla.org/transfer;1");
nst.Init(source, dest, t, e.Mime, 0, null, persist, false);
persist.SetPersistFlagsAttribute(2 | 32 | 16384);
persist.SetProgressListenerAttribute(nst);
persist.SaveURI(source, null, null, (uint)Gecko.nsIHttpChannelConsts.REFERRER_POLICY_NO_REFERRER, null, null, (nsISupports)dest, null);
}
saveFileDialog.Dispose();
}
#endregion
#region Enums
@@ -282,4 +318,4 @@ namespace mRemoteNG.Connection.Protocol.Http
#endregion
}
}
}