-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchForm.cs
More file actions
106 lines (94 loc) · 3.54 KB
/
SearchForm.cs
File metadata and controls
106 lines (94 loc) · 3.54 KB
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
// Copyright Bastian Eicher
// Licensed under the MIT License
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using NanoByte.Common.Controls;
using NanoByte.Common.Storage;
using NanoByte.Common.Tasks;
namespace NanoByte.LightTag
{
public partial class SearchForm : Form
{
public SearchForm()
{
InitializeComponent();
tags.TreeView.CheckedEntriesChanged += tags_TreeView_CheckedEntriesChanged;
textFolder.Text = Preferences.LastSearchDirectory;
}
private void SearchForm_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
}
private void SearchForm_DragDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;
var paths = e.Data.GetData(DataFormats.FileDrop) as string[];
if (paths == null) return;
// Replace "search" dialog with "tagging" dialog
var taggingForm = new TaggingForm(paths) {Location = Location, StartPosition = FormStartPosition.Manual};
taggingForm.FormClosing += delegate { Close(); };
taggingForm.Show();
Hide();
}
private void buttonBrowseFolder_Click(object sender, EventArgs e)
{
folderBrowserDialog.ShowDialog(this);
textFolder.Text = folderBrowserDialog.SelectedPath;
}
private void tags_TreeView_CheckedEntriesChanged(object sender, EventArgs e)
{
buttonSearch.Enabled = (tags.TreeView.CheckedEntries.Count != 0);
}
private void buttonSearch_Click(object sender, EventArgs e)
{
DirectoryInfo searchDirectory;
try
{
searchDirectory = new DirectoryInfo(textFolder.Text);
}
#region Error handling
catch (ArgumentException ex)
{
Msg.Inform(this, ex.Message, MsgSeverity.Error);
return;
}
#endregion
if (!searchDirectory.Exists)
{
Msg.Inform(this, "Directory does not exist.", MsgSeverity.Warn);
return;
}
Preferences.LastSearchDirectory = searchDirectory.FullName;
var resultSet = new ResultSet();
var checkedTags = tags.TreeView.CheckedEntries.Select(x => x.Name).ToList();
try
{
var handler = new DialogTaskHandler(this);
handler.RunTask(new ActionTask("Searching", () =>
searchDirectory.Walk(fileAction: file =>
{
var fileTags = file.ReadTags();
if (checkedTags.All(fileTags.Contains)) resultSet.Add(file);
})));
resultSet.Show();
}
#region Error handling
catch (IOException ex)
{
Msg.Inform(this, ex.Message, MsgSeverity.Error);
}
catch (UnauthorizedAccessException ex)
{
Msg.Inform(this, ex.Message, MsgSeverity.Error);
}
catch (Win32Exception ex)
{
Msg.Inform(this, ex.Message, MsgSeverity.Error);
}
#endregion
}
}
}