-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
340 lines (306 loc) · 12.6 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SonosWpfApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// UI setup and databinding inspired by this http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<ZoneData> _ZoneCollection = new ObservableCollection<ZoneData>();
ObservableCollection<PlaylistData> _PlaylistCollection = new ObservableCollection<PlaylistData>();
ObservableCollection<ZoneData> _MasterZones = new ObservableCollection<ZoneData>();
public MainWindow()
{
InitializeComponent();
}
public ObservableCollection<ZoneData> ZoneCollection
{ get { return _ZoneCollection; } }
public ObservableCollection<PlaylistData> PlaylistCollection
{ get { return _PlaylistCollection; } }
public ObservableCollection<ZoneData> MasterZones
{ get { return _MasterZones; }}
private MemoryStream playlist_stream;
/// <summary>
/// Processes the button click event when saving one playlist.
/// </summary>
private void playlistButton_Click(object sender, RoutedEventArgs e)
{
Button b = new Button();
b = (Button)sender;
string playlistQueue = ((PlaylistData)b.DataContext).PlaylistSQ;
string playlistName = ((PlaylistData)b.DataContext).PlaylistName;
string fileName = "sp_" + playlistName + ".xml";
string content = UPnP.QueryDevice.GetPlaylist(playlistQueue, UPnP.QueryDevice.PlaylistAction.Save, 0);
playlist_stream = new MemoryStream();
StreamWriter sw = new StreamWriter(playlist_stream);
sw.Write(content);
sw.Flush();
playlist_stream.Position = 0;
DataWindow dw = new DataWindow();
dw.stream = playlist_stream;
dw.ShowDialog();
// ExportPlaylist(fileName, content, "xml" /* type */);
}
private void M3UButton_Click(object sender, RoutedEventArgs e)
{
Button b = new Button();
b = (Button)sender;
string playlistQueue = ((PlaylistData)b.DataContext).PlaylistSQ;
string playlistName = ((PlaylistData)b.DataContext).PlaylistName;
string fileName = "m3u_" + playlistName + ".m3u";
string content = UPnP.QueryDevice.GetPlaylist(playlistQueue, UPnP.QueryDevice.PlaylistAction.Save, 0);
ExportPlaylist(fileName, content, "m3u" /* type */);
}
/// <summary>
/// Processes the button click event when saving all playlists.
/// </summary>
private void SaveAll_Click(object sender, RoutedEventArgs e)
{
// Iterate through playlists.
if (PlaylistCollection.Count > 0)
{
foreach (PlaylistData pd in PlaylistCollection)
{
string fileName = "sp_" + pd.PlaylistName + ".xml";
string content = UPnP.QueryDevice.GetPlaylist(pd.PlaylistSQ, UPnP.QueryDevice.PlaylistAction.Save, 0 /* start at zero */);
ExportPlaylist(fileName, content, "xml" /* type */);
}
RefreshMessage("Saved all playlists.");
}
}
/// <summary>
/// Processes the import button click.
/// Open dialog code inspired by http://www.kirupa.com/net/using_open_file_dialog_pg2.htm
/// </summary>
private void Import_Click(object sender, RoutedEventArgs e)
{
string playlistToImport = null;
string playlistToImportSafe = null;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
playlistToImport = ofd.FileName; // includes path
playlistToImportSafe = ofd.SafeFileName; // no path
playlistToImportSafe = System.IO.Path.GetFileNameWithoutExtension(playlistToImportSafe); // without extension
}
try
{
StringBuilder sb = new StringBuilder();
using (var stream = new StreamReader(playlistToImport))
{
RefreshMessage("Reading " + playlistToImport);
String line;
while ((line = stream.ReadLine()) != null)
{
sb.Append(line);
}
}
string DIDLString = sb.ToString();
UPnP.QueryDevice.ImportPlaylist(DIDLString, ((ZoneData)MasterZoneDropDown.SelectedItem).ZoneAddress, playlistToImportSafe);
RefreshMessage("Finished importing.");
}
catch (Exception ex)
{
RefreshMessage("Failed to read file " + playlistToImportSafe + " for import. " + ex.Message);
}
}
/// <summary>
/// Processes the discover button click.
/// </summary>
private void Discover_Click(object sender, RoutedEventArgs e)
{
ProgressBar pb = new ProgressBar();
try
{
pb.Show();
RefreshMessage("Discovering...");
Task tsk = Task.Factory.StartNew(() => doDiscovery()); // run in the background
Task.WaitAll(tsk);
RefreshMessage((UPnP.Discovery.Zones.Count != 0) ?
"Discovered " + UPnP.Discovery.Zones.Count.ToString() +
" devices. (" + DateTime.Now.ToString() + ")" : "No devices discovered. Try discovery again.");
foreach (string zone in UPnP.Discovery.Zones)
{
Uri uri = new Uri(zone);
ZoneData zd = new ZoneData
{
ZoneName = UPnP.Discovery.ZoneTable[zone],
ZoneAddress = uri.Host,
ZoneType = UPnP.Discovery.ZoneTypes[zone],
ZoneID = UPnP.Discovery.ZoneIDs[zone],
ZoneMaster = UPnP.Discovery.ZoneMasters[zone].ToString()
};
if (!_ZoneCollection.Contains(zd, new ZoneComparer()))
{
_ZoneCollection.Add(zd);
}
if (UPnP.Discovery.ZoneMasters[zone] && !_MasterZones.Contains(zd, new ZoneComparer()))
{
_MasterZones.Add(zd);
}
}
foreach (KeyValuePair<string, string> kvp in UPnP.QueryDevice.Playlists)
{
PlaylistData pd = new PlaylistData
{
PlaylistName = kvp.Value,
PlaylistSQ = kvp.Key,
NumItems = UPnP.QueryDevice.GetPlaylist(kvp.Key, UPnP.QueryDevice.PlaylistAction.Count, 0 /* not used for count */)
};
if (!_PlaylistCollection.Contains(pd, new PlaylistComparer()))
{
_PlaylistCollection.Add(pd);
}
}
}
catch (Exception ex)
{
RefreshMessage(ex.Message + " Try discovery again.");
}
finally
{
if (pb.IsLoaded) pb.Close();
}
}
private void doDiscovery()
{
//CG160513 - as Discovery now uses Async callbacks, cannot be static
UPnP.Discovery discovery = new UPnP.Discovery();
discovery.Discover();
UPnP.QueryDevice.QueryZoneAttributes();
UPnP.QueryDevice.FindMasters();
UPnP.QueryDevice.QueryZonePlayerXml();
UPnP.QueryDevice.GetPlaylists();
}
/// <summary>
/// Gets a valid file name, making sure not bad characters get in the name.
/// Code from: http://stackoverflow.com/questions/309485/c-sanitize-file-name
/// </summary>
private static string GetValidFileName(string name)
{
string invalidChars = Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()));
string invalidReStr = string.Format(@"[{0}]+", invalidChars);
return Regex.Replace(name, invalidReStr, "_");
}
/// <summary>
/// Exports a playlist.
/// </summary>
/// <param name="fileName">The name of the exported playlist file.</param>
/// <param name="content">The content to go inside the file.</param>
///
private void ExportPlaylist(string fileName, string content, string type)
{
string currDirectory = AppDomain.CurrentDomain.BaseDirectory;
string filePath = currDirectory + GetValidFileName(fileName);
try
{
using (var fileStream = new FileStream(filePath, FileMode.Create)) // Synchronous mode
{
if (type.Equals("xml"))
{
byte[] info = new UTF8Encoding(true).GetBytes(content);
fileStream.Write(info, 0, info.Length);
RefreshMessage("Saved " + fileName);
}
else
{
String playlist = UPnP.QueryDevice.CreateM3UPlaylist(content);
byte[] info = new UTF8Encoding(true).GetBytes(playlist);
fileStream.Write(info, 0, info.Length);
RefreshMessage("Saved " + fileName);
}
}
}
catch (Exception ex)
{
RefreshMessage("Failed to save " + fileName + ". " + ex.Message);
}
}
/// <summary>
/// Helper method to avoid typing the same two lines many times.
/// </summary>
/// <param name="message">Message to assign to UI.</param>
public void RefreshMessage(string message)
{
Message.Text = message;
Message.Refresh();
}
private void btnShowData_Click(object sender, RoutedEventArgs e)
{
DataWindow dw = new DataWindow();
dw.Show();
}
}
/// <summary>
/// Compares two zones to determine if they are equal.
/// </summary>
public class ZoneComparer : IEqualityComparer<ZoneData>
{
public bool Equals(ZoneData zd1, ZoneData zd2)
{
return zd1.ZoneID == zd2.ZoneID;
}
public int GetHashCode(ZoneData zd)
{
return zd.ZoneID.GetHashCode();
}
}
/// <summary>
/// Compares two playlists to determine if they are equal.
/// </summary>
public class PlaylistComparer : IEqualityComparer<PlaylistData>
{
public bool Equals(PlaylistData pd1, PlaylistData pd2)
{
return pd1.PlaylistSQ == pd2.PlaylistSQ;
}
public int GetHashCode(PlaylistData pd)
{
return (pd.PlaylistName + pd.PlaylistSQ).GetHashCode();
}
}
/// <summary>
/// Extension method to deal with refreshing UI element in WPF xaml.
/// Idea came from here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/878ea631-c76e-49e8-9e25-81e76a3c4fe3
/// </summary>
public static class ExtensionMethods
{
private static Action EmptyDelegate = delegate() { };
public static void Refresh(this UIElement uiElement)
{
uiElement.Dispatcher.Invoke(EmptyDelegate, System.Windows.Threading.DispatcherPriority.Render);
}
}
/// <summary>
/// Defines one zone.
/// </summary>
public class ZoneData
{
public string ZoneName { get; set; }
public string ZoneAddress { get; set; }
public string ZoneType { get; set; }
public string ZoneID { get; set; }
public string ZoneMaster { get; set; }
}
/// <summary>
/// Defines one playlist.
/// </summary>
public class PlaylistData
{
public string PlaylistName { get; set; }
public string PlaylistSQ { get; set; }
public string NumItems { get; set; }
}
}