2021-06-27 21:35:07 +12:00
|
|
|
|
// <copyright file="DragDropHelper.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
2021-10-26 08:45:49 +13:00
|
|
|
|
namespace SystemTrayMenu.Helper
|
2021-06-26 23:24:56 +12:00
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2021-11-17 12:13:46 +13:00
|
|
|
|
using System.Net.Http;
|
2021-06-26 23:24:56 +12:00
|
|
|
|
using System.Text;
|
2022-06-11 04:41:49 +12:00
|
|
|
|
using System.Threading;
|
2021-06-26 23:24:56 +12:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using SystemTrayMenu.DataClasses;
|
|
|
|
|
using SystemTrayMenu.UserInterface;
|
2022-07-06 07:53:38 +12:00
|
|
|
|
using SystemTrayMenu.Utilities;
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
|
|
|
|
public static class DragDropHelper
|
|
|
|
|
{
|
|
|
|
|
public static void DragEnter(object sender, DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
object data = e.Data.GetData("UniformResourceLocator");
|
2021-10-16 06:42:02 +13:00
|
|
|
|
|
|
|
|
|
if (data is MemoryStream memoryStream)
|
2021-06-26 23:24:56 +12:00
|
|
|
|
{
|
2021-10-16 06:42:02 +13:00
|
|
|
|
byte[] bytes = memoryStream.ToArray();
|
2021-06-26 23:24:56 +12:00
|
|
|
|
Encoding encod = Encoding.ASCII;
|
|
|
|
|
string url = encod.GetString(bytes);
|
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
e.Effect = DragDropEffects.Copy;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DragDrop(object sender, DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Menu menu = (Menu)sender;
|
|
|
|
|
string path;
|
2021-11-29 02:05:01 +13:00
|
|
|
|
if (menu != null)
|
2021-06-26 23:24:56 +12:00
|
|
|
|
{
|
2021-11-29 02:05:01 +13:00
|
|
|
|
RowData rowData = (RowData)menu.Tag;
|
|
|
|
|
if (rowData != null)
|
|
|
|
|
{
|
2022-06-18 00:38:09 +12:00
|
|
|
|
path = rowData.ResolvedPath;
|
2021-11-29 02:05:01 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
path = Config.Path;
|
|
|
|
|
}
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-11-29 02:05:01 +13:00
|
|
|
|
path = Config.Path;
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object data = e.Data.GetData("UniformResourceLocator");
|
|
|
|
|
MemoryStream ms = data as MemoryStream;
|
|
|
|
|
byte[] bytes = ms.ToArray();
|
|
|
|
|
Encoding encod = Encoding.ASCII;
|
|
|
|
|
string url = encod.GetString(bytes);
|
2022-06-11 04:41:49 +12:00
|
|
|
|
|
|
|
|
|
new Thread(CreateShortcutInBackground).Start();
|
|
|
|
|
void CreateShortcutInBackground()
|
|
|
|
|
{
|
|
|
|
|
CreateShortcut(url.Replace("\0", string.Empty), path);
|
|
|
|
|
}
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CreateShortcut(string url, string pathToStoreFile)
|
|
|
|
|
{
|
2022-06-11 04:41:49 +12:00
|
|
|
|
string pathToStoreIcons = Properties.Settings.Default.PathIcoDirectory;
|
2021-12-27 19:35:39 +13:00
|
|
|
|
if (!Directory.Exists(pathToStoreIcons))
|
2021-06-26 23:24:56 +12:00
|
|
|
|
{
|
2021-12-27 19:35:39 +13:00
|
|
|
|
Directory.CreateDirectory(pathToStoreIcons);
|
|
|
|
|
}
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-12-27 19:35:39 +13:00
|
|
|
|
Uri uri = new(url);
|
|
|
|
|
string hostname = uri.Host.ToString();
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-12-27 19:35:39 +13:00
|
|
|
|
string pathIconPng = Path.Combine(pathToStoreIcons, $"{hostname}.png");
|
2022-02-05 23:58:19 +13:00
|
|
|
|
|
|
|
|
|
string urlGoogleIconDownload = @"http://www.google.com/s2/favicons?sz=32&domain=" + url;
|
2022-02-06 00:47:27 +13:00
|
|
|
|
HttpClient client = new();
|
2022-07-06 07:53:38 +12:00
|
|
|
|
|
|
|
|
|
try
|
2022-02-05 23:58:19 +13:00
|
|
|
|
{
|
2022-07-06 07:53:38 +12:00
|
|
|
|
using HttpResponseMessage response = client.GetAsync(urlGoogleIconDownload).Result;
|
2022-02-06 00:47:27 +13:00
|
|
|
|
using HttpContent content = response.Content;
|
|
|
|
|
Stream stream = content.ReadAsStreamAsync().Result;
|
|
|
|
|
using var fileStream = File.Create(pathIconPng);
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.CopyTo(fileStream);
|
2022-02-05 23:58:19 +13:00
|
|
|
|
}
|
2022-07-06 07:53:38 +12:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"{nameof(CreateShortcut)} failed", ex);
|
|
|
|
|
}
|
2022-02-05 23:58:19 +13:00
|
|
|
|
|
2021-12-27 19:35:39 +13:00
|
|
|
|
string pathIcon = Path.Combine(pathToStoreIcons, $"{hostname}.ico");
|
2022-07-06 07:53:38 +12:00
|
|
|
|
if (!ImagingHelper.ConvertToIcon(pathIconPng, pathIcon, 32))
|
|
|
|
|
{
|
|
|
|
|
Log.Info("Failed to convert icon.");
|
|
|
|
|
}
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2022-07-06 07:53:38 +12:00
|
|
|
|
File.Delete(pathIconPng);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2022-07-06 07:53:38 +12:00
|
|
|
|
string title = url.Replace("/", " ").
|
2021-12-27 19:35:39 +13:00
|
|
|
|
Replace("https", string.Empty).
|
|
|
|
|
Replace("http", string.Empty);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-12-27 19:35:39 +13:00
|
|
|
|
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
|
|
|
|
|
foreach (char c in invalid)
|
|
|
|
|
{
|
|
|
|
|
title = title.Replace(c.ToString(), string.Empty);
|
|
|
|
|
}
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-12-27 19:35:39 +13:00
|
|
|
|
title = Truncate(title, 128); // max 255
|
|
|
|
|
static string Truncate(string value, int maxLength)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(value) &&
|
|
|
|
|
value.Length > maxLength)
|
2021-06-26 23:24:56 +12:00
|
|
|
|
{
|
2021-12-27 19:35:39 +13:00
|
|
|
|
value = value[..maxLength];
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 19:35:39 +13:00
|
|
|
|
return value;
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
2021-12-27 19:35:39 +13:00
|
|
|
|
|
|
|
|
|
using StreamWriter writer = new(pathToStoreFile + "\\" + title.Trim() + ".url");
|
|
|
|
|
writer.WriteLine("[InternetShortcut]");
|
|
|
|
|
writer.WriteLine($"URL={url.TrimEnd('\0')}");
|
|
|
|
|
writer.WriteLine("IconIndex=0");
|
|
|
|
|
writer.WriteLine($"HotKey=0");
|
|
|
|
|
writer.WriteLine($"IDList=");
|
|
|
|
|
writer.WriteLine($"IconFile={pathIcon}");
|
|
|
|
|
writer.Flush();
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|