Skip to content
Open
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
7 changes: 3 additions & 4 deletions OpenNetMeter/Models/ApplicationDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Reflection;
using DatabaseEngine;
using OpenNetMeter.Properties;

namespace OpenNetMeter.Models
{
Expand All @@ -22,10 +23,8 @@ public ApplicationDB(string dBFileName, string[]? extraParams = null)

public static string GetFilePath()
{
string? appName = Assembly.GetEntryAssembly()?.GetName().Name;
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
path = Path.Combine(path, appName ?? "OpenNetMeter");
Directory.CreateDirectory(path);
string path = SettingsManager.Current.Folder;
path = Path.Combine(path);
return path;
}

Expand Down
5 changes: 5 additions & 0 deletions OpenNetMeter/Properties/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;

namespace OpenNetMeter.Properties
{
Expand Down Expand Up @@ -43,6 +45,9 @@ public class AppSettings : INotifyPropertyChanged
private int miniWidgetTransparentSlider;
public int MiniWidgetTransparentSlider { get => miniWidgetTransparentSlider; set { if (miniWidgetTransparentSlider != value) { miniWidgetTransparentSlider = value; OnPropertyChanged("MiniWidgetTransparentSlider"); } } }

private string folder =Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), Assembly.GetEntryAssembly()?.GetName().Name ?? "OpenNetMeter");
public string Folder { get => folder; set { if (folder != value) { folder = value; OnPropertyChanged("Folder"); } } }


public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged(string propName) {
Expand Down
89 changes: 77 additions & 12 deletions OpenNetMeter/ViewModels/SettingsVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Input;
using TaskScheduler = Microsoft.Win32.TaskScheduler;

Expand Down Expand Up @@ -58,7 +59,7 @@ public bool UnlockOptionStartWin
}

private bool minimizeOnStart;
public bool MinimizeOnStart
public bool MinimizeOnStart
{
get { return minimizeOnStart; }
set
Expand All @@ -73,7 +74,7 @@ public bool MinimizeOnStart
}

private bool unlockMinimizeOnStart;
public bool UnlockMinimizeOnStart
public bool UnlockMinimizeOnStart
{
get { return unlockMinimizeOnStart; }

Expand Down Expand Up @@ -113,7 +114,7 @@ public int NetworkSpeedFormat
get { return networkSpeedFormat; }
set
{
if(networkSpeedFormat != value)
if (networkSpeedFormat != value)
{
networkSpeedFormat = value;
SettingsManager.Current.NetworkSpeedFormat = value;
Expand All @@ -122,6 +123,24 @@ public int NetworkSpeedFormat
}
}
}
private string? dataFolder;
public string DataFolder
{
get { return dataFolder ?? " "; }
set
{
if (dataFolder != value)
{
var oldvalue = dataFolder;
dataFolder = value;
SettingsManager.Current.Folder = value;
SettingsManager.Save();
OnPropertyChanged("DataFolder");
Debug.WriteLine($"folder:{dataFolder}");
}
}
}


private bool darkMode;
public bool DarkMode
Expand Down Expand Up @@ -149,10 +168,10 @@ public int MiniWidgetTransparentSlider
set
{
miniWidgetTransparentSlider = value;

OnPropertyChanged("MiniWidgetTransparentSlider");
//Debug.WriteLine($"MiniWidgetTransparentSlider: {value}");

//trigger the miniwidget's BackgroundColor property.
SetMiniWidgetBackgroundColor(DarkMode, value);

Expand All @@ -170,7 +189,7 @@ public bool DeleteAllFiles
if (deleteAllFiles != value)
{
deleteAllFiles = value;
if(value)
if (value)
OnPropertyChanged("DeleteAllFiles");
}
}
Expand All @@ -179,14 +198,15 @@ public bool DeleteAllFiles

private ConfirmationDialogVM? cdvm;
private MiniWidgetVM? mwvm;
public ICommand? BrowseFolderCommand { get; }

public SettingsVM(MiniWidgetVM mw_ref, ConfirmationDialogVM cdvm_ref)
{

mwvm = mw_ref;
cdvm = cdvm_ref;
cdvm.BtnCommand = new BaseCommand(ResetDataYesOrNo, true);
cdvm.DialogMessage = "Warning!!! This will delete all saved profiles.\nDo you still want to continue?";

taskFolder = "OpenNetMeter";
taskName = "OpenNetMeter" + "-" + Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString(3);

Expand All @@ -196,6 +216,10 @@ public SettingsVM(MiniWidgetVM mw_ref, ConfirmationDialogVM cdvm_ref)
MinimizeOnStart = SettingsManager.Current.MinimizeOnStart;
DarkMode = SettingsManager.Current.DarkMode;
MiniWidgetTransparentSlider = SettingsManager.Current.MiniWidgetTransparentSlider;
DataFolder = SettingsManager.Current.Folder;

BrowseFolderCommand = new BaseCommand(BrowseFolder, true);


if (SetStartWithWin)
UnlockMinimizeOnStart = false;
Expand Down Expand Up @@ -225,13 +249,54 @@ private void SetMiniWidgetBackgroundColor(bool darkMode, int transparency)

private void ResetData(object? obj)
{
if(cdvm != null)
if (cdvm != null)
cdvm.IsVisible = System.Windows.Visibility.Visible;
}
private void BrowseFolder(object? obj)
{
var oldvalue = DataFolder;
using (var dialog = new FolderBrowserDialog())
{
dialog.Description = "Select a folder";
dialog.ShowNewFolderButton = true;

if (dialog.ShowDialog() == DialogResult.OK)
{
DataFolder = dialog.SelectedPath;
}
}
moveAndDelete(oldvalue, dataFolder);

}
private void moveAndDelete(string? oldFolderPath, string? newPath)
{
if (oldFolderPath == null || !Directory.Exists(oldFolderPath))
oldFolderPath = AppDomain.CurrentDomain.BaseDirectory;
if (newPath == null || !Directory.Exists(newPath))
newPath = AppDomain.CurrentDomain.BaseDirectory;

string[] files = Directory.GetFiles(oldFolderPath);

foreach (string filePath in files)
{
string fileName = Path.GetFileName(filePath);
string destFile = Path.Combine(newPath, fileName);

try
{
// Move the file
File.Move(filePath, destFile);
}
catch (IOException ex)
{
Console.WriteLine($"Error moving file {fileName}: {ex.Message}");
}
}
}

private void ResetDataYesOrNo(object? obj)
{
if(obj != null)
if (obj != null)
{
if ((string)obj == "Yes")
DeleteAllFiles = true;
Expand All @@ -250,7 +315,7 @@ private void SetAppAsTask(bool set)
TaskScheduler.TaskFolder sub = TaskScheduler.TaskService.Instance.RootFolder.SubFolders["OpenNetMeter"];
if (!set)
{
for(int i = 0; i < sub.Tasks.Count; i++)
for (int i = 0; i < sub.Tasks.Count; i++)
{
sub.DeleteTask(sub.Tasks[i].Name);
}
Expand Down Expand Up @@ -310,14 +375,14 @@ private void CreateTask()
//set action to run application
TaskScheduler.ExecAction action = new TaskScheduler.ExecAction();
action.Path = Path.Combine(AppContext.BaseDirectory, "OpenNetMeter.exe");
if(MinimizeOnStart)
if (MinimizeOnStart)
action.Arguments = "/StartMinimized";
td.Actions.Add(action);

// Register the task in the sub folder
TaskScheduler.TaskService.Instance.RootFolder.SubFolders["OpenNetMeter"].RegisterTaskDefinition(taskName, td);
}
catch(Exception ex)
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
}
Expand Down
11 changes: 10 additions & 1 deletion OpenNetMeter/Views/MainWindowTabs/SettingsV.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
</StackPanel>
</StackPanel>

<Border Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="2" Style="{StaticResource BorderStyle1}">
</Border>
<StackPanel Grid.Row="3" Grid.Column="1" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5" Height="50">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource FontColor1}">Data Folder</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5" >
<TextBox Width="180" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0" Text="{Binding Path=DataFolder, UpdateSourceTrigger=PropertyChanged}" IsEnabled="False" />
<Button Content="Browse..." Margin="5,0" Padding="5,2" Command="{Binding Path=BrowseFolderCommand}" />
</StackPanel>
</StackPanel>

<Border Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Margin="2" Style="{StaticResource BorderStyle1}">
</Border>
<StackPanel Grid.Row="0" Grid.Column="3" Orientation="Vertical" Margin="5">
Expand Down Expand Up @@ -110,7 +120,6 @@
<RadioButton IsChecked="{Binding Path=NetworkSpeedFormat, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=1}" Margin="5,0" Padding="0" Style="{StaticResource FontColor1}">Bps (Bytes/sec)</RadioButton>
</StackPanel>
</StackPanel>

</Grid>
</Border>
</Grid>
Expand Down