forked from olejorgensen/CompactView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolsForm.cs
323 lines (285 loc) · 11.3 KB
/
ToolsForm.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
/**************************************************************************
Copyright (C) 2011-2014 Iván Costales Suárez
This file is part of CompactView.
CompactView is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CompactView is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CompactView. If not, see <http://www.gnu.org/licenses/>.
CompactView web site <http://sourceforge.net/p/compactview/>.
**************************************************************************/
using System;
using System.IO;
using System.Windows.Forms;
namespace CompactView
{
public partial class ToolsForm : Form
{
private SqlCeDb db = new SqlCeDb();
private string openPassword = null;
public ToolsForm(string fileName, string password)
{
InitializeComponent();
SetCultureTexts();
foreach (Version version in SqlCeDb.AvailableVersions)
cbVersion.Items.Add(version);
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
LoadDatabase(fileName, password);
SetActiveButtons();
}
private void SetActiveButtons()
{
btCompact.Enabled = btRepair.Enabled = btShrink.Enabled = btVerify.Enabled = db.IsOpen;
cbUpgradeTo.Enabled = btCompact.Enabled && cbUpgradeTo.Items.Count > 0;
btUpgrade.Enabled = cbUpgradeTo.Enabled && cbUpgradeTo.SelectedIndex >= 0;
btCreate.Enabled = cbVersion.Enabled = !db.IsOpen && !string.IsNullOrEmpty(tbFileName.Text) && !File.Exists(tbFileName.Text);
if (btCreate.Enabled)
cbVersion.SelectedIndex = SqlCeDb.AvailableVersions.Length - 1;
}
private void SetCultureTexts()
{
Text = GlobalText.GetValue("Tools");
groupBox1.Text = GlobalText.GetValue("Database");
label1.Text = $"{GlobalText.GetValue("FileName")}:";
label2.Text = $"{GlobalText.GetValue("Password")}:";
label4.Text = $"{GlobalText.GetValue("Version")}:";
toolTip2.SetToolTip(tbFileName, GlobalText.GetValue("FileNameTip"));
toolTip2.SetToolTip(tbPassword, GlobalText.GetValue("PasswordTip"));
toolTip2.SetToolTip(btSelect, GlobalText.GetValue("SelectTip"));
label5.Text = $"{GlobalText.GetValue("UpgradeToVersion")}:";
groupBox2.Text = GlobalText.GetValue("Action");
btCreate.Text = GlobalText.GetValue("Create");
btCompact.Text = GlobalText.GetValue("Compact");
btRepair.Text = GlobalText.GetValue("Repair");
btShrink.Text = GlobalText.GetValue("Shrink");
btUpgrade.Text = GlobalText.GetValue("Upgrade");
btVerify.Text = GlobalText.GetValue("Verify");
toolTip1.SetToolTip(btCreate, GlobalText.GetValue("CreateTip"));
toolTip1.SetToolTip(btCompact, GlobalText.GetValue("CompactTip"));
toolTip1.SetToolTip(btRepair, GlobalText.GetValue("RepairTip"));
toolTip1.SetToolTip(btShrink, GlobalText.GetValue("ShrinkTip"));
toolTip1.SetToolTip(btUpgrade, GlobalText.GetValue("UpgradeTip"));
toolTip1.SetToolTip(btVerify, GlobalText.GetValue("VerifyTip"));
label3.Text = GlobalText.GetValue("ToolsNote");
btClose.Text = GlobalText.GetValue("Close");
}
private void btSelect_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;
string fileName = openFileDialog1.FileName;
tbFileName.Text = fileName;
db.Close();
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
LoadDatabase(fileName, null);
SetActiveButtons();
}
private void LoadDatabase(string fileName, string password)
{
tbFileName.Text = fileName;
tbPassword.Text = password ?? string.Empty;
if (db.Open(fileName, password))
{
cbVersion.Text = db.Version.SqlceVersion;
openPassword = password;
cbUpgradeTo.Items.Clear();
bool ok = false;
foreach (Version version in SqlCeDb.AvailableVersions)
if (ok)
cbUpgradeTo.Items.Add(version);
else if (version == db.Version)
ok = true;
if (cbUpgradeTo.Items.Count == 1)
cbUpgradeTo.SelectedIndex = 0;
}
else
{
if (db.BadPassword)
{
db.Close();
var form = new GetPassForm();
if (form.ShowDialog() == DialogResult.OK)
LoadDatabase(fileName, form.edPass.Text.Trim());
}
}
}
private bool CreateBackup()
{
if (string.IsNullOrEmpty(db.FileName))
{
GlobalText.ShowError("NoDatabaseSelect");
return false;
}
if (!File.Exists(db.FileName))
{
GlobalText.ShowError("DatabaseMissing");
return false;
}
string backupFile = string.Empty;
int i = 1;
try
{
while (File.Exists(backupFile = Path.ChangeExtension(db.FileName, $".{i.ToString("0000")}{Path.GetExtension(db.FileName)}")))
i++;
File.Copy(db.FileName, backupFile);
return true;
}
catch (Exception ex)
{
GlobalText.ShowError("BackupError", ex.Message);
return false;
}
}
private void btCreate_Click(object sender, EventArgs e)
{
bool ok = false;
Cursor = Cursors.WaitCursor;
try
{
ok = db.CreateDatabase(tbFileName.Text, tbPassword.Text.Trim(), (Version)cbVersion.SelectedItem);
if (ok)
GlobalText.ShowInfo("CreateDone");
else
GlobalText.ShowError("CreateError", db.LastError);
}
catch (Exception ex)
{
GlobalText.ShowError("CreateError", ex.Message);
}
Cursor = Cursors.Default;
if (ok)
LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
SetActiveButtons();
}
private void btCompact_Click(object sender, EventArgs e)
{
if (!CreateBackup())
return;
bool ok = false;
Cursor = Cursors.WaitCursor;
try
{
ok = db.Compact(tbFileName.Text, openPassword, tbPassword.Text.Trim());
if (ok)
GlobalText.ShowInfo("CompactDone");
else
GlobalText.ShowError("CompactError", db.LastError);
}
catch (Exception ex)
{
GlobalText.ShowError("CompactError", ex.Message);
}
Cursor = Cursors.Default;
if (ok)
LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
SetActiveButtons();
}
private void btRepair_Click(object sender, EventArgs e)
{
if (!CreateBackup())
return;
bool ok = false;
Cursor = Cursors.WaitCursor;
try
{
ok = db.Repair(tbFileName.Text, openPassword, tbPassword.Text.Trim());
if (ok)
GlobalText.ShowInfo("RepairDone");
else
GlobalText.ShowError("RepairError", db.LastError);
}
catch (Exception ex)
{
GlobalText.ShowError("RepairError", ex.Message);
}
Cursor = Cursors.Default;
if (ok)
LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
SetActiveButtons();
}
private void btShrink_Click(object sender, EventArgs e)
{
if (!CreateBackup())
return;
bool ok = false;
Cursor = Cursors.WaitCursor;
try
{
ok = db.Shrink(tbFileName.Text, openPassword, tbPassword.Text.Trim());
if (ok)
GlobalText.ShowInfo("ShrinkDone");
else
GlobalText.ShowError("ShrinkError", db.LastError);
}
catch (Exception ex)
{
GlobalText.ShowError("ShrinkError", ex.Message);
}
Cursor = Cursors.Default;
if (ok)
LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
SetActiveButtons();
}
private void btUpgrade_Click(object sender, EventArgs e)
{
if (!CreateBackup())
return;
bool ok = false;
Cursor = Cursors.WaitCursor;
try
{
ok = db.Upgrade(tbFileName.Text, openPassword, tbPassword.Text.Trim(), (Version)cbUpgradeTo.SelectedItem);
if (ok)
GlobalText.ShowInfo("UpgradeDone");
else
GlobalText.ShowError("UpgradeError", db.LastError);
}
catch (Exception ex)
{
GlobalText.ShowError("UpgradeError", ex.Message);
}
Cursor = Cursors.Default;
if (ok)
LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
SetActiveButtons();
}
private void btVerify_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
if (db.Verify(tbFileName.Text, tbPassword.Text.Trim()))
GlobalText.ShowInfo("VerifyDone");
else if (string.IsNullOrEmpty(db.LastError))
GlobalText.ShowError("VerifyFault");
else
GlobalText.ShowError("VerifyError", db.LastError);
}
catch (Exception ex)
{
GlobalText.ShowError("VerifyError", ex.Message);
}
db.Close();
Cursor = Cursors.Default;
LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
}
private void tbFileName_Enter(object sender, EventArgs e)
{
// File name text box is read only, the file is selected by pressing the button at the right
btSelect.Focus();
}
private void cbUpgradeTo_SelectedIndexChanged(object sender, EventArgs e)
{
btUpgrade.Enabled = cbUpgradeTo.SelectedIndex >= 0;
}
private void ToolsForm_FormClosing(object sender, FormClosingEventArgs e)
{
db.Close();
}
}
}