2014-10-27 16:29:04 -06:00
# -*- coding: utf-8 -*-
2015-04-03 16:57:08 -06:00
import platform
2018-12-31 15:14:09 -06:00
from win32com . client import GetObject
2015-04-03 16:57:08 -06:00
""" there are lots of things not implemented for Gtk+ yet.
We ' ve started this effort 1 Apr 2015 so it isn ' t fully functional . We will remove the ifs statements when are no needed """
system = platform . system ( )
if system == " Windows " :
import sys
import os
#redirect the original stdout and stderr
stdout = sys . stdout
stderr = sys . stderr
sys . stdout = open ( os . path . join ( os . getenv ( " temp " ) , " stdout.log " ) , " w " )
sys . stderr = open ( os . path . join ( os . getenv ( " temp " ) , " stderr.log " ) , " w " )
2014-11-12 20:41:29 -06:00
import languageHandler
2014-10-27 16:29:04 -06:00
import paths
2016-01-20 02:18:27 -06:00
#check if TWBlue is installed (Windows only)
2019-06-06 11:52:23 -05:00
# ToDo: Remove this soon as this is done already when importing the paths module.
if os . path . exists ( os . path . join ( paths . app_path ( ) , " Uninstall.exe " ) ) :
2016-01-20 02:18:27 -06:00
paths . mode = " installed "
2014-10-27 16:29:04 -06:00
import commandline
2014-11-12 20:41:29 -06:00
import config
import output
2015-01-19 12:15:57 -06:00
import logging
import application
2015-02-04 17:00:03 -06:00
import keys
2015-01-27 17:09:28 -06:00
from mysc . thread_utils import call_threaded
2015-02-22 18:45:26 -06:00
import fixes
2015-04-03 16:57:08 -06:00
import widgetUtils
2015-07-29 14:05:26 -05:00
import webbrowser
from wxUI import commonMessageDialogs
2015-04-03 16:57:08 -06:00
if system == " Windows " :
from logger import logger
from update import updater
stdout_temp = sys . stdout
stderr_temp = sys . stderr
2015-02-28 08:31:19 -06:00
#if it's a binary version
2015-04-03 16:57:08 -06:00
if hasattr ( sys , ' frozen ' ) :
2019-06-06 11:52:23 -05:00
sys . stderr = open ( os . path . join ( paths . logs_path ( ) , " stderr.log " ) , ' w ' )
sys . stdout = open ( os . path . join ( paths . logs_path ( ) , " stdout.log " ) , ' w ' )
2015-04-03 16:57:08 -06:00
else :
sys . stdout = stdout
sys . stderr = stderr
2018-08-12 05:35:43 -05:00
# We are running from source, let's prepare vlc module for that situation
if system == " Windows " :
arch = " x86 "
if platform . architecture ( ) [ 0 ] [ : 2 ] == " 64 " :
arch = " x64 "
2019-06-06 11:52:23 -05:00
os . environ [ ' PYTHON_VLC_MODULE_PATH ' ] = os . path . abspath ( os . path . join ( paths . app_path ( ) , " .. " , " windows-dependencies " , arch ) )
os . environ [ ' PYTHON_VLC_LIB_PATH ' ] = os . path . abspath ( os . path . join ( paths . app_path ( ) , " .. " , " windows-dependencies " , arch , " libvlc.dll " ) )
2018-08-12 05:35:43 -05:00
#the final log files have been opened succesfully, let's close the temporary files
2015-04-03 16:57:08 -06:00
stdout_temp . close ( )
stderr_temp . close ( )
2018-08-12 06:38:39 -05:00
#finally, remove the temporary files. TW Blue doesn't need them anymore, and we will get more free space on the harddrive
2015-04-03 16:57:08 -06:00
os . remove ( stdout_temp . name )
os . remove ( stderr_temp . name )
2018-08-12 06:38:39 -05:00
import sound
2015-04-03 16:57:08 -06:00
if system == " Linux " :
from gi . repository import Gdk , GObject , GLib
2015-01-19 12:15:57 -06:00
log = logging . getLogger ( " main " )
2014-11-12 20:41:29 -06:00
def setup ( ) :
2015-04-19 18:10:34 -05:00
log . debug ( " Starting " + application . name + " %s " % ( application . version , ) )
2014-11-12 20:41:29 -06:00
config . setup ( )
2021-01-22 10:40:48 -06:00
proxy_setup ( )
2015-01-19 12:15:57 -06:00
log . debug ( " Using %s %s " % ( platform . system ( ) , platform . architecture ( ) [ 0 ] ) )
log . debug ( " Application path is %s " % ( paths . app_path ( ) , ) )
2015-02-22 11:38:22 -06:00
log . debug ( " config path is %s " % ( paths . config_path ( ) , ) )
2014-11-12 20:41:29 -06:00
sound . setup ( )
languageHandler . setLanguage ( config . app [ " app-settings " ] [ " language " ] )
2015-10-18 05:19:43 -05:00
fixes . setup ( )
2019-02-20 10:40:08 -06:00
output . setup ( )
2015-02-04 17:00:03 -06:00
keys . setup ( )
2019-06-06 11:52:23 -05:00
from controller import settings
2014-11-12 20:41:29 -06:00
from controller import mainController
from sessionmanager import sessionManager
2015-04-03 16:57:08 -06:00
app = widgetUtils . mainLoopObject ( )
2021-01-22 11:50:09 -06:00
check_pid ( )
2015-04-03 16:57:08 -06:00
if system == " Windows " :
2015-07-29 14:05:26 -05:00
if config . app [ " app-settings " ] [ " donation_dialog_displayed " ] == False :
donation ( )
2016-04-15 17:04:03 -05:00
if config . app [ ' app-settings ' ] [ ' check_for_updates ' ] :
updater . do_update ( )
2014-11-12 20:41:29 -06:00
sm = sessionManager . sessionManagerController ( )
sm . fill_list ( )
if len ( sm . sessions ) == 0 : sm . show ( )
else :
sm . do_ok ( )
2015-04-03 16:57:08 -06:00
if hasattr ( sm . view , " destroy " ) :
sm . view . destroy ( )
2014-11-12 20:41:29 -06:00
del sm
r = mainController . Controller ( )
2015-04-03 16:57:08 -06:00
r . view . show ( )
2015-01-27 17:09:28 -06:00
r . do_work ( )
2015-03-09 15:55:34 -06:00
r . check_invisible_at_startup ( )
2015-04-03 16:57:08 -06:00
if system == " Windows " :
call_threaded ( r . start )
elif system == " Linux " :
GLib . idle_add ( r . start )
app . run ( )
2014-11-12 20:41:29 -06:00
2021-01-22 10:40:48 -06:00
def proxy_setup ( ) :
if config . app [ " proxy " ] [ " server " ] != " " and config . app [ " proxy " ] [ " type " ] > 0 :
log . debug ( " Loading proxy settings " )
proxy_url = config . app [ " proxy " ] [ " server " ] + " : " + str ( config . app [ " proxy " ] [ " port " ] )
if config . app [ " proxy " ] [ " user " ] != " " and config . app [ " proxy " ] [ " password " ] != " " :
proxy_url = config . app [ " proxy " ] [ " user " ] + " : " + config . app [ " proxy " ] [ " password " ] + " @ " + proxy_url
2021-01-22 10:54:47 -06:00
elif config . app [ " proxy " ] [ " user " ] != " " and config . proxyTypes [ config . app [ " proxy " ] [ " type " ] ] in [ " socks4 " , " socks4a " ] :
proxy_url = config . app [ " proxy " ] [ " user " ] + " @ " + proxy_url
2021-01-22 10:40:48 -06:00
proxy_url = config . proxyTypes [ config . app [ " proxy " ] [ " type " ] ] + " :// " + proxy_url
os . environ [ " HTTP_PROXY " ] = proxy_url
os . environ [ " HTTPS_PROXY " ] = proxy_url
2015-07-29 14:05:26 -05:00
def donation ( ) :
dlg = commonMessageDialogs . donation ( )
if dlg == widgetUtils . YES :
2017-05-06 14:34:50 -05:00
webbrowser . open_new_tab ( _ ( " https://twblue.es/donate " ) )
2015-07-29 14:05:26 -05:00
config . app [ " app-settings " ] [ " donation_dialog_displayed " ] = True
2018-12-31 15:14:09 -06:00
def is_running ( pid ) :
" Check if the process with ID pid is running. Adapted from https://stackoverflow.com/a/568589 "
WMI = GetObject ( ' winmgmts: ' )
processes = WMI . InstancesOf ( ' Win32_Process ' )
return [ process . Properties_ ( ' ProcessID ' ) . Value for process in processes if process . Properties_ ( ' ProcessID ' ) . Value == pid ]
def check_pid ( ) :
" Insures that only one copy of the application is running at a time. "
pidpath = os . path . join ( os . getenv ( " temp " ) , " {} .pid " . format ( application . name ) )
if os . path . exists ( pidpath ) :
with open ( pidpath ) as fin :
pid = int ( fin . read ( ) )
if is_running ( pid ) :
# Display warning dialog
commonMessageDialogs . common_error ( _ ( u " {0} is already running. Close the other instance before starting this one. If you ' re sure that {0} isn ' t running, try deleting the file at {1} . If you ' re unsure of how to do this, contact the {0} developers. " ) . format ( application . name , pidpath ) )
sys . exit ( 1 )
else :
commonMessageDialogs . dead_pid ( )
# Write the new PID
with open ( pidpath , " w " ) as cam :
cam . write ( str ( os . getpid ( ) ) )
2015-04-03 16:57:08 -06:00
setup ( )