1# $Id$
2# Save, open Tkabber sessions
3#############################################################################
4#
5# Session is a list of {priority user server resource script}
6#
7
8namespace eval session {
9    variable session_file [file join $::configdir session.tcl]
10
11    custom::defgroup State [::msgcat::mc "Tkabber save state options."] \
12	-group Tkabber
13    custom::defvar options(save_on_exit) 1 \
14	[::msgcat::mc "Save state on Tkabber exit."] \
15	-type boolean -group State
16    custom::defvar options(open_on_start) 1 \
17	[::msgcat::mc "Load state on Tkabber start."] \
18	-type boolean -group State
19}
20
21#############################################################################
22
23proc session::save_session {} {
24    variable session_file
25
26    set session {}
27    hook::run save_session_hook session
28
29    set fd [open $session_file w]
30    fconfigure $fd -encoding utf-8
31    puts $fd $session
32    close $fd
33}
34
35#############################################################################
36
37proc session::save_session_on_exit {} {
38    variable options
39
40    if {$options(save_on_exit)} {
41	save_session
42    }
43}
44
45hook::add quit_hook [namespace current]::session::save_session_on_exit
46
47#############################################################################
48
49proc session::open_session {} {
50    variable session_file
51
52    set session_script_list {}
53    catch {
54	set fd [open $session_file r]
55	fconfigure $fd -encoding utf-8
56	set session_script_list [read $fd]
57	close $fd
58    }
59
60    foreach script [lsort -integer -index 0 $session_script_list] {
61	lassign $script priority user server resource command
62
63	set jid [::xmpp::jid::jid $user $server $resource]
64
65	if {($user != "") || ($server != "") || ($resource != "")} {
66	    # HACK. It works if called before any JID is connected
67	    set xlib [create_xlib $jid]
68	} else {
69	    set xlib ""
70	}
71
72	after idle [list eval $command [list $xlib $jid]]
73    }
74}
75
76#############################################################################
77
78proc session::open_session_on_start {} {
79    variable options
80
81    if {$options(open_on_start)} {
82	open_session
83    }
84}
85
86hook::add finload_hook [namespace current]::session::open_session_on_start 90
87
88#############################################################################
89
90proc session::setup_menu {} {
91    if {![cequal $::interface tk] && ![cequal $::interface ck]} return
92
93    catch {
94	set m [.mainframe getmenu tkabber]
95	set ind [expr {[$m index [::msgcat::mc "Chats"]] + 1}]
96
97	set mm .session_menu
98	menu $mm -tearoff $::ifacetk::options(show_tearoffs)
99	$mm add command -label [::msgcat::mc "Save state"] \
100	    -command [namespace current]::save_session
101	$mm add checkbutton -label [::msgcat::mc "Save state on exit"] \
102	    -variable [namespace current]::options(save_on_exit)
103	$mm add checkbutton -label [::msgcat::mc "Load state on start"] \
104	    -variable [namespace current]::options(open_on_start)
105
106	$m insert $ind cascade -label [::msgcat::mc "State"] -menu $mm
107    }
108}
109
110hook::add finload_hook [namespace current]::session::setup_menu 60
111
112#############################################################################
113
114