Implement the Downloader
This commit is contained in:
parent
44c1e66dd0
commit
d5ab22629b
6 changed files with 431 additions and 7 deletions
|
@ -655,6 +655,29 @@ Contents:
|
|||
}
|
||||
|
||||
break;
|
||||
case "download_start":
|
||||
if (File.Exists(msg.Contents))
|
||||
{
|
||||
server.DispatchTo(new Guid(msg.GUID), new NetObject("download", new ServerMessage
|
||||
{
|
||||
Name = "download_meta",
|
||||
GUID = "server",
|
||||
Contents = JsonConvert.SerializeObject(File.ReadAllBytes(msg.Contents))
|
||||
}));
|
||||
}
|
||||
else
|
||||
{
|
||||
server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage
|
||||
{
|
||||
Name = "shiftnet_file",
|
||||
GUID = "server",
|
||||
Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found.
|
||||
|
||||
The page you requested at was not found on this multi-user domain."
|
||||
}));
|
||||
|
||||
}
|
||||
break;
|
||||
case "shiftnet_get":
|
||||
string surl = args["url"] as string;
|
||||
while (surl.EndsWith("/"))
|
||||
|
|
61
ShiftOS.WinForms/Applications/Downloader.Designer.cs
generated
Normal file
61
ShiftOS.WinForms/Applications/Downloader.Designer.cs
generated
Normal file
|
@ -0,0 +1,61 @@
|
|||
namespace ShiftOS.WinForms.Applications
|
||||
{
|
||||
partial class Downloader
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Component Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.fllist = new System.Windows.Forms.FlowLayoutPanel();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// fllist
|
||||
//
|
||||
this.fllist.AutoScroll = true;
|
||||
this.fllist.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
|
||||
this.fllist.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.fllist.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
|
||||
this.fllist.Location = new System.Drawing.Point(0, 0);
|
||||
this.fllist.Name = "fllist";
|
||||
this.fllist.Size = new System.Drawing.Size(557, 149);
|
||||
this.fllist.TabIndex = 0;
|
||||
this.fllist.WrapContents = false;
|
||||
//
|
||||
// Downloader
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.Controls.Add(this.fllist);
|
||||
this.Name = "Downloader";
|
||||
this.Size = new System.Drawing.Size(557, 149);
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.FlowLayoutPanel fllist;
|
||||
}
|
||||
}
|
172
ShiftOS.WinForms/Applications/Downloader.cs
Normal file
172
ShiftOS.WinForms/Applications/Downloader.cs
Normal file
|
@ -0,0 +1,172 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Threading;
|
||||
using ShiftOS.Engine;
|
||||
using Newtonsoft.Json;
|
||||
using ShiftOS.WinForms.Controls;
|
||||
using ShiftOS.WinForms.Tools;
|
||||
|
||||
namespace ShiftOS.WinForms.Applications
|
||||
{
|
||||
[Launcher("Downloader", false, null, "Networking")]
|
||||
public partial class Downloader : UserControl, IShiftOSWindow
|
||||
{
|
||||
public Downloader()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
Action<int, int> pupdate = null;
|
||||
Action<string> completed = null;
|
||||
Action<Download> started = null;
|
||||
|
||||
public void OnLoad()
|
||||
{
|
||||
SetupUI();
|
||||
pupdate = (i, o) =>
|
||||
{
|
||||
this.Invoke(new Action(() =>
|
||||
{
|
||||
SetupUI();
|
||||
}));
|
||||
};
|
||||
started = (i) =>
|
||||
{
|
||||
this.Invoke(new Action(() =>
|
||||
{
|
||||
SetupUI();
|
||||
}));
|
||||
}; completed = (i) =>
|
||||
{
|
||||
this.Invoke(new Action(() =>
|
||||
{
|
||||
SetupUI();
|
||||
}));
|
||||
};
|
||||
DownloadManager.ProgressUpdate += pupdate;
|
||||
DownloadManager.DownloadStarted += started;
|
||||
DownloadManager.DownloadCompleted += completed;
|
||||
}
|
||||
|
||||
public void OnSkinLoad()
|
||||
{
|
||||
SetupUI();
|
||||
}
|
||||
|
||||
public bool OnUnload()
|
||||
{
|
||||
DownloadManager.ProgressUpdate -= pupdate;
|
||||
DownloadManager.DownloadStarted -= started;
|
||||
DownloadManager.DownloadCompleted -= completed;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void OnUpgrade()
|
||||
{
|
||||
}
|
||||
|
||||
public void SetupUI()
|
||||
{
|
||||
fllist.Controls.Clear();
|
||||
|
||||
int heightMultiplier = 0;
|
||||
|
||||
foreach(var download in DownloadManager.Downloads)
|
||||
{
|
||||
var pnl = new Panel();
|
||||
pnl.Width = fllist.Width;
|
||||
pnl.Height = 50;
|
||||
var picpreview = new PictureBox();
|
||||
picpreview.Size = new Size(42, 42);
|
||||
picpreview.Image = FileSkimmerBackend.GetImage(download.Destination);
|
||||
picpreview.Location = new Point(4, 4);
|
||||
if (heightMultiplier < 5)
|
||||
heightMultiplier++;
|
||||
pnl.Controls.Add(picpreview);
|
||||
picpreview.Show();
|
||||
var prg = new ShiftedProgressBar();
|
||||
prg.Maximum = 100;
|
||||
prg.Value = download.Progress;
|
||||
prg.Width = pnl.Width - 8;
|
||||
prg.Left = 4;
|
||||
prg.Top = picpreview.Height + 8;
|
||||
prg.Height = 20;
|
||||
var lbtitle = new Label();
|
||||
lbtitle.Tag = "header1";
|
||||
lbtitle.Text = download.ShiftnetUrl;
|
||||
lbtitle.Top = 4;
|
||||
lbtitle.Left = 8 + picpreview.Height;
|
||||
pnl.Controls.Add(lbtitle);
|
||||
lbtitle.Show();
|
||||
lbtitle.AutoSize = true;
|
||||
pnl.Controls.Add(prg);
|
||||
prg.Show();
|
||||
|
||||
fllist.Controls.Add(pnl);
|
||||
pnl.Show();
|
||||
ControlManager.SetupControls(pnl);
|
||||
}
|
||||
|
||||
if (heightMultiplier == 0)
|
||||
heightMultiplier = 1;
|
||||
|
||||
this.Parent.Height = 50 * heightMultiplier;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DownloadManager
|
||||
{
|
||||
public static Download[] Downloads
|
||||
{
|
||||
get
|
||||
{
|
||||
return _downloads.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Download> _downloads = new List<Download>();
|
||||
|
||||
public static event Action<int, int> ProgressUpdate;
|
||||
|
||||
public static event Action<string> DownloadCompleted;
|
||||
|
||||
public static event Action<Download> DownloadStarted;
|
||||
|
||||
public static void StartDownload(Download down)
|
||||
{
|
||||
var t = new Thread(() =>
|
||||
{
|
||||
int byteWrite = 256;
|
||||
_downloads.Add(down);
|
||||
DownloadStarted?.Invoke(down);
|
||||
for (int i = 0; i < down.Bytes.Length; i += byteWrite)
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
_downloads[_downloads.IndexOf(down)].Progress = i / down.Bytes.Length;
|
||||
ProgressUpdate?.Invoke(_downloads.IndexOf(down), i / down.Bytes.Length);
|
||||
}
|
||||
ShiftOS.Objects.ShiftFS.Utils.WriteAllBytes(down.Destination, down.Bytes);
|
||||
_downloads.Remove(down);
|
||||
DownloadCompleted?.Invoke(down.Destination);
|
||||
});
|
||||
t.IsBackground = true;
|
||||
t.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public class Download
|
||||
{
|
||||
|
||||
public string ShiftnetUrl { get; set; }
|
||||
public string Destination { get; set; }
|
||||
public byte[] Bytes { get; set; }
|
||||
public int Progress { get; set; }
|
||||
}
|
||||
}
|
120
ShiftOS.WinForms/Applications/Downloader.resx
Normal file
120
ShiftOS.WinForms/Applications/Downloader.resx
Normal file
|
@ -0,0 +1,120 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
|
@ -102,15 +102,54 @@ namespace ShiftOS.WinForms.Applications
|
|||
|
||||
public void ShiftnetNavigate(string Url, bool pushHistory = true)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(CurrentUrl) && pushHistory)
|
||||
History.Push(CurrentUrl);
|
||||
CurrentUrl = Url;
|
||||
|
||||
|
||||
ServerManager.SendMessage("shiftnet_get", JsonConvert.SerializeObject(new
|
||||
if (Url.EndsWith(".rnp") || !Url.Contains("."))
|
||||
{
|
||||
url = Url
|
||||
}));
|
||||
txturl.Text = Url;
|
||||
if (!string.IsNullOrEmpty(CurrentUrl) && pushHistory)
|
||||
History.Push(CurrentUrl);
|
||||
CurrentUrl = Url;
|
||||
ServerManager.SendMessage("shiftnet_get", JsonConvert.SerializeObject(new
|
||||
{
|
||||
url = Url
|
||||
}));
|
||||
txturl.Text = Url;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ServerMessageReceived smr = null;
|
||||
smr = (msg) =>
|
||||
{
|
||||
if(msg.Name == "download_meta")
|
||||
{
|
||||
var bytes = JsonConvert.DeserializeObject<byte[]>(msg.Contents);
|
||||
string destPath = null;
|
||||
string ext = Url.Split('.')[Url.Split('.').Length - 1];
|
||||
|
||||
FileSkimmerBackend.GetFile(new[] { ext }, FileOpenerStyle.Save, new Action<string>((file) =>
|
||||
{
|
||||
destPath = file;
|
||||
}));
|
||||
while (string.IsNullOrEmpty(destPath))
|
||||
{
|
||||
|
||||
}
|
||||
var d = new Download
|
||||
{
|
||||
ShiftnetUrl = Url,
|
||||
Destination = destPath,
|
||||
Bytes = bytes,
|
||||
Progress = 0,
|
||||
};
|
||||
DownloadManager.StartDownload(d);
|
||||
AppearanceManager.SetupWindow(new Downloader());
|
||||
ServerManager.MessageReceived -= smr;
|
||||
}
|
||||
};
|
||||
ServerManager.MessageReceived += smr;
|
||||
ServerManager.SendMessage("download_start", Url);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnLoad()
|
||||
|
|
|
@ -87,6 +87,12 @@
|
|||
<Compile Include="Applications\Dialog.Designer.cs">
|
||||
<DependentUpon>Dialog.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Applications\Downloader.cs">
|
||||
<SubType>UserControl</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Applications\Downloader.Designer.cs">
|
||||
<DependentUpon>Downloader.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Applications\FileDialog.cs">
|
||||
<SubType>UserControl</SubType>
|
||||
</Compile>
|
||||
|
@ -247,6 +253,9 @@
|
|||
<EmbeddedResource Include="Applications\Dialog.resx">
|
||||
<DependentUpon>Dialog.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Applications\Downloader.resx">
|
||||
<DependentUpon>Downloader.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Applications\FileDialog.resx">
|
||||
<DependentUpon>FileDialog.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
|
|
Reference in a new issue