Files
gajim-plugins/stt_voice_messages/gtk/config_dialog.py
2026-05-05 05:23:23 -03:00

186 lines
6.0 KiB
Python

# This file is part of Gajim.
#
# Gajim is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING, Any
from gi.repository import Gtk
from gajim.common import app
from gajim.gtk.builder import get_builder
from gajim.gtk.const import Setting, SettingKind, SettingType
from gajim.gtk.settings import SettingsBox
from gajim.gtk.sidebar_switcher import SideBarSwitcher
from gajim.plugins.helpers import get_builder
from gajim.plugins.plugins_i18n import _
from .. import stt_voice_messages
from ..models import openai_whisper
if TYPE_CHECKING:
from .. import stt_voice_messages
log = logging.getLogger('gajim.p.stt_voice_messages_config')
################################################################################
# Helper
################################################################################
def check_module(module: str) -> bool:
try:
__import__(module)
return True
except ModuleNotFoundError:
log.debug('Could not find module %s', module)
return False
except ImportError as ex:
log.debug(str(ex))
return False
################################################################################
# Plugin Settings
################################################################################
class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
def __init__(self, plugin: stt_voice_messages.STTVoiceMessagesPlugin,
parent: Gtk.Window) -> None:
Gtk.ApplicationWindow.__init__(self)
self.plugin = plugin
self.set_application(app.app)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_show_menubar(False)
self.set_name('PreferencesWindow')
self.set_default_size(900, 650)
self.set_resizable(True)
self.set_title(_('Preferences'))
ui_path = Path(__file__).parent
self._ui = get_builder(str(ui_path.resolve() / 'config_dialog.ui'))
self._prefs: dict[str, PreferenceBox] = {}
side_bar_switcher = SideBarSwitcher()
side_bar_switcher.set_stack(self._ui.stack)
self._ui.grid.attach(side_bar_switcher, 0, 0, 1, 1)
self.add(self._ui.grid)
prefs: list[tuple[str, type[PreferenceBox]]] = [
('stt_behaviour', STTBehaviour),
('models', Models),
('file_preview', FilePreview),
('whisper_general', openai_whisper.OpenAIWhisperGeneral),
]
self._add_prefs(prefs)
self.show_all()
def _add_prefs(self, prefs: list[tuple[str, type[PreferenceBox]]]):
for ui_name, klass in prefs:
pref_box = getattr(self._ui, ui_name)
print('pref_box = ', pref_box)
if pref_box is None:
continue
pref = klass(self) # pyright: ignore
pref_box.add(pref)
self._prefs[ui_name] = pref
def _on_setting(self, value: Any, data: Any) -> None:
self.plugin.config[data] = value
################################################################################
# Preference boxes
################################################################################
class PreferenceBox(SettingsBox):
def __init__(self, settings: list[Setting]) -> None:
SettingsBox.__init__(self, None)
self.get_style_context().add_class('border')
self.set_selection_mode(Gtk.SelectionMode.NONE)
self.set_vexpand(False)
self.set_valign(Gtk.Align.END)
for setting in settings:
self.add_setting(setting)
self.update_states()
class STTBehaviour(PreferenceBox):
def __init__(self, *args: Any) -> None:
main_window_on_startup_items = {
'always': _('Always'),
'never': _('Never'),
'last_state': _('Restore last state'),
}
settings = [
Setting(SettingKind.POPOVER,
_('Show on Startup'),
SettingType.CONFIG,
'show_main_window_on_startup',
props={'entries': main_window_on_startup_items},
desc=_('Show window when starting Gajim')),
]
PreferenceBox.__init__(self, settings)
class Models(PreferenceBox):
def __init__(self, *args: Any) -> None:
main_window_on_startup_items = {
'always': _('Always'),
'never': _('Never'),
'last_state': _('Restore last state'),
}
settings = [
Setting(SettingKind.POPOVER,
_('Show on Startup'),
SettingType.CONFIG,
'show_main_window_on_startup',
props={'entries': main_window_on_startup_items},
desc=_('Show window when starting Gajim')),
]
PreferenceBox.__init__(self, settings)
class FilePreview(PreferenceBox):
def __init__(self, *args: Any) -> None:
main_window_on_startup_items = {
'always': _('Always'),
'never': _('Never'),
'last_state': _('Restore last state'),
}
settings = [
Setting(SettingKind.POPOVER,
_('Show on Startup'),
SettingType.CONFIG,
'show_main_window_on_startup',
props={'entries': main_window_on_startup_items},
desc=_('Show window when starting Gajim')),
]
PreferenceBox.__init__(self, settings)