Skip to content

Commit

Permalink
Fixing options quirks (#98)
Browse files Browse the repository at this point in the history
* Fixing options quirks #74 #97

* Update changelog
  • Loading branch information
DavidMoore committed Aug 21, 2023
1 parent 16d4a77 commit 96f1542
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 42 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,6 +2,23 @@

## [Unreleased]

## [3.0.5-beta] - 2023-08-21

### Fixed

* Options window should always enable OK and Cancel buttons (#74)
* Fixed issue with deleting scheduled task after it's been disabled in options (#97)

## [3.0.4-beta] - 2023-08-19

### Fixed

* Task Scheduler and Options fixes by @DavidMoore in https://github.com/DavidMoore/ipfilter/pull/95
- Renamed "Save Settings" button in Options to "OK", and it automatically closes the window after saving (#74)
- Scheduled task should be created or deleted according to the chosen option (#72)
* The status / error message should auto-size to fit in the window (#67)
* Corrected the URL in Add/Remove Programs entry

## [3.0.3-beta] - 2023-08-18

### Changed
Expand Down Expand Up @@ -39,7 +56,9 @@

## [3.0.1.4-beta]

[Unreleased]: https://github.com/DavidMoore/ipfilter/compare/3.0.3-beta...HEAD
[Unreleased]: https://github.com/DavidMoore/ipfilter/compare/3.0.5-beta...HEAD
[3.0.5-beta]: https://github.com/DavidMoore/ipfilter/compare/3.0.4-beta...3.0.5-beta
[3.0.4-beta]: https://github.com/DavidMoore/ipfilter/compare/3.0.3-beta...3.0.4-beta
[3.0.3-beta]: https://github.com/DavidMoore/ipfilter/compare/3.0.2.9-beta...3.0.3-beta
[3.0.2.9-beta]: https://github.com/DavidMoore/ipfilter/compare/3.0.2.7-beta...3.0.2.9-beta
[3.0.2.7-beta]: https://github.com/DavidMoore/ipfilter/compare/3.0.2.4-beta...3.0.2.7-beta
Expand Down
68 changes: 35 additions & 33 deletions Code/IPFilter/Commands/ScheduledTaskCommand.cs
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;

namespace IPFilter.Commands
{
Expand Down Expand Up @@ -69,57 +70,58 @@ public static void Execute(bool enable)
if(!enable)
{
Trace.TraceInformation("Deleting the automatic schedule task...");
var folder = service.GetFolder("\\");
var existingTask = folder.GetTask(taskPath);
if (existingTask != null)
var rootFolder = service.GetFolder("\\");

try
{
var existingTask = rootFolder.GetTask(taskPath);
existingTask.Enabled = false;
existingTask.Stop(0);
folder.DeleteTask(taskPath, 0);
rootFolder.DeleteTask(taskPath, 0);
Trace.TraceInformation("Successfully deleted the scheduled task.");
return;
}
Trace.TraceInformation("No task found to delete.");
catch (FileNotFoundException)
{
Trace.TraceInformation("No task found to delete.");
}
return;
}

Trace.TraceInformation("Setting up the automatic schedule...");
var task = service.NewTask(0);
//using (var task = service.NewTask())
{
task.RegistrationInfo.Description = "Updates the IP Filter for bit torrent clients";

task.RegistrationInfo.Description = "Updates the IP Filter for bit torrent clients";

task.Triggers.Clear();
task.Triggers.Clear();

// Schedule to run daily at 4am
var now = DateTime.Now.AddDays(-1);
var date = new DateTime(now.Year, now.Month, now.Day, 4, 0, 0);
// Schedule to run daily at 4am
var now = DateTime.Now.AddDays(-1);
var date = new DateTime(now.Year, now.Month, now.Day, 4, 0, 0);

var trigger = task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
trigger.DaysInterval = 1;
trigger.StartBoundary = date.ToString("s");
trigger.RandomDelay = "PT15M"; // Delay randomly by 15 minutes to stagger the amount of requests hitting list servers
var trigger = task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
trigger.DaysInterval = 1;
trigger.StartBoundary = date.ToString("s");
trigger.RandomDelay = "PT15M"; // Delay randomly by 15 minutes to stagger the amount of requests hitting list servers

// Execute silently
//var action = (IExecAction) task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
var action = task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Path = Process.GetCurrentProcess().MainModule.FileName;
action.Arguments = "/silent";
// Execute silently
//var action = (IExecAction) task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
var action = task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Path = Process.GetCurrentProcess().MainModule.FileName;
action.Arguments = "/silent";

task.Settings.RunOnlyIfNetworkAvailable = true;
task.Settings.StartWhenAvailable = true;
task.Settings.WakeToRun = false;
task.Settings.Enabled = true;
task.Settings.RunOnlyIfNetworkAvailable = true;
task.Settings.StartWhenAvailable = true;
task.Settings.WakeToRun = false;
task.Settings.Enabled = true;

task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_LUA;
task.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN;
//task.Principal.UserId = identity.Name;
task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_LUA;
task.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN;
//task.Principal.UserId = identity.Name;

var folder = service.GetFolder("\\");
var folder = service.GetFolder("\\");

var registered = folder.RegisterTaskDefinition(taskPath, task, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null,null,_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN);
Trace.TraceInformation("Finished creating / updating scheduled task");
}
var registered = folder.RegisterTaskDefinition(taskPath, task, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null,null,_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN);
Trace.TraceInformation("Finished creating / updating scheduled task");
}
}
}
19 changes: 15 additions & 4 deletions Code/IPFilter/ViewModels/OptionsViewModel.cs
Expand Up @@ -57,22 +57,34 @@ void LoadSettings()

bool CanResetSettings(object o)
{
return PendingChanges;
return true;
}

void ResetSettings(object o)
{
Config.Reload();
LoadSettings();
if (o is not Window window) return;
window.Close();
}

bool CanSaveSettings(object o)
{
return PendingChanges;
return true;
}

void SaveSettings(object o)
{
var window = o as Window;

// Just close the dialog if we haven't changed settings
if (!PendingChanges)
{
if (window == null) return;
window.Close();
return;
}

ErrorMessage = string.Empty;

try
Expand Down Expand Up @@ -116,8 +128,7 @@ void SaveSettings(object o)
return;
}

if (o is not Window window) return;
window.Close();
window?.Close();
}

public DelegateCommand SaveSettingsCommand { get; private set; }
Expand Down
8 changes: 4 additions & 4 deletions Code/IPFilter/Views/OptionsWindow.xaml
Expand Up @@ -18,14 +18,14 @@
<DockPanel>

<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem >
<StatusBarItem>
<Button Padding="5" Margin="5" Content="OK" Command="{Binding SaveSettingsCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</StatusBarItem>
<StatusBarItem>
<Label Content="{Binding ErrorMessage}" Foreground="Red" Width="Auto"/>
<Button Padding="5" Margin="0,5,5,5" Content="Cancel" Command="{Binding ResetSettingsCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</StatusBarItem>
<StatusBarItem HorizontalAlignment="Right">
<Button Padding="5" Margin="5" Content="Cancel" Command="{Binding ResetSettingsCommand}" />
<StatusBarItem Width="Auto">
<TextBlock VerticalAlignment="Top" Text="{Binding ErrorMessage}" Foreground="Red" TextWrapping="WrapWithOverflow" Margin="0,0,5,0" />
</StatusBarItem>
</StatusBar>

Expand Down

0 comments on commit 96f1542

Please sign in to comment.