This repository was archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellChangeNotifierExample.xaml.cs
221 lines (188 loc) · 8.07 KB
/
ShellChangeNotifierExample.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using Jam.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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 Jam.Shell.WPF.Controls;
namespace ShellChangeNotifierExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ChangeNotifierViewModel();
}
private void ShellChangeNotifier_Change(object sender, Jam.Shell.ChangeNotificationEventArgs e)
{
textBoxNotifications.AppendText(e.Event.ToString() + ": ");
textBoxNotifications.AppendText(e.Path1);
if (!String.IsNullOrEmpty(e.Path2))
textBoxNotifications.AppendText(" -> " + e.Path2);
textBoxNotifications.AppendText(Environment.NewLine);
textBoxNotifications.ScrollToEnd();
}
private void textBoxNotifications_TextChanged(object sender, TextChangedEventArgs e)
{
logScroller.ScrollToEnd();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
textBoxNotifications.Clear();
}
}
/// <summary>
/// Example that demonstrates the usage of a <see cref="ShellChangeNotifier"/>.
/// <para>
/// An instance is created and configured in <see cref="ChangeNotifierViewModel()"/>, attaching the eventhandler <see cref="OnShellChangeNotifierChange(object, ChangeNotificationEventArgs)"/>.
/// It prints out the received notifications in a textbox.
/// </para>
/// </summary>
public class ChangeNotifierViewModel : INotifyPropertyChanged
{
#region members
private Jam.Shell.WPF.Controls.ShellChangeNotifier m_ShellChangeNotifer;
/// <summary>The <see cref="ShellChangeNotifier"/> instance central to this example.</summary>
/// <value>A ShellChangeNotifier.</value>
public Jam.Shell.WPF.Controls.ShellChangeNotifier ChangeNotifier
{
get { return m_ShellChangeNotifer; }
}
private string m_CaptionEvents;
/// <summary>Collects the checked notification evens as comma seperated list.</summary>
/// <value>A comma seperated list of events.</value>
public string CaptionEvents
{
get { return m_CaptionEvents; }
private set
{
m_CaptionEvents = value;
RaisePropertyChanged("CaptionEvents");
}
}
/// <summary>Collects the checked notification evens as comma seperated list.</summary>
private void RefreshCaptionEvents()
{
string result = String.Empty;
foreach (CheckedItem<NotificationEvents> lEvent in EventSelection)
{
if (lEvent.IsChecked)
{
if (result != String.Empty) result += ", ";
result += Enum.GetName(typeof(NotificationEvents), lEvent.Item);
}
}
CaptionEvents = result;
}
private string m_NotificationText;
/// <summary>Holds the text that collects notifications.</summary>
/// <value>The notification text.</value>
public string NotificationText
{
get { return m_NotificationText; }
set
{
m_NotificationText = value;
RaisePropertyChanged("NotificationText");
}
}
private ItemIdList m_WatchedDirectory = new ItemIdList(ShellFolder.Drives);
/// <summary>Holds the folder to watch as ItemIdList .</summary>
/// <value>The ItemIdList of the watched directory.</value>
public ItemIdList WatchedDirectory
{
get { return m_WatchedDirectory; }
set
{
m_WatchedDirectory = value;
m_ShellChangeNotifer.ItemIdList = m_WatchedDirectory;
}
}
/// <summary>A helping list that contains all events that can be watched for.</summary>
/// <value>List of all events.</value>
private IEnumerable<NotificationEvents> AllEvents
{
get
{
var result = Enum.GetValues(typeof(NotificationEvents)).Cast<NotificationEvents>().ToList();
result.Remove(NotificationEvents.Unknown); //this is an internal value.
return result;
}
}
private IEnumerable<CheckedItem<NotificationEvents>> m_EventSelection;
/// <summary>Contains a list of checkable NotificationEvents.</summary>
/// <value>A list of CheckedItems containing a NotificationEvent and a checkstate.</value>
public IEnumerable<CheckedItem<NotificationEvents>> EventSelection
{
get
{
if (m_EventSelection == null)
{
m_EventSelection =
AllEvents.Select((pEvent, pIndex) =>
new CheckedItem<NotificationEvents>(pEvent,
(pItem, pIsChecked) =>
{
//is executed, when an item in the list is checked or unchecked.
if (pIsChecked)
m_ShellChangeNotifer.EventFilter |= pItem;
else
m_ShellChangeNotifer.EventFilter ^= pItem;
RefreshCaptionEvents(); //update the text of the combobox.
},
(m_ShellChangeNotifer.EventFilter & pEvent) == pEvent));
}
return m_EventSelection;
}
}
#endregion
#region constructor
/// <summary>Default constructor. Especially creates and configures a <see cref="ShellChangeNotifier"/> instance.</summary>
public ChangeNotifierViewModel()
{
m_ShellChangeNotifer = new Jam.Shell.WPF.Controls.ShellChangeNotifier();
m_ShellChangeNotifer.EventFilter = NotificationEvents.FileCreate | NotificationEvents.FileChange | NotificationEvents.FileDelete | NotificationEvents.FileRename |
NotificationEvents.FolderCreate | NotificationEvents.FolderRename | NotificationEvents.FolderDelete | NotificationEvents.FolderUpdate;
RefreshCaptionEvents();
m_ShellChangeNotifer.Change += OnShellChangeNotifierChange;
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
#endregion
#region ShellChangeNotifier
/// <summary>Called when a notification matching the criteria (<see cref="EventSelection"/>, <see cref="WatchedDirectory"/>, <see cref="ShellChangeNotifier.Recursive"/>) is received.</summary>
/// <param name="sender">Source of the event.</param>
/// <param name="e">Event information to send to registered event handlers.</param>
private void OnShellChangeNotifierChange(object sender, ChangeNotificationEventArgs e)
{
string lNewText = m_NotificationText
+ DateTime.Now.ToString() + " "
+ e.Event.ToString() + ": "
+ e.Path1;
if (!String.IsNullOrEmpty(e.Path2))
lNewText+= (" -> " + e.Path2);
lNewText += Environment.NewLine;
NotificationText = lNewText;
}
#endregion
}
}