258 lines
8.8 KiB
C#
Raw Normal View History

2015-04-06 12:50:22 +02:00
// <copyright file="FileConverterExtension.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
namespace FileConverterExtension
{
using System;
using System.Collections.Generic;
2015-04-06 12:50:22 +02:00
using System.Diagnostics;
using System.IO;
using System.Linq;
2015-04-06 12:50:22 +02:00
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;
[ComVisible(true), Guid("AF9B72B5-F4E4-44B0-A3D9-B55B748EFE90")]
[COMServerAssociation(AssociationType.AllFiles)]
public class FileConverterExtension : SharpContextMenu
{
private string fileConverterPath;
private RegistryKey fileConverterRegistryKey;
private List<PresetDefinition> presetList = new List<PresetDefinition>();
private RegistryKey FileConverterRegistryKey
{
get
{
if (this.fileConverterRegistryKey == null)
{
this.fileConverterRegistryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter");
if (this.fileConverterRegistryKey == null)
{
throw new Exception("Can't retrieve file converter registry entry.");
}
}
return this.fileConverterRegistryKey;
}
}
private string FileConverterPath
{
get
{
if (string.IsNullOrEmpty(this.fileConverterPath))
{
this.fileConverterPath = this.FileConverterRegistryKey.GetValue("Path") as string;
}
return this.fileConverterPath;
}
}
2015-04-06 12:50:22 +02:00
protected override bool CanShowMenu()
{
foreach (string filePath in this.SelectedItemPaths)
{
string extension = Path.GetExtension(filePath);
if (string.IsNullOrEmpty(extension))
{
continue;
}
extension = extension.Substring(1).ToLowerInvariant();
switch (extension)
{
case "ape":
2015-04-06 12:50:22 +02:00
case "mp3":
case "wav":
case "ogg":
case "flac":
2015-04-06 20:10:39 +02:00
case "wma":
return true;
2015-04-06 12:50:22 +02:00
}
}
return false;
}
protected override ContextMenuStrip CreateMenu()
{
this.RefreshPresetList();
2015-04-06 12:50:22 +02:00
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem fileConverterItem = new ToolStripMenuItem
{
2015-05-27 10:05:22 +02:00
Text = "File Converter",
2015-05-27 10:48:38 +02:00
Image = Properties.Resources.ApplicationIcon_16x16.ToBitmap(),
2015-04-06 12:50:22 +02:00
};
for (int index = 0; index < this.presetList.Count; index++)
2015-04-06 12:50:22 +02:00
{
PresetDefinition preset = this.presetList[index];
2015-04-06 12:50:22 +02:00
ToolStripMenuItem subItem = new ToolStripMenuItem
2015-04-06 20:10:39 +02:00
{
Text = preset.Name,
Enabled = preset.Enabled
2015-04-06 20:10:39 +02:00
};
2015-04-06 12:50:22 +02:00
fileConverterItem.DropDownItems.Add(subItem);
subItem.Click += (sender, args) => this.ConvertFiles(preset.Name);
2015-04-06 12:50:22 +02:00
}
if (this.presetList.Count > 0)
2015-04-06 12:50:22 +02:00
{
fileConverterItem.DropDownItems.Add(new ToolStripSeparator());
2015-04-06 12:50:22 +02:00
}
{
ToolStripMenuItem subItem = new ToolStripMenuItem
{
Text = "Settings",
2015-04-06 12:50:22 +02:00
};
fileConverterItem.DropDownItems.Add(subItem);
subItem.Click += (sender, args) => this.OpenSettings();
2015-04-06 12:50:22 +02:00
}
menu.Items.Add(fileConverterItem);
return menu;
}
private void RefreshPresetList()
{
// Retrieve selected files extensions.
List<string> extensions = new List<string>();
foreach (string filePath in this.SelectedItemPaths)
2015-04-06 20:10:39 +02:00
{
string extension = Path.GetExtension(filePath);
if (string.IsNullOrEmpty(extension))
2015-04-06 20:10:39 +02:00
{
continue;
}
2015-04-06 20:10:39 +02:00
extension = extension.Substring(1).ToLowerInvariant();
if (extensions.Contains(extension))
{
continue;
}
extensions.Add(extension);
}
// Compute preset list.
this.presetList.Clear();
RegistryKey fileConverterKey = this.FileConverterRegistryKey;
for (int extensionIndex = 0; extensionIndex < extensions.Count; extensionIndex++)
{
string extension = extensions[extensionIndex];
RegistryKey extensionKey = fileConverterKey.OpenSubKey(extension);
if (extensionKey == null)
{
continue;
}
string presetsString = extensionKey.GetValue("Presets") as string;
if (presetsString == null)
{
continue;
}
string[] presets = presetsString.Split(';');
for (int presetIndex = 0; presetIndex < presets.Length; presetIndex++)
{
string presetName = presets[presetIndex];
PresetDefinition presetDefinition = this.presetList.FirstOrDefault(match => match.Name == presetName);
if (presetDefinition == null)
{
presetDefinition = new PresetDefinition(presetName);
this.presetList.Add(presetDefinition);
}
presetDefinition.ExtensionRefCount++;
}
2015-04-06 20:10:39 +02:00
}
// Update enable states.
for (int index = 0; index < this.presetList.Count; index++)
{
this.presetList[index].Enabled = this.presetList[index].ExtensionRefCount == extensions.Count;
}
}
2015-04-06 20:10:39 +02:00
private void OpenSettings()
{
if (string.IsNullOrEmpty(this.FileConverterPath))
{
MessageBox.Show(string.Format("Can't retrieve the file converter executable path. You should try to reinstall the application."));
return;
}
2015-04-06 12:50:22 +02:00
if (!File.Exists(this.FileConverterPath))
2015-04-06 12:50:22 +02:00
{
MessageBox.Show(string.Format("Can't find the file converter executable ({0}). You should try to reinstall the application.", this.FileConverterPath));
return;
2015-04-06 12:50:22 +02:00
}
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath);
processStartInfo.CreateNoWindow = false;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = false;
// Build arguments string.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("--settings");
processStartInfo.Arguments = stringBuilder.ToString();
Process exeProcess = Process.Start(processStartInfo);
2015-04-06 12:50:22 +02:00
}
private void ConvertFiles(string presetName)
2015-04-06 12:50:22 +02:00
{
if (string.IsNullOrEmpty(this.FileConverterPath))
2015-04-06 12:50:22 +02:00
{
MessageBox.Show(string.Format("Can't retrieve the file converter executable path. You should try to reinstall the application."));
return;
}
if (!File.Exists(this.FileConverterPath))
2015-04-06 12:50:22 +02:00
{
MessageBox.Show(string.Format("Can't find the file converter executable ({0}). You should try to reinstall the application.", this.FileConverterPath));
2015-04-06 12:50:22 +02:00
return;
}
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath);
2015-04-06 12:50:22 +02:00
processStartInfo.CreateNoWindow = false;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = false;
// Build arguments string.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("--conversion-preset ");
stringBuilder.Append(" \"");
stringBuilder.Append(presetName);
stringBuilder.Append("\"");
2015-04-06 12:50:22 +02:00
foreach (var filePath in this.SelectedItemPaths)
{
stringBuilder.Append(" \"");
stringBuilder.Append(filePath);
stringBuilder.Append("\"");
}
processStartInfo.Arguments = stringBuilder.ToString();
Process exeProcess = Process.Start(processStartInfo);
}
}
}