Skip to content

Commit

Permalink
Editable Locations. New IniFile class.
Browse files Browse the repository at this point in the history
  • Loading branch information
phaselden committed Jan 24, 2021
1 parent 74da446 commit ba33417
Show file tree
Hide file tree
Showing 14 changed files with 626 additions and 517 deletions.
7 changes: 5 additions & 2 deletions src/FlipIt.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.30907.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlipIt", "FlipIt\FlipIt.csproj", "{C79DC106-23F3-4FA7-BA69-6B8684C4AD9F}"
EndProject
Expand All @@ -19,4 +19,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {00565EC1-AD51-4412-973A-1416B2A4FEB9}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/FlipIt/FlipIt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="City.cs" />
<Compile Include="IniFile.cs" />
<Compile Include="Location.cs" />
<Compile Include="CurrentTimeScreen.cs" />
<Compile Include="DisplayType.cs" />
<Compile Include="DstIndicatorStyle.cs" />
<Compile Include="FlipItSettings.cs" />
<Compile Include="IniFile.cs" />
<Compile Include="Int32Extensions.cs" />
<Compile Include="RoundedRectangle.cs" />
<Compile Include="TimeScreen.cs" />
Expand Down
115 changes: 113 additions & 2 deletions src/FlipIt/FlipItSettings.cs
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('\\', '.');
}
}
}
159 changes: 108 additions & 51 deletions src/FlipIt/IniFile.cs
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);
}
}
}
}
Loading

0 comments on commit ba33417

Please sign in to comment.