/usr/share/treeline/recentfiles.py is in treeline 1.4.1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | #!/usr/bin/env python
#****************************************************************************
# recentfiles.py, classes to handle recent file lists and states
#
# Copyright (C) 2006, Douglas W. Bell
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License, either Version 2 or any later
# version. This program is distributed in the hope that it will be useful,
# but WITTHOUT ANY WARRANTY. See the included LICENSE file for details.
#*****************************************************************************
import os.path
import sys
import time
from PyQt4 import QtCore, QtGui
import globalref
class RecentFileList(list):
"""A list of RecentFile objects"""
def __init__(self):
list.__init__(self)
self.numEntries = globalref.options.intData('RecentFiles', 0, 99)
self.loadList()
def loadList(self):
"""Load recent files from options"""
self[:] = []
for num in range(self.numEntries):
entry = RecentFile()
entry.loadPath(num)
if entry.pathIsValid():
self.append(entry)
def writeList(self):
"""Write list of paths to options"""
for num in range(self.numEntries):
try:
entry = self[num]
except IndexError:
entry = RecentFile()
entry.writePath(num)
globalref.options.writeChanges()
def addEntry(self, path):
"""If path is in list, move to start;
otherwise create a new entry at the start"""
try:
entry = self.pop(self.index(RecentFile(path)))
except ValueError:
entry = RecentFile(path)
self.insert(0, entry)
self.updateMenu()
def removeEntry(self, path):
"""Remove the given pathname if found"""
try:
self.remove(RecentFile(path))
except ValueError:
pass
self.updateMenu()
def firstPath(self):
"""Return the first valid path"""
for entry in self:
path = os.path.dirname(entry.path)
if os.path.exists(path):
return path
return ''
def changeNumEntries(self, numEntries):
"""Modify number of available entries"""
for i in range(numEntries, self.numEntries):
globalref.options.changeData(RecentFile().optionTitle(i), '', True)
TreeState().writeState(i)
for i in range(self.numEntries, numEntries):
globalref.options.addDefaultKey(RecentFile().optionTitle(i))
globalref.options.addDefaultKey(TreeState().optionTitle(i))
self.numEntries = numEntries
self.updateMenu()
def updateMenu(self):
"""Refresh menu items"""
menu = globalref.mainWin.recentFileSep.parentWidget()
actionList = globalref.mainWin.recentFileActions
menuLength = min(self.numEntries, len(self))
while len(actionList) < menuLength:
actionList.append(RecentAction(globalref.mainWin))
menu.insertAction(globalref.mainWin.recentFileSep,
actionList[-1])
while len(actionList) > menuLength:
menu.removeAction(actionList[-1])
del actionList[-1]
for action, entry, num in zip(actionList, self,
range(menuLength)):
action.setPath(entry, num)
def saveTreeState(self, treeView):
"""Set the tree state from the current file"""
try:
entry = self[self.index(RecentFile(globalref.docRef.fileName))]
except ValueError:
return
entry.treeState.saveState(treeView)
def restoreTreeState(self, treeView):
"""Restore the state in the current file, return True if changed"""
try:
entry = self[self.index(RecentFile(globalref.docRef.fileName))]
except ValueError:
return False
return entry.treeState.restoreState(treeView)
def clearTreeStates(self):
"""Remove all saved tree state data"""
for entry in self:
entry.treeState = TreeState()
class RecentFile(object):
"""Contains path info and creates menu actions"""
maxEntryLength = 30
def __init__(self, path=''):
self.path = path
if self.path:
self.path = os.path.abspath(self.path)
self.treeState = TreeState()
def loadPath(self, num):
"""Load path from option position num"""
self.path = globalref.options.strData(self.optionTitle(num), True)
if self.path:
self.path = os.path.abspath(self.path)
self.treeState.loadState(num)
def pathIsValid(self):
"""Return True if has a valid path"""
return os.access(self.path.encode(sys.getfilesystemencoding()),
os.R_OK)
def writePath(self, num):
"""Save path to option at position num"""
globalref.options.changeData(self.optionTitle(num), self.path, True)
self.treeState.writeState(num)
def abbrevPath(self):
"""Return shortened version of path"""
abbrevPath = self.path
if len(self.path) > RecentFile.maxEntryLength:
truncLength = RecentFile.maxEntryLength - 3
pos = self.path.find(os.sep, len(self.path) - truncLength)
if pos < 0:
pos = len(self.path) - truncLength
abbrevPath = '...' + self.path[pos:]
return abbrevPath
def optionTitle(self, num):
"""Return option key"""
return 'RecentFile%d' % (num + 1)
def __cmp__(self, other):
"""Compare paths"""
return cmp(os.path.normcase(self.path), os.path.normcase(other.path))
class RecentAction(QtGui.QAction):
"""Menu item for a recent file entry"""
def __init__(self, parent):
QtGui.QAction.__init__(self, parent)
self.connect(self, QtCore.SIGNAL('triggered()'), self.openItem)
def setPath(self, recentFile, num):
"""Set menu title to abbreviated path name and add number"""
self.setText('&%d %s' % (num + 1, recentFile.abbrevPath()))
self.setStatusTip(recentFile.path)
def openItem(self):
"""Execute function to open this file"""
globalref.treeControl.recentOpen(unicode(self.statusTip()))
class TreeState(object):
"""Loads and saves the tree states of recent files"""
def __init__(self):
self.timeStamp = 0
self.topNode = 0
self.selectNodes = [0]
self.openNodes = [0]
def loadState(self, num):
"""Load state from option position num"""
stateStr = globalref.options.strData(self.optionTitle(num), True)
states = stateStr.split(':')
try:
self.timeStamp = int(states[0])
self.topNode = int(states[1])
self.selectNodes = [int(s) for s in states[2].split(',')]
self.openNodes = [int(s) for s in states[3].split(',')]
except (ValueError, IndexError):
pass
def writeState(self, num):
"""Save state to option at position num"""
stateStr = '%d:%d:%s:%s' % (self.timeStamp, self.topNode,
','.join([str(n) for n in
self.selectNodes]),
','.join([str(n) for n in self.openNodes]))
globalref.options.changeData(self.optionTitle(num), stateStr, True)
def saveState(self, treeView):
"""Set the state from the current file"""
self.timeStamp = int(time.time())
allNodes = globalref.docRef.root.descendantList(True)
self.topNode = allNodes.index(treeView.itemAt(0, 0).docItemRef)
self.selectNodes = [allNodes.index(item) for item in
globalref.docRef.selection]
if not self.selectNodes:
self.selectNodes = [0]
if not allNodes[0].open: # root closed, return default
self.openNodes = [0]
else:
self.openNodes = []
for i, node in enumerate(allNodes):
if node.open and \
not [child for child in node.childList if child.open] \
and node.allAncestorsOpen():
self.openNodes.append(i)
def restoreState(self, treeView):
"""Restore this state in the current file, return True if changed"""
fileTimeStamp = os.stat(globalref.docRef.fileName).st_mtime
if fileTimeStamp > self.timeStamp: # file modified externally
return False
allNodes = globalref.docRef.root.descendantList(True)
try:
selectedNodes = [allNodes[i] for i in self.selectNodes]
except IndexError:
selectedNodes = [allNodes[0]]
globalref.docRef.selection.replace(selectedNodes)
globalref.docRef.selection.prevSelects = []
for i in self.openNodes:
try:
node = allNodes[i]
while not node.open and node != allNodes[0]:
node.open = True
node = node.parent
except IndexError:
pass
globalref.updateViewAll()
try:
topNode = allNodes[self.topNode].viewData
treeView.scrollToItem(topNode,
QtGui.QAbstractItemView.PositionAtTop)
except IndexError:
pass
return True
def optionTitle(self, num):
"""Return option key"""
return 'TreeState%d' % (num + 1)
|