~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/gui2/viewer/bookmarkmanager.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import with_statement
 
2
 
 
3
__license__   = 'GPL v3'
 
4
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
5
 
 
6
import cPickle, os
 
7
 
 
8
from PyQt4.Qt import Qt, QDialog, QAbstractTableModel, QVariant, SIGNAL, \
 
9
    QModelIndex, QInputDialog, QLineEdit, QFileDialog
 
10
 
 
11
from calibre.gui2.viewer.bookmarkmanager_ui import Ui_BookmarkManager
 
12
from calibre.gui2 import NONE, qstring_to_unicode
 
13
 
 
14
class BookmarkManager(QDialog, Ui_BookmarkManager):
 
15
    def __init__(self, parent, bookmarks):
 
16
        QDialog.__init__(self, parent)
 
17
 
 
18
        self.setupUi(self)
 
19
 
 
20
        self.bookmarks = bookmarks[:]
 
21
        self.set_bookmarks()
 
22
 
 
23
        self.connect(self.button_revert, SIGNAL('clicked()'), self.set_bookmarks)
 
24
        self.connect(self.button_delete, SIGNAL('clicked()'), self.delete_bookmark)
 
25
        self.connect(self.button_edit, SIGNAL('clicked()'), self.edit_bookmark)
 
26
        self.connect(self.button_export, SIGNAL('clicked()'), self.export_bookmarks)
 
27
        self.connect(self.button_import, SIGNAL('clicked()'), self.import_bookmarks)
 
28
 
 
29
    def set_bookmarks(self, bookmarks=None):
 
30
        if bookmarks == None:
 
31
            bookmarks = self.bookmarks[:]
 
32
        self._model = BookmarkTableModel(self, bookmarks)
 
33
        self.bookmarks_table.setModel(self._model)
 
34
 
 
35
    def delete_bookmark(self):
 
36
        indexes = self.bookmarks_table.selectionModel().selectedIndexes()
 
37
        if indexes != []:
 
38
            self._model.remove_row(indexes[0].row())
 
39
 
 
40
    def edit_bookmark(self):
 
41
        indexes = self.bookmarks_table.selectionModel().selectedIndexes()
 
42
        if indexes != []:
 
43
            title, ok = QInputDialog.getText(self, _('Edit bookmark'), _('New title for bookmark:'), QLineEdit.Normal, self._model.data(indexes[0], Qt.DisplayRole).toString())
 
44
            title = QVariant(unicode(title).strip())
 
45
            if ok and title:
 
46
                self._model.setData(indexes[0], title, Qt.EditRole)
 
47
 
 
48
    def get_bookmarks(self):
 
49
        return self._model.bookmarks
 
50
 
 
51
    def export_bookmarks(self):
 
52
        filename = QFileDialog.getSaveFileName(self, _("Export Bookmarks"),
 
53
                '%s%suntitled.pickle' % (os.getcwdu(), os.sep),
 
54
                _("Saved Bookmarks (*.pickle)"))
 
55
        if filename == '':
 
56
            return
 
57
 
 
58
        with open(filename, 'w') as fileobj:
 
59
            cPickle.dump(self._model.bookmarks, fileobj)
 
60
 
 
61
    def import_bookmarks(self):
 
62
        filename = QFileDialog.getOpenFileName(self, _("Import Bookmarks"), '%s' % os.getcwdu(), _("Pickled Bookmarks (*.pickle)"))
 
63
        if filename == '':
 
64
            return
 
65
 
 
66
        imported = None
 
67
        with open(filename, 'r') as fileobj:
 
68
            imported = cPickle.load(fileobj)
 
69
 
 
70
        if imported != None:
 
71
            bad = False
 
72
            try:
 
73
                for bm in imported:
 
74
                    if len(bm) != 2:
 
75
                        bad = True
 
76
                        break
 
77
            except:
 
78
                pass
 
79
 
 
80
            if not bad:
 
81
                bookmarks = self._model.bookmarks[:]
 
82
                for bm in imported:
 
83
                    if bm not in bookmarks and bm[0] != 'calibre_current_page_bookmark':
 
84
                        bookmarks.append(bm)
 
85
                self.set_bookmarks(bookmarks)
 
86
 
 
87
 
 
88
class BookmarkTableModel(QAbstractTableModel):
 
89
    headers = [_("Name")]
 
90
 
 
91
    def __init__(self, parent, bookmarks):
 
92
        QAbstractTableModel.__init__(self, parent)
 
93
 
 
94
        self.bookmarks = bookmarks[:]
 
95
 
 
96
    def rowCount(self, parent):
 
97
        if parent and parent.isValid():
 
98
            return 0
 
99
        return len(self.bookmarks)
 
100
 
 
101
    def columnCount(self, parent):
 
102
        if parent and parent.isValid():
 
103
            return 0
 
104
        return len(self.headers)
 
105
 
 
106
    def data(self, index, role):
 
107
        if role in (Qt.DisplayRole, Qt.EditRole):
 
108
            ans = self.bookmarks[index.row()][0]
 
109
            return NONE if ans is None else QVariant(ans)
 
110
        return NONE
 
111
 
 
112
    def setData(self, index, value, role):
 
113
        if role == Qt.EditRole:
 
114
            self.bookmarks[index.row()] = (qstring_to_unicode(value.toString()).strip(), self.bookmarks[index.row()][1])
 
115
            self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), index, index)
 
116
            return True
 
117
        return False
 
118
 
 
119
    def flags(self, index):
 
120
        flags = QAbstractTableModel.flags(self, index)
 
121
        flags |= Qt.ItemIsEditable
 
122
        return flags
 
123
 
 
124
    def headerData(self, section, orientation, role):
 
125
        if role != Qt.DisplayRole:
 
126
            return NONE
 
127
        if orientation == Qt.Horizontal:
 
128
            return QVariant(self.headers[section])
 
129
        else:
 
130
            return QVariant(section+1)
 
131
 
 
132
    def remove_row(self, row):
 
133
        self.beginRemoveRows(QModelIndex(), row, row)
 
134
        del self.bookmarks[row]
 
135
        self.endRemoveRows()
 
136