2024-04-24 03:33:57 -03:00
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System;
|
2024-11-14 07:37:17 -03:00
|
|
|
|
using System.Windows.Forms;
|
2024-04-24 03:33:57 -03:00
|
|
|
|
using Application = System.Windows.Application;
|
2024-11-14 07:37:17 -03:00
|
|
|
|
using System.Diagnostics;
|
2024-04-24 08:15:57 -03:00
|
|
|
|
using System.Threading.Tasks;
|
2024-04-26 12:57:18 -03:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Windows.Data;
|
2024-05-28 08:43:16 -03:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-04-24 03:33:57 -03:00
|
|
|
|
|
|
|
|
|
namespace GTPCorrgir
|
|
|
|
|
{
|
|
|
|
|
public partial class App : Application
|
|
|
|
|
{
|
|
|
|
|
gtpask GTP = new gtpask();
|
2024-04-24 08:15:57 -03:00
|
|
|
|
NotifyIcon notificacion;
|
|
|
|
|
System.Windows.Forms.Timer timer;
|
|
|
|
|
bool CorreccionFinalizada = false;
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
2024-04-25 12:51:43 -03:00
|
|
|
|
private notificacion notificationWindow;
|
2024-11-14 07:37:17 -03:00
|
|
|
|
private readonly int TimeoutSeconds = 60; // Timeout máximo para esperar la respuesta
|
|
|
|
|
private CancellationTokenSource _cancellationTokenSource;
|
2024-04-25 12:51:43 -03:00
|
|
|
|
|
2024-05-28 08:43:16 -03:00
|
|
|
|
KeyboardHelper pasteLogic = new KeyboardHelper();
|
2024-04-24 08:15:57 -03:00
|
|
|
|
|
2024-04-24 03:33:57 -03:00
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnStartup(e);
|
2024-11-14 07:37:17 -03:00
|
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
2024-04-24 03:33:57 -03:00
|
|
|
|
|
2024-11-14 07:37:17 -03:00
|
|
|
|
try
|
2024-04-24 03:33:57 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
pasteLogic.SaveCurrentWindow();
|
2024-04-24 03:33:57 -03:00
|
|
|
|
|
2024-11-14 07:37:17 -03:00
|
|
|
|
if (System.Windows.Clipboard.ContainsText())
|
|
|
|
|
{
|
|
|
|
|
GTP.TextoACorregir = System.Windows.Clipboard.GetText();
|
|
|
|
|
}
|
2024-04-24 08:15:57 -03:00
|
|
|
|
|
2024-11-14 07:37:17 -03:00
|
|
|
|
if (Opciones.Instance.modo == Opciones.modoDeUso.Corregir ||
|
|
|
|
|
Opciones.Instance.modo == Opciones.modoDeUso.Ortografia)
|
|
|
|
|
{
|
|
|
|
|
GTP.Log.Log("Iniciando proceso de corrección");
|
|
|
|
|
stopwatch.Start();
|
2024-04-26 12:57:18 -03:00
|
|
|
|
|
2024-11-14 07:37:17 -03:00
|
|
|
|
ShowCustomNotification("Espera", $"Corrigiendo texto con {Opciones.Instance.nombreDeLLM()}...");
|
|
|
|
|
IniciarCronometro();
|
|
|
|
|
|
|
|
|
|
// Ejecuta la tarea de corrección con timeout
|
|
|
|
|
_ = ProcessCorreccionWithTimeout();
|
|
|
|
|
}
|
|
|
|
|
else if (Opciones.Instance.modo == Opciones.modoDeUso.Chat)
|
|
|
|
|
{
|
|
|
|
|
var chatWindows = new Chat(GTP);
|
|
|
|
|
chatWindows.Show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
GTP.Log.Log($"Error en OnStartup: {ex.Message}");
|
|
|
|
|
GTP.Log.Log($"StackTrace: {ex.StackTrace}");
|
|
|
|
|
ShowCustomNotification("Error", "Se produjo un error al iniciar la aplicación");
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-29 05:56:48 -03:00
|
|
|
|
|
2024-11-14 07:37:17 -03:00
|
|
|
|
private async Task ProcessCorreccionWithTimeout()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(TimeoutSeconds));
|
|
|
|
|
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
|
|
|
|
|
timeoutCts.Token, _cancellationTokenSource.Token);
|
2024-04-29 05:56:48 -03:00
|
|
|
|
|
2024-11-14 07:37:17 -03:00
|
|
|
|
var correccionTask = Task.Run(async () =>
|
2024-04-24 03:33:57 -03:00
|
|
|
|
{
|
2024-04-29 05:56:48 -03:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Iniciando corrección de texto");
|
2024-04-29 05:56:48 -03:00
|
|
|
|
await GTP.CorregirTexto();
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Corrección de texto completada");
|
2024-04-29 05:56:48 -03:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log($"Error durante la corrección: {ex.Message}");
|
|
|
|
|
throw;
|
2024-04-29 05:56:48 -03:00
|
|
|
|
}
|
2024-11-14 07:37:17 -03:00
|
|
|
|
}, linkedCts.Token);
|
|
|
|
|
|
|
|
|
|
await correccionTask;
|
|
|
|
|
|
|
|
|
|
await Dispatcher.InvokeAsync(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
2024-04-24 08:15:57 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Procesando resultado de la corrección");
|
|
|
|
|
CorreccionFinalizada = true;
|
|
|
|
|
DetenerCronometro();
|
|
|
|
|
|
|
|
|
|
if (GTP.TextoCorregido != null)
|
2024-04-24 08:15:57 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Copiando texto corregido al portapapeles");
|
|
|
|
|
System.Windows.Clipboard.SetText(GTP.TextoCorregido);
|
|
|
|
|
ShowCustomNotification("Se puede pegar",
|
|
|
|
|
$"Corrección en: {Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 1)} s");
|
|
|
|
|
|
|
|
|
|
if (Opciones.Instance.modo == Opciones.modoDeUso.Corregir)
|
2024-04-29 05:56:48 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Mostrando ventana de resultado");
|
|
|
|
|
var resultadoWindow = new VentanaResultado(GTP.TextoCorregido);
|
|
|
|
|
resultadoWindow.Show();
|
|
|
|
|
await Task.Delay(1000);
|
2024-04-29 05:56:48 -03:00
|
|
|
|
}
|
2024-11-14 07:37:17 -03:00
|
|
|
|
else if (Opciones.Instance.modo == Opciones.modoDeUso.Ortografia)
|
2024-04-29 05:56:48 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Ejecutando pegado automático");
|
|
|
|
|
pasteLogic.RestoreAndSimulatePaste();
|
2024-04-29 05:56:48 -03:00
|
|
|
|
}
|
2024-11-14 07:37:17 -03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GTP.Log.Log("Error: TextoCorregido es null");
|
|
|
|
|
ShowCustomNotification("Error", "No se pudo obtener el texto corregido");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
GTP.Log.Log($"Error en el procesamiento final: {ex.Message}");
|
|
|
|
|
ShowCustomNotification("Error", "Error al procesar el resultado");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
GTP.Log.Log("Cerrando aplicación");
|
|
|
|
|
Application.Current.Shutdown();
|
2024-04-29 05:56:48 -03:00
|
|
|
|
}
|
|
|
|
|
});
|
2024-11-14 07:37:17 -03:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
2024-04-29 05:56:48 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Operación cancelada por timeout");
|
|
|
|
|
await Dispatcher.InvokeAsync(() =>
|
|
|
|
|
{
|
|
|
|
|
ShowCustomNotification("Error", "La operación excedió el tiempo límite");
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
});
|
2024-04-29 05:56:48 -03:00
|
|
|
|
}
|
2024-11-14 07:37:17 -03:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
GTP.Log.Log($"Error no controlado: {ex.Message}");
|
|
|
|
|
await Dispatcher.InvokeAsync(() =>
|
|
|
|
|
{
|
|
|
|
|
ShowCustomNotification("Error", "Se produjo un error inesperado");
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-26 07:54:24 -03:00
|
|
|
|
|
2024-11-14 07:37:17 -03:00
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource?.Cancel();
|
|
|
|
|
_cancellationTokenSource?.Dispose();
|
|
|
|
|
base.OnExit(e);
|
2024-04-24 03:33:57 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 12:51:43 -03:00
|
|
|
|
private void ShowCustomNotification(string title, string message)
|
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
try
|
2024-04-25 12:51:43 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log($"Mostrando notificación: {title} - {message}");
|
|
|
|
|
if (notificationWindow == null)
|
|
|
|
|
{
|
|
|
|
|
notificationWindow = new notificacion();
|
|
|
|
|
notificationWindow.Show();
|
|
|
|
|
}
|
|
|
|
|
notificationWindow.UpdateNotification(title, message);
|
2024-04-25 12:51:43 -03:00
|
|
|
|
}
|
2024-11-14 07:37:17 -03:00
|
|
|
|
catch (Exception ex)
|
2024-04-24 03:33:57 -03:00
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log($"Error al mostrar notificación: {ex.Message}");
|
|
|
|
|
}
|
2024-04-24 08:15:57 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void IniciarCronometro()
|
|
|
|
|
{
|
|
|
|
|
timer = new System.Windows.Forms.Timer();
|
2024-11-14 07:37:17 -03:00
|
|
|
|
timer.Interval = 100;
|
2024-04-24 08:15:57 -03:00
|
|
|
|
timer.Tick += ActualizarCronometro;
|
|
|
|
|
timer.Start();
|
2024-11-14 07:37:17 -03:00
|
|
|
|
GTP.Log.Log("Cronómetro iniciado");
|
2024-04-24 08:15:57 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ActualizarCronometro(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
if (!CorreccionFinalizada)
|
|
|
|
|
{
|
|
|
|
|
ShowCustomNotification(
|
|
|
|
|
$"{Opciones.Instance.nombreDeLLM()} Trabajando..",
|
|
|
|
|
$"Texto en {GTP.IdiomaDetectado} pasados: {Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 1)} s"
|
|
|
|
|
);
|
2024-04-24 08:15:57 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DetenerCronometro()
|
|
|
|
|
{
|
2024-11-14 07:37:17 -03:00
|
|
|
|
if (timer != null)
|
|
|
|
|
{
|
|
|
|
|
timer.Stop();
|
|
|
|
|
timer.Dispose();
|
|
|
|
|
GTP.Log.Log("Cronómetro detenido");
|
|
|
|
|
}
|
2024-04-24 03:33:57 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 08:43:16 -03:00
|
|
|
|
public class KeyboardHelper
|
|
|
|
|
{
|
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
|
|
|
private static extern IntPtr GetForegroundWindow();
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
|
|
|
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
|
|
|
|
|
|
|
|
public const int KEYEVENTF_KEYUP = 0x0002; // Key up flag
|
|
|
|
|
public const int VK_CONTROL = 0x11; // Control key code
|
|
|
|
|
public const int V = 0x56; // V key code
|
|
|
|
|
|
|
|
|
|
private IntPtr previousWindow;
|
|
|
|
|
|
|
|
|
|
public void SaveCurrentWindow()
|
|
|
|
|
{
|
|
|
|
|
previousWindow = GetForegroundWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RestoreAndSimulatePaste()
|
|
|
|
|
{
|
|
|
|
|
// Restore focus to the previous window
|
|
|
|
|
SetForegroundWindow(previousWindow);
|
|
|
|
|
|
|
|
|
|
// Simulating Ctrl+V
|
|
|
|
|
keybd_event((byte)VK_CONTROL, 0x9d, 0, UIntPtr.Zero); // Ctrl Press
|
|
|
|
|
keybd_event((byte)V, 0x9e, 0, UIntPtr.Zero); // V Press
|
|
|
|
|
keybd_event((byte)V, 0x9e, KEYEVENTF_KEYUP, UIntPtr.Zero); // V Release
|
|
|
|
|
keybd_event((byte)VK_CONTROL, 0x9d, KEYEVENTF_KEYUP, UIntPtr.Zero); // Ctrl Release
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-14 07:37:17 -03:00
|
|
|
|
}
|