Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Who cares committed Sep 18, 2018
1 parent abfc8d5 commit 3790762
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 96 deletions.
12 changes: 6 additions & 6 deletions SAI-Autosaver/App.config
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SAI_Autosaver.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="SAI_Autosaver.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/>
</startup>
<userSettings>
<SAI_Autosaver.Properties.Settings>
Expand All @@ -26,7 +26,7 @@
<value>False</value>
</setting>
<setting name="BackupFolderPath" serializeAs="String">
<value />
<value/>
</setting>
<setting name="IndexBackupDelay" serializeAs="String">
<value>2</value>
Expand All @@ -36,4 +36,4 @@
</setting>
</SAI_Autosaver.Properties.Settings>
</userSettings>
</configuration>
</configuration>
53 changes: 29 additions & 24 deletions SAI-Autosaver/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ private bool SelectBackupFolder()
return false;
}

private void buttonSelectFolder_Click(object sender, EventArgs e)
{
SelectBackupFolder();
}

private void buttonOpenBackupFolder_Click(object sender, EventArgs e)
{
try
{
Process.Start(Properties.Settings.Default.BackupFolderPath);
}
catch (Exception)
{
MessageBox.Show(Properties.Strings.IncorrectBackupPath);

if (!SelectBackupFolder())
{
checkBackupIntoFolder.Checked = false;
}
}
}

private void checkEnableBackups_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = (sender as CheckBox).Checked;
Expand Down Expand Up @@ -187,28 +209,6 @@ private void checkBackupIntoFolder_CheckedChanged(object sender, EventArgs e)
Properties.Settings.Default.BackupIntoFolder = isChecked;
}

private void buttonSelectFolder_Click(object sender, EventArgs e)
{
SelectBackupFolder();
}

private void buttonOpenBackupFolder_Click(object sender, EventArgs e)
{
try
{
Process.Start(Properties.Settings.Default.BackupFolderPath);
}
catch (Exception)
{
MessageBox.Show(Properties.Strings.IncorrectBackupPath);

if (!SelectBackupFolder())
{
checkBackupIntoFolder.Checked = false;
}
}
}

private void checkRunWithWindows_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = (sender as CheckBox).Checked;
Expand All @@ -224,6 +224,7 @@ private void checkRunInTray_CheckedChanged(object sender, EventArgs e)

private void checkNotifyProjectNotSaved_CheckedChanged(object sender, EventArgs e)
{
timer.CurrentTime = 0;
Properties.Settings.Default.BackupNotifyNotSaved = (sender as CheckBox).Checked;
}

Expand Down Expand Up @@ -259,7 +260,10 @@ private void buttonManualBackup_Click(object sender, EventArgs e)
timer.CurrentTime = 0;
}

SaiHelper.CopyProjectFileInto(Properties.Settings.Default.BackupFolderPath);
if(Properties.Settings.Default.BackupIntoFolder)
{
SaiHelper.CopyProjectFileInto(Properties.Settings.Default.BackupFolderPath);
}
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -288,8 +292,9 @@ private void MainForm_Load(object sender, EventArgs e)

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();
timer.CurrentTime = 0;
TaskbarHelper.SetState(SaiHelper.AppProcess, TaskbarProgressBarState.NoProgress);
Properties.Settings.Default.Save();
}

private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions SAI-Autosaver/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.7")]
[assembly: AssemblyFileVersion("1.3.7")]
[assembly: AssemblyVersion("1.3.8")]
[assembly: AssemblyFileVersion("1.3.8")]
[assembly: NeutralResourcesLanguage("en")]

3 changes: 2 additions & 1 deletion SAI-Autosaver/SAI-Autosaver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>SAI_Autosaver</RootNamespace>
<AssemblyName>SAI-Autosaver</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<IsWebBootstrapper>false</IsWebBootstrapper>
Expand All @@ -26,6 +26,7 @@
<ApplicationVersion>1.3.5.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
4 changes: 2 additions & 2 deletions SAI-Autosaver/TimerDelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (Properties.Settings.Default.BackupEnabled)
{
if (SaiHelper.ObtainProcess())
if (SaiHelper.Connect())
{
if (SaiHelper.IsProjectOpened())
{
if (SaiHelper.IsProjectHasPath())
if (SaiHelper.IsProjectSavedLocally())
{
if (SaiHelper.IsProjectModified())
{
Expand Down
127 changes: 78 additions & 49 deletions SAI-Autosaver/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

Expand All @@ -16,75 +17,103 @@ static class SaiHelper
public static string AppName { get; set; }
public static Process AppProcess { get; private set; }

public static bool ObtainProcess()
private static readonly (string, string)[] CachedDriveInfos;
private static readonly (string, string)[] CachedPrincipals;

static SaiHelper()
{
var process = Process.GetProcessesByName(AppName).FirstOrDefault();
AppProcess = process;
return process != null;
CachedDriveInfos = DriveInfo.GetDrives()
.Where(x => x.IsReady)
.Select(x => (x.Name, x.VolumeLabel))
.ToArray();

CachedPrincipals = new PrincipalSearcher(new UserPrincipal(new PrincipalContext(ContextType.Machine))).FindAll()
.Select(x => (x.Name, x.DisplayName))
.ToArray();
}

public static string GetProjectAbsolutePath()
public static bool Connect()
{
var path = AppProcess.MainWindowTitle.Split(new char[] { '-' }, 2)[1].Trim();
if (IsProjectModified())
{
path = path.Replace("(*)", "").Trim();
}
AppProcess = Process.GetProcessesByName(AppName).FirstOrDefault();
return AppProcess != null;
}

public static bool IsProjectOpened()
{
return AppProcess.MainWindowTitle.Contains('-');
}

public static bool IsProjectSavedLocally()
{
return GetProjectPath() != null;
}

public static bool IsProjectModified()
{
return AppProcess.MainWindowTitle.EndsWith("(*)");
}

public static string GetProjectName()
{
return Path.GetFileNameWithoutExtension(GetProjectPath());
}

public static string GetProjectPath()
{
var path = Regex.Replace(AppProcess.MainWindowTitle.Split('-').Last(), @"\s*\([!|*|?]\)", "").Trim();
var newPath = path.Contains('/');


if (path.Contains('/'))
{
var pathSplit = path.Split('/').Select(x => x.Trim()).ToArray();
string[] pathSplit = path.Split('/').Select(x => x.Trim()).ToArray();

if (pathSplit.Length == 2 && pathSplit[0].ToLower() == "desktop")
string TryGetDesktop()
{
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), pathSplit[1]);
if (pathSplit.Length == 2 && pathSplit[0].ToLower() == "desktop")
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), pathSplit[1]);
}

return Path.Combine(new string[] { Environment.GetFolderPath(Environment.SpecialFolder.Desktop) }.Concat(pathSplit).ToArray());
}
else
{
var principalSearcher = new PrincipalSearcher(new UserPrincipal(new PrincipalContext(ContextType.Machine)));
var found = false;

foreach (var principal in principalSearcher.FindAll())
string TryGetProfile()
{
foreach (var (Name, DisplayName) in CachedPrincipals)
{
if (pathSplit[0] == principal.DisplayName || pathSplit[0] == principal.Name)
if (pathSplit[0] == DisplayName || pathSplit[0] == Name)
{
var userProfile = Path.Combine(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).ToString(), principal.Name);
path = Path.Combine(new string[] { userProfile }.Concat(pathSplit.Skip(1)).ToArray());

found = true;
break;
var userProfile = Path.Combine(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).ToString(), Name);
return Path.Combine(new string[] { userProfile }.Concat(pathSplit.Skip(1)).ToArray());
}
}

if (!found)
{
path = Path.Combine(new string[] { Environment.GetFolderPath(Environment.SpecialFolder.Desktop) }.Concat(pathSplit).ToArray());
}
}
}

return path.Contains('\\') ? File.Exists(path) ? path : null : null;
}

public static string GetProjectFileName()
=> GetProjectNameRaw().Replace("(*)", "");

public static string GetProjectName()
=> Path.GetFileNameWithoutExtension(GetProjectFileName());
return null;
}

public static bool IsProjectModified()
=> GetProjectNameRaw().EndsWith("(*)");
string TryGetDrive()
{
foreach (var (Name, VolumeLabel) in CachedDriveInfos)
{
try
{
if (pathSplit[0] == $"{VolumeLabel} ({Name.Substring(0, 2)})")
{
return Path.Combine(new string[] { Name }.Concat(pathSplit.Skip(1)).ToArray());
}
}
catch (IOException) { }
}

public static bool IsProjectHasPath()
=> GetProjectAbsolutePath() != null;
return null;
}

public static bool IsProjectOpened()
=> AppProcess.MainWindowTitle.Contains('-');
path = TryGetDrive() ?? TryGetProfile() ?? TryGetDesktop();
}

private static string GetProjectNameRaw()
{
var path = AppProcess.MainWindowTitle.Split(new char[] { '-' }, 2)[1].Trim();
return path.Contains('/') ? path.Split('/').Last().Trim() : Path.GetFileName(path);
return path != null ? File.Exists(path) ? Path.GetFullPath(path) : null : null;
}

public static void CopyProjectFileInto(string fileName)
Expand All @@ -94,7 +123,7 @@ public static void CopyProjectFileInto(string fileName)
Directory.CreateDirectory(fileName);
}

File.Copy(GetProjectAbsolutePath(), Path.Combine(fileName, $"{GetProjectName()} - {DateTime.Now.ToString("yyyy.MM.dd - HH.mm.ss")}{Path.GetExtension(GetProjectFileName())}"), true);
File.Copy(GetProjectPath(), Path.Combine(fileName, $"{GetProjectName()} - {DateTime.Now.ToString("yyyy.MM.dd - HH.mm.ss")}{Path.GetExtension(GetProjectPath())}"), true);
}

public static void SaveProject()
Expand Down
Loading

0 comments on commit 3790762

Please sign in to comment.