forked from Support/sp-hydra-veil-gui
229 lines
No EOL
7.3 KiB
Python
229 lines
No EOL
7.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
|
|
|
|
|
|
@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
|
|
needs_warning: bool
|
|
|
|
|
|
def _evaluate_app_warnings(profile_type: ProfileType, application: dict) -> bool:
|
|
"""Check if the application needs a warning to upgrade"""
|
|
|
|
if not profile_type == ProfileType.SESSION:
|
|
return False
|
|
|
|
if not application:
|
|
return False
|
|
|
|
format_revision = application.get("format_revision", 1)
|
|
if format_revision == 2:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
|
|
def _is_browser_box_this_image(image_path: Path, profile_type: ProfileType, is_tor: bool, needs_warning: bool) -> Path|None:
|
|
"""Should the browser box be the detail image? If so, give us the image path"""
|
|
|
|
if needs_warning:
|
|
app_image = "unsupported_browser_only.png"
|
|
else:
|
|
app_image = "browser only.png"
|
|
|
|
|
|
if profile_type == ProfileType.SESSION and not is_tor: # (tor doesn't need the browser border)
|
|
final_image_path = Path(image_path, app_image)
|
|
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)
|
|
|
|
application = raw_dict.get("application_version")
|
|
needs_warning = _evaluate_app_warnings(profile_type=profile_type, application=application)
|
|
|
|
# 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, needs_warning=needs_warning)
|
|
|
|
else:
|
|
raised_primary_asset = False
|
|
detail_image = get_location_icon_path(
|
|
image_path=image_path,
|
|
country_code=country_code,
|
|
code=code,
|
|
larger_version=True
|
|
)
|
|
|
|
# warning to false, so the text doesn't clash with the text,
|
|
primary_asset = _is_browser_box_this_image(image_path=image_path, profile_type=profile_type, is_tor=is_tor, needs_warning=False)
|
|
|
|
# # then because we need to warn them somehow, do so with the operator name,
|
|
# operator_name = "Upgrade Browser!"
|
|
# location_text = "Upgrade Browser!"
|
|
# nostr_public_key = "Go to Edit Profile to Upgrade"
|
|
|
|
|
|
|
|
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,
|
|
needs_warning=needs_warning
|
|
)
|
|
|
|
|
|
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"
|
|
)
|
|
|
|
# Warnings for unsupported browser:
|
|
needs_warning = _evaluate_app_warnings(profile_type=profile_type, application=application)
|
|
|
|
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
|
|
)
|
|
|
|
|
|
####################################################
|
|
### Above this line is the creation of the DTO
|
|
|
|
### Below is the use of the DTO in the display
|
|
|
|
|
|
def get_display_text(dto: RenderProfileDto) -> tuple:
|
|
"""Create the display text for when a profile is selected on the menu page."""
|
|
location_text = dto.location_text
|
|
operator_name = dto.operator_name
|
|
nostr_public_key = dto.nostr_public_key
|
|
needs_warning = dto.needs_warning
|
|
|
|
if needs_warning:
|
|
top_text = "Upgrade browser!"
|
|
bottom_text = "Go to Edit Profile, and Pick a New Browser."
|
|
else:
|
|
top_text = f"Location: {location_text}\n\nOperator: {operator_name}"
|
|
bottom_text = f"Nostr: {nostr_public_key}"
|
|
|
|
return top_text, bottom_text
|
|
|
|
|
|
def get_display_color(dto: RenderProfileDto) -> str:
|
|
"""Pick the display color, based on the profile type, from the DTO"""
|
|
profile_type = dto.profile_type
|
|
color = "black" if profile_type == ProfileType.SESSION else "white"
|
|
return color |