1

merging in archive changes

This commit is contained in:
Alex Yatskov 2011-11-19 08:50:47 -08:00
commit 4ace6e419f
2 changed files with 64 additions and 12 deletions

View File

@ -36,6 +36,7 @@ class MainWindowReader(QtGui.QMainWindow):
self.searchPosition = 0 self.searchPosition = 0
self.searchText = unicode() self.searchText = unicode()
self.scanPosition = 0 self.scanPosition = 0
self.archiveIndex = None
def __init__(self, parent=None, languages=None, filename=None, preferences=None, anki=None, closed=None, updated=None): def __init__(self, parent=None, languages=None, filename=None, preferences=None, anki=None, closed=None, updated=None):
@ -351,10 +352,15 @@ class MainWindowReader(QtGui.QMainWindow):
self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding)) self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding))
def openFileByExtension(self, filename): def openFileByExtension(self, filename):
self.clearArchiveFiles()
if tarfile.is_tarfile(filename): if tarfile.is_tarfile(filename):
# opening an empty tar file raises ReadError # opening an empty tar file raises ReadError
with tarfile.open(filename, 'r:*') as tp: with tarfile.open(filename, 'r:*') as tp:
files = [f for f in tp.getnames() if tp.getmember(f).isfile()] files = [f for f in tp.getnames() if tp.getmember(f).isfile()]
names = [f.decode('utf-8') for f in files]
self.updateArchiveFiles(filename, names)
if len(files) == 0: if len(files) == 0:
content = unicode() content = unicode()
elif len(files) == 1: elif len(files) == 1:
@ -363,33 +369,48 @@ class MainWindowReader(QtGui.QMainWindow):
fp.close() fp.close()
else: else:
# Using index because of encoding difficulties # Using index because of encoding difficulties
(index, ok) = self.selectItem([f.decode('utf-8') for f in files]) (index, ok) = self.selectFileName(names)
if ok: if ok:
fp = tp.extractfile(files[index - 1]) fp = tp.extractfile(files[index])
content = fp.read() content = fp.read()
fp.close() fp.close()
self.state.archiveIndex = index
else: else:
content = unicode() content = unicode()
else: else:
self.state.archiveIndex = None
with open(filename, 'rb') as fp: with open(filename, 'rb') as fp:
content = fp.read() content = fp.read()
return content return content
def selectItem(self, list):
items = [self.formatQString(i, x) for i, x in enumerate(list)] def selectFileName(self, names):
if self.state.archiveIndex is not None:
return (self.state.archiveIndex, True)
(item, ok) = QtGui.QInputDialog.getItem( (item, ok) = QtGui.QInputDialog.getItem(
self, self,
'Yomichan', 'Yomichan',
'Select file to open:', 'Select file to open:',
items, self.formatQStringList(names),
current = 0, current = 0,
editable=False) editable=False)
(index, success) = item.split('.').first().toInt() (index, success) = self.getItemIndex(item)
return (index, ok and success) return (index - 1, ok and success)
def getItemIndex(self, item):
return item.split('.').first().toInt()
def formatQStringList(self, list):
return [self.formatQString(i, x) for i, x in enumerate(list)]
def formatQString(self, index, item): def formatQString(self, index, item):
return QtCore.QString(str(index + 1) + '. ').append(QtCore.QString(item)) return QtCore.QString(str(index + 1) + '. ').append(QtCore.QString(item))
def closeFile(self): def closeFile(self):
self.setWindowTitle('Yomichan') self.setWindowTitle('Yomichan')
self.textContent.setPlainText(unicode()) self.textContent.setPlainText(unicode())
@ -504,6 +525,28 @@ class MainWindowReader(QtGui.QMainWindow):
self.textContent.setTextCursor(cursor) self.textContent.setTextCursor(cursor)
def clearArchiveFiles(self):
self.menuOpenArchive.clear()
self.menuOpenArchive.setEnabled(False)
def updateArchiveFiles(self, filename, names):
self.menuOpenArchive.setEnabled(True)
for name in self.formatQStringList(names):
(index, ok) = self.getItemIndex(name)
if ok:
index = index - 1
self.menuOpenArchive.addAction(name, (lambda fn=filename, idx=index: self.openFileInArchive(fn, idx)))
else:
self.menuOpenArchive.addAction(name, (lambda fn=filename: self.openFile(fn)))
def openFileInArchive(self, filename, index):
self.state.scanPosition = 0
self.state.archiveIndex = index
self.openFile(filename)
def clearRecentFiles(self): def clearRecentFiles(self):
self.preferences.clearRecentFiles() self.preferences.clearRecentFiles()
self.updateRecentFiles() self.updateRecentFiles()

View File

@ -47,12 +47,21 @@
<property name="title"> <property name="title">
<string>&amp;File</string> <string>&amp;File</string>
</property> </property>
<widget class="QMenu" name="menuOpenArchive">
<property name="enabled">
<bool>false</bool>
</property>
<property name="title">
<string>Open from &amp;archive</string>
</property>
</widget>
<widget class="QMenu" name="menuOpenRecent"> <widget class="QMenu" name="menuOpenRecent">
<property name="title"> <property name="title">
<string>Open &amp;recent</string> <string>Open &amp;recent</string>
</property> </property>
</widget> </widget>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="menuOpenArchive"/>
<addaction name="menuOpenRecent"/> <addaction name="menuOpenRecent"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionQuit"/> <addaction name="actionQuit"/>