naps2/NAPS2.Core/WinForms/FOcrLanguageDownload.cs

120 lines
4.4 KiB
C#
Raw Normal View History

2014-10-19 07:54:19 +04:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
2014-10-19 07:54:19 +04:00
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NAPS2.Lang.Resources;
using NAPS2.Ocr;
namespace NAPS2.WinForms
{
public partial class FOcrLanguageDownload : FormBase
{
2014-11-14 00:41:20 +03:00
private static readonly string DownloadBase = @"https://sourceforge.net/projects/naps2/files/components/tesseract-3.02/{0}/download";
2014-10-19 07:54:19 +04:00
2014-10-19 20:03:12 +04:00
private readonly OcrDependencyManager ocrDependencyManager;
public FOcrLanguageDownload(OcrDependencyManager ocrDependencyManager)
2014-10-19 07:54:19 +04:00
{
2014-10-19 20:03:12 +04:00
this.ocrDependencyManager = ocrDependencyManager;
2014-10-19 07:54:19 +04:00
InitializeComponent();
// Add missing languages to the list of language options
// Special case for English: sorted first, and checked by default
var languageOptions = this.ocrDependencyManager.GetMissingLanguages().OrderBy(x => x.Code == "eng" ? "AAA" : x.LangName);
2014-10-19 07:54:19 +04:00
foreach (var languageOption in languageOptions)
{
var item = new ListViewItem { Text = languageOption.LangName, Tag = languageOption };
if (languageOption.Code == "eng")
{
item.Checked = true;
}
lvLanguages.Items.Add(item);
}
UpdateView();
}
protected override void OnLoad(object sender, EventArgs eventArgs)
{
new LayoutManager(this)
.Bind(lvLanguages)
.WidthToForm()
.HeightToForm()
.Bind(labelSizeEstimate, btnCancel, btnDownload)
.BottomToForm()
.Bind(btnCancel, btnDownload)
.RightToForm()
.Activate();
}
private void UpdateView()
{
2014-10-19 20:03:12 +04:00
double downloadSize =
2014-10-19 07:54:19 +04:00
lvLanguages.Items.Cast<ListViewItem>().Where(x => x.Checked).Select(x => ((OcrLanguage)x.Tag).Size).Sum();
2014-10-19 20:03:12 +04:00
if (!ocrDependencyManager.IsExecutableDownloaded)
{
downloadSize += ocrDependencyManager.ExecutableFileSize;
}
labelSizeEstimate.Text = string.Format(MiscResources.EstimatedDownloadSize, downloadSize.ToString("f1"));
2014-10-19 07:54:19 +04:00
btnDownload.Enabled = lvLanguages.Items.Cast<ListViewItem>().Any(x => x.Checked);
}
private void lvLanguages_ItemChecked(object sender, ItemCheckedEventArgs e)
{
UpdateView();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void btnDownload_Click(object sender, EventArgs e)
{
var progressForm = FormFactory.Create<FDownloadProgress>();
2014-10-19 20:03:12 +04:00
if (!ocrDependencyManager.IsExecutableDownloaded)
{
2015-05-07 18:33:07 +03:00
progressForm.QueueFile(DownloadBase, ocrDependencyManager.ExecutableFileName, ocrDependencyManager.ExecutableFileSha1, tempPath =>
2014-10-19 20:03:12 +04:00
{
string exeFilePath = Path.Combine(ocrDependencyManager.GetExecutableDir().FullName,
ocrDependencyManager.ExecutableFileName.Replace(".gz", ""));
DecompressFile(tempPath, exeFilePath);
});
}
foreach (
var lang in
lvLanguages.Items.Cast<ListViewItem>().Where(x => x.Checked).Select(x => (OcrLanguage)x.Tag))
{
2015-05-07 18:33:07 +03:00
progressForm.QueueFile(DownloadBase, lang.Filename, lang.Sha1, tempPath =>
{
2014-10-19 20:03:12 +04:00
string langFilePath = Path.Combine(ocrDependencyManager.GetLanguageDir().FullName,
lang.Filename.Replace(".gz", ""));
2014-10-19 20:03:12 +04:00
DecompressFile(tempPath, langFilePath);
});
}
Close();
progressForm.ShowDialog();
}
2014-10-19 20:03:12 +04:00
private static void DecompressFile(string sourcePath, string destPath)
{
using (FileStream inFile = new FileInfo(sourcePath).OpenRead())
{
using (FileStream outFile = File.Create(destPath))
{
using (GZipStream decompress = new GZipStream(inFile, CompressionMode.Decompress))
{
decompress.CopyTo(outFile);
}
}
}
}
2014-10-19 07:54:19 +04:00
}
}