Skip to content

Commit

Permalink
password system optizmied
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammedTsmu committed Oct 9, 2024
1 parent 63654dc commit 5abfc7d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 46 deletions.
22 changes: 13 additions & 9 deletions WebsiteAndAppBlocker/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
</Grid.RowDefinitions>
<!-- TaskbarIcon Control -->
<tb:TaskbarIcon x:Name="TrayIcon"
IconSource="pack://application:,,,/Resources/shield_512px.ico"
IconSource="pack://application:,,,/WebsiteAndAppBlocker;component/Resources/shield_512px.ico"
Visibility="Collapsed"
ToolTipText="Website and App Blocker"
ToolTipText="Website and App Blocker - Double-click to open"
TrayMouseDoubleClick="TrayIcon_TrayMouseDoubleClick">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
Expand Down Expand Up @@ -63,19 +63,19 @@
<TextBlock Text="Website to Block:" Style="{StaticResource SectionHeaderStyle}"/>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="WebsiteTextBox" Width="300" Height="30" Margin="0,0,10,0"/>
<Button Content="Block" Click="BlockWebsiteButton_Click" Style="{StaticResource ModernButtonStyle}" Width="200" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<Button Content="Block" Click="BlockWebsiteButton_Click" Style="{StaticResource ModernButtonStyle}" Width="200" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>

<!-- Blocked Websites List -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical">
<TextBlock Text="Blocked Websites:" Style="{StaticResource SectionHeaderStyle}"/>
<Border BorderBrush="Gray" BorderThickness="1" CornerRadius="5" Width="714" HorizontalAlignment="Left">
<ScrollViewer Height="315" HorizontalAlignment="Center" Width="698" VerticalAlignment="Center">
<ListBox x:Name="BlockedWebsitesListBox" Width="688" HorizontalAlignment="Center"/>
<Border BorderBrush="Gray" BorderThickness="1" CornerRadius="5" HorizontalAlignment="Stretch">
<ScrollViewer Height="315">
<ListBox x:Name="BlockedWebsitesListBox" HorizontalAlignment="Stretch"/>
</ScrollViewer>
</Border>
<Button Content="Unblock Selected Website" Click="UnblockSelectedWebsiteButton_Click" Width="200" Style="{StaticResource ModernButtonStyle}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Button Content="Unblock Selected Website" Click="UnblockSelectedWebsiteButton_Click" Width="200" Style="{StaticResource ModernButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</Grid>
</TabItem>
Expand Down Expand Up @@ -132,10 +132,14 @@
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<!-- Password Status -->
<TextBlock x:Name="PasswordStatusTextBlock" Grid.Row="0" Margin="0,0,0,10" FontWeight="Bold"/>

<!-- Set Password Section -->
<StackPanel Grid.Row="0" Orientation="Vertical">
<StackPanel x:Name="SetPasswordSection" Grid.Row="1" Orientation="Vertical">
<TextBlock Text="Set Password:" Style="{StaticResource SectionHeaderStyle}"/>
<StackPanel Orientation="Horizontal">
<PasswordBox x:Name="PasswordBox" Width="300" Height="30" Margin="0,0,10,0"/>
Expand All @@ -144,7 +148,7 @@
</StackPanel>

<!-- Change Password Section -->
<StackPanel Grid.Row="1" Orientation="Vertical" Margin="0,20,0,0">
<StackPanel x:Name="ChangePasswordSection" Grid.Row="2" Orientation="Vertical" Margin="0,20,0,0">
<TextBlock Text="Change Password:" Style="{StaticResource SectionHeaderStyle}"/>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical" Margin="0,5,0,0">
Expand Down
98 changes: 61 additions & 37 deletions WebsiteAndAppBlocker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ public MainWindow()
}
else
{
// Optionally, start minimized even outside blocking period
// this.WindowState = WindowState.Minimized;
//Optionally, start minimized even outside blocking period
this.WindowState = WindowState.Minimized;
}

// Disable Set Password section if password already exists
CheckPasswordStatus();
}

// Administrator check
Expand Down Expand Up @@ -191,9 +194,6 @@ private void BlockWebsiteButton_Click(object sender, RoutedEventArgs e)
return;
}

//if (IsWithinBlockingPeriod() && !PromptForAuthentication())
// return;

string website = WebsiteTextBox.Text.Trim();
if (!string.IsNullOrEmpty(website))
{
Expand Down Expand Up @@ -300,9 +300,6 @@ private bool CanAttemptUnblock()

private void BlockSelectedAppButton_Click(object sender, RoutedEventArgs e)
{
//if (IsWithinBlockingPeriod() && !PromptForAuthentication())
// return;

if (RunningAppsListBox.SelectedValue != null)
{
string appName = RunningAppsListBox.SelectedValue.ToString();
Expand All @@ -326,9 +323,6 @@ private void BlockSelectedAppButton_Click(object sender, RoutedEventArgs e)

private void BlockAppButton_Click(object sender, RoutedEventArgs e)
{
//if (IsWithinBlockingPeriod() && !PromptForAuthentication())
// return;

string appName = AppTextBox.Text.Trim();
if (!string.IsNullOrEmpty(appName))
{
Expand Down Expand Up @@ -387,6 +381,15 @@ private void RefreshAppListButton_Click(object sender, RoutedEventArgs e)
// Password setting
private void SetPasswordButton_Click(object sender, RoutedEventArgs e)
{
string passwordFilePath = GetPasswordFilePath();

if (File.Exists(passwordFilePath))
{
// Password already exists
MessageBox.Show("A password has already been set. Please use the Change Password section to update your password.");
return;
}

string newPassword = PasswordBox.Password.Trim();

if (string.IsNullOrEmpty(newPassword))
Expand All @@ -399,18 +402,59 @@ private void SetPasswordButton_Click(object sender, RoutedEventArgs e)

try
{
string passwordFilePath = GetPasswordFilePath();
File.WriteAllText(passwordFilePath, hashedPassword);
MessageBox.Show("Password has been set successfully.");
PasswordBox.Clear();

// Disable the Set Password section
DisableSetPasswordSection();

// Update password status
PasswordStatusTextBlock.Text = "A password is currently set.";

// Show Change Password section
ChangePasswordSection.Visibility = Visibility.Visible;
}
catch (Exception ex)
{
MessageBox.Show($"Failed to set password: {ex.Message}");
}
}

private void CheckPasswordStatus()
{
string passwordFilePath = GetPasswordFilePath();

if (File.Exists(passwordFilePath))
{
// Password exists, disable Set Password section
DisableSetPasswordSection();
PasswordStatusTextBlock.Text = "A password is currently set.";

// Ensure Change Password section is visible
ChangePasswordSection.Visibility = Visibility.Visible;
}
else
{
PasswordStatusTextBlock.Text = "No password is set. Please set a password.";

// Hide Change Password section
DisableChangePasswordSection();
}
}

private void DisableSetPasswordSection()
{
SetPasswordSection.Visibility = Visibility.Collapsed;
}

private void DisableChangePasswordSection()
{
ChangePasswordSection.Visibility = Visibility.Collapsed;
}


// Helper methods
private string HashPassword(string password)
{
using (var sha256 = System.Security.Cryptography.SHA256.Create())
Expand All @@ -420,6 +464,7 @@ private string HashPassword(string password)
}
}


private string GetPasswordFilePath()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Expand All @@ -431,6 +476,7 @@ private string GetPasswordFilePath()
return Path.Combine(appFolder, "password.txt");
}


private void ChangePasswordButton_Click(object sender, RoutedEventArgs e)
{
string currentPassword = CurrentPasswordBox.Password.Trim();
Expand Down Expand Up @@ -467,6 +513,8 @@ private void ChangePasswordButton_Click(object sender, RoutedEventArgs e)
ConfirmNewPasswordBox.Clear();
}



// Authentication methods
private bool AuthenticateUser(string password)
{
Expand Down Expand Up @@ -498,11 +546,6 @@ private bool PromptForAuthentication()
}

// Helper methods
//private bool IsWithinBlockingPeriod()
//{
// TimeSpan now = DateTime.Now.TimeOfDay;
// return (now >= blockingStartTime) && (now <= blockingEndTime);
//}
private bool IsWithinBlockingPeriod()
{
TimeSpan now = DateTime.Now.TimeOfDay;
Expand Down Expand Up @@ -650,26 +693,7 @@ private void StartUIUpdateTimer()
uiUpdateTimer.Start();
}

//private void UIUpdateTimer_Tick(object sender, EventArgs e)
//{
// if (IsWithinBlockingPeriod())
// {
// if (this.IsVisible && !this.IsActive)
// {
// this.Hide();
// ShowTrayIcon();
// }
// }
// else
// {
// if (!this.IsVisible)
// {
// this.Show();
// this.WindowState = WindowState.Normal;
// HideTrayIcon();
// }
// }
//}

private void UIUpdateTimer_Tick(object sender, EventArgs e)
{
if (IsWithinBlockingPeriod())
Expand Down

0 comments on commit 5abfc7d

Please sign in to comment.