-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindow_save_station.xaml.cs
201 lines (164 loc) · 6.68 KB
/
Window_save_station.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Xml;
using System.Xml.Linq;
namespace Organizer
{
public partial class Window_save_station : Window
{
MainWindow MainWindow;
List_Stations List_Stations;
private string string_last_id;
private string string_new_id;
public Window_save_station(MainWindow mw, List_Stations ls)
{
MainWindow = mw;
List_Stations = ls;
InitializeComponent();
this.DataContext = List_Stations;
List_Stations.Request_answer = "Add here new radio station";
}
private void btn_save_new_station_data_Click(object sender, RoutedEventArgs e)
{
List_Stations.Request_answer = "Testing URL...";
Check_empty_fields();
if (Check_if_station_is_doubled() != false)
{
MessageBox.Show("Station name or Stream URL is doubled! Please change name and/or URL.");
return;
}
Get_new_id();
//Add_new_entry_to_XML();
Add_new_station_to_bindinglist();
}
private void Add_new_station_to_bindinglist()
{
Organizer_MainView omv = new Organizer_MainView();
omv.Id = string_new_id;
omv.Standard = "false";
omv.Station_Name = tb_add_new_name.Text;
omv.Station_homepage = tb_add_new_homepage.Text;
omv.Url = tb_add_new_stream_url.Text;
omv.Genre = tb_add_new_genre.Text;
omv.Comment = tb_add_comment.Text;
MainWindow.count_stations++;
omv.Number_of_stations = MainWindow.count_stations;
List_Stations.Stream_data_list.Add(new Get_Stream_Information(omv, List_Stations, 0));
Thread.Sleep(2000);
if (List_Stations.If_valid != true)
{
MessageBox.Show("Invalid MP3 stream link!");
List_Stations.Request_answer = "Invalid link";
return;
}
else
{
List_Stations.Request_answer = "Testing URL was succesful.";
List_Stations.Stations_data_List.Add(omv);
}
}
private void Check_empty_fields()
{
if (String.IsNullOrEmpty(tb_add_new_name.Text) && !String.IsNullOrEmpty(tb_add_new_stream_url.Text))
{
MessageBox.Show("Name of station can't be clear!");
return;
}
if (!String.IsNullOrEmpty(tb_add_new_name.Text) && String.IsNullOrEmpty(tb_add_new_stream_url.Text))
{
MessageBox.Show("Link of station can't be clear!");
return;
}
if (String.IsNullOrEmpty(tb_add_new_name.Text) && String.IsNullOrEmpty(tb_add_new_stream_url.Text))
{
MessageBox.Show("Stationname and stationlink can't be clear!");
return;
}
}
private bool Check_if_station_is_doubled()
{
bool isdoubled = false;
for (int i = 0; i < List_Stations.Stations_data_List.Count; i++)
{
if (List_Stations.Stations_data_List[i].Station_Name == tb_add_new_name.Text || List_Stations.Stations_data_List[i].Url == tb_add_new_stream_url.Text)
{
isdoubled = true;
break;
}
else
{
isdoubled = false;
}
}
return isdoubled;
}
private void Get_new_id()
{
XmlDocument doc = new XmlDocument();
XmlNodeList elemList = doc.GetElementsByTagName("station");
List<string> list_id = new List<string>();
int id;
doc.Load(List_Stations.XML_Document_Name);
for (int i = 0; i < elemList.Count; i++)
{
list_id.Add(elemList[i].Attributes["id"].Value);
}
List<int> list_int = liststring_to_listint(list_id);
string_last_id = Convert.ToString((id = return_id(list_int) - 1)); //-1 because of last node
string_new_id = Convert.ToString(id = return_id(list_int));
}
private void Add_new_entry_to_XML()
{
var xml = XDocument.Load(List_Stations.XML_Document_Name);
xml.Root
.Elements("station")
.FirstOrDefault(r => r.Attribute("id").Value == string_last_id)
.AddAfterSelf(new XElement("station",
new XAttribute("id", string_new_id),
new XAttribute("standard", "false"),
new XElement("station_name", tb_add_new_name.Text),
new XElement("station_homepage", tb_add_new_name.Text),
new XElement("link", tb_add_new_stream_url.Text),
new XElement("comment", tb_add_comment.Text),
new XElement("genre", tb_add_new_genre.Text)
));
xml.Save(List_Stations.XML_Document_Name);
string_last_id = null;
string_new_id = null;
}
private List<int> liststring_to_listint(List<string> list)
{
List<int> list_int = new List<int>();
for (int i = 0; i < list.Count; i++)
{
list_int.Add(Convert.ToInt32(list[i]));
}
return list_int;
}
private int return_id(List<int> list_int)
{
int a = 1;
int b = a;
for (int i = 0; i < list_int.Count; i++)
{
if (a == list_int[i])
{
a++;
}
else
{
break;
}
}
return a;
}
private void btn_back_to_main_app_Click(object sender, RoutedEventArgs e)
{
List_Stations.Request_answer = "Add here new radio station";
this.Close();
}
}
}