My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Threading;
using Microsoft.Win32;
using System.IO;
using System.Collections.ObjectModel;

using System.Security.Principal;

using System.Data;
using System.Data.SqlClient;

using System.Collections.Specialized;

using Microsoft.SqlServer.Server;

using System.Text.RegularExpressions;

using WinForms = System.Windows.Forms;

using System.Diagnostics; //Process.Start()

namespace YASBE
{
public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();

gridFilesWorkingSet.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(WPFHelpers.DataGridRightAlignAutoGeneratedNumericColumns); //nugget: this the most generic way recordindex could figure this so far...see helper comments
gridIncrementalHistory.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(WPFHelpers.DataGridRightAlignAutoGeneratedNumericColumns);

//bool isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator) ? true : false;
//if (isAdmin) {MessageBox.Show("you are an administrator");} else{ MessageBox.Show("You are not an administrator");}

LoadBackProfilesList();
}

private void LoadBackProfilesList()
{
//load initial backup profile names drop down list so that a previously selected entry stored in Property.Settings has something to Bind to when the Window first comes up
using (Proc BackupProfiles_s = new Proc("BackupProfiles_s"))
{
BackupProfiles_s["@BackupProfileID"] = YASBE.Properties.Settings.Default.SelectedBackupProfileID;

BackupProfiles_s.ExecuteDataSet();

cbxBackupProfiles.ItemsSource = BackupProfiles_s.Tables[0].DefaultView; //this fires cbxBackupProfiles_SelectionChanged which is assigned in XAML

cbxMediaSize.ItemsSource = BackupProfiles_s.dataSet.Tables[1].DefaultView; //I belive this order is what allowed the cbxBackupProfiles Selected row to properly drive the selected cbxMediaSize.MediaSizeID

lbxFavoriteBurnFolders.ItemsSource = BackupProfiles_s.Tables[2].DefaultView;

SelectedFoldersTable = BackupProfiles_s.Tables[3];

IncludedFilesTable = BackupProfiles_s.Tables[4];
IncludedFilesTable.PrimaryKey = new DataColumn[] { IncludedFilesTable.Columns["FullPath"] };
}

ResetAllCounters();
}

private void ResetAllCounters()
{
WorkingFilesTable = null;
gridFilesWorkingSet.ItemsSource = null;

lblCurrentDisc.Content = "-";
lblTotalBytes.Text = "-";
lblTotalFiles.Text = "-";
lblDiscCount.Text = "-";
lblQtySelected.Text = "-";
lblBytesSelected.Text = "-";
lblErrorCount.Text = "-";
}

private void cbxBackupProfiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (Proc BackupProfile_s = new Proc("BackupProfile_s"))
{
BackupProfile_s["@BackupProfileID"] = cbxBackupProfiles.SelectedValue;
gridIncrementalHistory.ItemsSource = BackupProfile_s.Table0.DefaultView;
FileSystemNode.LoadSelectedNodes(BackupProfile_s.Tables[1]);
}
}

static public string[] WindowsBurnStagingFolders
{
get
{
using (RegistryKey cdburning = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\CD Burning\StagingInfo"))
{
try
{
return(from volumekey in cdburning.GetSubKeyNames() select cdburning.OpenSubKey(volumekey).GetValue("StagingPath").ToString()).Distinct().ToArray<string>(); //ToArray is necessary to immediately execute rather than returning a delayed execution so that we can immediate close and dispose of the RegistryKey
}
finally { cdburning.Close(); }
}
}
}

private void OpenStagingFolder_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(cbxBurnFolders.SelectedItem.ToString());
}

private void BackupProfileSave_Click(object sender, RoutedEventArgs e)
{
DataRowView SelectedProfile = cbxBackupProfiles.SelectedItem as DataRowView;
using (Proc BackupProfile_u = new Proc("BackupProfile_u"))
{
BackupProfile_u["@BackupProfileID"] = cbxBackupProfiles.SelectedValue;
BackupProfile_u["@Name"] = SelectedProfile["Name"];
BackupProfile_u["@MediaSizeID"] = SelectedProfile["MediaSizeID"];
BackupProfile_u["@Folders"] = FileSystemNode.GetSelected(SelectedFoldersTable);
BackupProfile_u.ExecuteNonQuery();
}

}

private void NewIncremental_Click(object sender, RoutedEventArgs e)
{
throw (new Exception("needs some work still"));

using (Proc Incremental_i = new Proc("Incremental_i"))
{
Incremental_i["@BackupProfileID"] = cbxBackupProfiles.SelectedValue;
gridIncrementalHistory.ItemsSource = Incremental_i.ExecuteDataTable().DefaultView;
}
}

private const string DefaultSort = "FullPath asc, Size desc";
private const string BigNumberStringFormat = "#,#,#";

private DataTable WorkingFilesTable = null;
private DataTable IncludedFilesTable = null;
private DataTable SelectedFoldersTable = null;

private void chkShowErrorsOnly_Click(object sender, RoutedEventArgs e)
{
WorkingFilesTable.DefaultView.RowFilter = chkShowErrorsOnly.IsChecked.Value ? "SkipError = 1" : "";
}

private void IdentifyNextMediaSubset_Click(object sender, RoutedEventArgs e)
{
using (WaitCursorWrapper w = new WaitCursorWrapper())
//using (DataView files = new DataView(WorkingFilesTable))
{
if (gridIncrementalHistory.Items.Count == 0)
{
MessageBox.Show("Press [New Incremental Backup] button -OR-\r\nRight mouse the Incremental Backup History grid\r\nto establish the container for these new files",
"No Incremental Backup Container has been established", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}

FileSystemNode.GetSelected(SelectedFoldersTable, IncludedFilesTable);

using (Proc Files_UploadCompare = new Proc("Files_UploadCompare"))
{
Files_UploadCompare["@BackupProfileID"] = (int)cbxBackupProfiles.SelectedValue;
Files_UploadCompare["@AllFiles"] = IncludedFilesTable; // <= ******
WorkingFilesTable = Files_UploadCompare.ExecuteDataTable();
lblCurrentDisc.Content = Files_UploadCompare["@NextDiscNumber"].ToString();
}
DataView files = WorkingFilesTable.DefaultView;
gridFilesWorkingSet.ItemsSource = files;

chkShowErrorsOnly.IsChecked = false;
WorkingFilesTable.DefaultView.RowFilter = "";

files.Sort = DefaultSort;

long maxbytes = (long)((decimal)((DataRowView)cbxMediaSize.SelectedItem)["SizeGB"] * 1024 * 1024 * 1024);
long remainingbytes = files.Cast<DataRowView>().Select(drv => (long)drv["Size"]).Sum();

lblTotalBytes.Text = remainingbytes.ToString(BigNumberStringFormat); //nugget: requires System.Data.DataSetExtensions assembly added to project References
lblTotalFiles.Text = files.Count.ToString(BigNumberStringFormat);

long remainder = 0;
long DiscCount = Math.DivRem(remainingbytes, maxbytes, out remainder);
lblDiscCount.Text = String.Format("{0} Full + {1:#.###} GB leftover", DiscCount, remainder / 1024 / 1024 / 1024);

int retrycount = 10;

long bytecount = 0;
long recordcount = 0;
long errorcount = 0;

int recordindex = 0; //we need to know this loopcount outside the loop at the end in order to scroll to this current location in the grid
for (recordindex = 0; recordindex < files.Count; recordindex++)
{
//DataGridRow gridrow = WPFHelpers.GetDataGridRow(gridFilesWorkingSet, recordindex);
long nextsize = Convert.ToInt64(files[recordindex]["Size"]);
if (bytecount + nextsize > maxbytes)
{
//initially assume we just ran into too big of a file to pack on near the end of our free space...
//so for a few times, try to find another slightly smaller file...
if (--retrycount > 0) continue;
//and after those retries are exhausted, we've successully crammed as close to 100% full as we can at that point
break;
}

retrycount = 10; //when we successfully squeeze on another file, reset the retry count

if (CheckFileLocked(files[recordindex]["FullPath"].ToString()))
{
files[recordindex]["Selected"] = true;
bytecount += nextsize;
}
else
{
files[recordindex]["SkipError"] = true;
errorcount++;
}
}

lblQtySelected.Text = recordcount.ToString(BigNumberStringFormat);
lblBytesSelected.Text = bytecount.ToString(BigNumberStringFormat);
lblErrorCount.Text = errorcount.ToString(BigNumberStringFormat);

gridFilesWorkingSet.ScrollIntoView(gridFilesWorkingSet.Items[recordindex-1]);
}
}

private DataView GetSelectedFiles()
{
DataView SelectedFiles = new DataView(WorkingFilesTable);
SelectedFiles.RowFilter = "Selected = 1";
return (SelectedFiles);
}

private void SymLinkToBurn_Click(object sender, RoutedEventArgs e)
{
Process WipeStagingFolder = Process.Start("cmd.exe", "/c deltree.exe \"" + System.IO.Path.Combine(cbxBurnFolders.SelectedValue.ToString(), "*.*") + "\" & pause");
WipeStagingFolder.WaitForExit();

Regex rgx = new Regex("[;:]", RegexOptions.Compiled);

using (WaitCursorWrapper w = new WaitCursorWrapper())
using (DataView SelectedFiles = GetSelectedFiles())
{
foreach (DataRowView r in SelectedFiles)
{
string fullpath = r["FullPath"].ToString();

string sympath = System.IO.Path.Combine(cbxBurnFolders.SelectedValue.ToString(), rgx.Replace(fullpath, ""));
Directory.CreateDirectory(sympath.Replace(System.IO.Path.GetFileName(sympath), ""));
if (rdoSymLink.IsChecked.Value)
Win32Helpers.CreateSymbolicLink(sympath, fullpath, Win32Helpers.SYMBOLIC_LINK_FLAG.File);
else
File.Copy(fullpath, sympath);
}
}

OpenStagingFolder_Click(null, null);
}

private void MediaSubsetCommit_Click(object sender, RoutedEventArgs e)
{
using (Proc MediaSubset_Commit = new Proc("MediaSubset_Commit"))
using (DataView SelectedFiles = GetSelectedFiles())
using (DataTable UploadFiles = SqlClientHelpers.NewTableFromDataView(SelectedFiles, "FullPath", "ModifiedDate", "Size"))
{
MediaSubset_Commit["@BackupProfileID"] = (int)cbxBackupProfiles.SelectedValue;
MediaSubset_Commit["@Files"] = UploadFiles;
MediaSubset_Commit.ExecuteNonQuery();
}

ResetAllCounters();
}

private bool CheckFileLocked(string fullpath)
{
try
{
using (FileStream inputStream = File.Open(fullpath, FileMode.Open, FileAccess.Read, FileShare.None))
{
inputStream.Close();
return true;
}
}
catch (IOException ex)
{
return false;
}
}

private void EditMediaSize_Click(object sender, RoutedEventArgs e)
{
DataRowView r = ((DataRowView)cbxMediaSize.SelectedItem);
string size = Microsoft.VisualBasic.Interaction.InputBox("New Size (in GB):", "Edit Media Size", r["SizeGB"].ToString());

using (Proc MediaSize_u = new Proc("MediaSize_u"))
{
MediaSize_u["@MediaSizeID"] = r["MediaSizeID"];
MediaSize_u["@Size"] = size;
MediaSize_u.ExecuteNonQuery();
}

LoadBackProfilesList();
}

private void AddNewFavoriteTempBurnFolder_Click(object sender, RoutedEventArgs e)
{
WinForms.FolderBrowserDialog ChooseFolder = new WinForms.FolderBrowserDialog();
if (ChooseFolder.ShowDialog(this.GetWin32Window()) != WinForms.DialogResult.OK) return;

using (Proc FavoriteTempBurnFolder_i = new Proc("FavoriteTempBurnFolder_i"))
{
FavoriteTempBurnFolder_i["@NewFolder"] = ChooseFolder.SelectedPath;
lbxFavoriteBurnFolders.ItemsSource = null;
lbxFavoriteBurnFolders.ItemsSource = FavoriteTempBurnFolder_i.ExecuteDataTable().DefaultView;
}
}

private void AssignNewBurnFolder(string newfolder)
{
using (RegistryKey cdburning = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\CD Burning\StagingInfo"))
{
foreach (string volumekeystring in cdburning.GetSubKeyNames())
{
using (RegistryKey volume = cdburning.OpenSubKey(volumekeystring, true))
{
if (volume.GetValue("StagingPath").ToString() == cbxBurnFolders.SelectedValue.ToString())
{
volume.SetValue("StagingPath", newfolder);
break;
}
}
}
}

cbxBurnFolders.ItemsSource = null;
cbxBurnFolders.ItemsSource = WindowsBurnStagingFolders;
}

private void AssignFavoriteToSelectedBurnStagingFolder_Click(object sender, RoutedEventArgs e)
{
AssignNewBurnFolder(lbxFavoriteBurnFolders.SelectedValue.ToString());
}

private void RefreshProfile_Click(object sender, RoutedEventArgs e)
{
LoadBackProfilesList();
}

}

public class FileTreeBackgroundBrushConverter : WPFValueConverters.MarkupExtensionConverter, IMultiValueConverter
{
public FileTreeBackgroundBrushConverter() { } //to avoid an annoying warning from XAML designer: "No constructor for type 'xyz' has 0 parameters." Somehow the inherited one doesn'SelectedFoldersTable do the trick!?! I guess it's a reflection bug.

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (WPFHelpers.DesignMode) return (DependencyProperty.UnsetValue);

//values[0] = IsSelected
//values[1] = IsExcluded
//hard coded as much as possible to make sure there's no unecessary cycles lost in this critical section... this routine fires *for every sub tree node* *whenever a checkbox changes* (recordindex.e. A LOT)
return ((bool)values[1] ? Brushes.LightPink : (bool)values[0] ? Brushes.LightGreen : DependencyProperty.UnsetValue);
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

}

Change log

r18 by Beej2020 on Dec 22, 2011   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r17 by Beej2020 on Dec 22, 2011   Diff
[No log message]
r16 by beej2020 on Feb 21, 2011   Diff
[No log message]
r15 by beej2020 on Feb 13, 2011   Diff
[No log message]
All revisions of this file

File info

Size: 15427 bytes, 383 lines
Powered by Google Project Hosting