Skip to main content

game_manager_lib/commands/platforms/
epic.rs

1//! Epic Games - Login OAuth e importação (biblioteca completa + instalados)
2
3use crate::commands::platforms::core::{
4    format_import_empty, format_import_summary, format_login_success, persist_source_games,
5};
6use crate::database::AppState;
7use crate::errors::AppError;
8use crate::sources::epic::{merge_local_install_status, EpicSource};
9use crate::sources::providers::OAuthGameSource;
10use tauri::{AppHandle, Emitter, State};
11use tracing::info;
12
13#[tauri::command]
14pub async fn epic_login(app: AppHandle) -> Result<String, AppError> {
15    let source = EpicSource::new(app, None);
16    source.login().await?;
17    Ok(format_login_success("Epic"))
18}
19
20#[tauri::command]
21pub fn epic_logout(app: AppHandle) -> Result<(), AppError> {
22    let source = EpicSource::new(app, None);
23    source.logout()
24}
25
26#[tauri::command]
27pub fn epic_is_authenticated(app: AppHandle) -> Result<bool, AppError> {
28    let source = EpicSource::new(app, None);
29    source.is_authenticated()
30}
31
32#[tauri::command]
33pub async fn import_epic_games(
34    app: AppHandle,
35    state: State<'_, AppState>,
36    wine_prefix: Option<String>,
37) -> Result<String, AppError> {
38    let prefix = wine_prefix
39        .filter(|s| !s.trim().is_empty())
40        .map(std::path::PathBuf::from);
41
42    let source = EpicSource::new(app.clone(), prefix);
43
44    let local_games = source.import_installed().await?;
45
46    // Sem login, mantém o comportamento atual: só jogos instalados.
47    let mut games = if source.is_authenticated().unwrap_or(false) {
48        source.fetch_library_detailed().await?
49    } else {
50        Vec::new()
51    };
52
53    merge_local_install_status(&mut games, local_games);
54
55    if games.is_empty() {
56        return Ok(format_import_empty("Epic"));
57    }
58
59    let (inserted, updated, _newly_imported) = persist_source_games(&state, games).await?;
60    let message = format_import_summary("Epic", inserted, updated);
61    info!("{}", message);
62
63    let _ = app.emit("library_updated", ());
64
65    Ok(message)
66}