2014-11-12 20:41:29 -06:00
# -*- coding: utf-8 -*-
2015-03-05 11:27:48 -06:00
import shutil
import widgetUtils
2015-03-31 17:46:15 -06:00
import platform
2016-01-30 10:14:41 -06:00
import output
2015-03-31 17:46:15 -06:00
if platform . system ( ) == " Windows " :
import wxUI as view
2015-04-22 15:34:44 -05:00
from controller import settings
2015-03-31 17:46:15 -06:00
elif platform . system ( ) == " Linux " :
import gtkUI as view
2014-11-12 20:41:29 -06:00
import paths
import time
import os
2015-01-18 17:19:39 -06:00
import logging
2014-11-12 20:41:29 -06:00
import session
import manager
2015-03-30 10:55:56 -06:00
import config_utils
2015-01-02 09:55:27 -06:00
import config
2014-11-12 20:41:29 -06:00
2015-01-18 17:19:39 -06:00
log = logging . getLogger ( " sessionmanager.sessionManager " )
2014-11-12 20:41:29 -06:00
class sessionManagerController ( object ) :
2015-04-22 15:34:44 -05:00
def __init__ ( self , started = False ) :
2014-11-12 20:41:29 -06:00
super ( sessionManagerController , self ) . __init__ ( )
2015-01-18 17:19:39 -06:00
log . debug ( " Setting up the session manager. " )
2015-04-22 15:34:44 -05:00
self . started = started
2014-11-12 20:41:29 -06:00
manager . setup ( )
2015-03-05 11:27:48 -06:00
self . view = view . sessionManagerWindow ( )
widgetUtils . connect_event ( self . view . new , widgetUtils . BUTTON_PRESSED , self . manage_new_account )
widgetUtils . connect_event ( self . view . remove , widgetUtils . BUTTON_PRESSED , self . remove )
2015-04-22 15:34:44 -05:00
if self . started == False :
widgetUtils . connect_event ( self . view . configuration , widgetUtils . BUTTON_PRESSED , self . configuration )
else :
self . view . hide_configuration ( )
2015-03-04 17:52:27 -06:00
self . new_sessions = { }
2015-03-05 11:27:48 -06:00
self . removed_sessions = [ ]
2014-11-12 20:41:29 -06:00
def fill_list ( self ) :
sessionsList = [ ]
2015-01-18 17:19:39 -06:00
log . debug ( " Filling the sessions list. " )
2014-11-12 20:41:29 -06:00
self . sessions = [ ]
for i in os . listdir ( paths . config_path ( ) ) :
2015-02-26 15:21:26 -06:00
if os . path . isdir ( paths . config_path ( i ) ) :
2015-01-18 17:19:39 -06:00
log . debug ( " Adding session %s " % ( i , ) )
2014-11-12 20:41:29 -06:00
strconfig = " %s /session.conf " % ( paths . config_path ( i ) )
2015-03-30 10:55:56 -06:00
config_test = config_utils . load_config ( strconfig )
2016-05-13 12:09:38 -05:00
if len ( config_test ) == 0 :
try :
log . debug ( " Deleting session %s " % ( i , ) )
shutil . rmtree ( paths . config_path ( i ) )
continue
except :
output . speak ( " An exception was raised while attempting to clean malformed session data. See the error log for details. If this message persists, contact the developers. " , True )
os . exception ( " Exception thrown while removing malformed session " )
continue
2014-11-12 20:41:29 -06:00
name = config_test [ " twitter " ] [ " user_name " ]
2015-03-03 13:48:57 -06:00
if config_test [ " twitter " ] [ " user_key " ] != " " and config_test [ " twitter " ] [ " user_secret " ] != " " :
2014-11-12 20:41:29 -06:00
sessionsList . append ( name )
self . sessions . append ( i )
2015-05-13 22:20:54 -05:00
else :
try :
log . debug ( " Deleting session %s " % ( i , ) )
shutil . rmtree ( paths . config_path ( i ) )
except :
output . speak ( " An exception was raised while attempting to clean malformed session data. See the error log for details. If this message persists, contact the developers. " , True )
os . exception ( " Exception thrown while removing malformed session " )
2015-03-05 11:27:48 -06:00
self . view . fill_list ( sessionsList )
2014-11-12 20:41:29 -06:00
def show ( self ) :
2015-03-05 11:27:48 -06:00
if self . view . get_response ( ) == widgetUtils . OK :
self . do_ok ( )
2015-04-03 16:57:08 -06:00
# else:
self . view . destroy ( )
2014-11-12 20:41:29 -06:00
def do_ok ( self ) :
2015-01-18 17:19:39 -06:00
log . debug ( " Starting sessions... " )
2014-11-12 20:41:29 -06:00
for i in self . sessions :
2015-03-04 17:52:27 -06:00
if session . sessions . has_key ( i ) == True : continue
2014-11-12 20:41:29 -06:00
s = session . Session ( i )
s . get_configuration ( )
2015-02-26 15:21:26 -06:00
if i not in config . app [ " sessions " ] [ " ignored_sessions " ] :
s . login ( )
2014-11-12 20:41:29 -06:00
session . sessions [ i ] = s
2015-03-04 17:52:27 -06:00
self . new_sessions [ i ] = s
2015-04-03 16:57:08 -06:00
# self.view.destroy()
2014-11-12 20:41:29 -06:00
2015-03-05 11:27:48 -06:00
def manage_new_account ( self , * args , * * kwargs ) :
if self . view . new_account_dialog ( ) == widgetUtils . YES :
location = ( str ( time . time ( ) ) [ - 6 : ] )
log . debug ( " Creating session in the %s path " % ( location , ) )
s = session . Session ( location )
manager . manager . add_session ( location )
s . get_configuration ( )
2015-04-03 16:57:08 -06:00
# try:
s . authorise ( )
self . sessions . append ( location )
self . view . add_new_session_to_list ( )
2017-01-14 06:11:41 -06:00
s . settings . write ( )
2015-04-03 16:57:08 -06:00
# except:
# log.exception("Error authorising the session")
# self.view.show_unauthorised_error()
# return
2015-03-05 11:27:48 -06:00
def remove ( self , * args , * * kwargs ) :
if self . view . remove_account_dialog ( ) == widgetUtils . YES :
selected_account = self . sessions [ self . view . get_selected ( ) ]
self . view . remove_session ( self . view . get_selected ( ) )
self . removed_sessions . append ( selected_account )
self . sessions . remove ( selected_account )
shutil . rmtree ( path = paths . config_path ( selected_account ) , ignore_errors = True )
2015-04-03 16:57:08 -06:00
2015-04-22 15:34:44 -05:00
def configuration ( self , * args , * * kwargs ) :
""" Opens the global settings dialogue. """
d = settings . globalSettingsController ( )
if d . response == widgetUtils . OK :
d . save_configuration ( )