| 1 |
using System; |
| 2 |
using System.Windows; |
| 3 |
using System.Windows.Controls; |
| 4 |
using System.Windows.Input; |
| 5 |
using Toast.IsolationLayer; |
| 6 |
using properties = Toast.Scone.Properties; |
| 7 |
using dbg = SeedCreate.CommonLibrary.DebugHelper; |
| 8 |
using SeedCreate.Controls.ControlBase; |
| 9 |
|
| 10 |
namespace Toast.Scone |
| 11 |
{ |
| 12 |
/// <summary> |
| 13 |
/// Window1.xaml の相互作用ロジック |
| 14 |
/// </summary> |
| 15 |
public sealed partial class MainWindow : Window |
| 16 |
{ |
| 17 |
public static readonly RoutedCommand StartCommand = |
| 18 |
new RoutedCommand("StartCommand", typeof(MainWindow)); |
| 19 |
|
| 20 |
public static readonly RoutedCommand StopCommand = |
| 21 |
new RoutedCommand("StopCommand", typeof(MainWindow)); |
| 22 |
|
| 23 |
private Repeater repeater; |
| 24 |
|
| 25 |
private TextBoxChecker cheker; |
| 26 |
|
| 27 |
private bool startCommandCanExecute; |
| 28 |
private bool stopCommandCanExecute; |
| 29 |
|
| 30 |
public MainWindow() |
| 31 |
{ |
| 32 |
InitializeComponent(); |
| 33 |
|
| 34 |
repeater = new Repeater(Dispatcher); |
| 35 |
repeater.ProgramStarted += new EventHandler<Toast.NicoNico.DetectProgramStartEventArgs>(repeater__ProgramStarted); |
| 36 |
|
| 37 |
|
| 38 |
repeater.TwitterWriteFailed += new EventHandler<TwitterWriteFailedEventArgs>(repeater__TwitterWriteFailed); |
| 39 |
|
| 40 |
cheker = new TextBoxChecker(); |
| 41 |
cheker.AddControl(smileId); |
| 42 |
cheker.AddControl(password); |
| 43 |
cheker.AddControl(userId); |
| 44 |
cheker.AddControl(format); |
| 45 |
|
| 46 |
CommandBinding bind = new CommandBinding(StartCommand, new ExecutedRoutedEventHandler(StartCommandExecute), |
| 47 |
new CanExecuteRoutedEventHandler(CanExecuteStartCommand)); |
| 48 |
|
| 49 |
CommandBindings.Add(bind); |
| 50 |
|
| 51 |
bind = new CommandBinding(StopCommand, new ExecutedRoutedEventHandler(StopCommandExecute), |
| 52 |
new CanExecuteRoutedEventHandler(CanExecuteStopCommand)); |
| 53 |
|
| 54 |
CommandBindings.Add(bind); |
| 55 |
|
| 56 |
password.Password = properties::Settings.Default.PassWord; |
| 57 |
userId.EmbodimentText = properties::Settings.Default.UserId; |
| 58 |
format.EmbodimentText = properties::Settings.Default.Format; |
| 59 |
|
| 60 |
smileId.EmbodimentText = properties::Settings.Default.SmileID; |
| 61 |
|
| 62 |
SmileIdValidationRule rule = new SmileIdValidationRule(); |
| 63 |
|
| 64 |
if (!rule.Validate(smileId.EmbodimentText, System.Threading.Thread.CurrentThread.CurrentCulture).IsValid) |
| 65 |
{ |
| 66 |
smileId.IsInvalid = true; |
| 67 |
} |
| 68 |
|
| 69 |
startCommandCanExecute = false; |
| 70 |
stopCommandCanExecute = false; |
| 71 |
} |
| 72 |
|
| 73 |
public static readonly RoutedEvent StartCommandCanExecuteStateChangingEvent |
| 74 |
= EventManager.RegisterRoutedEvent("StartCommandCanExecuteStateChanging", |
| 75 |
RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<bool>), |
| 76 |
typeof(MainWindow)); |
| 77 |
|
| 78 |
public event RoutedPropertyChangedEventHandler<bool> StartCommandCanExecuteStateChanging |
| 79 |
{ |
| 80 |
add |
| 81 |
{ |
| 82 |
AddHandler(StartCommandCanExecuteStateChangingEvent, value); |
| 83 |
} |
| 84 |
remove |
| 85 |
{ |
| 86 |
RemoveHandler(StartCommandCanExecuteStateChangingEvent, value); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private void OnStartCommandCanExecuteStateChanging(RoutedPropertyChangedEventArgs<bool> e) |
| 91 |
{ |
| 92 |
RaiseEvent(e); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static readonly RoutedEvent StopCommandCanExecuteStateChangingEvent |
| 98 |
= EventManager.RegisterRoutedEvent("StopCommandCanExecuteStateChainging", |
| 99 |
RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<bool>), |
| 100 |
typeof(MainWindow)); |
| 101 |
|
| 102 |
public event RoutedPropertyChangedEventHandler<bool> StopCommandCanExecuteStateChanging |
| 103 |
{ |
| 104 |
add |
| 105 |
{ |
| 106 |
AddHandler(StopCommandCanExecuteStateChangingEvent, value); |
| 107 |
} |
| 108 |
remove |
| 109 |
{ |
| 110 |
RemoveHandler(StopCommandCanExecuteStateChangingEvent, value); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private void OnStopCommandCanExecuteStateChanging(RoutedPropertyChangedEventArgs<bool> e) |
| 115 |
{ |
| 116 |
RaiseEvent(e); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
private void repeater__TwitterWriteFailed(object sender, TwitterWriteFailedEventArgs e) |
| 121 |
{ |
| 122 |
MessageBox.Show(properties::Resources.WriteFailedMessage, properties::Resources.WriteFailedCaption, |
| 123 |
MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); |
| 124 |
|
| 125 |
CommandBindings[0].Command.CanExecute(null); |
| 126 |
userId.Focus(); |
| 127 |
//エラー直後に、なんかしたけりゃここにかく |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
private void repeater__ProgramStarted(object sender, Toast.NicoNico.DetectProgramStartEventArgs e) |
| 132 |
{ |
| 133 |
mainStatus.Text = string.Format(properties::Resources.WriteSuccessFormat, e.TargetProgram.Title); |
| 134 |
} |
| 135 |
|
| 136 |
private bool CheckStartInterlock() |
| 137 |
{ |
| 138 |
|
| 139 |
bool State = false; |
| 140 |
|
| 141 |
if (repeater.IsRunning) |
| 142 |
{ |
| 143 |
State = false; |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
Control ret = cheker.Check(); |
| 148 |
|
| 149 |
if (ret == null) |
| 150 |
{ |
| 151 |
State = true; |
| 152 |
} |
| 153 |
else if (ret is BiStateTextBox) |
| 154 |
{ |
| 155 |
WriteState((BiStateTextBox)ret); |
| 156 |
State = false; |
| 157 |
} |
| 158 |
else if (ret is PasswordBox) |
| 159 |
{ |
| 160 |
WriteState((PasswordBox)ret); |
| 161 |
State = false; |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
throw new SeedCreate.InconsistencyException("Unexpected control type"); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return State; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
private bool CheckStopInterlock() |
| 175 |
{ |
| 176 |
bool State = false; |
| 177 |
|
| 178 |
if (repeater.IsRunning) |
| 179 |
{ |
| 180 |
State = true; |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
State = false; |
| 185 |
} |
| 186 |
|
| 187 |
return State; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
private void WriteState(PasswordBox passwordBox) |
| 192 |
{ |
| 193 |
WriteState(properties::Resources.EmptyPassword); |
| 194 |
} |
| 195 |
|
| 196 |
private void WriteState(BiStateTextBox biStateTextBox) |
| 197 |
{ |
| 198 |
if (biStateTextBox.IsEmpty) |
| 199 |
{ |
| 200 |
WriteState(string.Format(properties::Resources.EmptyStateMessageFormat, biStateTextBox.Name)); |
| 201 |
} |
| 202 |
|
| 203 |
if (biStateTextBox.IsInvalid) |
| 204 |
{ |
| 205 |
WriteState(string.Format(properties::Resources.InvalidStateMessageFormat, biStateTextBox.Name)); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
private void WriteState(string message) |
| 211 |
{ |
| 212 |
mainStatus.Text = message; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
private void StartCommandExecute(object sender, ExecutedRoutedEventArgs e) |
| 217 |
{ |
| 218 |
repeater.Start(uint.Parse(smileId.EmbodimentText), |
| 219 |
userId.EmbodimentText, |
| 220 |
password.Password, |
| 221 |
format.EmbodimentText); |
| 222 |
//CheckStopInterlock(); |
| 223 |
|
| 224 |
WriteState(properties::Resources.Start); |
| 225 |
} |
| 226 |
|
| 227 |
private void CanExecuteStartCommand(object sender, CanExecuteRoutedEventArgs e) |
| 228 |
{ |
| 229 |
e.CanExecute=CheckStartInterlock(); |
| 230 |
|
| 231 |
if (e.CanExecute != startCommandCanExecute) |
| 232 |
{ |
| 233 |
OnStartCommandCanExecuteStateChanging(new RoutedPropertyChangedEventArgs<bool>(startCommandCanExecute, e.CanExecute, |
| 234 |
StartCommandCanExecuteStateChangingEvent)); |
| 235 |
startCommandCanExecute = e.CanExecute; |
| 236 |
|
| 237 |
dbg.TinyDebugWriter.WriteLine("StartChanged:{0}", e.CanExecute); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
private void StopCommandExecute(object sender, ExecutedRoutedEventArgs e) |
| 242 |
{ |
| 243 |
repeater.Stop(); |
| 244 |
//CheckStopInterlock(); |
| 245 |
|
| 246 |
WriteState(properties::Resources.ApplicationName); |
| 247 |
} |
| 248 |
|
| 249 |
private void CanExecuteStopCommand(object sender, CanExecuteRoutedEventArgs e) |
| 250 |
{ |
| 251 |
e.CanExecute = CheckStopInterlock(); |
| 252 |
|
| 253 |
if (e.CanExecute != stopCommandCanExecute) |
| 254 |
{ |
| 255 |
OnStopCommandCanExecuteStateChanging(new RoutedPropertyChangedEventArgs<bool>(stopCommandCanExecute, e.CanExecute, |
| 256 |
StopCommandCanExecuteStateChangingEvent)); |
| 257 |
stopCommandCanExecute = e.CanExecute; |
| 258 |
|
| 259 |
dbg.TinyDebugWriter.WriteLine("StopChanged:{0}", e.CanExecute); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
private void Format_EmbodimentTextChanged(object sender, EmbodimentTextChangedEventArgs e) |
| 264 |
{ |
| 265 |
properties::Settings.Default.Format = |
| 266 |
e.Current == null ? string.Empty : format.EmbodimentText; |
| 267 |
|
| 268 |
properties::Settings.Default.Save(); |
| 269 |
} |
| 270 |
|
| 271 |
private void SmileId_EmbodimentTextChanged(object sender, EmbodimentTextChangedEventArgs e) |
| 272 |
{ |
| 273 |
|
| 274 |
if (!smileId.IsInvalid) |
| 275 |
{ |
| 276 |
properties::Settings.Default.SmileID = |
| 277 |
e.Current == null ? string.Empty : smileId.EmbodimentText; |
| 278 |
|
| 279 |
properties::Settings.Default.Save(); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
private void UserId_EmbodimentTextChanged(object sender, EmbodimentTextChangedEventArgs e) |
| 284 |
{ |
| 285 |
properties::Settings.Default.UserId = |
| 286 |
e.Current == null ? string.Empty : userId.EmbodimentText; |
| 287 |
|
| 288 |
properties::Settings.Default.Save(); |
| 289 |
} |
| 290 |
|
| 291 |
private void PassWord_LostFocus(object sender, RoutedEventArgs e) |
| 292 |
{ |
| 293 |
|
| 294 |
properties::Settings.Default.PassWord = |
| 295 |
password.Password == null ? string.Empty : password.Password; |
| 296 |
|
| 297 |
properties::Settings.Default.Save(); |
| 298 |
} |
| 299 |
} |
| 300 |
} |