Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ src/Application.vala
src/Backend/DeepL/DeepL.vala
src/Constants.vala
src/Enums/StatusCode.vala
src/Enums/ProviderType.vala
src/Views/ErrorView.vala
src/Views/LogView.vala
src/Views/TranslationView.vala
Expand All @@ -20,6 +21,7 @@ src/Widgets/Panes/Pane.vala
src/Widgets/Panes/SourcePane.vala
src/Widgets/Panes/TargetPane.vala
src/Widgets/Popovers/OptionsPopover.vala
src/Widgets/Popovers/ProviderPopover.vala
src/Widgets/Popovers/SettingsPopover.vala
src/Widgets/PopoverWidgets/ApiEntry.vala
src/Widgets/PopoverWidgets/ApiLevel.vala
Expand Down
114 changes: 114 additions & 0 deletions src/Backend/Backend.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025-2026 Stella & Charlie (teamcons.carrd.co)
*/

/**
* a
*/
public class Inscriptions.Backend : Object {

public signal void provider_changed ();

private const uint TIMEOUT = 3000; //ms
public bool busy = false; //

Secrets secrets;
Soup.Session session;
Inscriptions.Provider translation_provider;

Inscriptions.ProviderType _provider_type;
public Inscriptions.ProviderType provider_type {
get {return _provider_type;}
set {
translation_provider = value.to_provider ();
_provider_type = value;
provider_changed ();
}
}

// Ensure only once instance, accessible whenever needed.
private static Backend? instance;
public static Backend get_default () {
if (instance == null) {
instance = new Backend ();
}
return instance;
}

// Only access through get_default ()
private Backend () {}

construct {
secrets = Secrets.get_default ();

session = new Soup.Session () {
timeout = TIMEOUT
};

var logger = new Soup.Logger (Soup.LoggerLogLevel.BODY);
session.add_feature (logger);

logger.set_printer ((_1, _2, dir, text) => {
stderr.printf ("%c %s\n", dir, text);
});

//translation_provider = Inscriptions.Providers.DeepL ();
}

public async Inscriptions.BackendAnswer translate (Inscriptions.TranslationRequest translation_request) {

busy = true;

/*
// Ask Secret for API key
// Ask Provider for URL, giving it API Key
// Ask Provider for wrapped body

// Create Msg
soup_translation_request = provider.prepare_translation_request (api_key, request);

// send message, get answer

// Ask provider to unwrap answer

var answerdata = AnswerData ();
if (StatusCode == OK) {
var answer_data = translation_provider.unwrap_answer (json_answer);
var message = answer_data.message;
var language_detected = answer_data.detected_language_code;

} else {
var message = translation_provider.unwrap_error (json_answer);
}



*/

busy = false;

return BackendAnswer () {
status_code = StatusCode.OK,
message = "string message",
language_detected = "string? language_detected",
initial_request = translation_request
};
}

public Provider.Features get_supported_features () {
return translation_provider.get_supported_features ();
}

public string[] get_supported_formality () {
return translation_provider.get_supported_formality ();
}

public Lang[] get_source_languages () {
return translation_provider.get_source_languages ();
}

public Lang[] get_target_languages () {
return translation_provider.get_target_languages ();
}
}
92 changes: 0 additions & 92 deletions src/Backend/BackendTemplate.vala

This file was deleted.

4 changes: 2 additions & 2 deletions src/Backend/DeepL/DeepL.vala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Inscriptions.DeepL : Object {
on_source_lang_changed ();
on_target_lang_changed ();

secrets.changed.connect (debounce_check);
//secrets.changed.connect (debounce_check);
Application.settings_translate.changed[KEY_SOURCE_LANGUAGE].connect (on_source_lang_changed);
Application.settings_translate.changed[KEY_TARGET_LANGUAGE].connect (on_target_lang_changed);
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public class Inscriptions.DeepL : Object {
}

public void on_key_changed () {
api_key = secrets.cached_key;
api_key = "";

if (api_key.chomp () == "") {
answer_received (StatusCode.NO_KEY, _("Missing API Key"));
Expand Down
3 changes: 3 additions & 0 deletions src/Backend/DeepL/DeepLUtils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ namespace Inscriptions.DeepLUtils {
//print ("\nBackend: Detected system language: " + minicode);
return minicode;
}


}

69 changes: 69 additions & 0 deletions src/Backend/Provider.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025-2026 Stella & Charlie (teamcons.carrd.co)
*/

/**
* a
*
*/
public interface Inscriptions.Provider : Object {

internal const string PROVIDER_SETTINGS_PREFIX = "";

[Flags]
public enum Features {
NONE,
CHECK_USAGE,
SET_FORMALITY,
SET_CONTEXT
}

public struct Usage {int current; int max;}
public struct AnswerData {string message; string detected_language_code;}

/**
* NONE is for providers who can only translate, nothing else
*
* CHECK_USAGE: Providers should have an implementation of {@link Provider.prepare_check_usage} and {@link Provider.unwrap_check_usage}
*
* SET_FORMALITY and SET_CONTEXT: {@link Inscriptions.TranslationRequest} contains both. Simply ignore it/null it if unsupported.
*/
public abstract string get_name ();
public abstract string get_auth_header ();
public abstract Features get_supported_features ();
public virtual string[] get_supported_formality () {return {};}

/**
* Managing their internal settings is up to the provider
*/
internal abstract Settings? settings {get; set; default = null;}



/**
* Provider gets as much info as possible, to allow it maximum flexibility
*
* The message is sent by the backend itself
*/
public abstract Soup.Message prepare_translation_request (string api_key, Inscriptions.TranslationRequest request);

/**
* Errors and the explanation given by the provider should be handled and returned if the status code is not OK
*
*/
public abstract AnswerData unwrap_translation_answer (string json_answer);

public abstract string unwrap_error (string json_answer);

/**
* Override this if your backend supports CHECK_USAGE
*
*/
public virtual Soup.Message prepare_check_usage (string api_key) {return new Soup.Message ("", "");}
public virtual Usage unwrap_check_usage (string json_answer) {return Usage () {current = 0, max = 0};}


public abstract Lang[] get_source_languages ();
public abstract Lang[] get_target_languages ();
}
Loading
Loading