mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-10 17:34:07 +02:00
Add Save image as... option to context menu
This commit is contained in:
parent
8c21011ac7
commit
9e5a39e9fc
Core
@ -1,12 +1,14 @@
|
||||
using CefSharp;
|
||||
using TweetDck.Core.Utils;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using TweetDck.Core.Utils;
|
||||
|
||||
namespace TweetDck.Core.Handling{
|
||||
abstract class ContextMenuBase : IContextMenuHandler{
|
||||
private const int MenuOpenUrlInBrowser = 26500;
|
||||
private const int MenuCopyUrl = 26501;
|
||||
private const int MenuOpenImageInBrowser = 26502;
|
||||
private const int MenuSaveImage = 26503;
|
||||
private const int MenuCopyImageUrl = 26504;
|
||||
|
||||
public virtual void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model){
|
||||
@ -20,6 +22,7 @@ public virtual void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser bro
|
||||
|
||||
if (parameters.TypeFlags.HasFlag(ContextMenuType.Media) && parameters.HasImageContents){
|
||||
model.AddItem((CefMenuCommand)MenuOpenImageInBrowser,"Open image in browser");
|
||||
model.AddItem((CefMenuCommand)MenuSaveImage,"Save image as...");
|
||||
model.AddItem((CefMenuCommand)MenuCopyImageUrl,"Copy image URL");
|
||||
model.AddSeparator();
|
||||
}
|
||||
@ -39,6 +42,29 @@ public virtual bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser br
|
||||
BrowserUtils.OpenExternalBrowser(parameters.SourceUrl);
|
||||
break;
|
||||
|
||||
case MenuSaveImage:
|
||||
string fileName = GetImageFileName(parameters.SourceUrl);
|
||||
string extension = Path.GetExtension(fileName);
|
||||
string saveTarget;
|
||||
|
||||
using(SaveFileDialog dialog = new SaveFileDialog{
|
||||
AutoUpgradeEnabled = true,
|
||||
OverwritePrompt = true,
|
||||
Title = "Save image",
|
||||
FileName = fileName,
|
||||
Filter = "Image ("+(string.IsNullOrEmpty(extension) ? "unknown" : extension)+")|*.*"
|
||||
}){
|
||||
saveTarget = dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : null;
|
||||
}
|
||||
|
||||
if (saveTarget != null){
|
||||
BrowserUtils.DownloadFileAsync(parameters.SourceUrl,saveTarget,ex => {
|
||||
MessageBox.Show("An error occurred while downloading the image: "+ex.Message,Program.BrandName+" Has Failed :(",MessageBoxButtons.OK,MessageBoxIcon.Error);
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case MenuCopyImageUrl:
|
||||
Clipboard.SetText(parameters.SourceUrl,TextDataFormat.Text);
|
||||
break;
|
||||
@ -64,5 +90,21 @@ protected void RemoveSeparatorIfLast(IMenuModel model){
|
||||
model.RemoveAt(model.Count-1);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetImageFileName(string url){
|
||||
// twimg adds a colon after file extension
|
||||
int dot = url.LastIndexOf('.');
|
||||
|
||||
if (dot != -1){
|
||||
int colon = url.IndexOf(':',dot);
|
||||
|
||||
if (colon != -1){
|
||||
url = url.Substring(0,colon);
|
||||
}
|
||||
}
|
||||
|
||||
// return file name
|
||||
return Path.GetFileName(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,5 +28,18 @@ public static string HeaderUserAgent{
|
||||
public static void OpenExternalBrowser(string url){ // TODO implement mailto
|
||||
Process.Start(url);
|
||||
}
|
||||
|
||||
public static void DownloadFileAsync(string url, string target, Action<Exception> onFailure){
|
||||
WebClient client = new WebClient{ Proxy = null };
|
||||
client.Headers[HttpRequestHeader.UserAgent] = HeaderUserAgent;
|
||||
|
||||
client.DownloadFileCompleted += (sender, args) => {
|
||||
if (args.Error != null){
|
||||
onFailure(args.Error);
|
||||
}
|
||||
};
|
||||
|
||||
client.DownloadFileAsync(new Uri(url),target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user