2014-11-12 20:41:29 -06:00
# -*- coding: utf-8 -*-
2022-02-24 12:25:45 -06:00
""" Base GUI (Wx) class for the Session manager module."""
2014-11-12 20:41:29 -06:00
import wx
2023-02-03 10:29:21 -06:00
import application
2022-02-24 12:25:45 -06:00
from pubsub import pub
2014-11-12 20:41:29 -06:00
from multiplatform_widgets import widgets
2018-08-17 05:12:49 -05:00
2014-11-12 20:41:29 -06:00
class sessionManagerWindow ( wx . Dialog ):
2022-02-24 12:25:45 -06:00
""" Dialog that displays all session managing capabilities to users. """
2021-06-16 16:18:41 -05:00
def __init__ ( self ):
super ( sessionManagerWindow , self ) . __init__ ( parent = None , title = _ ( u "Session manager" ), size = wx . DefaultSize )
panel = wx . Panel ( self )
sizer = wx . BoxSizer ( wx . VERTICAL )
label = wx . StaticText ( panel , - 1 , _ ( u "Accounts list" ), size = wx . DefaultSize )
listSizer = wx . BoxSizer ( wx . HORIZONTAL )
self . list = widgets . list ( panel , _ ( u "Account" ), style = wx . LC_SINGLE_SEL | wx . LC_REPORT )
listSizer . Add ( label , 0 , wx . ALL , 5 )
listSizer . Add ( self . list . list , 0 , wx . ALL , 5 )
sizer . Add ( listSizer , 0 , wx . ALL , 5 )
self . new = wx . Button ( panel , - 1 , _ ( u "New account" ), size = wx . DefaultSize )
2022-02-24 12:25:45 -06:00
self . new . Bind ( wx . EVT_BUTTON , self . on_new_account )
2021-06-16 16:18:41 -05:00
self . remove = wx . Button ( panel , - 1 , _ ( u "Remove account" ))
2022-02-24 12:25:45 -06:00
self . remove . Bind ( wx . EVT_BUTTON , self . on_remove )
2021-06-16 16:18:41 -05:00
self . configuration = wx . Button ( panel , - 1 , _ ( u "Global Settings" ))
2022-02-24 12:25:45 -06:00
self . configuration . Bind ( wx . EVT_BUTTON , self . on_configuration )
2021-06-16 16:18:41 -05:00
ok = wx . Button ( panel , wx . ID_OK , size = wx . DefaultSize )
ok . SetDefault ()
cancel = wx . Button ( panel , wx . ID_CANCEL , size = wx . DefaultSize )
buttons = wx . BoxSizer ( wx . HORIZONTAL )
buttons . Add ( self . new , 0 , wx . ALL , 5 )
buttons . Add ( self . configuration , 0 , wx . ALL , 5 )
buttons . Add ( ok , 0 , wx . ALL , 5 )
buttons . Add ( cancel , 0 , wx . ALL , 5 )
sizer . Add ( buttons , 0 , wx . ALL , 5 )
panel . SetSizer ( sizer )
min = sizer . CalcMin ()
self . SetClientSize ( min )
2014-11-12 20:41:29 -06:00
2021-06-16 16:18:41 -05:00
def fill_list ( self , sessionsList ):
for i in sessionsList :
self . list . insert_item ( False , i )
if self . list . get_count () > 0 :
self . list . select_item ( 0 )
self . list . list . SetSize ( self . list . list . GetBestSize ())
2014-11-12 20:41:29 -06:00
2021-06-16 16:18:41 -05:00
def ok ( self , ev ):
if self . list . get_count () == 0 :
wx . MessageDialog ( None , _ ( u "You need to configure an account." ), _ ( u "Account Error" ), wx . ICON_ERROR ) . ShowModal ()
return
self . EndModal ( wx . ID_OK )
2014-11-12 20:41:29 -06:00
2022-02-24 12:25:45 -06:00
def on_new_account ( self , * args , ** kwargs ):
menu = wx . Menu ()
mastodon = menu . Append ( wx . ID_ANY , _ ( "Mastodon" ))
menu . Bind ( wx . EVT_MENU , self . on_new_mastodon_account , mastodon )
2025-05-30 16:16:21 +00:00
2026-01-10 19:46:53 +01:00
blueski = menu . Append ( wx . ID_ANY , _ ( "Blueski (Bluesky)" ))
menu . Bind ( wx . EVT_MENU , self . on_new_blueski_account , blueski )
2025-05-30 16:16:21 +00:00
2022-02-24 12:25:45 -06:00
self . PopupMenu ( menu , self . new . GetPosition ())
def on_new_mastodon_account ( self , * args , ** kwargs ):
dlg = wx . MessageDialog ( self , _ ( "You will be prompted for your Mastodon data (instance URL, email address and password) so we can authorise TWBlue in your instance. Would you like to authorise your account now?" ), _ ( u "Authorization" ), wx . YES_NO )
response = dlg . ShowModal ()
dlg . Destroy ()
if response == wx . ID_YES :
pub . sendMessage ( "sessionmanager.new_account" , type = "mastodon" )
2026-01-10 19:46:53 +01:00
def on_new_blueski_account ( self , * args , ** kwargs ):
dlg = wx . MessageDialog ( self , _ ( "You will be prompted for your Blueski (Bluesky) data (user handle and App Password) to authorize TWBlue. Would you like to authorize your account now?" ), _ ( u "Blueski Authorization" ), wx . YES_NO )
2025-05-26 14:11:01 +00:00
response = dlg . ShowModal ()
dlg . Destroy ()
if response == wx . ID_YES :
2026-01-10 19:46:53 +01:00
pub . sendMessage ( "sessionmanager.new_account" , type = "blueski" )
2025-05-26 14:11:01 +00:00
2021-06-16 16:18:41 -05:00
def add_new_session_to_list ( self ):
total = self . list . get_count ()
name = _ ( u "Authorized account %d " ) % ( total + 1 )
self . list . insert_item ( False , name )
if self . list . get_count () == 1 :
self . list . select_item ( 0 )
2015-03-05 11:27:48 -06:00
2021-06-16 16:18:41 -05:00
def show_unauthorised_error ( self ):
wx . MessageDialog ( None , _ ( u "Your access token is invalid or the authorization has failed. Please try again." ), _ ( u "Invalid user token" ), wx . ICON_ERROR ) . ShowModal ()
2015-03-05 11:27:48 -06:00
2021-06-16 16:18:41 -05:00
def get_response ( self ):
return self . ShowModal ()
2015-03-05 11:27:48 -06:00
2022-02-24 12:25:45 -06:00
def on_remove ( self , * args , ** kwargs ):
dlg = wx . MessageDialog ( self , _ ( u "Do you really want to delete this account?" ), _ ( u "Remove account" ), wx . YES_NO )
response = dlg . ShowModal ()
dlg . Destroy ()
if response == wx . ID_YES :
selected = self . list . get_selected ()
pub . sendMessage ( "sessionmanager.remove_account" , index = selected )
def on_configuration ( self , * args , ** kwargs ):
pub . sendMessage ( "sessionmanager.configuration" )
2015-03-05 11:27:48 -06:00
2021-06-16 16:18:41 -05:00
def get_selected ( self ):
return self . list . get_selected ()
2015-03-05 11:27:48 -06:00
2021-06-16 16:18:41 -05:00
def remove_session ( self , sessionID ):
self . list . remove_item ( sessionID )
2015-03-05 11:27:48 -06:00
2021-06-16 16:18:41 -05:00
def hide_configuration ( self ):
self . configuration . Hide ()
2015-06-26 08:19:40 -05:00
2021-06-16 16:18:41 -05:00
def destroy ( self ):
self . Destroy ()