simplenotesdesktop/SimpleNotes/PreferenceFile.cs

65 lines
2.2 KiB
C#
Executable File

/*
SimpleNotes, A server backed note taking app
Copyright (C) 2023 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using Newtonsoft.Json;
namespace SimpleNotes
{
public class PreferenceFile
{
public PreferenceFile()
{
}
[JsonIgnore]
public bool HasValue => !string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password) && !string.IsNullOrWhiteSpace(ServerUrl);
[JsonProperty("url")]
public string ServerUrl {get;set;}
[JsonProperty("username")]
public string Username {get;set;}
[JsonProperty("password")]
public string Password {get;set;}
private static string GetPreferenceFilePath()
{
string localConfig = Path.Combine(Path.GetDirectoryName(typeof(PreferenceFile).Assembly.Location),"simplenotesconfig.json");
if(File.Exists(localConfig))
{
return localConfig;
}
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData,Environment.SpecialFolderOption.Create), "simplenotesconfig.json");
}
public static PreferenceFile GetPreferences()
{
string path = GetPreferenceFilePath();
if(File.Exists(path))
{
return JsonConvert.DeserializeObject<PreferenceFile>(File.ReadAllText(path));
}
return new PreferenceFile();
}
public void Save()
{
var path=GetPreferenceFilePath();
File.WriteAllText(path,JsonConvert.SerializeObject(this));
}
}
}