-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
263 lines (244 loc) · 10.4 KB
/
Form1.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
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace AudirvanaPlaylistSorter {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
#region Functions
void ErrorMsgBox(string ex) {
MessageBox.Show(ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
DataTable SQLite(string Query, int CommandTimeout = -1, string ErrorText = "Default") {
var oSQL = v.oConn.CreateCommand();
oSQL.CommandText = Query;
if (CommandTimeout > 0) {
oSQL.CommandTimeout = CommandTimeout;
}
DataSet DS = new DataSet();
SQLiteDataAdapter DA = new SQLiteDataAdapter(oSQL);
try {
DA.Fill(DS);
} catch (Exception ex) {
if (ErrorText == "Default") {
ErrorMsgBox(ex.ToString());
} else {
ErrorMsgBox(ErrorText);
}
return null;
}
return DS.Tables[0];
}
void RestoreCheckedPlaylists() {
string sSelectedItems = v.RegKey.GetValue(v.RegPathSelectedItems).ToString();
for (int i = 0; i < clsPlaylists.Items.Count; i++) {
if (sSelectedItems.Contains(v.Splitter + clsPlaylists.Items[i].ToString() + v.Splitter)) {
clsPlaylists.SetItemChecked(i, true);
}
}
}
void SaveCheckedPlaylists() {
string sSelectedItems = v.Splitter;
foreach (var Item in clsPlaylists.CheckedItems) {
sSelectedItems += Item.ToString() + v.Splitter;
}
v.RegKey.SetValue(v.RegPathSelectedItems, sSelectedItems);
}
void SelectDatabasePath(bool SetDatabasePathYN = true) {
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Select Audirvana database file";
openFileDialog1.Filter = "sqlite files (*.sqlite)|*.sqlite";
openFileDialog1.CheckFileExists = true;
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
v.sPathDatabase = openFileDialog1.FileName;
}
openFileDialog1.Dispose();
if (SetDatabasePathYN == true) {
SetDatabasePath();
}
}
void SetDatabasePath() {
v.RegKey.SetValue(v.RegPathDatabase, v.sPathDatabase);
txtPathDatabase.Text = v.sPathDatabase;
}
string GetAudirvanaInnerJoins() {
string sSQL;
sSQL = "FROM TRACKS" + v.nl;
sSQL += "INNER JOIN PLAYLISTS_TRACKS ON TRACKS.track_id = PLAYLISTS_TRACKS.track_id" + v.nl;
sSQL += "INNER JOIN ALBUMS ON TRACKS.album_id = ALBUMS.album_id" + v.nl;
sSQL += "INNER JOIN TRACKS_ARTISTS ON TRACKS.track_id = TRACKS_ARTISTS.track_id" + v.nl;
sSQL += "INNER JOIN ARTISTS ON TRACKS_ARTISTS.artist_id = ARTISTS.artist_id" + v.nl;
return sSQL;
}
void SortPlaylists(string Playlist) {
string sSQL;
int c, RecordCount, LastRecord = 999000;
var oSQL = v.oConn.CreateCommand();
var DS = SQLite("SELECT playlist_id FROM PLAYLISTS WHERE title = '" + Playlist + "';");
string PlaylistID = DS.Rows[0]["playlist_id"].ToString();
DS.Clear();
foreach (string String in new string[] { "The ", "A " }) {
oSQL.CommandText = "UPDATE ARTISTS SET sort_name = SUBSTR(name, " + (String.Length + 1) + ") WHERE name LIKE '" + String + "%';";
oSQL.ExecuteNonQuery();
}
sSQL = "SELECT TRACKS.track_id, PLAYLISTS_TRACKS.position" + v.nl;
sSQL += GetAudirvanaInnerJoins() + v.nl;
sSQL += "WHERE PLAYLISTS_TRACKS.playlist_id = " + PlaylistID + v.nl;
sSQL += "ORDER BY UPPER(ARTISTS.sort_name), UPPER(ALBUMS.title), TRACKS.track_number;";
DS = SQLite(sSQL);
RecordCount = DS.Rows.Count;
// Get highest position in playlist
sSQL = "SELECT MAX(PLAYLISTS_TRACKS.position)" + v.nl;
sSQL += GetAudirvanaInnerJoins() + v.nl;
sSQL += "WHERE PLAYLISTS_TRACKS.playlist_id = " + PlaylistID + v.nl + ";";
var DS2 = SQLite(sSQL);
try {
LastRecord = Convert.ToInt32(DS2.Rows[0][0]) + 5;
} catch (Exception ex) {
ErrorMsgBox(ex.ToString());
}
DS2.Clear();
foreach (int Number in new int[] { LastRecord, 0 }) {
c = Number;
for (int i = 0; i < RecordCount; i++) {
c++;
sSQL = "UPDATE PLAYLISTS_TRACKS SET position = " + c + " ";
sSQL += "WHERE track_id = " + DS.Rows[i]["track_id"] + " AND playlist_id = " + PlaylistID + ";";
oSQL.CommandText = sSQL;
try {
oSQL.ExecuteNonQuery();
} catch (Exception ex) {
ErrorMsgBox(sSQL + Environment.NewLine + ex.ToString());
}
}
}
DS.Clear();
}
void LockUI(bool Enabled) {
clsPlaylists.Enabled = Enabled;
btnSort.Enabled = Enabled;
btnSelectAll.Enabled = Enabled;
btnSelectNone.Enabled = Enabled;
btnSelectDatabasePath.Enabled = Enabled;
btnCancel.Enabled = !Enabled;
btnCancel.Visible = !Enabled;
txtPathDatabase.Enabled = Enabled;
}
#endregion
#region Events
void Form1_Load(object sender, EventArgs e) {
// Create RegKey if not exist
do {
v.RegKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\AudirvanaPlaylistSorter", true);
if (v.RegKey == null) {
Registry.CurrentUser.CreateSubKey(@"SOFTWARE\AudirvanaPlaylistSorter");
}
} while (v.RegKey == null);
// Set window size
this.Size = new System.Drawing.Size(Convert.ToInt32(v.RegKey.GetValue(v.RegPathWindowWidth)),
Convert.ToInt32(v.RegKey.GetValue(v.RegPathWindowHeight)));
v.sPathDatabase = Convert.ToString(v.RegKey.GetValue(v.RegPathDatabase));
if (File.Exists(v.sPathDatabase) == false) {
SelectDatabasePath(false);
}
SetDatabasePath();
v.oConn = new SQLiteConnection();
v.oConn.ConnectionString = "Data Source=" + v.sPathDatabase;
try {
v.oConn.Open();
} catch (Exception ex) {
ErrorMsgBox(ex.ToString());
}
try {
v.oConn.EnableExtensions(true);
} catch (Exception ex) {
ErrorMsgBox(ex.ToString());
}
try {
v.oConn.LoadExtension("SQLite.Interop.dll", "sqlite3_fts5_init");
} catch (Exception ex) {
ErrorMsgBox(ex.ToString());
}
var DS = SQLite("SELECT title FROM PLAYLISTS WHERE predicate IS NULL;", 5, "Couldn't query playlists! Please close Audirvana.");
if (DS == null) {
this.Close();
return;
}
foreach (DataRow Row in DS.Rows) {
clsPlaylists.Items.Add(Row["title"].ToString());
}
RestoreCheckedPlaylists();
}
async void btnSort_Click(object sender, EventArgs e) {
clsPlaylists.SelectedIndex = -1;
progressBar1.Style = ProgressBarStyle.Marquee;
LockUI(false);
v.Stop = false;
int iPlaylistCount = clsPlaylists.Items.Count;
for (int i = 0; i < iPlaylistCount; i++) {
if (clsPlaylists.GetItemChecked(i) == true && v.Stop == false) {
clsPlaylists.SetSelected(i, true);
await Task.Run(() => SortPlaylists(clsPlaylists.Items[i].ToString()));
}
}
clsPlaylists.SelectedIndex = -1;
progressBar1.Style = ProgressBarStyle.Blocks;
LockUI(true);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e) {
v.oConn.Close();
v.oConn.Dispose();
// Save window size
switch (WindowState) {
case FormWindowState.Normal:
v.RegKey.SetValue(v.RegPathWindowWidth, Size.Width.ToString());
v.RegKey.SetValue(v.RegPathWindowHeight, Size.Height.ToString());
break;
default:
v.RegKey.SetValue(v.RegPathWindowWidth, RestoreBounds.Size.Width);
v.RegKey.SetValue(v.RegPathWindowHeight, RestoreBounds.Size.Height);
break;
}
SaveCheckedPlaylists();
}
void btnSelectAll_Click(object sender, EventArgs e) {
for (int i = 0; i <= (clsPlaylists.Items.Count - 1); i++) {
clsPlaylists.SetItemCheckState(i, CheckState.Checked);
}
}
void btnSelectNone_Click(object sender, EventArgs e) {
for (int i = 0; i <= (clsPlaylists.Items.Count - 1); i++) {
clsPlaylists.SetItemCheckState(i, CheckState.Unchecked);
}
}
void btnCancel_Click(object sender, EventArgs e) {
btnCancel.Enabled = false;
v.Stop = true;
}
void btnSelectDatabasePath_Click(object sender, EventArgs e) {
SelectDatabasePath();
Application.Restart();
}
#endregion
}
public static class v {
// General
public static string nl = Environment.NewLine;
public static string sPathDatabase;
public static bool Stop;
public static SQLiteConnection oConn;
public static string Splitter = "|##|";
// Registry
public static RegistryKey RegKey;
public static string RegPathDatabase = "PathDatabase";
public static string RegPathWindowWidth = "WindowWidth";
public static string RegPathWindowHeight = "WindowHeight";
public static string RegPathSelectedItems = "SelectedItems";
}
}