Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing options quirks #98

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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