1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-24 15:15:49 +02:00

Refactor method order and return types in config file classes

This commit is contained in:
chylex 2017-11-07 18:36:52 +01:00
parent 9a6b615174
commit 5b2daf9746
5 changed files with 20 additions and 30 deletions
Configuration
Core/Other/Analytics
Plugins
tests/Configuration

View File

@ -31,13 +31,11 @@ private SystemConfig(string file){
this.file = file;
}
public bool Save(){
public void Save(){
try{
Serializer.Write(file, this);
return true;
}catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not save the system configuration file.", true, e);
return false;
}
}

View File

@ -140,7 +140,7 @@ private UserConfig(string file){
this.file = file;
}
public bool Save(){
public void Save(){
try{
if (File.Exists(file)){
string backupFile = GetBackupFile(file);
@ -149,29 +149,23 @@ public bool Save(){
}
Serializer.Write(file, this);
return true;
}catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not save the configuration file.", true, e);
return false;
}
}
public bool Reload(){
public void Reload(){
try{
LoadInternal(false);
return true;
}catch(FileNotFoundException){
try{
Serializer.Write(file, new UserConfig(file));
LoadInternal(false);
return true;
}catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not regenerate configuration file.", true, e);
return false;
}
}catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not reload configuration file.", true, e);
return false;
}
}

View File

@ -26,13 +26,11 @@ private AnalyticsFile(string file){
this.file = file;
}
public bool Save(){
public void Save(){
try{
Serializer.Write(file, this);
return true;
}catch(Exception e){
Program.Reporter.HandleException("Analytics File Error", "Could not save the analytics file.", true, e);
return false;
}
}

View File

@ -32,6 +32,20 @@ public bool IsEnabled(Plugin plugin){
return !disabled.Contains(plugin.Identifier);
}
public void Save(string file){
try{
using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None), Encoding.UTF8)){
writer.WriteLine("#Disabled");
foreach(string identifier in disabled){
writer.WriteLine(identifier);
}
}
}catch(Exception e){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not save the plugin configuration file.", true, e);
}
}
public void Load(string file){
try{
using(StreamReader reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read), Encoding.UTF8)){
@ -54,19 +68,5 @@ public void Load(string file){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not read the plugin configuration file. If you continue, the list of disabled plugins will be reset to default.", true, e);
}
}
public void Save(string file){
try{
using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None), Encoding.UTF8)){
writer.WriteLine("#Disabled");
foreach(string identifier in disabled){
writer.WriteLine(identifier);
}
}
}catch(Exception e){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not save the plugin configuration file.", true, e);
}
}
}
}

View File

@ -76,7 +76,7 @@ public void TestReload(){
cfg.Save();
cfg.ZoomLevel = 200;
Assert.IsTrue(cfg.Reload());
cfg.Reload();
Assert.AreEqual(123, cfg.ZoomLevel);
}
@ -87,7 +87,7 @@ public void TestReset(){
cfg.Save();
File.Delete("reset");
Assert.IsTrue(cfg.Reload());
cfg.Reload();
Assert.AreEqual(100, cfg.ZoomLevel);
Assert.IsTrue(File.Exists("reset"));
}