-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editable Locations. New IniFile class.
- Loading branch information
Showing
14 changed files
with
626 additions
and
517 deletions.
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
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 |
---|---|---|
@@ -1,18 +1,129 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Windows.Forms; | ||
|
||
namespace ScreenSaver | ||
{ | ||
public class FlipItSettings | ||
{ | ||
// Make it creatable only via Load command | ||
private FlipItSettings() | ||
{ | ||
} | ||
|
||
public bool Display24HrTime { get; set; } | ||
public int Scale { get; set; } = 70; | ||
|
||
public List<ScreenSetting> ScreenSettings { get; set; } = new List<ScreenSetting>(); | ||
|
||
public ScreenSetting GetScreen(string screenDeviceName) | ||
{ | ||
return ScreenSettings.SingleOrDefault(s => s.DeviceName == screenDeviceName); | ||
var cleanName = CleanScreenDeviceName(screenDeviceName); | ||
return ScreenSettings.SingleOrDefault(s => s.DeviceName == cleanName); | ||
} | ||
|
||
public static FlipItSettings Load(Screen[] allScreens) | ||
{ | ||
IniFile iniFile = null; | ||
|
||
var settings = new FlipItSettings(); | ||
var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FlipIt"); | ||
var iniFilePath = Path.Combine(settingsFolder, "Settings.ini"); | ||
if (File.Exists(iniFilePath)) | ||
{ | ||
iniFile = new IniFile(iniFilePath); | ||
settings.Display24HrTime = iniFile.GetBool("General", "Display24Hr", false); | ||
settings.Scale = iniFile.GetInt("General", "Scale", 70); | ||
} | ||
else | ||
{ | ||
settings.Display24HrTime = false; | ||
} | ||
|
||
var screenNum = 0; | ||
foreach (var screen in allScreens) | ||
{ | ||
screenNum++; | ||
var cleanDeviceName = CleanScreenDeviceName(screen.DeviceName); | ||
var screenSectionName = $"Screen {cleanDeviceName}"; | ||
|
||
var screenSetting = new ScreenSetting(screenNum, cleanDeviceName, screen.Bounds.Width, screen.Bounds.Height); | ||
if (iniFile != null) | ||
{ | ||
// if (iniFile.SectionExists(screenSectionName)) | ||
if (iniFile.SectionExists(screenSectionName)) | ||
{ | ||
screenSetting.DisplayType = (DisplayType)iniFile.GetInt(screenSectionName, "DisplayType", (int)DisplayType.CurrentTime); | ||
} | ||
|
||
var screenLocationsSectionName = $"Screen {cleanDeviceName} Locations"; | ||
if (iniFile.SectionExists(screenLocationsSectionName)) | ||
{ | ||
var locationNames = iniFile.GetKeys(screenLocationsSectionName); | ||
foreach (var locationName in locationNames) | ||
{ | ||
var timeZoneID = iniFile.GetString(screenLocationsSectionName, locationName); | ||
if (!String.IsNullOrWhiteSpace(timeZoneID)) | ||
{ | ||
screenSetting.Locations.Add(new Location(timeZoneID, locationName)); | ||
} | ||
else | ||
{ | ||
iniFile.DeleteKey(screenLocationsSectionName, locationName); | ||
} | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
screenSetting.DisplayType = DisplayType.CurrentTime; | ||
} | ||
|
||
settings.ScreenSettings.Add(screenSetting); | ||
} | ||
return settings; | ||
} | ||
|
||
public void Save() | ||
{ | ||
var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FlipIt"); | ||
if (!Directory.Exists(settingsFolder)) | ||
{ | ||
Directory.CreateDirectory(settingsFolder); | ||
} | ||
var iniFilePath = Path.Combine(settingsFolder, "Settings.ini"); | ||
if (!File.Exists(iniFilePath)) | ||
{ | ||
File.Create(iniFilePath).Dispose(); // create an empty file | ||
} | ||
|
||
var iniFile = new IniFile(iniFilePath); | ||
iniFile.SetBool("General", "Display24Hr", Display24HrTime); | ||
iniFile.SetInt("General", "Scale", Scale); | ||
|
||
foreach (var screenSetting in ScreenSettings) | ||
{ | ||
var screenSectionName = $"Screen {screenSetting.DeviceName}"; | ||
iniFile.SetInt(screenSectionName, "DisplayType", (int)screenSetting.DisplayType); | ||
|
||
var screenLocationsSectionName = $"Screen {screenSetting.DeviceName} Locations"; | ||
if (iniFile.SectionExists(screenLocationsSectionName)) | ||
{ | ||
iniFile.DeleteSection(screenLocationsSectionName); | ||
} | ||
foreach (var location in screenSetting.Locations) | ||
{ | ||
iniFile.SetString(screenLocationsSectionName, location.DisplayName, location.TimeZoneID); | ||
} | ||
} | ||
iniFile.Save(); | ||
} | ||
|
||
private static string CleanScreenDeviceName(string deviceName) | ||
{ | ||
return deviceName.TrimStart('\\', '.'); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,98 +1,155 @@ | ||
// Initially from https://stackoverflow.com/a/14906422/1899 | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace ScreenSaver | ||
{ | ||
public class IniFile | ||
public class IniFile | ||
{ | ||
private const int BufferSize = 1024; | ||
private readonly string _path; | ||
private readonly string _iniFilePath; | ||
private readonly List<Entry> _entries = new List<Entry>(); | ||
|
||
[DllImport("kernel32", CharSet = CharSet.Unicode)] | ||
static extern long WritePrivateProfileString(string section, string key, string value, string filePath); | ||
private class Entry | ||
{ | ||
public Entry(string section, string key, string value) | ||
{ | ||
Section = section; | ||
Key = key; | ||
Value = value; | ||
} | ||
|
||
[DllImport("kernel32", CharSet = CharSet.Unicode)] | ||
static extern int GetPrivateProfileString(string section, string key, string @default, StringBuilder retVal, int size, string filePath); | ||
public string Section { get; } | ||
public string Key { get; } | ||
public string Value { get; set; } | ||
} | ||
|
||
public IniFile(string iniPath) | ||
{ | ||
_path = iniPath; | ||
_iniFilePath = iniPath; | ||
|
||
string currentSection = null; | ||
|
||
var lines = File.ReadAllLines(iniPath); | ||
foreach (var rawLine in lines) | ||
{ | ||
var line = rawLine.Trim(); | ||
if (line == "") | ||
continue; | ||
if (line.StartsWith("[") && line.EndsWith("]")) | ||
{ | ||
currentSection = line.Substring(1, line.Length - 2); | ||
} | ||
else | ||
{ | ||
var keyPair = line.Split(new char[] {'='}, 2); | ||
if (keyPair.Length < 2) | ||
{ | ||
// it could be a comment | ||
continue; | ||
} | ||
|
||
if (currentSection == null) | ||
{ | ||
currentSection = "ROOT"; | ||
} | ||
|
||
var entry = new Entry(currentSection, keyPair[0], keyPair[1]); | ||
_entries.Add(entry); | ||
} | ||
} | ||
} | ||
|
||
public string ReadString(string section, string key) | ||
public bool SectionExists(string section) | ||
{ | ||
var retVal = new StringBuilder(BufferSize); | ||
GetPrivateProfileString(section, key, "", retVal, BufferSize, _path); | ||
return retVal.ToString(); | ||
return _entries.Exists(e => e.Section == section); | ||
} | ||
|
||
public void WriteBool(string section, string key, bool value) | ||
public string[] GetKeys(string section) | ||
{ | ||
WriteString(section, key, value ? "1": "0"); | ||
return _entries.Where(e => e.Section == section).Select(e => e.Key).ToArray(); | ||
} | ||
|
||
public bool ReadBool(string section, string key, bool defaultValue) | ||
public void DeleteSection(string section) | ||
{ | ||
var val = ReadString(section, key); | ||
if (val == "") | ||
return defaultValue; | ||
Debug.Assert(val == "1" || val == "0"); | ||
return val == "1"; | ||
_entries.RemoveAll(e => e.Section == section); | ||
} | ||
public int ReadInt(string section, string key, int defaultValue) | ||
|
||
public string GetString(string section, string key) | ||
{ | ||
var val = ReadString(section, key); | ||
if (val == "") | ||
return defaultValue; | ||
return Int32.Parse(val); | ||
return GetEntry(section, key)?.Value; | ||
} | ||
|
||
public void WriteInt(string section, string key, int value) | ||
public int GetInt(string section, string key, int defaultValue) | ||
{ | ||
WriteString(section, key, value.ToString()); | ||
var value = GetString(section, key); | ||
return value != null ? Convert.ToInt32(value) : defaultValue ; | ||
} | ||
|
||
public string[] ReadSections() | ||
public bool GetBool(string section, string key, bool defaultValue) | ||
{ | ||
var keyNames = ReadString(null, null); | ||
return keyNames.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); | ||
var value = GetInt(section, key, defaultValue ? 1 : 0); | ||
return value == 1; | ||
} | ||
|
||
public bool SectionExists(string section) | ||
public void SetString(string section, string key, string value) | ||
{ | ||
var keyNames = ReadSection(section); | ||
return keyNames.Length > 0; | ||
var entry = GetEntry(section, key); | ||
if (entry != null) | ||
{ | ||
entry.Value = value; | ||
} | ||
else | ||
{ | ||
_entries.Add(new Entry(section, key, value)); | ||
} | ||
} | ||
|
||
public string[] ReadSection(string section) | ||
public void SetBool(string section, string key, bool value) | ||
{ | ||
var keyNames = ReadString(section, null); | ||
return keyNames.Split(new []{'\n'}, StringSplitOptions.RemoveEmptyEntries); | ||
SetString(section, key, value ? "1" : "0"); | ||
} | ||
|
||
public void WriteString(string section, string key, string value) | ||
public void SetInt(string section, string key, int value) | ||
{ | ||
WritePrivateProfileString(section, key, value, _path); | ||
SetString(section, key, value.ToString()); | ||
} | ||
|
||
public void DeleteKey(string section, string key) | ||
private Entry GetEntry(string section, string key) | ||
{ | ||
WriteString(section, key, null); | ||
return _entries.SingleOrDefault(e => e.Section == section && e.Key == key); | ||
} | ||
|
||
public void DeleteSection(string section = null) | ||
public void DeleteKey(String section, String key) | ||
{ | ||
WriteString(section, null, null); | ||
_entries.RemoveAll(e => e.Section == section && e.Key == key); | ||
} | ||
|
||
public void Save(string filePath) | ||
{ | ||
var sb = new StringBuilder(); | ||
var sectionNames = _entries.Select(e => e.Section).Distinct(); | ||
foreach (var section in sectionNames) | ||
{ | ||
sb.AppendLine($"[{section}]"); | ||
|
||
var sectionEntries = _entries.Where(e => e.Section == section); | ||
foreach (var entry in sectionEntries) | ||
{ | ||
if (entry.Value == null) | ||
continue; | ||
|
||
sb.AppendLine($"{entry.Key}={entry.Value}"); | ||
} | ||
sb.AppendLine(); | ||
} | ||
File.WriteAllText(filePath, sb.ToString()); | ||
} | ||
|
||
public bool KeyExists(string key, string section = null) | ||
public void Save() | ||
{ | ||
return ReadString(section, key).Length > 0; | ||
Save(_iniFilePath); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.