Skip to content

Commit

Permalink
[#] Clean up code and catch exceptions to prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Mar 5, 2018
1 parent c2b43bb commit 62d9ddd
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 39 deletions.
4 changes: 2 additions & 2 deletions PasteEx/Core/ClipData.cs → PasteEx/Core/ClipboardData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace PasteEx.Core
{
public class ClipData
public class ClipboardData
{
public IDataObject IAcquisition { get; set; }

Expand All @@ -21,7 +21,7 @@ protected virtual void OnSaveCompleted()
SaveCompleted?.Invoke();
}

public ClipData(IDataObject iDataObject)
public ClipboardData(IDataObject iDataObject)
{
IAcquisition = iDataObject;
Storage = new DataObject();
Expand Down
4 changes: 2 additions & 2 deletions PasteEx/Core/Processor/BaseProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace PasteEx.Core
public abstract class BaseProcessor
{

public ClipData Data { get; set; }
public ClipboardData Data { get; set; }

public BaseProcessor(ClipData clipData)
public BaseProcessor(ClipboardData clipData)
{
Data = clipData;
}
Expand Down
2 changes: 1 addition & 1 deletion PasteEx/Core/Processor/FileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PasteEx.Core
{
public class FileProcessor : BaseProcessor
{
public FileProcessor(ClipData clipData) : base(clipData)
public FileProcessor(ClipboardData clipData) : base(clipData)
{
Data = clipData;
}
Expand Down
2 changes: 1 addition & 1 deletion PasteEx/Core/Processor/HtmlProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace PasteEx.Core
{
public class HtmlProcessor : BaseProcessor
{
public HtmlProcessor(ClipData clipData) : base(clipData)
public HtmlProcessor(ClipboardData clipData) : base(clipData)
{
Data = clipData;
}
Expand Down
21 changes: 16 additions & 5 deletions PasteEx/Core/Processor/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ImageProcessor : BaseProcessor

private string analyzeExt;

public ImageProcessor(ClipData clipData) : base(clipData)
public ImageProcessor(ClipboardData clipData) : base(clipData)
{
Data = clipData;
}
Expand Down Expand Up @@ -209,21 +209,32 @@ private Bitmap GetImageFromDataObjectOld(DataObject retrievedData)
return bitmap;
}

private void GetImageFromUrl(string url, string path)
private async void GetImageFromUrl(string url, string path)
{
WebClient client = new WebClient();
client.DownloadFileCompleted += (sender, e) =>
{
OnSaveAsFileCompleted();
//...
};
client.DownloadProgressChanged += (sender, e) =>
{
//this.proBarDownLoad.Minimum = 0;
//this.proBarDownLoad.Maximum = (int)e.TotalBytesToReceive;
//this.proBarDownLoad.Value = (int)e.BytesReceived;
FormMain.GetInstance().ChangeTsslCurrentLocation($"下载图片中...{e.ProgressPercentage}%");
FormMain.GetInstance().ChangeTsslCurrentLocation(
String.Format(Resources.Resource_zh_CN.TipPictureDownloading, e.ProgressPercentage));
};
client.DownloadFileTaskAsync(new Uri(url), path);
try
{
await client.DownloadFileTaskAsync(new Uri(url), path);
}
catch (Exception ex)
{
Logger.Error(ex);
MessageBox.Show(Resources.Resource_zh_CN.TipDownloadFailed + " : " + ex.Message,
Resources.Resource_zh_CN.TitleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
OnSaveAsFileCompleted();
}
}
}
2 changes: 1 addition & 1 deletion PasteEx/Core/Processor/RtfProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace PasteEx.Core
{
public class RtfProcessor : BaseProcessor
{
public RtfProcessor(ClipData clipData) : base(clipData)
public RtfProcessor(ClipboardData clipData) : base(clipData)
{
Data = clipData;
}
Expand Down
2 changes: 1 addition & 1 deletion PasteEx/Core/Processor/TextProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PasteEx.Core
{
public class TextProcessor : BaseProcessor
{
public TextProcessor(ClipData clipData) : base(clipData)
public TextProcessor(ClipboardData clipData) : base(clipData)
{
Data = clipData;
}
Expand Down
19 changes: 9 additions & 10 deletions PasteEx/FormMain.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using PasteEx.Core;
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PasteEx
Expand All @@ -13,7 +10,7 @@ public partial class FormMain : Form
{
private static FormMain dialogue = null;

private ClipData data;
private ClipboardData data;

private string currentLocation;

Expand Down Expand Up @@ -54,7 +51,7 @@ public FormMain(string location)

private void FormMain_Load(object sender, EventArgs e)
{
data = new ClipData(Clipboard.GetDataObject());
data = new ClipboardData(Clipboard.GetDataObject());
data.SaveCompleted += () => Application.Exit(); // exit when save completed
string[] extensions = data.Analyze();
cboExtension.Items.AddRange(extensions);
Expand Down Expand Up @@ -214,7 +211,7 @@ private void btnSave_Click(object sender, EventArgs e)

if (File.Exists(path))
{
DialogResult result = MessageBox.Show(String.Format("目标文件{0}已经存在,是否覆盖?", path),
DialogResult result = MessageBox.Show(String.Format(Resources.Resource_zh_CN.TipTargetFileExisted, path),
Resources.Resource_zh_CN.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Expand Down Expand Up @@ -279,7 +276,7 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

public static void QuickPasteEx(string location)
{
ClipData data = new ClipData(Clipboard.GetDataObject());
ClipboardData data = new ClipboardData(Clipboard.GetDataObject());
string[] extensions = data.Analyze();

if (extensions.Length > 0)
Expand All @@ -293,14 +290,15 @@ public static void QuickPasteEx(string location)
string path = location + GenerateFileName(currentLocation, extensions[0]) + "." + extensions[0];
if (!Directory.Exists(currentLocation))
{
MessageBox.Show("粘贴目标路径不存在",
Console.WriteLine(Resources.Resource_zh_CN.TipTargetPathNotExist);
MessageBox.Show(Resources.Resource_zh_CN.TipTargetPathNotExist,
Resources.Resource_zh_CN.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (File.Exists(path))
{
DialogResult result = MessageBox.Show(String.Format("目标文件{0}已经存在,是否覆盖?", path),
DialogResult result = MessageBox.Show(String.Format(Resources.Resource_zh_CN.TipTargetFileExisted, path),
Resources.Resource_zh_CN.Title, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Expand All @@ -319,7 +317,8 @@ public static void QuickPasteEx(string location)
}
else
{
MessageBox.Show("剪贴板内容为空或不被支持",
Console.WriteLine(Resources.Resource_zh_CN.TipAnalyzeFailedWithoutPrompt);
MessageBox.Show(Resources.Resource_zh_CN.TipAnalyzeFailedWithoutPrompt,
Resources.Resource_zh_CN.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Expand Down
18 changes: 18 additions & 0 deletions PasteEx/Library/Kernel32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Runtime.InteropServices;

namespace PasteEx.Library
{
internal class Kernel32
{
[DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
internal static extern int AllocConsole();

[DllImport("kernel32.dll")]
internal static extern bool AttachConsole(int dwProcessId);

}
}
2 changes: 2 additions & 0 deletions PasteEx/PasteEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Library\Kernel32.cs" />
<Compile Include="Core\Processor\BaseProcessor.cs" />
<Compile Include="Core\ClipData.cs" />
<Compile Include="Core\Processor\FileProcessor.cs" />
Expand All @@ -84,6 +85,7 @@
<DependentUpon>FormSetting.cs</DependentUpon>
</Compile>
<Compile Include="Client.cs" />
<Compile Include="Util\CommandLine.cs" />
<Compile Include="Util\Device.cs" />
<Compile Include="Util\EasyJson.cs" />
<Compile Include="Util\ImageHelper.cs" />
Expand Down
4 changes: 3 additions & 1 deletion PasteEx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ static void Main(string[] args)

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

CommandLine.RedirectConsoleOutput();
if (args.Length > 0)
{

List<string> commands = new List<string>(args);
if (commands[0] == "/reg")
{
Expand Down Expand Up @@ -91,5 +92,6 @@ static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEv
Logger.Error(e.ExceptionObject as Exception);
MessageBox.Show((e.ExceptionObject as Exception).Message, Resources.Resource_zh_CN.TitleError, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
}
78 changes: 75 additions & 3 deletions PasteEx/Resources/Resource-en-US.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 62d9ddd

Please sign in to comment.