hydraveil-gui/gui/v2/presenters/data/profile_data.py

170 lines
5.3 KiB
Python

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, get_larger_protocol_image
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 core.models.enum_models import ProtocolChoice
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
raised_primary_asset: bool
def _is_browser_box_this_image(image_path: Path, profile_type: ProfileType, is_tor: bool) -> Path|None:
"""Should the browser box be the detail image? If so, give us the image path"""
if profile_type == ProfileType.SESSION and not is_tor: # (tor doesn't need the browser border)
final_image_path = Path(image_path, "browser only.png")
if not final_image_path.exists():
final_image_path = None
else:
final_image_path = None
return final_image_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")
protocol = raw_dict.get("protocol")
if not location:
print(raw_dict)
print("Couldn't find a location in the above dict")
return None
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
country_code = location.get("country_code", "na") if location else "na"
code = location.get("code", "na") if location else "na"
nostr_public_key = operator.get("nostr_public_key", "Nostr not setup yet.")
operator_name = operator.get("name", "N/A")
sp_operator = bool(operator_name in ("Simplified Privacy", "simplified privacy"))
is_tor = bool(protocol == ProtocolChoice.TOR)
detail_image = None
if sp_operator or is_tor:
raised_primary_asset = True
primary_asset = get_larger_protocol_image(
image_path=image_path,
country_code=country_code,
code=code,
protocol=protocol
)
detail_image = _is_browser_box_this_image(image_path=image_path, profile_type=profile_type, is_tor=is_tor)
else:
raised_primary_asset = False
detail_image = get_location_icon_path(
image_path=image_path,
country_code=country_code,
code=code,
larger_version=True
)
primary_asset = _is_browser_box_this_image(image_path=image_path, profile_type=profile_type, is_tor=is_tor)
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,
raised_primary_asset=raised_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
)