# -*- coding: euc-kr -*-

# ÇöÀç programÀÇ »óÈ²À» ³ªÅ¸³»´Â º¯¼öµé

from roxiadebug import *
import config
#import errorcode   # moved since importing procedence

outgoing_ani_index = -1

#Roxia Begin smyook 06.07.06
wifi_dhcp_chFirst = False
#Roxia End smyook
profile_detail_changed = False
# 06.07.04 eicho
#on_profile_changing = False

voip_setting_changed = False

not_screen_saver = False

supervision_not_allowed = 0

#ka...3800 codec 2007.04.26
wideband_codec = 0 #default = False
receiveCallInIncoming = False

slideshow_preview = False
slideshow_running = False

current_display_hour = 0
current_display_minute = 0

ext_gallery_copy_error = 0 # 0: NoError, -1: Duplication, -2: Copy Failure, -3: Unsupported Format
ext_gallery_copy_error_filename = ''

callErrorSet = False

wifi_test = False

# 20050921
# Narrow band(PSTN), Broad band(IP) °£¿¡ Videocall½Ã »ç¿ë
indirect_cid = 0
from_none = 0
from_bb = 1
from_nb = 2
WhatFrom = from_none
def FromDiffNetwork():
	global WhatFrom, from_bb, from_nb
	import profile
	if profile.ip_mode == 0:
		if WhatFrom == from_bb:
			return True
	else:
		if WhatFrom == from_nb:
			return True
	return False

def CheckRequestDtmf(dtmf):
	global WhatFrom, from_bb, from_nb, indirect_cid
	import config
	if dtmf == config.videocall_request_dtmf_nb:
		WhatFrom = from_nb
		indirect_cid = caller_number
	elif dtmf == config.videocall_request_dtmf_bb:
		WhatFrom = from_bb
		indirect_cid = caller_number
	else:
		return False
	return True

def CheckAnswerDtmf(dtmf):
	global WhatFrom, from_bb, from_nb, indirect_cid
	import config
	if dtmf == config.videocall_answer_dtmf_nb:
		WhatFrom = from_nb
		indirect_cid = caller_number
	elif dtmf == config.videocall_answer_dtmf_bb:
		WhatFrom = from_bb
		indirect_cid = caller_number
	else:
		return False
	return True

def VideoRequestDtmf():
	import profile, config
	if profile.ip_mode == 0:
		return config.videocall_request_dtmf_nb
	return config.videocall_request_dtmf_bb

def VideoAnswerDtmf():
	import profile, config
	if profile.ip_mode == 0:
		return config.videocall_answer_dtmf_nb
	return config.videocall_answer_dtmf_bb

supervision_idle = 0
supervision_remote = 0
supervision_call_on = 0
wifi_connected = False
phonebook_emergency = False
phonebook_in_calling = False
emergency_call_number = None
phonebook_isempty = False

password_style = False

profile_changing = False

critical_entered = False

video_pid = None

Disconnected = 0
VoiceConnected = 1
PPPConnected = 2
Dialing = 3

phone_status = Disconnected

OneCallConnected =  0
TwoCallOneHold  = 1
TwoCallConnected = 2
TwoCallOneIncoming = 3
TwoCallOneDialing = 4

business_call_status = OneCallConnected

NotSelected = 0
CallHoldOption = 1
CallTransferOption = 2
BlindTransferOption = 3
CallConferenceOption = 4
CallAlternateOption = 5

business_option_status = NotSelected

business_option_request_status = 0

# Call status
NotSelected = 0
IncomingCall = 1
OutgoingCall = 2

#hcryoo : [20070416_1]
IncomingCallSupervison = False
#hcryoo : [20070416_1]==

call_status = NotSelected
first_call_status = NotSelected
second_call_status = NotSelected

service_name = ''
first_service_name = ''
second_service_name = ''

active_call_index = 0

# hcryoo : [20070601_1] / changing port range 50000
start_ch = 1
# hcryoo : [20070601_1]==
outgoing_ch = 0

first_active_ch = 0
second_active_ch = 0

mwi_notification_window = 0
mwi_normal_new = 0
mwi_normal_old = 0
mwi_urgent_new = 0
mwi_urgent_old = 0

phone_hook = True

IDLE, HS, SPK = 'idle', 'handset', 'speaker'
modem_audio_state = IDLE
modem_audio_state_for_special_cid = IDLE

special_cid = False
special_nas_number = ''
pstn_indirect_video = False

AnalogOption = False

video_number_on = False

low_storage_in_message = False

def set_modem_audio_state(state):
	from setting import setting
	global modem_audio_state

	modem_audio_state = state

	import runtime, config
	if not runtime.manager:
		return

	if config.audio_debug:
		print 'set audio state: stage', runtime.manager.stage.name, 'state', state
	if runtime.manager.stage.name == 'connected' or runtime.manager.stage.name == 'VideoCallConnected' or runtime.manager.stage.name == 'PSTNTwoCallOneHold' :
		stage = runtime.manager.stage
		if state in (HS, IDLE):
			stage.ui.hands_free_image.hide()
		else:
			stage.ui.hands_free_image.show()
#MMW move to dtmf.py
	'''
	if config.dual_audio_volume:
		if state == HS:
			setting.set_volume(setting.handset_volume)
		elif state == SPK:
			setting.set_volume(setting.speaker_volume)
	'''
#end of MMW

# will be inited at AddEntryStage
phone_item = None
#Thang[20080324]
alarm_item = None
editing_alarm_index = -1
editing_calendar_index = -1
calendar_item = None
warning_item = None
#Thang[20080324] =====================

background_select_index = 0
privacy_select_index = 0

# hcryoo : [20070518_2]
first_active_ch_hold = False
second_active_ch_hold = False

first_active_ch1_held = False
second_active_ch1_held = False
first_active_ch2_held = False
second_active_ch2_held = False

# hcryoo : [20070525_4]
def toggle_all_hold_and_held_flag():
	global first_active_ch_hold, second_active_ch_hold
	global first_active_ch1_held, second_active_ch1_held
	global first_active_ch2_held, second_active_ch2_held

	temp_active_ch_hold = first_active_ch_hold
	temp_active_ch1_held = first_active_ch1_held
	temp_active_ch2_held = first_active_ch2_held

	first_active_ch_hold = second_active_ch_hold
	first_active_ch1_held = second_active_ch1_held
	first_active_ch2_held = second_active_ch2_held

	second_active_ch_hold = temp_active_ch_hold
	second_active_ch1_held = temp_active_ch1_held
	second_active_ch2_held = temp_active_ch2_held
# hcryoo : [20070525_4]==
	
def reset_all_hold_and_held_flag():
	global first_active_ch_hold, second_active_ch_hold
	global first_active_ch1_held, second_active_ch1_held
	global first_active_ch2_held, second_active_ch2_held

	first_active_ch_hold = False
	second_active_ch_hold = False
	first_active_ch1_held = False
	second_active_ch1_held = False
	first_active_ch2_held = False
	second_active_ch2_held = False

def reset_all_hold_flag():
	global first_active_ch_hold, second_active_ch_hold

	first_active_ch_hold = False
	second_active_ch_hold = False

def reset_all_held_flag():
	global first_active_ch1_held, second_active_ch1_held
	global first_active_ch2_held, second_active_ch2_held

	first_active_ch1_held = False
	second_active_ch1_held = False
	first_active_ch2_held = False
	second_active_ch2_held = False
		
def get_hold_flag(index):
	global first_active_ch_hold, second_active_ch_hold
	import config
	if config.roxia_event:
		print 'GET HOLD_FLAG ::', index
		print first_active_ch_hold, second_active_ch_hold
	if index == 0:
		return first_active_ch_hold
	else:
		return second_active_ch_hold
		
def set_hold_flag(index):
	global first_active_ch_hold, second_active_ch_hold
	import config
	if config.roxia_event:
		print 'SET_HOLD_FLAG :: ', index
	if index == 0:
		first_active_ch_hold = True
	else:
		second_active_ch_hold = True
	if config.roxia_event:
		print first_active_ch_hold, second_active_ch_hold
	
def reset_hold_flag(index):
	global first_active_ch_hold, second_active_ch_hold
	import config
	if config.roxia_event:
		print 'RESET_HOLD_FLAG :: ', index
	if index == 0:
		first_active_ch_hold = False
	else:
		second_active_ch_hold = False
	if config.roxia_event:
		print first_active_ch_hold, second_active_ch_hold

def get_held_flag(index):
	global first_active_ch1_held, second_active_ch1_held
	global first_active_ch2_held, second_active_ch2_held
	import config
	if config.roxia_event:
		print 'GET HELD_FLAG ::', index
		print first_active_ch1_held, first_active_ch2_held, second_active_ch1_held, second_active_ch2_held
	if index == 0:
		if first_active_ch2_held:
			return first_active_ch2_held
		else:
			return first_active_ch1_held
	else:
		if second_active_ch2_held:
			return second_active_ch2_held
		else:
			return second_active_ch1_held

def set_held_flag(index):
	import config
	if config.roxia_event:
		print 'SET_HELD_FLAG :: ', index

	global first_active_ch1_held, second_active_ch1_held
	global first_active_ch2_held, second_active_ch2_held
	if index == 0:
		if not first_active_ch1_held :
			first_active_ch1_held = True
		else:
			first_active_ch2_held = True
	else:
		if not second_active_ch1_held :
			second_active_ch1_held = True
		else:
			second_active_ch2_held = True

	import config
	if config.roxia_event:
		print first_active_ch1_held, first_active_ch2_held, second_active_ch1_held, second_active_ch2_held

def reset_held_flag(index):
	global first_active_ch1_held, second_active_ch1_held
	global first_active_ch2_held, second_active_ch2_held
	import config
	if config.roxia_event:
		print 'RESET_HELD_FLAG :: ', index
	if index == 0:
		if first_active_ch2_held :
			first_active_ch2_held = False
		else:
			first_active_ch1_held = False
	else:
		if second_active_ch2_held:
			second_active_ch2_held = False
		else:
			second_active_ch1_held = False

	import config
	if config.roxia_event:
		print first_active_ch1_held, first_active_ch2_held, second_active_ch1_held, second_active_ch2_held

# hcryoo : [20070518_2]==

def get_current_channel():
	global active_call_index
	if active_call_index == 0:
		return first_active_ch
	else:
		return second_active_ch

# 06.11.28 eicho add.
def get_inactive_channel():
	global active_call_index
	if active_call_index == 0:
		return second_active_ch
	else:
		return first_active_ch
# end

def get_video_mode(index):
	global first_video_mode, second_video_mode
	if index == 0:
		return first_video_mode
	else:
		return second_video_mode

def display_caller_number(index):
	global first_caller_number, first_dial_number
	global second_caller_number, second_dial_number
	number = ''

	if index == 0:
		if first_caller_number:
			number = first_caller_number
		else:
			number = first_dial_number
	else:
		if second_caller_number:
			number = second_caller_number
		else:
			number = second_dial_number
	return number

def display_caller_name(index):
	from setting import setting
	global first_caller_number, first_dial_number, first_service_name, first_dialed_by_user
	global second_caller_number, second_dial_number, second_service_name, second_dialed_by_user

	number = ''
	name = ''
	if index == 0:
		if first_caller_number:
			number = first_caller_number
		else:
			number = first_dial_number

		if first_service_name == _('Unknown'):
			name = _('Unknown')
		elif first_service_name == _('Private'):
			name = _('anonymous')
		else:
			if setting.phone_extension:
				if not first_dialed_by_user and number.startswith(setting.phone_extension):
					number = number[len(setting.phone_extension)+1:]
			from phonedb import phonedb
			name = phonedb.get_name(number)

			if name == number:
				name = first_service_name or number

		# ÀüÈ­¸¦ ¹ÞÀº °æ¿ì¿¡¸¸ text width Á¶Àý
		if first_caller_number:
			import utils
			import uiconfig
			name = utils.resize_text(name, uiconfig.incoming_call_text_width, uiconfig.incoming_call_font)[0]

	else: # index == 1
		if second_caller_number:
			number = second_caller_number
		else:
			number = second_dial_number

		if second_service_name == _('Unknown'):
			name = _('Unknown')
		elif second_service_name == _('Private'):
			name = _('anonymous')
		else:
			if setting.phone_extension:
				if not second_dialed_by_user and number.startswith(setting.phone_extension):
					number = number[len(setting.phone_extension)+1:]
			from phonedb import phonedb
			name = phonedb.get_name(number)

			if name == number:
				name = second_service_name or number

		# ÀüÈ­¸¦ ¹ÞÀº °æ¿ì¿¡¸¸ text width Á¶Àý
		if second_caller_number:
			import utils
			import uiconfig
			name = utils.resize_text(name, uiconfig.incoming_call_text_width, uiconfig.incoming_call_font)[0]

	if not name:
		name = _('anonymous')

	return name

# ÀüÈ­¸¦ °Å´Â °æ¿ì »ó´ë¹æ ÀüÈ­¹øÈ£
dial_number = ''
first_dial_number = ''
second_dial_number = ''

# replaced set_dial_number
def set_dial_number(num):
	global first_dial_number, second_dial_number
	global active_call_index, dial_number
	if active_call_index == 0:
		first_dial_number = num
	else:
		second_dial_number = num
	dial_number = num

def set_caller_number(num):
	global first_caller_number, second_caller_number
	global active_call_index, caller_number
	if active_call_index == 0:
		first_caller_number = num
	else:
		second_caller_number = num
	caller_number = num

def set_service_name(service_name_value):
	global service_name, active_call_index
	global first_service_name, second_service_name
	if active_call_index == 0:
		first_service_name = service_name_value
	else:
		second_service_name = service_name_value
	service_name = service_name_value

def set_video_mode(value):
	global video_mode, active_call_index
	global first_video_mode, second_video_mode
	if active_call_index == 0:
		first_video_mode = value
	else:
		second_video_mode = value
	video_mode = value

# ÀüÈ­ ÅëÈ­ ½ÃÀÛ ½Ã°£
call_time = -1	# set -1 as default 10.19
video_call_time = -1 # set -1 as default 10.19

first_call_time = -1 # set -1 as default 10.19
second_call_time = -1 # set -1 as default 10.19

def set_call_time():
	global first_call_time, second_call_time
	global active_call_index, call_time
	import time, ntptime
	call_time = ntptime.ntime()
	if active_call_index == 0:
		first_call_time = call_time
	else:
		second_call_time = call_time

# ÀüÈ­¸¦ ¹Þ´Â °æ¿ì »ó´ë¹æ ÀüÈ­¹øÈ£
caller_number = ''
first_caller_number = ''
second_caller_number = ''  
#ka...CLIP name 2006.11.06
caller_CLIP_name = ''

# KA: [20080722] Second call info
first_info_number = ''
second_info_number = ''
second_info_call_duration_start = 0
second_info_call_mode = 0
second_info_time = 0
second_info_accept = False

isConferenceCall = False
isCallWaitingCall = False

# 0310 TIM Mobile phone °úÀÇ ¿¬µ¿
check_mobile_call = False

# received time, date (cid °¡ ¿Ã¶§ °°ÀÌ µé¾î¿Â ½Ã°£,³¯Â¥)
received_time = None
received_date = None
# RHC / [20061018_2]
received_name = None
# RHC / [20061018_2]--
received_call_time = None

stat_reg_status = 0
stat_reg_send_count = 0
stat_reg_receive_count = 0
stat_reg_last_result = 0
stat_reg_last_desc = ''

update_section = False

def get_reg_stat():
	import runtime
	if runtime.vdciapp:
		runtime.vdciapp.get_statistics()

#####
audio_mute = 0
first_audio_mute = 0
second_audio_mute = 0

def release_audio_mute():
	global active_call_index, audio_mute
	global first_audio_mute, second_audio_mute

	if active_call_index == 0:
		first_audio_mute = 0
		audio_mute = first_audio_mute
	else:
		second_audio_mute = 0
		audio_mute = second_audio_mute
	# vpark 06.02.08
	import runtime
	runtime.dspg.audio_mute(audio_mute)

def set_current_audio_mute():
	global active_call_index, audio_mute
	import runtime
	runtime.dspg.audio_mute(audio_mute)

# eicho add release all audio mute. 06.01.19
def clear_audio_mute():
	global audio_mute
	global first_audio_mute, second_audio_mute

	audio_mute = 0
	first_audio_mute = 0
	second_audio_mute = 0

def toggle_active_call():
	global active_call_index, audio_mute
	global first_audio_mute, second_audio_mute
	active_call_index = 1 - active_call_index
	if active_call_index == 0:
		audio_mute = first_audio_mute
	else:
		audio_mute = second_audio_mute

# replaced toggle_audio_mute
def toggle_audio_mute():
	global active_call_index, audio_mute
	global first_audio_mute, second_audio_mute
	global videocall_mode, VideoCallConnected
	global phone_status, VoiceConnected
	global modem_audio_state
	import runtime, vdciapp, config

	if active_call_index == 0:
		first_audio_mute = 1 - first_audio_mute
		audio_mute = first_audio_mute
	else:
		second_audio_mute = 1 - second_audio_mute
		audio_mute = second_audio_mute

	import profile
	if profile.ip_mode == 1:
		# vpark begin 06.02.08
		#runtime.vdciapp.send_mesg(code1=config.MESG_SET_PARAMS, \
		#				code2=config.MESG_SET_AUDIO_MUTE, \
		#				mesg1=audio_mute)
		runtime.dspg.audio_mute(audio_mute)
		# vpark end 06.02.08

	else: # PSTN
		if videocall_mode == VideoCallConnected:
			runtime.vdciapp.send_mesg(code1=config.MESG_SET_PARAMS, \
						code2=config.MESG_SET_AUDIO_MUTE, \
						mesg1=audio_mute)
		else:
			runtime.dspg.audio_mute(audio_mute)

# waiting
waiting = 0
def toggle_waiting():
	global waiting
	waiting = 1 - waiting
	import runtime, utils, config
	if waiting:
		roxia_trace('Enable waiting')
		if modem_audio_state == SPK:
			runtime.dspg.set_state(runtime.dspg.ST_SPK_OFFHK_BPLAY)
		elif modem_audio_state == HS:
			runtime.dspg.set_state(runtime.dspg.ST_HS_OFFHK_BPLAY)

		# play melody to both
		utils.player.play(config.wait_melody, change_state=False)

	else:
		roxia_trace('Disable waiting')
		# stop melody
		utils.player.stop()
		import time
		time.sleep(0.5)
		import runtime
		state = runtime.dspg.get_state()
		# eicho remove it.  06.02.15
		# runtime.dspg.set_state(runtime.dspg.ST_SPK_OFFHK_CONV)

		if state in (runtime.dspg.ST_HS_OFFHK_CONV_MUTE, runtime.dspg.ST_SPK_OFFHK_CONV_MUTE):
			runtime.dspg.set_state(state)
		else:
			if modem_audio_state == SPK:
				runtime.dspg.set_state(runtime.dspg.ST_SPK_OFFHK_CONV)
			elif modem_audio_state == HS:
				runtime.dspg.set_state(runtime.dspg.ST_HS_OFFHK_CONV)
			else:
				# not necessary, but.. 06.02.15
				runtime.dspg.set_state(runtime.dspg.ST_SPK_OFFHK_CONV)
############ test answer delay
heldline = 0
incoming_umts_call = False

ring_timer = None
ring_count = 0
ring_guard_timer = None
ring_continuous = False

###
missed_call_count = 0

def save_current_call_status():
	global call_status, dial_number, caller_number
	global audio_mute, call_time, dialed_by_user
	global service_name, videocall_codec, video_mode
	global active_call_index
	global first_dial_number, first_caller_number, first_call_status
	global first_audio_mute, first_call_time, first_dialed_by_user
	global first_service_name, first_videocall_codec, first_video_mode
	global second_dial_number, second_caller_number, second_call_status
	global second_audio_mute, second_call_time, second_dialed_by_user
	global second_service_name, second_videocall_codec, second_video_mode

	if active_call_index == 0:
		first_dial_number = dial_number
		first_caller_number = caller_number
		first_call_status = call_status
		first_audio_mute = audio_mute
		first_call_time = call_time
		first_dialed_by_user = dialed_by_user
		first_service_name = service_name
		first_videocall_codec = videocall_codec
		first_video_mode = video_mode
	else:
		second_dial_number = dial_number
		second_caller_number = caller_number
		second_call_status = call_status
		second_audio_mute = audio_mute
		second_call_time = call_time
		second_dialed_by_user = dialed_by_user
		second_service_name = service_name
		second_videocall_codec = videocall_codec
		second_video_mode = video_mode

def reset_call_status():
	global active_call_index
	global first_call_status, first_dial_number, first_caller_number
	global first_audio_mute, first_call_time, first_dialed_by_user
	global first_service_name, first_videocall_codec, first_video_mode
	global second_call_status, second_dial_number, second_caller_number
	global second_audio_mute, second_call_time, second_dialed_by_user
	global second_service_name, second_videocall_codec, second_video_mode

	if active_call_index == 0:
		#first_call_status = NotSelected
		first_dial_number = ''
		first_caller_number = ''
		first_audio_mute = 0
		first_call_time = -1
		first_dialed_by_user = False
		first_service_name = ''
		first_videocall_codec = AudioOnly
		first_video_mode = VideoIdle
	else:
		#second_call_status = NotSelected
		second_dial_number = ''
		second_caller_number = ''
		second_audio_mute = 0
		second_call_time = -1
		second_dialed_by_user = False
		second_service_name = ''
		second_videocall_codec = AudioOnly
		second_video_mode = VideoIdle

def clear_current_call_status(clearAudioCallTime=True, clearVideoCallTime=True):
	global call_status, dial_number, caller_number
	global audio_mute, call_time, dialed_by_user
	global service_name, videocall_codec, video_mode
	global video_call_time, heldline

	# 06.07.06 kakim added
	heldline = 0
	call_status = NotSelected
	dial_number = ''
	caller_number = ''
	caller_CLIP_name = ''
	received_name = None
	audio_mute = 0
	if clearAudioCallTime:
		call_time = -1
	if clearVideoCallTime:
		video_call_time = 0
	dialed_by_user = False
	service_name = ''
	videocall_codec = AudioOnly
	video_mode = VideoIdle
	wideband_codec = 0
	second_info_number = ''
	second_info_time = 0
	second_info_accept = False
	

def set_current_call_status():
	global call_status, dial_number, caller_number
	global audio_mute, call_time, dialed_by_user
	global service_name, videocall_codec, video_mode
	global active_call_index
	global first_call_status, first_dial_number, first_caller_number
	global first_audio_mute, first_call_time, first_dialed_by_user
	global first_service_name, first_videocall_codec, first_video_mode
	global second_call_status, second_dial_number, second_caller_number
	global second_audio_mute, second_call_time, second_dialed_by_user
	global second_service_name, second_videocall_codec, second_video_mode

	if active_call_index == 0:
		call_status = first_call_status
		dial_number = first_dial_number
		caller_number = first_caller_number
		audio_mute = first_audio_mute
		call_time = first_call_time
		dialed_by_user = first_dialed_by_user
		service_name = first_service_name
		videocall_codec = first_videocall_codec
		video_mode = first_video_mode
	else:
		call_status = second_call_status
		dial_number = second_dial_number
		caller_number = second_caller_number
		audio_mute = second_audio_mute
		call_time = second_call_time
		dialed_by_user = second_dialed_by_user
		service_name = second_service_name
		videocall_codec = second_videocall_codec
		video_mode = second_video_mode

def assign_outgoing_channel():
	global active_call_index, call_status
	global first_active_ch, second_active_ch
	global first_call_status, second_call_status
	# hcryoo : [20071128_1] incoming call channel ÇÒ´ç º¯°æ
	global outgoing_ch

	outgoing_ch = (outgoing_ch % 4) + 1

	if outgoing_ch == first_active_ch or outgoing_ch == second_active_ch:
		assign_outgoing_channel()
	else:
	# hcryoo : [20071128_1] incoming call channel ÇÒ´ç º¯°æ==
		if active_call_index == 0:
			first_active_ch = outgoing_ch
			first_call_status = OutgoingCall
		else:
			second_active_ch = outgoing_ch
			second_call_status = OutgoingCall
		call_status = OutgoingCall

		roxia_event('after assign outgoing')
		roxia_event('active_call_index: ', active_call_index)
		roxia_event('current channel: ',first_active_ch, '/', second_active_ch)
		roxia_event('call status = ',call_status, ' :: ', first_call_status, '/', second_call_status)

def assign_incoming_channel(channel):
	global active_call_index, call_status
	global first_active_ch, second_active_ch
	global first_call_status, second_call_status

	if active_call_index == 0:
		first_active_ch = int(channel)
		first_call_status = IncomingCall
	else:
		second_active_ch = int(channel)
		second_call_status = IncomingCall
	call_status = IncomingCall

	roxia_event('after assign incoming')
	roxia_event('active_call_index: ', active_call_index)
	roxia_event('current channel: ',first_active_ch, '/', second_active_ch)
	roxia_event('call status: ',call_status, ':::',first_call_status, '/', second_call_status)

def release_channel():
	global active_call_index, call_status
	global first_active_ch, second_active_ch
	global first_call_status, second_call_status

	if first_active_ch == 0 and second_active_ch == 0:
		return

	if active_call_index == 0:
		if first_call_status == IncomingCall:
			first_active_ch = 0
			first_call_status = NotSelected
		else:
			first_active_ch = 0
			first_call_status = NotSelected
	else:
		if second_call_status == IncomingCall:
			second_active_ch = 0
			second_call_status = NotSelected
		else:
			second_active_ch = 0
			second_call_status = NotSelected

	call_status = NotSelected

	roxia_event('after release')
	roxia_event('active_call_index: ', active_call_index)
	roxia_event('current channel: ',first_active_ch, '/', second_active_ch)
	roxia_event('call status: ', call_status, ':::',first_call_status, second_call_status)

# SMS
message_reference = 0


##
calldb_editing_type = ''
calldb_editing_index = -1
calldb_editing_detail = None

# SmsCompose
sms_read_message = None

# EmailWrite
email_read_message = None

# RHC / [20060901_1]
email_inbox_message_all = None
# RHC / [20060901_2]
email_inbox_message_delete_mark_index = None
email_inbox_message_download_mark_index = None
email_inbox_message_delete_index = None
email_inbox_message_download_index = None
# RHC / [20060901_2]
# RHC / [20060901_1]--

# RHC / [20061109_2]
call_during_sending_email = False
# RHC / [20061109_2]--

# RHC / [20061114_2]
now_sending_email = False
# RHC / [20061114_2]--

# melody¸¦ °áÁ¤ÇÏ´Â Stage¿¡¼­ ÆÄ»ýµÈ È­¸éÀÎÁö¸¦ ¾Ë¸²
# ringer.py¿¡¼­ ÁöÁ¤µÇ°í sounds.py¿¡¼­ »ç¿ëµÊ
melody_editing = False

snapshot_editing = False
background_editing = False

# TI ¼­ºñ½º¸¦ ÀÌ¿ëÇÏ´Â °æ¿ì ¹øÈ£¿¡ ´ëÇÑ ¼³¸í
# connection È­¸é¿¡¼­ »ç¿ëµÊ.
# start_callÀÌÀü¿¡ ¹Ì¸® ¼ÂÆÃµÇ¾î¾ßÇÔ.
# stop_call¿¡¼­ Ç×»ó resetµÊ.
service_name = ''

# hcryoo : [20071107_1] : ¿µ»ó ±âº» ºÎ°¡ ¼­ºñ½º¸¦ À§ÇÑ ¿µ»ó ¼­ºñ½º ¼³Á¤
in_service = ''
KT_service = None
# hcryoo : [20071107_1] : ¿µ»ó ±âº» ºÎ°¡ ¼­ºñ½º¸¦ À§ÇÑ ¿µ»ó ¼­ºñ½º ¼³Á¤==

## calling style
dtmf_overlap_mode = False

## ÇöÀç ÆíÁýÇÏ°í ÀÖ´Â phonebook item index
editing_phone_number_index = -1

editing_calendar_number_index = -1
## connected»óÅÂ¿¡¼­ º¸³»°í ÀÖ´Â dtmfµé
# ÀüÈ­¹øÈ£³ª ÆíÁýµÈ ¹øÈ£·Î callÀ» ÇÏ´Â °æ¿ì connected»óÅÂ·Î ÀüÀÌÈÄ
# ÀÏÁ¤ ½Ã°£À» µÎ°í dtmf¸¦ °è¼Ó º¸³»ÁØ´Ù.
sending_dtmf = ''

# hook flash¸¦ º¸³»´Â timer
hook_flash_tag = 0

CallTypeAudio = 0
CallTypeVideo = 1
call_type = CallTypeAudio

videocall_byuser = False
# video call status
VideoCallDisconnected = 0
VideoCallRequest = 1
VideoCallRequested = 2
VideoCallConnecting = 3
VideoCallConnected = 4
# hcryoo : [20070419_1]
VideoCallRequestedForCallTrading = 5
VideoCallRequestForCallTrading = 6
# hcryoo : [20070419_1]==
videocall_mode = VideoCallDisconnected

AudioOnly = 0
AudioAndVideo = 1
videocall_codec = AudioOnly

first_videocall_codec = AudioOnly # vpark-05.08.24
second_videocall_codec = AudioOnly # vpark-05.08.24

VideoIdle = 0
VideoError = 1
VideoDisconnecting = 2
VideoDisconnected = 3
VideoRequesting = 4
VideoIncoming = 5
VideoConnected = 6
AudioError = 7
AudioDisconnecting = 8
AudioDisconnected = 9
AudioRequesting = 10
AudioIncoming = 11
AudioConnected = 12

video_mode = VideoIdle

snmp_start = False

first_video_mode = VideoIdle # vpark-05.08.24
second_video_mode = VideoIdle # vpark-05.08.24

AutoAnswering = False

LanConnected = False

# myann service
curr_cookie_file = None

#######################
# shchun : Link status

NetNotAvailable = 0		# initial status
NetLinkOk = 1			# when link up event happened.
NetIPOk = 2				# when ipchanged event happened
NetOk = 3				# when icmp test passed

network_status = NetNotAvailable
net_timer = None

def set_netstatus(nstate):
	global registered_status
	global network_status
	global telnum_visible
	global net_timer

	if network_status == nstate:
		debugLogN('skip: set_netstatus (%d)'%nstate)
		return 	
		

	debugLogN('\tset_netstatus (%d)'%nstate)
	network_status = nstate

	if nstate == NetNotAvailable:
		#set_register_mode(NotRegistered)
		#set_regstatus(0)		
		net_timer = None
	elif nstate == NetIPOk:
		# timer - cannot maintain this transient status long
		import utils
		import runtime

		import errorcode
		#errorcode.set_idle_errmsg('')
		errorcode.set_idle_errmsg('', delay_msec=2000, refresh=False)

# shchun -(depreciated)  this code intended not to remain NetIPOk status to long, 
#			since this status is temporary and the ping test is expected to end within an minute
#
#		def timeout_cb():
#			if nstate == NetIPOk:
#				debugLogC('netstatus NetIPOk timeout(%d) status --> 0'%nstate)
#				set_netstatus(NetNotAvailable)
#			return False
#			
#		net_timer = utils.Timer(config.net_monitor_interval, timeout_cb)

	elif nstate == NetOk:
		import runtime

		import errorcode
		#errorcode.set_idle_errmsg('')		
		errorcode.set_idle_errmsg('', delay_msec=2000, refresh=False)
		runtime.manager.linkmonitor.restart_net_monitor()

		net_timer = None
		#if runtime.vdciapp:
		#	runtime.vdciapp.req_deregister()

import os
def set_need_ping_chk_flag(flag=True):
	# shchun: in recovery process, "killall python" is called from time to time.
	# this flag is designed to survive from "killall python", but to deleted "reboot"
	# purpose - if the flag is on, the recovery will hold until ping check (once per minute) is passed.
	# --> KT spec changed, the ping check is not needed any longer.
	# but to be stable, I keep this code. The ping check will return success everytime.
	
	debugLogN('SET NEED-PING-CHK-FLAG(%d) '%flag)
	if flag:
		# set flag
		os.system('rm /tmp/need_ping_chk')
		os.system('touch /tmp/need_ping_chk')
	else:
		# remove flag
		os.system('rm /tmp/need_ping_chk')

def get_need_ping_chk_flag():
	# shchun: get status of ping flag.
	if os.access('/tmp/need_ping_chk', os.F_OK):
		return True
	return False


#######################
Registering = 0
RegisterError = 1
NotRegistered = 2
Registered = 3

register_mode = NotRegistered
registered_status = 0
# eicho: 06.07.07
register_mode_before_state = NotRegistered

register_proxy = '0'

reason_code = '-1'

dnsPingCount = False

def set_regstatus(rgst): # 0/1
	global registered_status
	global telnum_visible
	import runtime, profile

	debugLogN('\tset_regstatus (%d)'%rgst)
	if profile.ip_mode == 1:
		if rgst == 1:
			telnum_visible = True
		else:
			telnum_visible = False

	registered_status = rgst

	runtime.manager.linkmonitor.update_idlemsg()
	
#	try:
#		if runtime.manager.stage.get_name() == 'idle':
#			runtime.manager.stage.ui.update_my_number()
#	except:
#		print 'set_regstatus except'

# VDCI °ü·Ã rigi¸¸ È®ÀÎ.	
def get_regstatus(): # 0/1
#	debugLogN('get_regstatus (%d)'%registered_status)
	return registered_status

def get_connection():
	import profile, runtime
	cur_profile = profile.profile.get_profile()		
	if cur_profile == 1:
		if runtime.peek_lanlink():
			return True
	else:
		if runtime.peek_wifilink():
			return True
	return False
	
# LAN ÀÌ Á¤»ó µ¿ÀÛÇÏ°í regi°¡ µÈ °ÍÀ» È®ÀÎÇÔ.
def get_regstatus_mmi():
	import profile, runtime
	cur_profile = profile.profile.get_profile()		
	if cur_profile == 1:
		if runtime.peek_lanlink() and registered_status:
			return True
	else:
		if runtime.peek_wifilink() and registered_status:
			return True
	return False
	
def get_register_mode():
#	debugLogC('GET_REG_MODE (%d)'%register_mode)
	return register_mode

def set_register_mode(rmode):
	global register_mode
	debugLogN('\tSET_REG_MODE (%d)'%rmode)
	register_mode = rmode
	
telnum = ''
telnum_visible = False #idleÈ­¸é¿¡¼­ telnumÀ» º¸¿©ÁÙÁö ¿©ºÎ

# hcryoo : [20071114_1] : Debug menu enable	
debug_mode = True
# hcryoo : [20071114_1] : Debug menu enable=

VideoMuteOff = 0
VideoMuteOn = 1
videocall_mute = VideoMuteOff
videocall_remote_mute = VideoMuteOff

VideoDetailsHide = 0
VideoDetailsShowMinimum = 1
VideoDetailsShow = 2
videocall_show_details = VideoDetailsHide

#0 : remote full
#1 : remote small
#2 : remote/local small
#3 : remote big local small

#videocall_dimension = config.DIMENSION_REMOTE_BIG_LOCAL_SMALL_BOTTOM
#precapture_dimension = config.DIMENSION_REMOTE_BIG_LOCAL_SMALL_BOTTOM
videocall_dimension = config.DIMENSION_REMOTE_BIG_LOCAL_SMALL_WQVGA
precapture_dimension = config.DIMENSION_REMOTE_BIG_LOCAL_SMALL_WQVGA
videocall_dimension_mobile = False

VideoCallConnectingSTA = 0	# Connecting start
VideoCallConnectingNAS = 1 # NAS Handshaking
VideoCallConnectingPPP = 2 # PPP negotiation
VideoCallConnectingREG = 3	# SIP Register
VideoCallConnectingINV = 4	# SIP Invite
VideoCallConnectingJOIN = 5	# Remote user join

videocall_connecting = VideoCallConnectingSTA

# for snmp
last_key = None


# call connected³ª terminated¿¡¼­ ½ÇÁ¦ ¼ýÀÚ´ë½Å¿¡ Ç¥½ÃÇÒ number
# ºñ¹Ð ¹øÈ£µîÀÌ µé¾î°¡´Â °÷¿¡¼­¸¸ ¾²ÀÓ
visual_number = ''

# SMS ¹Þ´Â »ç¶÷ÀÇ Terminal Number
callee_terminal_number = ''

sms_phone_number = ''
sms_use_notify = False

email_address = None

# while sac3 call, sac3_handler refer Sac3Handler instance
last_update_check_time = 0
sac3_handler = None
disp_update_tag = None


# automata¸¦ »ç¿ëÇÏ¿© ÆíÁýÇÏ°í ÀÖ´ÂÁö¸¦ ³ªÅ¸³¿.
# digit long keyÃ³¸®¸¦ À§ÇØ ÇÊ¿ä
editing = False

screensaver_activated = False
# eicho add tag 06.04.12
screensaver_activated_byhook = False
# eicho end

incoming_sms = False

bluetooth_activated = False

date_time_ready = False
#Roxia Begin jhbang 06.03.16
ntp_running = False
#Roxia End jhbang

# alarm ¸Þ´º·ÎºÎÅÍ date ¼¼ÆÃ¿¡ µé¾î°¬À» ½Ã¿¡´Â
# feedbackÀ» ¾ø¾Ö±â À§ÇØ¼­ »ç¿ë
set_date_from_alarm = False

# image & sound¿¡¼­ phonebook Find¸¦ È£Ãâ½Ã ÇöÀç assignÇÒ·Á°í ÇÏ´Â
# ÆÄÀÏÀÇ ÀÌ¸§À» ±â¾ïÇÏ´Â Àå¼Ò
assign_melody = ''
assign_image = ''
phonelist_activate = None

##
save_callback = None
interrupt_key = ''
save_callback_other = None

phonebook_save_stage_name = ''

sms_storage_full = False
mms_storage_full = False
mms_received_count = 0
mms_received_info = ''
mms_notify_exist = False

# PABX ÄÚµå¸¦ ¾ø¾Ö´ÂÁö ¾Æ´ÑÁö¿¡ ´ëÇÑ Ã¼Å©¸¦ À§ÇÔ
# True ÀÏ °æ¿ì, user°¡ Á÷Á¢ dial ÇÑ °Í, PABX ÄÚµå¸¦ ºÙÀÌÁö¾ÊÀ½
dialed_by_user = False

inUseByDect = False

# TdE supplementary service call ÀÎÁö ±¸º°ÇÏ±â À§ÇÑ º¯¼ö
dialed_TdESupplementary = False 	# eicho 06.10.12

first_dialed_by_user = False # vpark-05.08.24
second_dialed_by_user = False # vpark-05.08.24

# restart µÇ¾úÀ» °æ¿ì¿¡´Â ÃÊ±â 1¹øÀÇ sac3 callÀ» disable ÇÏ±â À§ÇØ »ç¿ë
lgvp_restarted = False

# KA: [20071105] TI IMS subscriber
subscriber = False
precondition = 0

sync_result = True

# info Ã³¸®¸¦ À§ÇØ ÀÓ½Ã·Î »ç¿ëÇÔ.
info_status = False

# dect¿¡¼­ sms Ã³¸® ÁßÀÎÁö¸¦ ÆÇ´ÜÇÏ´Âµ¥ »ç¿ë
isSmsProcessing = False
tempMessagingTimer = None

# È£ ¹ß½Å ÈÄ ¿¬°á Àü¿¡´Â softkey¸¦ º¸¿©ÁÖÁö ¾ÊÀ½.
showSoftkeyFlag = True

TEMP_MSG_CONDITION = False

def backup_status():
	from setting import setting

	global date_time_ready

	backups = {'date_time_ready':date_time_ready}

	if setting.agenda_time and setting.agenda_date:
		backups['agenda_time'] = setting.agenda_date + setting.agenda_time
	if setting._agenda_message:
		backups['agenda_message'] = setting._agenda_message
	if setting._agenda_tag:
		backups['agenda_enabled'] = 1
### eicho add 06.11.07
	if setting._agenda_snooze_cnt :
		backups['agenda_snooze_count'] = setting._agenda_snooze_cnt

# end
	# eicho modify for w.129 in NB. 
	#name = '/tmp/status-backup'
	name = '/usr/local/lgvp/agenda_backup'
	fp = open(name, 'w')
	for key, value in backups.items():
		fp.write("%s=%s\n" % (key, repr(value)))

def restore_status():
	from setting import setting
	# eicho modify for w.129 in NB. 
	#name = '/tmp/status-backup'
	name = '/usr/local/lgvp/agenda_backup'
	global date_time_ready, lgvp_restarted
	lgvp_restarted = True
	agenda_enabled = False

	try:
		for line in file(name):
			line = line.strip()
			key, value = line.split('=', 1)
			if key == 'date_time_ready':
				date_time_ready = eval(value)
			elif key == 'agenda_time':
				t = eval(value)
				setting.agenda_date = t[0],t[1],t[2]
				setting.agenda_time = t[3],t[4]
			elif key == 'agenda_enabled':
				agenda_enabled = True
			elif key == 'agenda_message':
				setting._agenda_message = eval(value)
### eicho add 06.11.07
			elif key == 'agenda_snooze_count':
				setting._agenda_snooze_cnt = eval(value)

# end

		if agenda_enabled:
			import alarm, runtime
			setting.register_agenda(alarm.agenda_activate, setting._agenda_snooze_cnt)
	except:
		pass

def set_telnum():
	import profile, status
	from setting import setting
	from vdcisetting import vdci_setting
	if profile.ip_mode == 1:
		status.telnum = vdci_setting.tel_num
	else:
		status.telnum = setting.pstn_telnum

## eicho add 06.11.14
flag_state_incoming_ss = False
def Off_flag_incoming_ss():
	import status
	status.flag_state_incoming_ss = False
def On_flag_incoming_ss():
	import status
	status.flag_state_incoming_ss = True

# KA: [20071126] DTMF nego
RFC2833 = 0
INFO_DTMF = 1
INBAND = 2
ANY_DTMF = 3

dtmf_type = ANY_DTMF
first_dtmf_type = ANY_DTMF
second_dtmf_type = ANY_DTMF
#Third_dtmf_type = ANY_DTMF

def get_current_dtmf_type():
	global active_call_index
	global dtmf_type, first_dtmf_type, second_dtmf_type
	if active_call_index == 0:
		return first_dtmf_type
	else:
		return second_dtmf_type
	
def set_dtmf_type(type):
	global active_call_index
	global dtmf_type, first_dtmf_type, second_dtmf_type
	if active_call_index == 0:
		first_dtmf_type = type
	else:
		second_dtmf_type = type
	dtmf_type = type
	
# KA: [20071126] DTMF nego ==

MMI_KEY_BLOCK = False

KT_CALL_TESTER = None
DECT_PATH_TEST = False
DECT_RESET_TEST = False

currentCallDuration = (0, 0, 0)
currentCallDuration_copy = (0, 0, 0)
sipResponseCodeForTrap = None

# shchun:  for snmp global temp-storage. 
t_ipaddress = ''
t_netmask = ''
t_dhcp = ''
t_gateway = ''
t_dnsserver = ''

# flag for restart
is_restart = False

trapChecked = False

def dumpTempIpInfo():
	print '==== temp ip info ===='
	print 'ip_address: ', t_ipaddress
	print 'netmask: ' , t_netmask
	print 'dhcp: ', t_dhcp
	print 'geteway: ', t_gateway
	print 't_dnsserver: ', t_dnsserver
	print '================='
# end shchun 

#admin_voip_auth_name_only_for_display = ''
voip_auth_pwd_type = 0
real_phonedb_index = [] # phonebook index list

ScreenSaverOn = False
lcdOn = True

# by choinhwa : LCD off Áß 'killall python' ½Ã lcdOnÀÇ °ªÀÌ True·Î ÃÊ±âÈ­µÇ´Âµ¥ 
#                      LCD ´Â ²¨Áø »óÅÂ·Î µ¿ÀÛµÇ´Â °æ¿ì°¡ Á¸ÀçÇÏ¹Ç·Î °­Á¦·Î ÇÑ¹ø lcd¸¦ ÄÔ.
os.system('lcdoff 0')

RichTextFocus= 0
prev_tc = None
prev_tc_box = None
crnt_key = None

FirstRegistered_check_flag_for_QoS_setting = False
pushVideoAd = False

firstCallType = None # 1: audio / 2: video
isFirstCallTypeChanged = False

serviceStatusType = None

alarm_item_bak = None
alarm_hold_item = None
alarm_active_item = None

def getCurrentCallStatus():
	import runtime
	
	from mmiSoundPath import SP_Context
	from dectHandler import DectCallManager
	baseCallStatus = runtime.SP_context.getBaseCallStatus()
	baseStatus = runtime.SP_context.getBaseStatus()
	dectCallStatus = runtime.dectCallManager.getDectCallStatus()
	dectStatus = runtime.dectCallManager.getDectStatus()

	runtime.mmiDebug.mmiTrace('getCurrentCallStatus : baseCallStatus = ', baseCallStatus)
	runtime.mmiDebug.mmiTrace('getCurrentCallStatus : baseStatus = ', baseStatus)
	runtime.mmiDebug.mmiTrace('getCurrentCallStatus : dectCallStatus = ', dectCallStatus)
	runtime.mmiDebug.mmiTrace('getCurrentCallStatus : dectStatus = ', dectStatus)

	return [baseCallStatus, baseStatus, dectCallStatus, dectStatus]

def setClearCallStatus():
	import runtime
	
	runtime.mmiDebug.mmiTrace('setClearCallStatus : remove dect call status')

	from mmiSoundPath import SP_Context
	from dectHandler import DectCallManager
	
	runtime.dectCallManager.setDectCallStatus(DectCallManager.IDLE)
	runtime.dectCallManager.setDectStatus(DectCallManager.IDLE)
	runtime.SP_context.setBaseCallStatus(SP_Context.IDLE)
	runtime.SP_context.setBaseStatus(SP_Context.IDLE)

