Skip to content

Commit 22edf27

Browse files
Added route picture and description
1 parent 7c404cd commit 22edf27

File tree

3 files changed

+154
-31
lines changed

3 files changed

+154
-31
lines changed

Source/Menu/DownloadContentForm.Designer.cs

Lines changed: 58 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Menu/DownloadContentForm.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using System.ComponentModel;
3030
using System.Net;
3131
using System.IO.Compression;
32+
using System.Drawing;
3233

3334
namespace ORTS
3435
{
@@ -40,6 +41,9 @@ public partial class DownloadContentForm : Form
4041

4142
private string RouteName;
4243

44+
private readonly string ImageTempFilename;
45+
private Thread ImageThread;
46+
4347
public DownloadContentForm(UserSettings settings)
4448
{
4549
InitializeComponent();
@@ -59,13 +63,65 @@ public DownloadContentForm(UserSettings settings)
5963
dataGridViewDownloadContent.Sort(dataGridViewDownloadContent.Columns[0], ListSortDirection.Ascending);
6064

6165
InstallPathTextBox.Text = settings.Content.InstallPath;
66+
67+
ImageTempFilename = Path.GetTempFileName();
6268
}
6369

6470
void dataGridViewDownloadContent_SelectionChanged(object sender, EventArgs e)
6571
{
6672
RouteName = dataGridViewDownloadContent.CurrentRow.Cells[0].Value.ToString();
6773

6874
DownloadContentButton.Enabled = string.IsNullOrWhiteSpace(Routes[RouteName].DateInstalled);
75+
76+
// picture box handling
77+
78+
if (pictureBoxRoute.Image != null)
79+
{
80+
pictureBoxRoute.Image.Dispose();
81+
pictureBoxRoute.Image = null;
82+
}
83+
84+
if (!string.IsNullOrEmpty(Routes[RouteName].Image))
85+
{
86+
if (ImageThread != null)
87+
{
88+
if (ImageThread.IsAlive)
89+
{
90+
ImageThread.Abort();
91+
}
92+
}
93+
94+
// use a thread as grabbing the picture from the web might take a few seconds
95+
ImageThread = new Thread(() =>
96+
{
97+
// wait one second as the user might scroll through the list
98+
Stopwatch sw = Stopwatch.StartNew();
99+
while (sw.ElapsedMilliseconds <= 1000) { }
100+
101+
try
102+
{
103+
using (WebClient myWebClient = new WebClient())
104+
{
105+
myWebClient.DownloadFile(Routes[RouteName].Image, ImageTempFilename);
106+
}
107+
}
108+
catch
109+
{
110+
// route lives whithout a picture, not such a problem
111+
}
112+
113+
if (File.Exists(ImageTempFilename))
114+
{
115+
pictureBoxRoute.Image = new Bitmap(ImageTempFilename);
116+
}
117+
});
118+
ImageThread.Start();
119+
}
120+
121+
122+
// text box with description
123+
124+
textBoxRoute.Text = Routes[RouteName].Description;
69125
}
70126

71127
private void InstallPathButton_Click(object sender, EventArgs e)
@@ -397,6 +453,23 @@ private bool insertRowInOptions(string installPathRoute)
397453
}
398454
return true;
399455
}
456+
457+
private void DownloadContentForm_FormClosing(object sender, FormClosingEventArgs e)
458+
{
459+
try
460+
{
461+
if (pictureBoxRoute.Image != null)
462+
{
463+
pictureBoxRoute.Image.Dispose();
464+
pictureBoxRoute.Image = null;
465+
}
466+
File.Delete(ImageTempFilename);
467+
}
468+
catch
469+
{
470+
// just ignore, it's a file in the user temp directory anyway
471+
}
472+
}
400473
}
401474

402475
}

Source/ORTS.Settings/RouteSettings.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ public class Route
3636
public long DownloadSize;
3737
public long InstallSize;
3838

39-
public Route(string dateInstalled, string url, long downloadSize, long installSize)
39+
public string Image;
40+
41+
public string Description;
42+
43+
public Route(string dateInstalled, string url, long downloadSize, long installSize, string image, string description)
4044
{
4145
DateInstalled = dateInstalled;
4246
Url = url;
4347
DownloadSize = downloadSize;
4448
InstallSize = installSize;
49+
Image = image;
50+
Description = description;
4551
}
4652
}
4753

@@ -116,12 +122,14 @@ public void LoadContentAndInstalled()
116122
string url = result["url"].ToString();
117123
long downloadSize = convertResultToLong(result, "downloadSize");
118124
long installSize = convertResultToLong(result, "installSize");
125+
string image = convertResultToString(result, "image");
126+
string description = convertResultToString(result, "description");
119127

120128
if (url.EndsWith(".git") || url.EndsWith(".zip"))
121129
{
122130
if (!Routes.ContainsKey(routeName))
123131
{
124-
Routes.Add(routeName, new RouteSettings.Route("", url, downloadSize, installSize));
132+
Routes.Add(routeName, new RouteSettings.Route("", url, downloadSize, installSize, image, description));
125133
}
126134
}
127135
}
@@ -137,7 +145,7 @@ public void LoadContentAndInstalled()
137145
return;
138146
}
139147

140-
long convertResultToLong(JToken result, string fieldName)
148+
private long convertResultToLong(JToken result, string fieldName)
141149
{
142150
if (result[fieldName] != null)
143151
{
@@ -149,6 +157,18 @@ long convertResultToLong(JToken result, string fieldName)
149157
}
150158
}
151159

160+
private string convertResultToString(JToken result, string fieldName)
161+
{
162+
if (result[fieldName] != null)
163+
{
164+
return result[fieldName].ToString();
165+
}
166+
else
167+
{
168+
return "";
169+
}
170+
}
171+
152172
private void directoryDelete(string directoryName)
153173
{
154174
if (Directory.Exists(directoryName))

0 commit comments

Comments
 (0)