-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.py
More file actions
381 lines (306 loc) · 13.2 KB
/
book.py
File metadata and controls
381 lines (306 loc) · 13.2 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Copyright (C) 2010 Alex Yatskov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from PyQt4 import QtGui, QtCore, QtXml
import zipfile
import image
from image import ImageFlags
from about import DialogAbout
from options import DialogOptions
from convert import DialogConvert
from ui.book_ui import Ui_MainWindowBook
class Book:
DefaultDevice = 'Kindle 3'
DefaultOverwrite = True
DefaultImageFlags = ImageFlags.Orient | ImageFlags.Resize | ImageFlags.Quantize
def __init__(self):
self.images = []
self.filename = None
self.modified = False
self.title = None
self.device = Book.DefaultDevice
self.overwrite = Book.DefaultOverwrite
self.imageFlags = Book.DefaultImageFlags
def save(self, filename):
document = QtXml.QDomDocument()
root = document.createElement('book')
document.appendChild(root)
root.setAttribute('title', self.title)
root.setAttribute('overwrite', 'true' if self.overwrite else 'false')
root.setAttribute('device', self.device)
root.setAttribute('imageFlags', self.imageFlags)
for filenameImg in self.images:
itemImg = document.createElement('image')
root.appendChild(itemImg)
itemImg.setAttribute('filename', filenameImg)
textXml = document.toString(4).toUtf8()
try:
fileXml = open(unicode(filename), 'w')
fileXml.write(textXml)
fileXml.close()
except IOError:
raise RuntimeError('Cannot create book file %s' % filename)
self.filename = filename
self.modified = False
def load(self, filename):
try:
fileXml = open(unicode(filename), 'r')
textXml = fileXml.read()
fileXml.close()
except IOError:
raise RuntimeError('Cannot open book file %s' % filename)
document = QtXml.QDomDocument()
if not document.setContent(QtCore.QString.fromUtf8(textXml)):
raise RuntimeError('Error parsing book file %s' % filename)
root = document.documentElement()
if root.tagName() != 'book':
raise RuntimeError('Unexpected book format in file %s' % filename)
self.title = root.attribute('title', 'Untitled')
self.overwrite = root.attribute('overwrite', 'true' if Book.DefaultOverwrite else 'false') == 'true'
self.device = root.attribute('device', Book.DefaultDevice)
self.imageFlags = int(root.attribute('imageFlags', str(Book.DefaultImageFlags)))
self.filename = filename
self.modified = False
self.images = []
items = root.elementsByTagName('image')
if items == None:
return
for i in xrange(0, len(items)):
item = items.at(i).toElement()
if item.hasAttribute('filename'):
self.images.append(item.attribute('filename'))
class MainWindowBook(QtGui.QMainWindow, Ui_MainWindowBook):
def __init__(self, filename=None):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.connect(self.actionFileNew, QtCore.SIGNAL('triggered()'), self.onFileNew)
self.connect(self.actionFileOpen, QtCore.SIGNAL('triggered()'), self.onFileOpen)
self.connect(self.actionFileSave, QtCore.SIGNAL('triggered()'), self.onFileSave)
self.connect(self.actionFileSaveAs, QtCore.SIGNAL('triggered()'), self.onFileSaveAs)
self.connect(self.actionBookOptions, QtCore.SIGNAL('triggered()'), self.onBookOptions)
self.connect(self.actionBookAddFiles, QtCore.SIGNAL('triggered()'), self.onBookAddFiles)
self.connect(self.actionBookAddDirectory, QtCore.SIGNAL('triggered()'), self.onBookAddDirectory)
self.connect(self.actionBookShiftUp, QtCore.SIGNAL('triggered()'), self.onBookShiftUp)
self.connect(self.actionBookShiftDown, QtCore.SIGNAL('triggered()'), self.onBookShiftDown)
self.connect(self.actionBookRemove, QtCore.SIGNAL('triggered()'), self.onBookRemove)
self.connect(self.actionBookExport, QtCore.SIGNAL('triggered()'), self.onBookExport)
self.connect(self.actionHelpAbout, QtCore.SIGNAL('triggered()'), self.onHelpAbout)
self.connect(self.actionHelpHomepage, QtCore.SIGNAL('triggered()'), self.onHelpHomepage)
self.connect(self.listWidgetFiles, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.onFilesContextMenu)
self.connect(self.listWidgetFiles, QtCore.SIGNAL('itemDoubleClicked (QListWidgetItem *)'), self.onFilesDoubleClick)
self.listWidgetFiles.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.book = Book()
if filename != None:
self.loadBook(filename)
def closeEvent(self, event):
if not self.saveIfNeeded():
event.ignore()
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
def dropEvent(self, event):
directories = []
filenames = []
for url in event.mimeData().urls():
filename = url.toLocalFile()
if self.isImageFile(filename):
filenames.append(filename)
elif os.path.isdir(unicode(filename)):
directories.append(filename)
self.addImageDirs(directories)
self.addImageFiles(filenames)
def onFileNew(self):
if self.saveIfNeeded():
self.book = Book()
self.listWidgetFiles.clear()
def onFileOpen(self):
if not self.saveIfNeeded():
return
filename = QtGui.QFileDialog.getOpenFileName(
parent=self,
caption='Select a book file to open',
filter='Mangle files (*.mngl);;All files (*.*)'
)
if not filename.isNull():
self.loadBook(self.cleanupBookFile(filename))
def onFileSave(self):
self.saveBook(False)
def onFileSaveAs(self):
self.saveBook(True)
def onFilesContextMenu(self, point):
menu = QtGui.QMenu(self)
menu.addAction(self.menu_Add.menuAction())
if len(self.listWidgetFiles.selectedItems()) > 0:
menu.addAction(self.menu_Shift.menuAction())
menu.addAction(self.actionBookRemove)
menu.exec_(self.listWidgetFiles.mapToGlobal(point))
def onFilesDoubleClick(self, item):
services = QtGui.QDesktopServices()
services.openUrl(QtCore.QUrl.fromLocalFile(item.text()))
def onBookAddFiles(self):
filenames = QtGui.QFileDialog.getOpenFileNames(
parent=self,
caption='Select image file(s) to add',
filter='Image files (*.jpeg *.jpg *.gif *.png);;All files (*.*)'
)
self.addImageFiles(filenames)
def onBookAddDirectory(self):
directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select an image directory to add')
if not directory.isNull():
self.addImageDirs([directory])
def onBookShiftUp(self):
self.shiftImageFiles(-1)
def onBookShiftDown(self):
self.shiftImageFiles(1)
def onBookRemove(self):
self.removeImageFiles()
def onBookOptions(self):
dialog = DialogOptions(self, self.book)
dialog.exec_()
def onBookExport(self):
if len(self.book.images) == 0:
QtGui.QMessageBox.warning(self, 'Mangle', 'This book has no images to export')
return
if self.book.title == None:
dialog = DialogOptions(self, self.book)
if dialog.exec_() == QtGui.QDialog.Rejected:
return
directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select a directory to export book to')
if not directory.isNull():
dialog = DialogConvert(self, self.book, directory)
dialog.exec_()
def onHelpHomepage(self):
services = QtGui.QDesktopServices()
services.openUrl(QtCore.QUrl('http://foosoft.net/mangle'))
def onHelpAbout(self):
dialog = DialogAbout(self)
dialog.exec_()
def saveIfNeeded(self):
if not self.book.modified:
return True
result = QtGui.QMessageBox.question(
self,
'Mangle',
'Save changes to the current book?',
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel,
QtGui.QMessageBox.Yes
)
return (
result == QtGui.QMessageBox.No or
result == QtGui.QMessageBox.Yes and self.saveBook()
)
def saveBook(self, browse=False):
if self.book.title == None:
QtGui.QMessageBox.warning(self, 'Mangle', 'You must specify a title for this book before saving')
return False
filename = self.book.filename
if filename == None or browse:
filename = QtGui.QFileDialog.getSaveFileName(
parent=self,
caption='Select a book file to save as',
filter='Mangle files (*.mngl);;All files (*.*)'
)
if filename.isNull():
return False
filename = self.cleanupBookFile(filename)
try:
self.book.save(filename)
except RuntimeError, error:
QtGui.QMessageBox.critical(self, 'Mangle', str(error))
return False
return True
def loadBook(self, filename):
try:
self.book.load(filename)
except RuntimeError, error:
QtGui.QMessageBox.critical(self, 'Mangle', str(error))
else:
self.listWidgetFiles.clear()
for image in self.book.images:
self.listWidgetFiles.addItem(image)
def shiftImageFile(self, row, delta):
validShift = (
(delta > 0 and row < self.listWidgetFiles.count() - delta) or
(delta < 0 and row >= abs(delta))
)
if not validShift:
return
item = self.listWidgetFiles.takeItem(row)
self.listWidgetFiles.insertItem(row + delta, item)
self.listWidgetFiles.setItemSelected(item, True)
self.book.modified = True
self.book.images[row], self.book.images[row + delta] = (
self.book.images[row + delta], self.book.images[row]
)
def shiftImageFiles(self, delta):
items = self.listWidgetFiles.selectedItems()
rows = sorted([self.listWidgetFiles.row(item) for item in items])
for row in rows if delta < 0 else reversed(rows):
self.shiftImageFile(row, delta)
def removeImageFiles(self):
for item in self.listWidgetFiles.selectedItems():
row = self.listWidgetFiles.row(item)
self.listWidgetFiles.takeItem(row)
self.book.images.remove(item.text())
self.book.modified = True
def addImageFiles(self, filenames):
filenamesListed = []
for i in xrange(0, self.listWidgetFiles.count()):
filenamesListed.append(self.listWidgetFiles.item(i).text())
for filename in filenames:
if filename not in filenamesListed:
filename = QtCore.QString(filename)
self.listWidgetFiles.addItem(filename)
self.book.images.append(filename)
self.book.modified = True
def addImageDirs(self, directories):
filenames = []
for directory in directories:
for root, subdirs, subfiles in os.walk(unicode(directory)):
for filename in subfiles:
path = os.path.join(root, filename)
if self.isImageFile(path):
filenames.append(path)
if self.isZipFile(path):
ret = self.openZipFile(path)
filenames.append(ret)
filenames.sort()
self.addImageFiles(filenames)
def openZipFile(self, filename):
archive = zipfile.ZipFile(filename, "r")
for name in archive.namelist():
try:
data = archive.read()
data = image.convert_to_image(data)
self.book.images.append(data)
except:
pass
return False
def isImageFile(self, filename):
imageExts = ['.jpeg', '.jpg', '.gif', '.png']
filename = unicode(filename)
return (
os.path.isfile(filename) and
os.path.splitext(filename)[1].lower() in imageExts
)
def isZipFile(self, filename):
filename = unicode(filename)
return (
os.path.isfile(filename) and zipfile.is_zipfile(filename)
)
def cleanupBookFile(self, filename):
if len(os.path.splitext(unicode(filename))[1]) == 0:
filename += '.mngl'
return filename