232 lines
8.7 KiB
C#
232 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using libObsidean;
|
|
using ShortcutsHelper.Models;
|
|
using System.IO;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace ShortcutsHelper.Services
|
|
{
|
|
public class ShortcutService
|
|
{
|
|
private readonly Obsidean _obsidean = new();
|
|
private readonly Dictionary<string, List<ShortcutRecord>> _shortcutsByApplication = new();
|
|
private bool _isLoaded = false;
|
|
|
|
public event EventHandler<string>? ApplicationShortcutsChanged;
|
|
|
|
public void LoadAllShortcuts()
|
|
{
|
|
if (_isLoaded) return;
|
|
|
|
try
|
|
{
|
|
var tabla = _obsidean.LeerShortcuts();
|
|
if (tabla != null && tabla.GetLength(0) > 1)
|
|
{
|
|
_shortcutsByApplication.Clear();
|
|
|
|
for (int i = 1; i < tabla.GetLength(0); i++)
|
|
{
|
|
string app = tabla[i, 0] ?? "";
|
|
string shortcut = tabla[i, 1] ?? "";
|
|
string description = tabla[i, 2] ?? "";
|
|
|
|
if (!string.IsNullOrWhiteSpace(app) && (!string.IsNullOrWhiteSpace(shortcut) || !string.IsNullOrWhiteSpace(description)))
|
|
{
|
|
bool isFavorite = false;
|
|
if (tabla.GetLength(1) > 3 && !string.IsNullOrWhiteSpace(tabla[i, 3]))
|
|
{
|
|
bool.TryParse(tabla[i, 3], out isFavorite);
|
|
}
|
|
|
|
var record = new ShortcutRecord
|
|
{
|
|
Application = app,
|
|
Shortcut = shortcut,
|
|
Description = description,
|
|
IsFavorite = isFavorite,
|
|
IsModified = false
|
|
};
|
|
|
|
if (!_shortcutsByApplication.ContainsKey(app))
|
|
{
|
|
_shortcutsByApplication[app] = new List<ShortcutRecord>();
|
|
}
|
|
|
|
_shortcutsByApplication[app].Add(record);
|
|
}
|
|
}
|
|
}
|
|
|
|
_isLoaded = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al cargar shortcuts: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public List<ShortcutRecord> GetShortcutsForApplication(string applicationName)
|
|
{
|
|
if (!_isLoaded) LoadAllShortcuts();
|
|
|
|
if (_shortcutsByApplication.TryGetValue(applicationName, out var shortcuts))
|
|
{
|
|
return shortcuts.OrderByDescending(s => s.IsFavorite).ThenBy(s => s.Shortcut).ToList();
|
|
}
|
|
|
|
return new List<ShortcutRecord>();
|
|
}
|
|
|
|
public void AddOrUpdateShortcut(ShortcutRecord shortcut)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(shortcut.Application)) return;
|
|
|
|
if (!_shortcutsByApplication.ContainsKey(shortcut.Application))
|
|
{
|
|
_shortcutsByApplication[shortcut.Application] = new List<ShortcutRecord>();
|
|
}
|
|
|
|
var existing = _shortcutsByApplication[shortcut.Application]
|
|
.FirstOrDefault(s => s.Shortcut == shortcut.Shortcut);
|
|
|
|
if (existing != null)
|
|
{
|
|
existing.Description = shortcut.Description;
|
|
existing.IsFavorite = shortcut.IsFavorite;
|
|
// No marcar como IsModified = false aquí, dejar que SaveAllShortcuts() lo haga
|
|
}
|
|
else
|
|
{
|
|
// No marcar como IsModified = false aquí, dejar que SaveAllShortcuts() lo haga
|
|
_shortcutsByApplication[shortcut.Application].Add(shortcut);
|
|
}
|
|
|
|
ApplicationShortcutsChanged?.Invoke(this, shortcut.Application);
|
|
}
|
|
|
|
public void RemoveShortcut(string applicationName, string shortcut)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(applicationName) || string.IsNullOrWhiteSpace(shortcut)) return;
|
|
|
|
if (_shortcutsByApplication.TryGetValue(applicationName, out var shortcuts))
|
|
{
|
|
var toRemove = shortcuts.FirstOrDefault(s => s.Shortcut == shortcut);
|
|
if (toRemove != null)
|
|
{
|
|
shortcuts.Remove(toRemove);
|
|
ApplicationShortcutsChanged?.Invoke(this, applicationName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SaveAllShortcuts()
|
|
{
|
|
try
|
|
{
|
|
var allShortcuts = new List<ShortcutRecord>();
|
|
foreach (var appShortcuts in _shortcutsByApplication.Values)
|
|
{
|
|
allShortcuts.AddRange(appShortcuts.Where(s => !string.IsNullOrWhiteSpace(s.Shortcut)));
|
|
}
|
|
|
|
if (allShortcuts.Count == 0) return;
|
|
|
|
// Crear tabla para guardar
|
|
var tabla = new string[allShortcuts.Count + 1, 4];
|
|
tabla[0, 0] = "Application";
|
|
tabla[0, 1] = "Shortcut";
|
|
tabla[0, 2] = "Description";
|
|
tabla[0, 3] = "IsFavorite";
|
|
|
|
for (int i = 0; i < allShortcuts.Count; i++)
|
|
{
|
|
var shortcut = allShortcuts[i];
|
|
tabla[i + 1, 0] = shortcut.Application;
|
|
tabla[i + 1, 1] = shortcut.Shortcut;
|
|
tabla[i + 1, 2] = shortcut.Description;
|
|
tabla[i + 1, 3] = shortcut.IsFavorite.ToString();
|
|
}
|
|
|
|
// Guardar en Obsidian
|
|
var vaultPath = GetVaultPath("VM");
|
|
if (!string.IsNullOrEmpty(vaultPath))
|
|
{
|
|
string pathToMarkdown = Path.Combine(vaultPath, "DB", "Shortcuts", "Shortcuts.md");
|
|
libObsidean.Obsidean.SaveTableToMarkdown(pathToMarkdown, tabla);
|
|
|
|
// Marcar todos como no modificados
|
|
foreach (var appShortcuts in _shortcutsByApplication.Values)
|
|
{
|
|
foreach (var shortcut in appShortcuts)
|
|
{
|
|
shortcut.IsModified = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al guardar shortcuts: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public bool HasModifiedShortcuts()
|
|
{
|
|
return _shortcutsByApplication.Values
|
|
.Any(shortcuts => shortcuts.Any(s => s.IsModified));
|
|
}
|
|
|
|
public List<string> GetAllApplicationNames()
|
|
{
|
|
if (!_isLoaded) LoadAllShortcuts();
|
|
return _shortcutsByApplication.Keys.OrderBy(k => k).ToList();
|
|
}
|
|
|
|
private string? GetVaultPath(string vaultName)
|
|
{
|
|
try
|
|
{
|
|
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
string pathToJsonFile = Path.Combine(appDataPath, "obsidian", "obsidian.json");
|
|
if (File.Exists(pathToJsonFile))
|
|
{
|
|
string jsonContent = File.ReadAllText(pathToJsonFile);
|
|
var jsonObject = JObject.Parse(jsonContent);
|
|
var vaults = jsonObject["vaults"] as JObject;
|
|
|
|
if (vaults != null)
|
|
{
|
|
foreach (var vault in vaults)
|
|
{
|
|
var pathToken = vault.Value?["path"];
|
|
if (pathToken != null)
|
|
{
|
|
string? path = pathToken.ToString();
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
string? dirPath = Path.GetDirectoryName(path.TrimEnd('\\') + "\\");
|
|
if (!string.IsNullOrEmpty(dirPath))
|
|
{
|
|
string lastDirectoryName = Path.GetFileName(dirPath);
|
|
if (lastDirectoryName.Equals(vaultName, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error al leer vault: " + ex.Message);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |