from core.models.enum_models import ProtocolChoice from core.models.BaseProfile import ProfileType from gui.v2.infrastructure.folders import FolderStash from gui.v2.presenters.data.location_data import get_location_icon_path from gui.v2.presenters.data.app_data import get_app_icon_path from gui.v2.presenters.data.protocol_data import get_protocol_icon_path from dataclasses import dataclass from typing import Optional from pathlib import Path @dataclass class ProfileDto(): profile_id: int profile_name: str location_icon_path: Path app_icon_path: Path protocol_icon_path: Path needs_warning: bool = False @dataclass class RenderProfileDto(): profile_id: int profile_type: ProfileType location_text: str operator_name: str nostr_public_key: str detail_image: Path primary_asset: Path def convert_one_render_dto(raw_dict: dict) -> Optional[RenderProfileDto]: """Get a DTO for the left side of the menu's rendering.""" if not raw_dict or not isinstance(raw_dict, dict): return None image_path = Path(FolderStash().image_path) if not image_path: return None profile_id = raw_dict.get("id") profile_type = raw_dict.get("type") location = raw_dict.get("location") if profile_type == ProfileType.SESSION: primary_asset = Path(image_path, "browser only.png") if not primary_asset.exists(): primary_asset = None else: primary_asset = None if not location: print(raw_dict) print("Couldn't find a location in the above dict") return None detail_image = get_location_icon_path( image_path=image_path, country_code=location.get("country_code", "na") if location else "na", code=location.get("code", "na") if location else "na", larger_version=True ) country_name = location.get("country_name", "N/A") country_city = location.get("name", "N/A") location_text = f"{country_city}, {country_name}" operator = location.get("operator") if not operator: print(location) print("Couldn't find the operator in the above location") return None nostr_public_key = operator.get("nostr_public_key", "Nostr not setup yet.") operator_name = operator.get("name", "N/A") return RenderProfileDto( profile_id=profile_id, profile_type=profile_type, location_text=location_text, operator_name=operator_name, nostr_public_key=nostr_public_key, detail_image=detail_image, primary_asset=primary_asset ) def convert_to_dto(raw_dict: dict) -> ProfileDto: """Convert a profile dictionary into a DTO with image paths""" if not raw_dict or not isinstance(raw_dict, dict): return None image_path = Path(FolderStash().image_path) if not image_path: return None profile_id = raw_dict.get("id") name = raw_dict.get("name", f"Profile {str(profile_id)}") profile_type = raw_dict.get("type", ProfileType.SYSTEM) location = raw_dict.get("location") location_icon_path = get_location_icon_path( image_path=image_path, country_code=location.get("country_code", "na") if location else "na", code=location.get("code", "na") if location else "na", larger_version=False ) application = raw_dict.get("application_version") app_icon_path = get_app_icon_path( image_path=image_path, profile_type=profile_type, app_name=application.get('application_code', "na") if application else "na" ) format_revision = raw_dict.get("format_revision", 1) if profile_type == ProfileType.SYSTEM or format_revision == 2: needs_warning = False else: needs_warning = True protocol_icon_path = get_protocol_icon_path( image_path=image_path, protocol=raw_dict.get("protocol") ) return ProfileDto( profile_id=profile_id, profile_name=name, location_icon_path=location_icon_path, app_icon_path=app_icon_path, protocol_icon_path=protocol_icon_path, needs_warning=needs_warning )