ShareX/ShareX/Program.cs

608 lines
20 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2015-08-13 13:07:38 +12:00
Copyright (c) 2007-2015 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
using ShareX.Properties;
2014-12-11 12:19:28 +13:00
using ShareX.UploadersLib;
using SingleInstanceApplication;
2013-11-03 23:53:49 +13:00
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace ShareX
{
internal static class Program
{
2015-09-12 12:09:05 +12:00
public static ShareXBuild Build
{
get
{
#if STEAM
2015-09-12 12:09:05 +12:00
return ShareXBuild.Steam;
#elif RELEASE
return ShareXBuild.Release;
#elif DEBUG
2015-09-12 12:09:05 +12:00
return ShareXBuild.Debug;
#else
return ShareXBuild.Unknown;
#endif
}
}
2015-09-30 00:43:41 +13:00
public static bool IsBeta { get; } = false;
2014-09-14 01:50:44 +12:00
public static string Title
{
get
{
Version version = Version.Parse(Application.ProductVersion);
string title = string.Format("ShareX {0}.{1}", version.Major, version.Minor);
if (version.Build > 0) title += "." + version.Build;
if (IsPortable) title += " Portable";
if (IsBeta) title += " Beta";
return title;
}
}
2014-11-07 00:27:23 +13:00
public static CLIManager CLI { get; private set; }
2014-09-14 01:50:44 +12:00
public static bool IsMultiInstance { get; private set; }
public static bool IsPortable { get; private set; }
public static bool IsSilentRun { get; private set; }
public static bool IsSandbox { get; private set; }
2015-09-11 12:45:40 +12:00
public static bool IsFirstTimeConfig { get; private set; }
2015-09-30 05:58:38 +13:00
public static bool NoHotkeys { get; private set; }
2014-09-14 01:50:44 +12:00
public static ApplicationConfig Settings { get; private set; }
public static TaskSettings DefaultTaskSettings { get; private set; }
public static UploadersConfig UploadersConfig { get; private set; }
public static HotkeysConfig HotkeysConfig { get; private set; }
public static ManualResetEvent UploaderSettingsResetEvent { get; private set; }
public static ManualResetEvent HotkeySettingsResetEvent { get; private set; }
public static MainForm MainForm { get; private set; }
public static Stopwatch StartTimer { get; private set; }
public static HotkeyManager HotkeyManager { get; set; }
public static WatchFolderManager WatchFolderManager { get; set; }
2013-11-03 23:53:49 +13:00
#region Paths
public static readonly string StartupPath = Application.StartupPath;
public static readonly string DefaultPersonalPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ShareX");
private static readonly string PortablePersonalPath = Path.Combine(StartupPath, "ShareX");
2013-11-03 23:53:49 +13:00
private static readonly string PersonalPathConfig = Path.Combine(StartupPath, "PersonalPath.cfg");
private static readonly string ApplicationConfigFilename = "ApplicationConfig.json";
private static readonly string UploadersConfigFilename = "UploadersConfig.json";
private static readonly string HotkeysConfigFilename = "HotkeysConfig.json";
private static readonly string HistoryFilename = "History.xml";
private static readonly string LogFileName = "ShareX-Log-{0:yyyy-MM}.txt";
2013-11-03 23:53:49 +13:00
public static string CustomPersonalPath { get; private set; }
private static FileSystemWatcher uploaderConfigWatcher;
private static WatchFolderDuplicateEventTimer uploaderConfigWatcherTimer;
2013-11-03 23:53:49 +13:00
public static string PersonalPath
{
get
{
2014-10-05 06:59:46 +13:00
if (!string.IsNullOrEmpty(CustomPersonalPath))
2013-11-03 23:53:49 +13:00
{
return CustomPersonalPath;
}
return DefaultPersonalPath;
}
}
public static string ApplicationConfigFilePath
{
get
{
if (!IsSandbox)
{
return Path.Combine(PersonalPath, ApplicationConfigFilename);
}
return null;
}
}
2014-03-29 01:22:16 +13:00
private static string UploadersConfigFolder
{
get
{
if (Settings != null && !string.IsNullOrEmpty(Settings.CustomUploadersConfigPath))
{
return Settings.CustomUploadersConfigPath;
}
return PersonalPath;
}
}
2013-11-03 23:53:49 +13:00
public static string UploadersConfigFilePath
{
get
{
if (!IsSandbox)
{
2014-03-29 01:22:16 +13:00
return Path.Combine(UploadersConfigFolder, UploadersConfigFilename);
2013-11-03 23:53:49 +13:00
}
return null;
}
}
2014-03-29 01:22:16 +13:00
private static string HotkeysConfigFolder
{
get
{
if (Settings != null && !string.IsNullOrEmpty(Settings.CustomHotkeysConfigPath))
{
return Settings.CustomHotkeysConfigPath;
}
return PersonalPath;
}
}
2013-11-03 23:53:49 +13:00
public static string HotkeysConfigFilePath
{
get
{
if (!IsSandbox)
{
2014-03-29 01:22:16 +13:00
return Path.Combine(HotkeysConfigFolder, HotkeysConfigFilename);
2013-11-03 23:53:49 +13:00
}
return null;
}
}
public static string HistoryFilePath
{
get
{
if (!IsSandbox)
2013-11-03 23:53:49 +13:00
{
return Path.Combine(PersonalPath, HistoryFilename);
2013-11-03 23:53:49 +13:00
}
return null;
2013-11-03 23:53:49 +13:00
}
}
2015-08-27 12:49:03 +12:00
private static string LogsFolder => Path.Combine(PersonalPath, "Logs");
2013-11-03 23:53:49 +13:00
2014-03-29 01:22:16 +13:00
public static string LogsFilePath
2013-11-03 23:53:49 +13:00
{
get
{
string filename = string.Format(LogFileName, DateTime.Now);
2014-03-29 01:22:16 +13:00
return Path.Combine(LogsFolder, filename);
2013-11-03 23:53:49 +13:00
}
}
public static string ScreenshotsParentFolder
2013-11-03 23:53:49 +13:00
{
get
{
if (Settings != null && Settings.UseCustomScreenshotsPath && !string.IsNullOrEmpty(Settings.CustomScreenshotsPath))
{
return Environment.ExpandEnvironmentVariables(Settings.CustomScreenshotsPath);
2013-11-03 23:53:49 +13:00
}
return Path.Combine(PersonalPath, "Screenshots");
}
}
public static string ScreenshotsFolder
2013-11-03 23:53:49 +13:00
{
get
{
string subFolderName = NameParser.Parse(NameParserType.FolderPath, Settings.SaveImageSubFolderPattern);
return Path.Combine(ScreenshotsParentFolder, subFolderName);
2013-11-03 23:53:49 +13:00
}
}
2015-08-27 12:49:03 +12:00
public static string ScreenRecorderCacheFilePath => Path.Combine(PersonalPath, "ScreenRecorder.avi");
2013-11-03 23:53:49 +13:00
2015-08-27 12:49:03 +12:00
private static string BackupFolder => Path.Combine(PersonalPath, "Backup");
public static string ToolsFolder => Path.Combine(PersonalPath, "Tools");
2013-11-03 23:53:49 +13:00
2015-08-27 13:13:43 +12:00
public static string ChromeHostManifestPath => Path.Combine(ToolsFolder, "Chrome-host-manifest.json");
public static string ChromeHostPath => Helpers.GetAbsolutePath("ShareX_Chrome.exe");
2015-10-01 23:28:08 +13:00
public static string SteamInAppPath => Helpers.GetAbsolutePath("Steam");
2013-11-03 23:53:49 +13:00
#endregion Paths
private static bool restarting;
2013-11-03 23:53:49 +13:00
[STAThread]
private static void Main(string[] args)
{
2015-10-10 07:15:29 +13:00
DebugHelper.Init(LogsFilePath);
2013-11-03 23:53:49 +13:00
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
StartTimer = Stopwatch.StartNew(); // For be able to show startup time
2014-11-07 00:27:23 +13:00
CLI = new CLIManager(args);
2014-11-07 01:40:42 +13:00
CLI.ParseCommands();
2014-09-07 07:13:21 +12:00
2015-09-12 11:05:31 +12:00
#if STEAM
if (CheckUninstall()) return; // Steam will run ShareX with -Uninstall when uninstalling
#endif
2014-09-07 07:13:21 +12:00
if (CheckAdminTasks()) return; // If ShareX opened just for be able to execute task as Admin
2014-11-07 00:27:23 +13:00
IsMultiInstance = CLI.IsCommandExist("multi", "m");
2014-11-07 00:27:23 +13:00
if (IsMultiInstance || ApplicationInstanceManager.CreateSingleInstance(SingleInstanceCallback, args))
{
2014-09-14 01:50:44 +12:00
using (Mutex mutex = new Mutex(false, "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC")) // Required for installer
{
Run();
}
if (restarting)
{
Process.Start(Application.ExecutablePath);
}
}
2013-11-03 23:53:49 +13:00
}
private static void Run()
2013-11-03 23:53:49 +13:00
{
2014-10-05 06:59:46 +13:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2014-11-07 00:27:23 +13:00
IsSilentRun = CLI.IsCommandExist("silent", "s");
IsSandbox = CLI.IsCommandExist("sandbox");
2013-11-03 23:53:49 +13:00
2014-09-14 01:50:44 +12:00
if (!IsSandbox)
2013-11-03 23:53:49 +13:00
{
2014-11-07 00:27:23 +13:00
IsPortable = CLI.IsCommandExist("portable", "p");
2013-11-03 23:53:49 +13:00
2014-09-14 01:50:44 +12:00
if (IsPortable)
2013-11-03 23:53:49 +13:00
{
2014-09-14 01:50:44 +12:00
CustomPersonalPath = PortablePersonalPath;
}
else
{
CheckPersonalPathConfig();
}
2013-11-03 23:53:49 +13:00
2014-10-05 06:59:46 +13:00
if (!Directory.Exists(PersonalPath))
2014-09-14 01:50:44 +12:00
{
2014-10-05 06:59:46 +13:00
try
{
Directory.CreateDirectory(PersonalPath);
}
catch (Exception e)
{
2014-10-19 10:48:47 +13:00
MessageBox.Show(Resources.Program_Run_Unable_to_create_folder_ + string.Format(" \"{0}\"\r\n\r\n{1}", PersonalPath, e),
"ShareX - " + Resources.Program_Run_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
2014-10-05 06:59:46 +13:00
CustomPersonalPath = "";
}
2013-11-03 23:53:49 +13:00
}
2014-09-14 01:50:44 +12:00
}
2013-11-03 23:53:49 +13:00
2015-09-30 05:58:38 +13:00
#if STEAM
2015-09-11 12:45:40 +12:00
IsFirstTimeConfig = CLI.IsCommandExist("SteamConfig");
2015-09-30 05:58:38 +13:00
#endif
NoHotkeys = CLI.IsCommandExist("NoHotkeys");
2015-09-11 12:45:40 +12:00
2015-09-12 12:09:05 +12:00
DebugHelper.WriteLine($"{Title} {Build} build");
2014-09-14 01:50:44 +12:00
DebugHelper.WriteLine("Operating system: " + Environment.OSVersion.VersionString);
DebugHelper.WriteLine("Command line: " + Environment.CommandLine);
DebugHelper.WriteLine("Personal path: " + PersonalPath);
LoadProgramSettings();
2014-09-14 01:50:44 +12:00
UploaderSettingsResetEvent = new ManualResetEvent(false);
HotkeySettingsResetEvent = new ManualResetEvent(false);
2014-10-19 10:48:47 +13:00
TaskEx.Run(LoadSettings);
2013-11-03 23:53:49 +13:00
LanguageHelper.ChangeLanguage(Settings.Language);
2014-09-14 01:50:44 +12:00
DebugHelper.WriteLine("MainForm init started");
MainForm = new MainForm();
DebugHelper.WriteLine("MainForm init finished");
2013-11-03 23:53:49 +13:00
2014-09-14 01:50:44 +12:00
Application.Run(MainForm);
if (WatchFolderManager != null) WatchFolderManager.Dispose();
SaveSettings();
BackupSettings();
DebugHelper.WriteLine("ShareX closing");
}
public static void Restart()
{
restarting = true;
Application.Exit();
}
2014-09-14 01:50:44 +12:00
private static void SingleInstanceCallback(object sender, InstanceCallbackEventArgs args)
{
if (WaitFormLoad(5000))
{
Action d = () =>
2013-11-03 23:53:49 +13:00
{
2014-09-14 01:50:44 +12:00
if (args.CommandLineArgs == null || args.CommandLineArgs.Length < 1)
{
if (MainForm.niTray != null && MainForm.niTray.Visible)
{
// Workaround for Windows startup tray icon bug
MainForm.niTray.Visible = false;
MainForm.niTray.Visible = true;
}
MainForm.ShowActivate();
}
else if (MainForm.Visible)
{
MainForm.ShowActivate();
}
2014-11-07 00:27:23 +13:00
CLIManager cli = new CLIManager(args.CommandLineArgs);
2014-11-07 01:40:42 +13:00
cli.ParseCommands();
2014-11-07 00:27:23 +13:00
MainForm.UseCommandLineArgs(cli.Commands);
2014-09-14 01:50:44 +12:00
};
MainForm.InvokeSafe(d);
}
}
2013-11-03 23:53:49 +13:00
2014-09-14 01:50:44 +12:00
private static bool WaitFormLoad(int wait)
{
Stopwatch timer = Stopwatch.StartNew();
2014-08-07 14:22:45 +12:00
2014-09-14 01:50:44 +12:00
while (timer.ElapsedMilliseconds < wait)
{
if (MainForm != null && MainForm.IsReady) return true;
2014-08-07 14:22:45 +12:00
2014-09-14 01:50:44 +12:00
Thread.Sleep(10);
2014-08-07 14:22:45 +12:00
}
2014-09-14 01:50:44 +12:00
return false;
}
2013-11-03 23:53:49 +13:00
public static void LoadSettings()
{
LoadUploadersConfig();
UploaderSettingsResetEvent.Set();
LoadHotkeySettings();
HotkeySettingsResetEvent.Set();
ConfigureUploadersConfigWatcher();
2013-11-03 23:53:49 +13:00
}
public static void LoadProgramSettings()
{
Settings = ApplicationConfig.Load(ApplicationConfigFilePath);
DefaultTaskSettings = Settings.DefaultTaskSettings;
}
public static void LoadUploadersConfig()
{
UploadersConfig = UploadersConfig.Load(UploadersConfigFilePath);
}
public static void LoadHotkeySettings()
{
HotkeysConfig = HotkeysConfig.Load(HotkeysConfigFilePath);
}
public static void SaveSettings()
{
if (Settings != null) Settings.Save(ApplicationConfigFilePath);
if (UploadersConfig != null) UploadersConfig.Save(UploadersConfigFilePath);
if (HotkeysConfig != null) HotkeysConfig.Save(HotkeysConfigFilePath);
}
public static void SaveSettingsAsync()
{
if (Settings != null) Settings.SaveAsync(ApplicationConfigFilePath);
UploadersConfigSaveAsync();
if (HotkeysConfig != null) HotkeysConfig.SaveAsync(HotkeysConfigFilePath);
}
2013-11-03 23:53:49 +13:00
public static void BackupSettings()
{
Helpers.BackupFileWeekly(ApplicationConfigFilePath, BackupFolder);
Helpers.BackupFileWeekly(HotkeysConfigFilePath, BackupFolder);
Helpers.BackupFileWeekly(UploadersConfigFilePath, BackupFolder);
Helpers.BackupFileWeekly(HistoryFilePath, BackupFolder);
}
private static void CheckPersonalPathConfig()
{
string customPersonalPath = ReadPersonalPathConfig();
if (!string.IsNullOrEmpty(customPersonalPath))
{
customPersonalPath = Environment.ExpandEnvironmentVariables(customPersonalPath);
CustomPersonalPath = Helpers.GetAbsolutePath(customPersonalPath);
if (CustomPersonalPath.Equals(PortablePersonalPath, StringComparison.InvariantCultureIgnoreCase))
{
IsPortable = true;
}
}
}
public static string ReadPersonalPathConfig()
2013-11-03 23:53:49 +13:00
{
if (File.Exists(PersonalPathConfig))
{
return File.ReadAllText(PersonalPathConfig, Encoding.UTF8).Trim();
}
2013-11-03 23:53:49 +13:00
return string.Empty;
}
public static void WritePersonalPathConfig(string path)
{
if (path == null)
{
path = string.Empty;
}
else
{
path = path.Trim();
}
bool isDefaultPath = string.IsNullOrEmpty(path) && !File.Exists(PersonalPathConfig);
if (!isDefaultPath)
{
string currentPath = ReadPersonalPathConfig();
if (!path.Equals(currentPath, StringComparison.InvariantCultureIgnoreCase))
{
try
{
File.WriteAllText(PersonalPathConfig, path, Encoding.UTF8);
}
catch (UnauthorizedAccessException)
{
MessageBox.Show(string.Format(Resources.Program_WritePersonalPathConfig_Cant_access_to_file, PersonalPathConfig),
"ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
2013-11-03 23:53:49 +13:00
}
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
OnError(e.Exception);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
2013-11-03 23:53:49 +13:00
{
OnError((Exception)e.ExceptionObject);
}
private static void OnError(Exception e)
{
using (ErrorForm errorForm = new ErrorForm(e, LogsFilePath, Links.URL_ISSUES))
2013-11-03 23:53:49 +13:00
{
errorForm.ShowDialog();
}
}
public static void ConfigureUploadersConfigWatcher()
{
2014-10-19 10:48:47 +13:00
if (Settings.DetectUploaderConfigFileChanges && uploaderConfigWatcher == null)
{
2014-10-19 10:48:47 +13:00
uploaderConfigWatcher = new FileSystemWatcher(Path.GetDirectoryName(UploadersConfigFilePath), Path.GetFileName(UploadersConfigFilePath));
2014-04-16 07:30:46 +12:00
uploaderConfigWatcher.Changed += uploaderConfigWatcher_Changed;
2014-10-19 10:48:47 +13:00
uploaderConfigWatcherTimer = new WatchFolderDuplicateEventTimer(UploadersConfigFilePath);
2014-04-16 07:30:46 +12:00
uploaderConfigWatcher.EnableRaisingEvents = true;
}
2014-04-16 07:30:46 +12:00
else if (uploaderConfigWatcher != null)
{
2014-04-16 07:30:46 +12:00
uploaderConfigWatcher.Dispose();
uploaderConfigWatcher = null;
}
}
private static void uploaderConfigWatcher_Changed(object sender, FileSystemEventArgs e)
{
if (!uploaderConfigWatcherTimer.IsDuplicateEvent(e.FullPath))
{
2014-06-01 18:59:40 +12:00
Action onCompleted = () => ReloadUploadersConfig(e.FullPath);
Helpers.WaitWhileAsync(() => Helpers.IsFileLocked(e.FullPath), 250, 5000, onCompleted, 1000);
uploaderConfigWatcherTimer = new WatchFolderDuplicateEventTimer(e.FullPath);
}
}
private static void ReloadUploadersConfig(string filePath)
{
2014-06-01 18:59:40 +12:00
UploadersConfig = UploadersConfig.Load(filePath);
}
public static void UploadersConfigSaveAsync()
{
if (UploadersConfig != null)
{
if (uploaderConfigWatcher != null) uploaderConfigWatcher.EnableRaisingEvents = false;
TaskEx.Run(() =>
{
UploadersConfig.Save(UploadersConfigFilePath);
},
() =>
{
if (uploaderConfigWatcher != null) uploaderConfigWatcher.EnableRaisingEvents = true;
});
}
}
2014-09-07 07:13:21 +12:00
private static bool CheckAdminTasks()
{
2014-11-07 00:27:23 +13:00
if (CLI.IsCommandExist("dnschanger"))
2014-09-07 07:13:21 +12:00
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DNSChangerForm());
return true;
}
return false;
}
2015-09-12 11:05:31 +12:00
private static bool CheckUninstall()
{
2015-09-12 11:38:10 +12:00
if (CLI.IsCommandExist("uninstall"))
2015-09-12 11:05:31 +12:00
{
try
{
IntegrationHelpers.Uninstall();
}
catch
{
}
return true;
}
return false;
}
2013-11-03 23:53:49 +13:00
}
}