-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8a276f
commit c964d4b
Showing
4 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace DicomGrep.Models | ||
{ | ||
public class Configuration | ||
{ | ||
public List<string> SearchPathHistory { get; set; } | ||
public List<string> FileTypesHistory { get; set; } | ||
public List<string> SopClassUidHistory { get; set; } | ||
public List<string> DicomTagHistory { get; set; } | ||
public List<string> SearchTextHistory { get; set; } | ||
|
||
// todo: other configuration item | ||
public int HistoryCapacity { get; set; } | ||
public SearchCriteria SearchCriteria { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace DicomGrep.Models | ||
{ | ||
public class SearchCriteria | ||
{ | ||
public string SearchPath { get; set; } | ||
public string SearchSopClassUid { get; set; } | ||
|
||
private string _searchTag; | ||
|
||
public string SearchTag | ||
{ | ||
get => _searchTag; | ||
set | ||
{ | ||
_searchTag = value; | ||
_searchTagFlattened = value?.Substring(0, value.IndexOf(':') > 0 ? value.IndexOf(':') : value.Length) | ||
.Replace("(", "") | ||
.Replace(")", "") | ||
.Replace(",", "") | ||
.Replace(".", "") | ||
.Replace(" ", "") | ||
.ToUpper(); | ||
} | ||
} | ||
|
||
private string _searchTagFlattened; | ||
[JsonIgnore] | ||
public string SearchTagFlattened => _searchTagFlattened; | ||
|
||
public string FileTypes { get; set; } | ||
|
||
public string SearchText { get; set; } | ||
|
||
public bool CaseSensitive { get; set; } | ||
public bool WholeWord { get; set; } | ||
public bool IncludeSubfolders { get; set; } | ||
|
||
[JsonIgnore] | ||
public bool SearchInResults { get; set; } | ||
|
||
public int SearchThreads { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return | ||
$"SearchPath = '{SearchPath}', " + | ||
$"SearchSopClassUid = '{SearchSopClassUid}', " + | ||
$"SearchTag = '{SearchTag}', " + | ||
$"FileTypes = '{FileTypes}', " + | ||
$"SearchText = '{SearchText}', " + | ||
$"CaseSensitive = {CaseSensitive}, " + | ||
$"WholeWord= {WholeWord}, " + | ||
$"IncludeSubfolders = {IncludeSubfolders}, " + | ||
$"SearchInResults = {SearchInResults}, " + | ||
$"SearchThreads = {SearchThreads}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using DicomGrep.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace DicomGrep.Services | ||
{ | ||
public class ConfigurationService | ||
{ | ||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); | ||
private static Configuration CurrentConfiguration { get; set; } | ||
public const string CONFIG_FILE = "config.json"; | ||
|
||
public Configuration GetConfiguration() => CurrentConfiguration; | ||
|
||
public void SetConfiguration(Configuration config) | ||
{ | ||
CurrentConfiguration = config; | ||
//todo: raise configuration changed event | ||
} | ||
|
||
|
||
|
||
public bool Save() | ||
{ | ||
logger.Info("Saving the configuration to file"); | ||
|
||
JsonSerializerOptions option = new JsonSerializerOptions { WriteIndented = true }; | ||
try | ||
{ | ||
File.WriteAllText(CONFIG_FILE, JsonSerializer.Serialize(CurrentConfiguration, option)); | ||
} | ||
catch(Exception ex) | ||
{ | ||
logger.Error(ex, "Failed to save the configuration to file."); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public bool Load() | ||
{ | ||
logger.Info("Loading the configuration from file"); | ||
try | ||
{ | ||
if (File.Exists(CONFIG_FILE)) | ||
{ | ||
CurrentConfiguration = JsonSerializer.Deserialize<Configuration>(File.ReadAllText(CONFIG_FILE)); | ||
return true; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.Error(ex, "Failed to load the configuration from file."); | ||
} | ||
CurrentConfiguration = DefaultConfiguration(); | ||
return false; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
private static Configuration DefaultConfiguration() | ||
{ | ||
logger.Info("Load default configuration"); | ||
|
||
SearchCriteria criteria = new SearchCriteria | ||
{ | ||
SearchPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), | ||
FileTypes = "*.dcm", | ||
SearchSopClassUid = "", | ||
SearchTag = "", | ||
SearchText = "TREATMENT", | ||
IncludeSubfolders = true, | ||
SearchInResults = false, | ||
SearchThreads = 1 | ||
}; | ||
|
||
return new Configuration | ||
{ | ||
SopClassUidHistory = new List<string> { criteria.SearchSopClassUid }, | ||
DicomTagHistory = new List<string> { criteria.SearchTag }, | ||
FileTypesHistory = new List<string> { criteria.FileTypes }, | ||
SearchPathHistory = new List<string> { criteria.SearchPath }, | ||
SearchTextHistory = new List<string> { criteria.SearchText }, | ||
HistoryCapacity = 10, | ||
SearchCriteria = criteria | ||
}; | ||
} | ||
|
||
} | ||
} |