Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Downtify.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Downtify", "Downtify\Downtify.csproj", "{311EACED-AFD3-42BE-B5BE-E5D046A69AAF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DowntifyUpdater", "DowntifyUpdater\DowntifyUpdater.csproj", "{EC8D9553-021D-4787-9BD3-278F81C15397}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{311EACED-AFD3-42BE-B5BE-E5D046A69AAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{311EACED-AFD3-42BE-B5BE-E5D046A69AAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{311EACED-AFD3-42BE-B5BE-E5D046A69AAF}.Release|Any CPU.Build.0 = Release|Any CPU
{EC8D9553-021D-4787-9BD3-278F81C15397}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC8D9553-021D-4787-9BD3-278F81C15397}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC8D9553-021D-4787-9BD3-278F81C15397}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC8D9553-021D-4787-9BD3-278F81C15397}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 7 additions & 4 deletions Downtify/Downtify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@
</Reference>
<Reference Include="yeti.mmedia, Version=1.0.5245.33655, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\yeti.mmedia.dll</HintPath>
<HintPath>Libs\yeti.mmedia.dll</HintPath>
</Reference>
<Reference Include="yeti.mp3, Version=1.0.5245.33657, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\yeti.mp3.dll</HintPath>
<HintPath>Libs\yeti.mp3.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -91,6 +91,9 @@
<Compile Include="GUI\frmMain.Designer.cs">
<DependentUpon>frmMain.cs</DependentUpon>
</Compile>
<Compile Include="GUI\TextProgressBar.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="GUI\PlaceholderTextBox.cs">
<SubType>Component</SubType>
</Compile>
Expand Down Expand Up @@ -168,12 +171,12 @@
</None>
</ItemGroup>
<ItemGroup>
<None Include="language/en.xml">
<None Include="language\en.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="language/de.xml">
<None Include="language\de.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
43 changes: 43 additions & 0 deletions Downtify/GUI/TextProgressBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Downtify.GUI
{
public class TextProgressBar : ProgressBar
{
public override string Text { get; set; }
public int TotalTracks { get; set; }
public int CurrentTrack { get; set; }
public bool ShowText { get; set; }

public TextProgressBar()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Text = "";
ShowText = false;
TotalTracks = 0;
CurrentTrack = 0;
}

protected override void OnPaint(PaintEventArgs e)
{
Rectangle rect = ClientRectangle;

ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rect);
rect.Inflate(-3, -3);
if (Value > 0)
{
Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, clip);
}
if (ShowText)
{
string text = String.Format(Text, CurrentTrack, TotalTracks);
float center_x = this.Width / 2.0f - (e.Graphics.MeasureString(text, SystemFonts.DefaultFont).Width / 2.0f);
float center_y = this.Height / 2.0f - (e.Graphics.MeasureString(text, SystemFonts.DefaultFont).Height / 2.0f);
e.Graphics.DrawString(text, SystemFonts.DefaultFont, Brushes.Black, new PointF(center_x, center_y));
}
}
}
}
8 changes: 6 additions & 2 deletions Downtify/GUI/frmMain.Designer.cs

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

152 changes: 110 additions & 42 deletions Downtify/GUI/frmMain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Xml;

namespace Downtify.GUI
{
Expand All @@ -14,8 +19,9 @@ public frmMain()
{
InitializeComponent();

downloader = new SpotifyDownloader();
configuration = new XmlConfiguration("config.xml");
configuration.LoadConfigurationFile();
downloader = new SpotifyDownloader();
downloader.OnLoginResult += OnLoginResult;
downloader.OnDownloadComplete += downloader_OnDownloadComplete;
downloader.OnDownloadProgress += downloader_OnDownloadProgress;
Expand All @@ -40,7 +46,7 @@ private void downloader_OnDownloadComplete(bool successfully)
EnableControls(true);
return;
}

progressBar1.CurrentTrack++;
downloader.Download(((TrackItem)listBoxTracks.SelectedItems[0]).Track);
}

Expand All @@ -60,22 +66,54 @@ private void frmMain_Load(object sender, EventArgs e)
EnableControls(false);
}

private void frmMain_Shown(object sender, EventArgs e)
private async void frmMain_Shown(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(200);
this.Activate();

// very ugly, use config parser (json for example) would be nicer
string username = "", password = "";
configuration.LoadConfigurationFile();
TransferConfig();
username = configuration.GetConfiguration("username");
password = configuration.GetConfiguration("password");
lang = new LanguageXML(configuration.GetConfiguration("language"));
lang = new LanguageXML(configuration.GetConfiguration("language", "en"));

textBoxLink.Placeholder = lang.GetString("download/paste_uri");
progressBar1.Text = lang.GetString("download/progression");

downloader.Login(username, password);

if (configuration.GetConfiguration("continue_dl", "false").ToLower() == "true" && File.Exists("download.xml"))
{
XmlDocument doc = new XmlDocument();
doc.Load("download.xml");
foreach (XmlNode node in doc.SelectNodes("tracks/track"))
await AddDownload(node.InnerText);
}
}

private void frmMain_Closing(object sender, FormClosingEventArgs e)
{
if (buttonDownload.Enabled == false && configuration.GetConfiguration("continue_dl", "false").ToLower() == "true")
{
if (File.Exists("download.xml"))
File.Delete("download.xml");
List<string> tracks = new List<string>();
foreach (TrackItem track in listBoxTracks.SelectedItems)
tracks.Add(SpotifySharp.Link.CreateFromTrack(track.Track, 0).AsString());
if (tracks.Count > 0)
{
XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateElement("tracks");
doc.AppendChild(root);
foreach (string trackLink in tracks)
{
XmlNode track = doc.CreateElement("track");
track.InnerText = trackLink;
root.AppendChild(track);
}
doc.Save("download.xml");
}
}
}

private void TransferConfig()
Expand Down Expand Up @@ -121,37 +159,85 @@ private void EnableControls(bool enable)
((Control)control).Enabled = enable;
}

private async void textBoxLink_TextChanged(object sender, EventArgs e)
private async void textBoxLink_TextChanged(object sender, EventArgs e)
{
await AddDownload(textBoxLink.Text);
}

private void listBoxTracks_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if (listBoxTracks.SelectedItems.Count == 0)
return;

var list = new TrackItem[listBoxTracks.SelectedItems.Count];
listBoxTracks.SelectedItems.CopyTo(list, 0);

foreach (var track in list)
listBoxTracks.Items.Remove(track);
}
else if (e.KeyCode == Keys.A && e.Control)
{
var list = new TrackItem[listBoxTracks.Items.Count];
listBoxTracks.Items.CopyTo(list, 0);

listBoxTracks.SelectedItems.Clear();
foreach (var track in list)
listBoxTracks.SelectedItems.Add(track);
}
}

private void buttonDownload_Click(object sender, EventArgs e)
{
if (listBoxTracks.SelectedItems.Count == 0)
{
MessageBox.Show(lang.GetString("error/no_download_selection"), lang.GetString("title/error"));
return;
}

progressBar1.TotalTracks = listBoxTracks.SelectedItems.Count;
progressBar1.CurrentTrack = 1;
progressBar1.ShowText = true;

EnableControls(false);
downloader.Download(((TrackItem)listBoxTracks.SelectedItems[0]).Track);
}

private async Task AddDownload(string link)
{
var link = textBoxLink.Text;
try
{
EnableControls(false);

//Validate pasted URI
if(link.Length > 0 && !link.ToLower().StartsWith("spotify:"))
if (link.Length > 0 && !link.ToLower().StartsWith("spotify:") && !link.Contains("play.spotify.com"))
{
MessageBox.Show(lang.GetString("download/invalid_uri"));
textBoxLink.Clear();
return;
}
else if (link.Contains("play.spotify.com"))
{
link = BuildSpotifyURI(link);
}

if (link.ToLower().Contains("playlist"))
{
var playlist = await downloader.FetchPlaylist(textBoxLink.Text);
var playlist = await downloader.FetchPlaylist(link);
for (int i = 0; i < playlist.NumTracks(); i++)
listBoxTracks.Items.Add(new TrackItem(playlist.Track(i)));
textBoxLink.Clear();
}
else if (link.ToLower().Contains("track"))
{
var track = await downloader.FetchTrack(textBoxLink.Text);
var track = await downloader.FetchTrack(link);
listBoxTracks.Items.Add(new TrackItem(track));
textBoxLink.Clear();
}
else if(link.ToLower().Contains("album"))
else if (link.ToLower().Contains("album"))
{
var album = await downloader.FetchAlbum(textBoxLink.Text);
var album = await downloader.FetchAlbum(link);
for (int i = 0; i < album.NumTracks(); i++)
listBoxTracks.Items.Add(new TrackItem(album.Track(i)));
textBoxLink.Clear();
Expand All @@ -166,40 +252,22 @@ private async void textBoxLink_TextChanged(object sender, EventArgs e)
}
}

private void listBoxTracks_KeyDown(object sender, KeyEventArgs e)
private string BuildSpotifyURI(string url)
{
if (e.KeyCode == Keys.Delete)
{
if (listBoxTracks.SelectedItems.Count == 0)
return;
string uri = url;

var list = new TrackItem[listBoxTracks.SelectedItems.Count];
listBoxTracks.SelectedItems.CopyTo(list, 0);
Regex regex = new Regex(@"https?:\/\/play\.spotify\.com\/(user\/(?<uid>(\d.+))\/)?(?<type>playlist|album|track)\/(?<tid>.+)"); //ToDo: Optimize this RegEx

foreach (var track in list)
listBoxTracks.Items.Remove(track);
}
else if (e.KeyCode == Keys.A && e.Control)
{
var list = new TrackItem[listBoxTracks.Items.Count];
listBoxTracks.Items.CopyTo(list, 0);
Match match = regex.Match(url);

listBoxTracks.SelectedItems.Clear();
foreach (var track in list)
listBoxTracks.SelectedItems.Add(track);
}
}

private void buttonDownload_Click(object sender, EventArgs e)
{
if (listBoxTracks.SelectedItems.Count == 0)
if (match.Success)
{
MessageBox.Show(lang.GetString("error/no_download_selection"), lang.GetString("title/error"));
return;
string uid = match.Groups["uid"].Value != "" ? "user:" + match.Groups["uid"].Value + ":" : "";
string type = match.Groups["type"].Value;
string tid = match.Groups["tid"].Value;
uri = "spotify:" + uid + type + ":" + tid;
}

EnableControls(false);
downloader.Download(((TrackItem)listBoxTracks.SelectedItems[0]).Track);
return uri;
}
}
}
Loading