2016-02-08 21:11:03 +01:00
|
|
|
|
using System;
|
2016-05-19 15:27:07 +02:00
|
|
|
|
using System.Collections.Generic;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
using System.IO;
|
2016-05-19 15:27:07 +02:00
|
|
|
|
using System.Text;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
|
|
|
|
|
namespace Nikse.SubtitleEdit.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configuration settings via Singleton pattern
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Configuration
|
|
|
|
|
{
|
|
|
|
|
private static readonly Lazy<Configuration> Instance = new Lazy<Configuration>(() => new Configuration());
|
|
|
|
|
|
|
|
|
|
private readonly Lazy<Settings> _settings;
|
2016-05-19 15:27:07 +02:00
|
|
|
|
private readonly IEnumerable<Encoding> _encodings;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
|
2016-10-31 02:21:37 +01:00
|
|
|
|
public static readonly string BaseDirectory = GetBaseDirectory();
|
|
|
|
|
public static readonly string DataDirectory = GetDataDirectory();
|
2018-09-23 20:24:39 +02:00
|
|
|
|
public static readonly string TesseractOriginalDirectory = BaseDirectory + "Tesseract302" + Path.DirectorySeparatorChar;
|
2016-10-31 02:21:37 +01:00
|
|
|
|
public static readonly string DictionariesDirectory = DataDirectory + "Dictionaries" + Path.DirectorySeparatorChar;
|
|
|
|
|
public static readonly string SpectrogramsDirectory = DataDirectory + "Spectrograms" + Path.DirectorySeparatorChar;
|
|
|
|
|
public static readonly string SceneChangesDirectory = DataDirectory + "SceneChanges" + Path.DirectorySeparatorChar;
|
|
|
|
|
public static readonly string AutoBackupDirectory = DataDirectory + "AutoBackup" + Path.DirectorySeparatorChar;
|
|
|
|
|
public static readonly string VobSubCompareDirectory = DataDirectory + "VobSub" + Path.DirectorySeparatorChar;
|
2018-03-27 17:24:47 +02:00
|
|
|
|
public static readonly string TesseractDirectory = DataDirectory + "Tesseract4" + Path.DirectorySeparatorChar;
|
2018-09-19 22:22:19 +02:00
|
|
|
|
public static readonly string Tesseract302Directory = DataDirectory + "Tesseract302" + Path.DirectorySeparatorChar;
|
2016-10-31 02:21:37 +01:00
|
|
|
|
public static readonly string WaveformsDirectory = DataDirectory + "Waveforms" + Path.DirectorySeparatorChar;
|
|
|
|
|
public static readonly string PluginsDirectory = DataDirectory + "Plugins" + Path.DirectorySeparatorChar;
|
|
|
|
|
public static readonly string IconsDirectory = BaseDirectory + "Icons" + Path.DirectorySeparatorChar;
|
|
|
|
|
public static readonly string OcrDirectory = DataDirectory + "Ocr" + Path.DirectorySeparatorChar;
|
2016-11-03 19:29:49 +01:00
|
|
|
|
public static readonly string SettingsFileName = DataDirectory + "Settings.xml";
|
2016-10-31 02:21:37 +01:00
|
|
|
|
public static readonly string TesseractDataDirectory = GetTesseractDataDirectory();
|
2018-09-19 22:22:19 +02:00
|
|
|
|
public static readonly string Tesseract302DataDirectory = GetTesseract302DataDirectory();
|
2016-10-31 02:21:37 +01:00
|
|
|
|
|
|
|
|
|
|
2016-02-08 21:11:03 +01:00
|
|
|
|
private Configuration()
|
|
|
|
|
{
|
2016-05-19 15:27:07 +02:00
|
|
|
|
_encodings = GetAvailableEncodings();
|
2016-02-08 21:11:03 +01:00
|
|
|
|
_settings = new Lazy<Settings>(Settings.GetSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsRunningOnLinux()
|
|
|
|
|
{
|
|
|
|
|
return Environment.OSVersion.Platform == PlatformID.Unix && !IsRunningOnMac();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsRunningOnMac()
|
|
|
|
|
{
|
|
|
|
|
// Current versions of Mono report the platform as Unix
|
|
|
|
|
return Environment.OSVersion.Platform == PlatformID.MacOSX ||
|
|
|
|
|
(Environment.OSVersion.Platform == PlatformID.Unix &&
|
|
|
|
|
Directory.Exists("/Applications") &&
|
|
|
|
|
Directory.Exists("/System") &&
|
|
|
|
|
Directory.Exists("/Users"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-27 17:24:47 +02:00
|
|
|
|
public static Settings Settings => Instance.Value._settings.Value;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
|
2018-03-27 17:24:47 +02:00
|
|
|
|
public static IEnumerable<Encoding> AvailableEncodings => Instance.Value._encodings;
|
2016-05-19 15:27:07 +02:00
|
|
|
|
|
2016-02-08 21:11:03 +01:00
|
|
|
|
private static string GetInstallerPath()
|
|
|
|
|
{
|
|
|
|
|
const string valueName = "InstallLocation";
|
|
|
|
|
var value = RegistryUtil.GetValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\SubtitleEdit_is1", valueName);
|
|
|
|
|
if (value != null && Directory.Exists(value))
|
|
|
|
|
{
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value = RegistryUtil.GetValue(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\SubtitleEdit_is1", valueName);
|
|
|
|
|
if (value != null && Directory.Exists(value))
|
|
|
|
|
{
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetBaseDirectory()
|
|
|
|
|
{
|
|
|
|
|
var assembly = System.Reflection.Assembly.GetEntryAssembly();
|
|
|
|
|
var baseDirectory = Path.GetDirectoryName(assembly == null
|
|
|
|
|
? System.Reflection.Assembly.GetExecutingAssembly().Location
|
|
|
|
|
: assembly.Location);
|
|
|
|
|
|
|
|
|
|
return baseDirectory.EndsWith(Path.DirectorySeparatorChar)
|
|
|
|
|
? baseDirectory
|
|
|
|
|
: baseDirectory + Path.DirectorySeparatorChar;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 02:21:37 +01:00
|
|
|
|
private static string GetDataDirectory()
|
2016-02-08 21:11:03 +01:00
|
|
|
|
{
|
2016-04-26 10:17:35 +02:00
|
|
|
|
var appDataRoamingPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Subtitle Edit");
|
|
|
|
|
|
2016-02-08 21:11:03 +01:00
|
|
|
|
if (IsRunningOnLinux() || IsRunningOnMac())
|
|
|
|
|
{
|
2016-10-31 02:21:37 +01:00
|
|
|
|
if (!Directory.Exists(appDataRoamingPath) && !File.Exists(Path.Combine(BaseDirectory, ".PACKAGE-MANAGER")))
|
2016-04-26 10:17:35 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-10-31 02:21:37 +01:00
|
|
|
|
var path = Path.Combine(Directory.CreateDirectory(Path.Combine(BaseDirectory, "Dictionaries")).FullName, "not-a-word-list");
|
2016-04-26 10:17:35 +02:00
|
|
|
|
File.Create(path).Close();
|
|
|
|
|
File.Delete(path);
|
2016-10-31 02:21:37 +01:00
|
|
|
|
return BaseDirectory; // user installation
|
2016-04-26 10:17:35 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2018-03-27 17:24:47 +02:00
|
|
|
|
// ignored
|
2016-04-26 10:17:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(appDataRoamingPath, "Dictionaries"));
|
|
|
|
|
return appDataRoamingPath + Path.DirectorySeparatorChar; // system installation
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var installerPath = GetInstallerPath();
|
2016-10-31 02:21:37 +01:00
|
|
|
|
var hasUninstallFiles = Directory.GetFiles(BaseDirectory, "unins*.*").Length > 0;
|
|
|
|
|
var hasDictionaryFolder = Directory.Exists(Path.Combine(BaseDirectory, "Dictionaries"));
|
2016-02-08 21:11:03 +01:00
|
|
|
|
|
2016-10-31 02:21:37 +01:00
|
|
|
|
if ((installerPath == null || !installerPath.TrimEnd(Path.DirectorySeparatorChar).Equals(BaseDirectory.TrimEnd(Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase))
|
2016-02-08 21:11:03 +01:00
|
|
|
|
&& !hasUninstallFiles && (hasDictionaryFolder || !Directory.Exists(Path.Combine(appDataRoamingPath, "Dictionaries"))))
|
|
|
|
|
{
|
2016-10-31 02:21:37 +01:00
|
|
|
|
return BaseDirectory;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(appDataRoamingPath))
|
|
|
|
|
{
|
|
|
|
|
return appDataRoamingPath + Path.DirectorySeparatorChar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(appDataRoamingPath);
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(appDataRoamingPath, "Dictionaries"));
|
|
|
|
|
return appDataRoamingPath + Path.DirectorySeparatorChar;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Please re-install Subtitle Edit (installer version)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 02:21:37 +01:00
|
|
|
|
private static string GetTesseractDataDirectory()
|
|
|
|
|
{
|
|
|
|
|
if (IsRunningOnLinux() || IsRunningOnMac())
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists("/usr/share/tesseract-ocr/tessdata"))
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2016-10-31 02:21:37 +01:00
|
|
|
|
return "/usr/share/tesseract-ocr/tessdata";
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 02:21:37 +01:00
|
|
|
|
if (Directory.Exists("/usr/share/tesseract/tessdata"))
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2016-10-31 02:21:37 +01:00
|
|
|
|
return "/usr/share/tesseract/tessdata";
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 02:21:37 +01:00
|
|
|
|
if (Directory.Exists("/usr/share/tessdata"))
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2016-10-31 02:21:37 +01:00
|
|
|
|
return "/usr/share/tessdata";
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
2016-10-31 02:21:37 +01:00
|
|
|
|
}
|
2017-03-29 20:47:31 +02:00
|
|
|
|
return Path.Combine(TesseractDirectory, "tessdata");
|
2016-10-31 02:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 22:22:19 +02:00
|
|
|
|
private static string GetTesseract302DataDirectory()
|
|
|
|
|
{
|
|
|
|
|
if (IsRunningOnLinux() || IsRunningOnMac())
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists("/usr/share/tesseract-ocr/tessdata"))
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2018-09-19 22:22:19 +02:00
|
|
|
|
return "/usr/share/tesseract-ocr/tessdata";
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 22:22:19 +02:00
|
|
|
|
if (Directory.Exists("/usr/share/tesseract/tessdata"))
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2018-09-19 22:22:19 +02:00
|
|
|
|
return "/usr/share/tesseract/tessdata";
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 22:22:19 +02:00
|
|
|
|
if (Directory.Exists("/usr/share/tessdata"))
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2018-09-19 22:22:19 +02:00
|
|
|
|
return "/usr/share/tessdata";
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
2018-09-19 22:22:19 +02:00
|
|
|
|
}
|
|
|
|
|
return Path.Combine(Tesseract302Directory, "tessdata");
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 15:27:07 +02:00
|
|
|
|
private static IEnumerable<Encoding> GetAvailableEncodings()
|
|
|
|
|
{
|
|
|
|
|
var encodings = new List<Encoding>();
|
|
|
|
|
foreach (var ei in Encoding.GetEncodings())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
encodings.Add(Encoding.GetEncoding(ei.CodePage));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// though advertised, this code page is not supported
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-05 19:09:34 +02:00
|
|
|
|
return encodings;
|
2016-05-19 15:27:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|