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

Commit

Permalink
Moves processing logic to craxcellibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
petemc89 committed Sep 23, 2020
1 parent c0401c1 commit b6e26be
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 77 deletions.
53 changes: 38 additions & 15 deletions CraxcelLibrary/CraxcelProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using craXcel;
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using static CraxcelLibrary.Enums;
using CraxcelLibrary.Interfaces;

namespace CraxcelLibrary
{
Expand All @@ -10,30 +12,51 @@ namespace CraxcelLibrary
/// </summary>
public static class CraxcelProcessor
{

/// <summary>
/// Unlocks the specified file within the option parameters set by the user.
/// Main entry point from the UI.
/// </summary>
/// <param name="filePath">The filepath of the file to unlock.</param>
/// <returns>Bool stating if the file was successfully unlocked.</returns>
public static bool UnlockFile(string filePath, Logger logger)
/// <param name="filePaths">The file paths of those files being unlocked.</param>
/// <returns>The number of successfully unlocked files.</returns>
public static int UnlockFiles(List<string> filePaths, ILogger logger = null)
{
try
if (logger == null)
{
logger = new TextFileLogger();
}

logger.Add($"craXcel started");
logger.Add($"{filePaths.Count} files selected");

int filesUnlocked = 0;

foreach (var filePath in filePaths)
{
var file = new FileInfo(filePath);

SupportedApplication application = IdentifyApplication(file);
try
{
SupportedApplication application = IdentifyApplication(file);

ILockedFile lockedFile = CreateLockedFileInstance(file, application);
ILockedFile lockedFile = CreateLockedFileInstance(file, application);

lockedFile.Unlock();
lockedFile.Unlock();

return true;
}
catch
{
return false;
filesUnlocked++;

logger.Add($"Successfully unlocked: {file.Name} ({file.FullName})");
}
catch
{
logger.Add($"Failed to unlock: {file.Name} ({file.FullName})");
}
}

logger.Add($"{filesUnlocked}/{filePaths.Count} files unlocked");
logger.Add("craXcel finished");

logger.Save();

return filesUnlocked;
}

private static SupportedApplication IdentifyApplication(FileInfo file)
Expand Down
15 changes: 15 additions & 0 deletions CraxcelLibrary/Interfaces/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CraxcelLibrary.Interfaces
{
public interface ILogger
{
public List<string> Log { get; }

public void Add(string logMessage);

public void Save();
}
}
7 changes: 4 additions & 3 deletions CraxcelLibrary/Logger.cs → CraxcelLibrary/TextFileLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CraxcelLibrary.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand All @@ -8,13 +9,13 @@ namespace CraxcelLibrary
/// <summary>
/// Basic logger, writing lines to a list of strings and then outputting that list to a text file.
/// </summary>
public class Logger
public class TextFileLogger : ILogger
{
public List<string> Log { get; } = new List<string>();

public FileInfo LogFile { get; }

public Logger()
public TextFileLogger()
{
var dateTime = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var fileName = $"log_{dateTime}.txt";
Expand Down
32 changes: 18 additions & 14 deletions FormUI/ApplicationForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 38 additions & 45 deletions FormUI/ApplicationForm.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using CraxcelLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
Expand All @@ -10,12 +12,9 @@ namespace FormUI
{
public partial class ApplicationForm : Form
{
private DirectoryInfo UserHomeDirectory { get; }

public ApplicationForm()
{
InitializeComponent();
UserHomeDirectory = ApplicationSettings.CRAXCEL_DIR;
}

private void addFilesButton_Click(object sender, EventArgs e)
Expand All @@ -24,7 +23,7 @@ private void addFilesButton_Click(object sender, EventArgs e)

OpenFileDialog openFileDialog = new OpenFileDialog()
{
InitialDirectory = UserHomeDirectory.FullName,
InitialDirectory = ApplicationSettings.CRAXCEL_DIR.FullName,
Multiselect = true,
Filter = SupportedApplicationsFileDialogFilter()
};
Expand Down Expand Up @@ -92,53 +91,35 @@ private void unlockFilesButton_Click(object sender, EventArgs e)
return;
}

progressBar.Maximum = fileListBox.Items.Count;

var logger = new Logger();
logger.Add($"craXcel started");
logger.Add($"{fileListBox.Items.Count} files selected");
List<string> filePaths = fileListBox.Items.OfType<string>().ToList();

var filesUnlocked = 0;
var logger = new TextFileLogger();

foreach (var item in fileListBox.Items)
{
var filePath = item.ToString();
var file = new FileInfo(filePath);
int filesUnlocked = CraxcelProcessor.UnlockFiles(filePaths, logger);

var wasSuccessful = CraxcelProcessor.UnlockFile(filePath, logger);
MessageBox.Show($"{filesUnlocked}/{filePaths.Count} files unlocked.", "Complete");

if (wasSuccessful)
// TO-DO - Possibly only works on Windows?
try
{
var craxcelDir = new ProcessStartInfo(ApplicationSettings.CRAXCEL_DIR.FullName)
{
logger.Add($"Successfully unlocked: {file.Name} ({file.FullName})");
filesUnlocked++;
}
else
UseShellExecute = true,
Verb = "open"
};

var logFile = new ProcessStartInfo(logger.LogFile.FullName)
{
logger.Add($"Failed to unlock: {file.Name} ({file.FullName})");
}
UseShellExecute = true,
Verb = "open"
};

progressBar.Value++;
Process.Start(craxcelDir);
Process.Start(logFile);
}

logger.Add($"{filesUnlocked}/{fileListBox.Items.Count} files unlocked");
logger.Add("craXcel finished");

logger.Save();

MessageBox.Show($"{filesUnlocked}/{fileListBox.Items.Count} files unlocked.", "Complete");

// TO-DO - Implement additional OS support
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
try
{
Process.Start("explorer.exe", ApplicationSettings.CRAXCEL_DIR.FullName);
Process.Start("notepad.exe", logger.LogFile.FullName);
}
catch
{

}
catch
{

}

ResetForm();
Expand All @@ -150,13 +131,12 @@ private void openOptionsFormButton_Click(object sender, EventArgs e)
{
var optionsForm = new UserOptionsForm();

optionsForm.Show();
optionsForm.ShowDialog();
}

private void ResetForm()
{
fileListBox.Items.Clear();
progressBar.Value = 0;
}

private void ToggleAllButtons(bool enableButtons)
Expand All @@ -170,5 +150,18 @@ private void ToggleAllButtons(bool enableButtons)
}
}
}

private void githubLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var info = new ProcessStartInfo("https://github.com/petemc89/craXcel-desktop")
{
UseShellExecute = true,
Verb = "open"
};

Process.Start(info);

//Process.Start("https://github.com/petemc89/craXcel-desktop");
}
}
}
15 changes: 15 additions & 0 deletions FormUI/FormUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,19 @@
<ProjectReference Include="..\CraxcelLibrary\CraxcelLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

</Project>
Loading

0 comments on commit b6e26be

Please sign in to comment.