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
|
|
|
|
|
{
|
2015-05-26 12:37:44 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-04-06 12:50:22 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2015-05-26 15:18:14 +02:00
|
|
|
|
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;
|
2015-05-26 12:37:44 +02:00
|
|
|
|
private RegistryKey fileConverterRegistryKey;
|
2015-05-26 15:18:14 +02:00
|
|
|
|
private List<PresetDefinition> presetList = new List<PresetDefinition>();
|
2015-05-26 12:37:44 +02:00
|
|
|
|
|
|
|
|
|
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 "mp3":
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case "wav":
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case "ogg":
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case "flac":
|
|
|
|
|
return true;
|
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()
|
|
|
|
|
{
|
2015-05-26 12:37:44 +02:00
|
|
|
|
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",
|
|
|
|
|
Image = Properties.Resource.ApplicationIcon_16x16.ToBitmap(),
|
2015-04-06 12:50:22 +02:00
|
|
|
|
};
|
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
for (int index = 0; index < this.presetList.Count; index++)
|
2015-04-06 12:50:22 +02:00
|
|
|
|
{
|
2015-05-26 15:18:14 +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
|
|
|
|
{
|
2015-05-26 15:18:14 +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);
|
2015-05-26 15:18:14 +02:00
|
|
|
|
subItem.Click += (sender, args) => this.ConvertFiles(preset.Name);
|
2015-04-06 12:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
if (this.presetList.Count > 0)
|
2015-04-06 12:50:22 +02:00
|
|
|
|
{
|
2015-05-26 12:37:44 +02:00
|
|
|
|
fileConverterItem.DropDownItems.Add(new ToolStripSeparator());
|
2015-04-06 12:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ToolStripMenuItem subItem = new ToolStripMenuItem
|
|
|
|
|
{
|
2015-05-26 12:37:44 +02:00
|
|
|
|
Text = "Settings",
|
2015-04-06 12:50:22 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fileConverterItem.DropDownItems.Add(subItem);
|
2015-05-26 12:37:44 +02:00
|
|
|
|
subItem.Click += (sender, args) => this.OpenSettings();
|
2015-04-06 12:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
menu.Items.Add(fileConverterItem);
|
|
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RefreshPresetList()
|
|
|
|
|
{
|
2015-05-26 15:18:14 +02:00
|
|
|
|
// Retrieve selected files extensions.
|
|
|
|
|
List<string> extensions = new List<string>();
|
2015-05-26 12:37:44 +02:00
|
|
|
|
foreach (string filePath in this.SelectedItemPaths)
|
2015-04-06 20:10:39 +02:00
|
|
|
|
{
|
2015-05-26 12:37:44 +02:00
|
|
|
|
string extension = Path.GetExtension(filePath);
|
|
|
|
|
if (string.IsNullOrEmpty(extension))
|
2015-04-06 20:10:39 +02:00
|
|
|
|
{
|
2015-05-26 12:37:44 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-04-06 20:10:39 +02:00
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
extension = extension.Substring(1).ToLowerInvariant();
|
2015-05-26 15:18:14 +02:00
|
|
|
|
if (extensions.Contains(extension))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extensions.Add(extension);
|
|
|
|
|
}
|
2015-05-26 12:37:44 +02:00
|
|
|
|
|
2015-05-26 15:18:14 +02:00
|
|
|
|
// Compute preset list.
|
|
|
|
|
this.presetList.Clear();
|
|
|
|
|
RegistryKey fileConverterKey = this.FileConverterRegistryKey;
|
|
|
|
|
for (int extensionIndex = 0; extensionIndex < extensions.Count; extensionIndex++)
|
|
|
|
|
{
|
|
|
|
|
string extension = extensions[extensionIndex];
|
2015-05-26 12:37:44 +02:00
|
|
|
|
RegistryKey extensionKey = fileConverterKey.OpenSubKey(extension);
|
|
|
|
|
if (extensionKey == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string presetsString = extensionKey.GetValue("Presets") as string;
|
|
|
|
|
if (presetsString == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] presets = presetsString.Split(';');
|
2015-05-26 15:18:14 +02:00
|
|
|
|
|
|
|
|
|
for (int presetIndex = 0; presetIndex < presets.Length; presetIndex++)
|
2015-05-26 12:37:44 +02:00
|
|
|
|
{
|
2015-05-26 15:18:14 +02:00
|
|
|
|
string presetName = presets[presetIndex];
|
|
|
|
|
PresetDefinition presetDefinition = this.presetList.FirstOrDefault(match => match.Name == presetName);
|
|
|
|
|
if (presetDefinition == null)
|
2015-05-26 12:37:44 +02:00
|
|
|
|
{
|
2015-05-26 15:18:14 +02:00
|
|
|
|
presetDefinition = new PresetDefinition(presetName);
|
|
|
|
|
this.presetList.Add(presetDefinition);
|
2015-05-26 12:37:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 15:18:14 +02:00
|
|
|
|
presetDefinition.ExtensionRefCount++;
|
2015-05-26 12:37:44 +02:00
|
|
|
|
}
|
2015-04-06 20:10:39 +02:00
|
|
|
|
}
|
2015-05-26 15:18:14 +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-05-26 12:37:44 +02:00
|
|
|
|
}
|
2015-04-06 20:10:39 +02:00
|
|
|
|
|
2015-05-26 12:37:44 +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
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
if (!File.Exists(this.FileConverterPath))
|
2015-04-06 12:50:22 +02:00
|
|
|
|
{
|
2015-05-26 12:37:44 +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
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 12:37:44 +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
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
private void ConvertFiles(string presetName)
|
2015-04-06 12:50:22 +02:00
|
|
|
|
{
|
2015-05-26 12:37:44 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
if (!File.Exists(this.FileConverterPath))
|
2015-04-06 12:50:22 +02:00
|
|
|
|
{
|
2015-05-26 12:37:44 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 12:37:44 +02:00
|
|
|
|
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();
|
2015-05-26 12:37:44 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|