2020-07-03 00:24:31 -07:00
|
|
|
using Microsoft.Win32;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2021-01-07 04:34:41 -08:00
|
|
|
using System.Net;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
2021-03-01 18:57:19 -08:00
|
|
|
using System.Text.Json;
|
2021-01-07 04:34:41 -08:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading.Tasks;
|
2020-07-03 00:24:31 -07:00
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
namespace SourceGit {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Application.
|
|
|
|
/// </summary>
|
|
|
|
public partial class App : Application {
|
|
|
|
/// <summary>
|
2020-12-16 19:55:06 -08:00
|
|
|
/// Getter/Setter for application user setting.
|
2020-07-03 00:24:31 -07:00
|
|
|
/// </summary>
|
2020-12-16 19:55:06 -08:00
|
|
|
public static Preference Setting { get; set; }
|
2020-07-03 00:24:31 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Check if GIT has been configured.
|
|
|
|
/// </summary>
|
|
|
|
public static bool IsGitConfigured {
|
|
|
|
get {
|
2020-12-16 19:55:06 -08:00
|
|
|
return !string.IsNullOrEmpty(Setting.Tools.GitExecutable)
|
|
|
|
&& File.Exists(Setting.Tools.GitExecutable);
|
2020-07-03 00:24:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 00:31:03 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Load text from locales.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static string Text(string key) {
|
|
|
|
return Current.FindResource("Text." + key) as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Format text
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key"></param>
|
|
|
|
/// <param name="args"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static string Format(string key, params object[] args) {
|
|
|
|
return string.Format(Text(key), args);
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:24:31 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Raise error message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message"></param>
|
2020-08-06 00:41:25 -07:00
|
|
|
public static void RaiseError(string msg) {
|
2020-08-07 08:40:23 -07:00
|
|
|
Current.Dispatcher.Invoke(() => {
|
|
|
|
(Current.MainWindow as UI.Launcher).Errors.Add(msg);
|
|
|
|
});
|
2020-08-04 18:32:24 -07:00
|
|
|
}
|
|
|
|
|
2020-12-23 04:40:12 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Open repository.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="repo"></param>
|
|
|
|
public static void Open(Git.Repository repo) {
|
|
|
|
(Current.MainWindow as UI.Launcher).Open(repo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Save settings.
|
|
|
|
/// </summary>
|
|
|
|
public static void SaveSetting() {
|
|
|
|
var settingFile = Path.Combine(
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
|
|
"SourceGit",
|
|
|
|
"preference.json");
|
|
|
|
|
|
|
|
var dir = Path.GetDirectoryName(settingFile);
|
|
|
|
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
|
|
|
|
|
2021-03-01 18:57:19 -08:00
|
|
|
var data = JsonSerializer.Serialize(Setting, new JsonSerializerOptions() { WriteIndented = true });
|
2020-12-23 04:40:12 -08:00
|
|
|
File.WriteAllText(settingFile, data);
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:24:31 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Startup event.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
/// <param name="e"></param>
|
|
|
|
private void OnAppStartup(object sender, StartupEventArgs e) {
|
2020-07-22 23:04:42 -07:00
|
|
|
// Use this app as a sequence editor?
|
2020-12-23 04:40:12 -08:00
|
|
|
if (OpenAsEditor(e)) return;
|
2020-07-22 23:04:42 -07:00
|
|
|
|
2020-12-16 19:55:06 -08:00
|
|
|
// Load settings.
|
|
|
|
var settingFile = Path.Combine(
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
|
|
"SourceGit",
|
|
|
|
"preference.json");
|
|
|
|
if (!File.Exists(settingFile)) {
|
|
|
|
Setting = new Preference();
|
|
|
|
} else {
|
2021-03-01 18:57:19 -08:00
|
|
|
Setting = JsonSerializer.Deserialize<Preference>(File.ReadAllText(settingFile));
|
2020-12-16 19:55:06 -08:00
|
|
|
}
|
|
|
|
|
2021-03-31 19:54:05 -07:00
|
|
|
// Make sure avatar cache folder exists
|
|
|
|
if (!Directory.Exists(Helpers.Avatar.CACHE_PATH)) Directory.CreateDirectory(Helpers.Avatar.CACHE_PATH);
|
|
|
|
|
2020-07-03 00:24:31 -07:00
|
|
|
// Try auto configure git via registry.
|
2021-02-06 21:56:22 -08:00
|
|
|
if (Setting == null || !IsGitConfigured) {
|
2020-07-03 00:24:31 -07:00
|
|
|
var root = RegistryKey.OpenBaseKey(
|
|
|
|
RegistryHive.LocalMachine,
|
|
|
|
Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
|
|
|
|
|
|
|
|
var git = root.OpenSubKey("SOFTWARE\\GitForWindows");
|
|
|
|
if (git != null) {
|
2020-12-16 19:55:06 -08:00
|
|
|
Setting.Tools.GitExecutable = Path.Combine(
|
2020-12-23 04:40:12 -08:00
|
|
|
git.GetValue("InstallPath") as string,
|
|
|
|
"bin",
|
2020-07-03 00:24:31 -07:00
|
|
|
"git.exe");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply themes
|
2020-12-16 19:55:06 -08:00
|
|
|
if (Setting.UI.UseLightTheme) {
|
2020-07-03 00:24:31 -07:00
|
|
|
foreach (var rs in Current.Resources.MergedDictionaries) {
|
|
|
|
if (rs.Source != null && rs.Source.OriginalString.StartsWith("pack://application:,,,/Resources/Themes/")) {
|
|
|
|
rs.Source = new Uri("pack://application:,,,/Resources/Themes/Light.xaml", UriKind.Absolute);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 00:31:03 -08:00
|
|
|
// Apply locales
|
|
|
|
if (Setting.UI.Locale != "en_US") {
|
|
|
|
foreach (var rs in Current.Resources.MergedDictionaries) {
|
|
|
|
if (rs.Source != null && rs.Source.OriginalString.StartsWith("pack://application:,,,/Resources/Locales/")) {
|
|
|
|
rs.Source = new Uri($"pack://application:,,,/Resources/Locales/{Setting.UI.Locale}.xaml", UriKind.Absolute);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:24:31 -07:00
|
|
|
// Show main window
|
2021-01-31 17:39:14 -08:00
|
|
|
if (e.Args.Length == 1) {
|
|
|
|
MainWindow = new UI.Launcher(e.Args[0]);
|
|
|
|
} else {
|
|
|
|
MainWindow = new UI.Launcher(null);
|
|
|
|
}
|
2021-01-07 04:34:41 -08:00
|
|
|
MainWindow.Show();
|
|
|
|
|
2021-01-31 17:39:14 -08:00
|
|
|
|
2021-01-07 04:34:41 -08:00
|
|
|
// Check for update.
|
|
|
|
if (Setting.CheckUpdate && Setting.LastCheckUpdate != DateTime.Now.DayOfYear) {
|
|
|
|
Setting.LastCheckUpdate = DateTime.Now.DayOfYear;
|
|
|
|
SaveSetting();
|
|
|
|
Task.Run(CheckUpdate);
|
|
|
|
}
|
2020-07-03 00:24:31 -07:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:21:45 -08:00
|
|
|
/// <summary>
|
2020-12-23 04:40:12 -08:00
|
|
|
/// Deactivated event.
|
2020-11-29 23:21:45 -08:00
|
|
|
/// </summary>
|
2020-12-23 04:40:12 -08:00
|
|
|
/// <param name="sender"></param>
|
|
|
|
/// <param name="e"></param>
|
|
|
|
private void OnAppDeactivated(object sender, EventArgs e) {
|
|
|
|
GC.Collect();
|
2021-01-07 04:34:41 -08:00
|
|
|
SaveSetting();
|
2020-11-29 23:21:45 -08:00
|
|
|
}
|
|
|
|
|
2020-12-16 19:55:06 -08:00
|
|
|
/// <summary>
|
2020-12-23 04:40:12 -08:00
|
|
|
/// Try to open app as git editor
|
2020-12-16 19:55:06 -08:00
|
|
|
/// </summary>
|
2020-12-23 04:40:12 -08:00
|
|
|
/// <param name="e"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
private bool OpenAsEditor(StartupEventArgs e) {
|
|
|
|
if (e.Args.Length < 3) return false;
|
2020-12-16 19:55:06 -08:00
|
|
|
|
2020-12-23 04:40:12 -08:00
|
|
|
switch (e.Args[0]) {
|
|
|
|
case "--sequence":
|
|
|
|
var output = File.CreateText(e.Args[2]);
|
|
|
|
output.Write(File.ReadAllText(e.Args[1]));
|
|
|
|
output.Flush();
|
|
|
|
output.Close();
|
2020-12-16 19:55:06 -08:00
|
|
|
|
2020-12-23 04:40:12 -08:00
|
|
|
Environment.Exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-16 19:55:06 -08:00
|
|
|
|
2020-12-23 04:40:12 -08:00
|
|
|
return true;
|
2020-07-03 00:24:31 -07:00
|
|
|
}
|
2021-01-07 04:34:41 -08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Check for update.
|
|
|
|
/// </summary>
|
|
|
|
private void CheckUpdate() {
|
|
|
|
try {
|
|
|
|
var web = new WebClient() { Encoding = Encoding.UTF8 };
|
|
|
|
var raw = web.DownloadString("https://gitee.com/api/v5/repos/sourcegit/SourceGit/releases/latest");
|
2021-03-01 18:57:19 -08:00
|
|
|
var ver = JsonSerializer.Deserialize<Git.Version>(raw);
|
2021-01-07 04:34:41 -08:00
|
|
|
var cur = Assembly.GetExecutingAssembly().GetName().Version;
|
|
|
|
|
|
|
|
var matches = Regex.Match(ver.TagName, @"^v(\d+)\.(\d+).*");
|
|
|
|
if (!matches.Success) return;
|
|
|
|
|
|
|
|
var major = int.Parse(matches.Groups[1].Value);
|
|
|
|
var minor = int.Parse(matches.Groups[2].Value);
|
|
|
|
if (major > cur.Major || (major == cur.Major && minor > cur.Minor)) {
|
|
|
|
Dispatcher.Invoke(() => {
|
|
|
|
var dialog = new UI.UpdateAvailable(ver);
|
|
|
|
dialog.Owner = MainWindow;
|
|
|
|
dialog.ShowDialog();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// IGNORE
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 00:24:31 -07:00
|
|
|
}
|
|
|
|
}
|