]> Dogcows Code - chaz/openbox/blob - scripts/historyplacement.py
catch exception if the file doesnt exist
[chaz/openbox] / scripts / historyplacement.py
1 ##############################################################################
2 ### The history window placement algorithm. ebind historyplacement.place ###
3 ### to the ob.EventAction.PlaceWindow event to use it. ###
4 ##############################################################################
5
6 import windowplacement # fallback routines
7
8 ##############################################################################
9 ### Options for the historyplacement module (Options in the ###
10 ### windowplacement module also apply!) ###
11 ##############################################################################
12 IGNORE_REQUESTED_POSITIONS = 0
13 """When true, the placement algorithm will attempt to place windows even
14 when they request a position (like XMMS). Note this only applies to
15 normal windows, not to special cases like desktops and docks."""
16 FALLBACK = windowplacement.random
17 """The window placement algorithm that will be used when history placement
18 does not have a place for the window."""
19 CONFIRM_CALLBACK = 0
20 """Set this to a function to have the function called before attempting to
21 place a window via history. If the function returns a non-zero, then an
22 attempt will be made to place the window. If it returns zero, the
23 fallback method will be directly applied instead."""
24 FILENAME = 'historydb'
25 """The name of the file where history data will be stored. The number of
26 the screen is appended onto this filename."""
27 ##############################################################################
28
29 def place(data):
30 """Place a window usingthe history placement algorithm."""
31 _place(data)
32
33 ###########################################################################
34 ###########################################################################
35
36 ###########################################################################
37 ### Internal stuff, should not be accessed outside the module. ###
38 ###########################################################################
39
40 import otk
41 import ob
42 import os
43 import string
44
45 _data = []
46
47 class _state:
48 def __init__(self, appname, appclass, role, x, y):
49 self.appname = appname
50 self.appclass = appclass
51 self.role = role
52 self.x = x
53 self.y = y
54 def __eq__(self, other):
55 if self.appname == other.appname and \
56 self.appclass == other.appclass and \
57 self.role == other.role:
58 return 1
59 return 0
60
61 def _load(data):
62 global _data
63 try:
64 file = open(os.environ['HOME'] + '/.openbox/' + FILENAME+"." +
65 str(data.screen), 'r')
66 # read data
67 for line in file.readlines():
68 line = line[:-1] # drop the '\n'
69 try:
70 s = string.split(line, '\0')
71 state = _state(s[0], s[1], s[2],
72 string.atoi(s[3]), string.atoi(s[4]))
73
74 while len(_data)-1 < data.screen:
75 _data.append([])
76 _data[data.screen].append(state)
77
78 except ValueError: pass
79 except IndexError: pass
80 file.close()
81 except IOError: pass
82
83 def _save(data):
84 global _data
85 file = open(os.environ['HOME']+'/.openbox/'+FILENAME+"."+str(data.screen),
86 'w')
87 if file:
88 while len(_data)-1 < data.screen:
89 _data.append([])
90 for i in _data[data.screen]:
91 file.write(i.appname + '\0' +
92 i.appclass + '\0' +
93 i.role + '\0' +
94 str(i.x) + '\0' +
95 str(i.y) + '\n')
96 file.close()
97
98 def _create_state(data):
99 global _data
100 area = data.client.area()
101 return _state(data.client.appName(), data.client.appClass(),
102 data.client.role(), area.x(), area.y())
103
104 def _find(screen, state):
105 global _data
106 try:
107 return _data[screen].index(state)
108 except ValueError:
109 return -1
110 except IndexError:
111 while len(_data)-1 < screen:
112 _data.append([])
113 return _find(screen, state) # try again
114
115 def _place(data):
116 global _data
117 if data.client:
118 if not (IGNORE_REQUESTED_POSITIONS and data.client.normal()):
119 if data.client.positionRequested(): return
120 state = _create_state(data)
121 try:
122 if not CONFIRM_CALLBACK or CONFIRM_CALLBACK(data):
123 print "looking for : " + state.appname + " : " + \
124 state.appclass + " : " + state.role
125
126 i = _find(data.screen, state)
127 if i >= 0:
128 coords = _data[data.screen][i]
129 print "Found in history ("+str(coords.x)+","+\
130 str(coords.y)+")"
131 data.client.move(coords.x, coords.y)
132 return
133 else:
134 print "No match in history"
135 except TypeError:
136 pass
137 if FALLBACK: FALLBACK(data)
138
139 def _save_window(data):
140 global _data
141 if data.client:
142 state = _create_state(data)
143 print "looking for : " + state.appname + " : " + state.appclass + \
144 " : " + state.role
145
146 i = _find(data.screen, state)
147 if i >= 0:
148 print "replacing"
149 _data[data.screen][i] = state # replace it
150 else:
151 print "appending"
152 _data[data.screen].append(state)
153
154 ob.ebind(ob.EventAction.CloseWindow, _save_window)
155 ob.ebind(ob.EventAction.Startup, _load)
156 ob.ebind(ob.EventAction.Shutdown, _save)
157
158 print "Loaded historyplacement.py"
This page took 0.045938 seconds and 5 git commands to generate.