OSDN Git Service

Set Has Focus property.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / session.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import pygtk
19 pygtk.require('2.0')
20 import gtk
21 import gobject
22 import gconf
23 import traceback
24
25 import uri_opener
26 from BbsType import bbs_type_judge_uri
27 from BbsType import bbs_type_exception
28 import config
29
30 # key: /bbs/board/thread value: toplevel window widget
31 _windows = {}
32
33 def get_window(key):
34     if key in _windows:
35         return _windows[key]
36     else:
37         return None
38
39 def regist(winwrap):
40     if not winwrap:
41         raise ValueError, "parameter must not be empty"
42
43     key = winwrap.get_uri()
44     if not key:
45         raise ValueError, "uri must not be empty"
46
47     if key in _windows:
48         return False
49
50     _windows[key] = winwrap
51     print "regist to _windows", key
52     return True
53
54 def unregist(winwrap):
55     key = winwrap.get_uri()
56     if key not in _windows:
57         print key, "is not found in _windows"
58         return
59
60     del _windows[key]
61     print "unregist from _windows", key
62
63     if not _windows:
64         print "all window unregist"
65         on_all_window_destroy()
66
67 def on_all_window_destroy():
68     gtk.main_quit()
69
70 def thread_idx_updated(thread_uri, idx_dic):
71     if not thread_uri or not idx_dic:
72         raise ValueError, "parameter must not be empty"
73
74     gobject.idle_add(on_thread_idx_updated, thread_uri, idx_dic)
75
76 def on_thread_idx_updated(thread_uri, idx_dic):
77     bbs_type = bbs_type_judge_uri.get_type(thread_uri)
78     winwrap = get_window(bbs_type.get_uri_base())
79     if winwrap:
80         winwrap.on_thread_idx_updated(thread_uri, idx_dic)
81
82 def main_quit():
83     print "session main quit"
84
85     uris = _windows.keys()
86     try:
87         gconf_client = gconf.client_get_default()
88         gconf_key_windows = config.gconf_app_key_base() + "/windows"
89         if uris:
90             gconf_client.set_list(
91                 gconf_key_windows, gconf.VALUE_STRING, uris)
92             print "save windows", uris
93         else:
94             gconf_client.unset(gconf_key_windows)
95             print "save no window"
96     except:
97         pass
98
99     temp = dict(_windows)
100     for uri, winwrap in temp.iteritems():
101         try:
102             winwrap.destroy()
103         except:
104             traceback.print_exc()
105
106     # not reach here.
107     gtk.main_quit()
108
109 def restore():
110     gconf_client = gconf.client_get_default()
111
112     gconf_key_windows = config.gconf_app_key_base() + "/windows"
113     uris = gconf_client.get_list(gconf_key_windows, gconf.VALUE_STRING)
114     for uri in uris:
115         try:
116             uri_opener.open_uri(uri)
117         except bbs_type_exception.BbsTypeError, msg:
118             print msg
119
120 def start():
121     restore()
122     if not _windows:
123         uri_opener.open_uri("http://ex11.2ch.net/morningcoffee/")
124     gtk.main()