a/src/python/samples/recollgui/qrecoll.py b/src/python/samples/recollgui/qrecoll.py
1
#!/usr/bin/env python
1
#!/usr/bin/env python
2
from __future__ import print_function
2
3
3
import sys
4
import sys
4
import datetime
5
import datetime
5
try:
6
try:
6
    from recoll import recoll
7
    from recoll import recoll
...
...
65
        self.pagelen = 10
66
        self.pagelen = 10
66
        self.attrs = ("filename", "title", "mtime", "url", "ipath")
67
        self.attrs = ("filename", "title", "mtime", "url", "ipath")
67
68
68
    def rowCount(self, parent):
69
    def rowCount(self, parent):
69
        ret = len(self.docs)
70
        ret = len(self.docs)
70
        #print "RecollQuery.rowCount(): ", ret
71
        #print("RecollQuery.rowCount(): %d"% ret)
71
        return ret
72
        return ret
72
73
73
    def columnCount(self, parent):
74
    def columnCount(self, parent):
74
        #print "RecollQuery.columnCount()"
75
        #print("RecollQuery.columnCount()")
75
        if parent.isValid():
76
        if parent.isValid():
76
            return 0
77
            return 0
77
        else:
78
        else:
78
            return len(self.attrs)
79
            return len(self.attrs)
79
80
80
    def setquery(self, db, q, sortfield="", ascending=True):
81
    def setquery(self, db, q, sortfield="", ascending=True):
81
        """Parse and execute query on open db"""
82
        """Parse and execute query on open db"""
82
        #print "RecollQuery.setquery():"
83
        #print("RecollQuery.setquery():")
83
        # Get query object
84
        # Get query object
84
        self.query = db.query()
85
        self.query = db.query()
85
        if sortfield:
86
        if sortfield:
86
            self.query.sortby(sortfield, ascending)
87
            self.query.sortby(sortfield, ascending)
87
        # Parse/run input query string
88
        # Parse/run input query string
...
...
96
            return self.docs[index.row()]
97
            return self.docs[index.row()]
97
        else:
98
        else:
98
            return None
99
            return None
99
100
100
    def sort(self, col, order):
101
    def sort(self, col, order):
101
        #print "sort", col, order
102
        #print("sort %s %s", (col, order))
102
        self.setquery(self.db, self.qtext, sortfield=self.attrs[col],
103
        self.setquery(self.db, self.qtext, sortfield=self.attrs[col],
103
                      ascending = order)
104
                      ascending = order)
104
105
105
    def headerData(self, idx, orient, role):
106
    def headerData(self, idx, orient, role):
106
        if orient == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
107
        if orient == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
107
            return self.attrs[idx]
108
            return self.attrs[idx]
108
        return None
109
        return None
109
110
110
    def data(self, index, role):
111
    def data(self, index, role):
111
        #print "RecollQuery.data: row %d, role: " % (index.row(),), role
112
        #print("RecollQuery.data: row %d, role: %s" % (index.row(),role))
112
        if not index.isValid():
113
        if not index.isValid():
113
            return QtCore.QVariant()
114
            return QtCore.QVariant()
114
115
115
        if index.row() >= len(self.docs):
116
        if index.row() >= len(self.docs):
116
            return QtCore.QVariant()
117
            return QtCore.QVariant()
117
118
118
        if role == QtCore.Qt.DisplayRole:
119
        if role == QtCore.Qt.DisplayRole:
119
            #print "RecollQuery.data: row %d, col %d role: " % \
120
            #print("RecollQuery.data: row %d, col %d role: %s" % \
120
             #     (index.row(), index.column()), role
121
            #     (index.row(), index.column() role))
121
            attr = self.attrs[index.column()]
122
            attr = self.attrs[index.column()]
122
            value = getattr(self.docs[index.row()], attr)
123
            value = getattr(self.docs[index.row()], attr)
123
            if attr == "mtime":
124
            if attr == "mtime":
124
                dte = datetime.datetime.fromtimestamp(int(value))
125
                dte = datetime.datetime.fromtimestamp(int(value))
125
                value = str(dte)
126
                value = str(dte)
126
            return value
127
            return value
127
        else:
128
        else:
128
            return QtCore.QVariant()
129
            return QtCore.QVariant()
129
130
130
    def canFetchMore(self, parent):
131
    def canFetchMore(self, parent):
131
        #print "RecollQuery.canFetchMore:"
132
        #print("RecollQuery.canFetchMore:")
132
        if len(self.docs) < self.totres:
133
        if len(self.docs) < self.totres:
133
            return True
134
            return True
134
        else:
135
        else:
135
            return False
136
            return False
136
137
137
    def fetchMore(self, parent):
138
    def fetchMore(self, parent):
138
        #print "RecollQuery.fetchMore:"
139
        #print("RecollQuery.fetchMore:")
139
        self.beginInsertRows(QtCore.QModelIndex(), len(self.docs), \
140
        self.beginInsertRows(QtCore.QModelIndex(), len(self.docs), \
140
                             len(self.docs) + self.pagelen)
141
                             len(self.docs) + self.pagelen)
141
        for count in range(self.pagelen):
142
        for count in range(self.pagelen):
142
            try:
143
            try:
143
                self.docs.append(self.query.fetchone())
144
                self.docs.append(self.query.fetchone())
...
...
153
        QMainWindow.__init__(self, parent)
154
        QMainWindow.__init__(self, parent)
154
        self.ui = rclmain.Ui_MainWindow()
155
        self.ui = rclmain.Ui_MainWindow()
155
        self.ui.setupUi(self)
156
        self.ui.setupUi(self)
156
        self.db = db
157
        self.db = db
157
        self.qmodel = RecollQuery()
158
        self.qmodel = RecollQuery()
158
        scq = QShortcut(QKeySequence("Ctrl+Q"), self);
159
        scq = QShortcut(QKeySequence("Ctrl+Q"), self)
159
        scq.activated.connect(self.onexit)
160
        scq.activated.connect(self.onexit)
160
        header = self.ui.resTable.horizontalHeader();
161
        header = self.ui.resTable.horizontalHeader()
161
  header.setSortIndicatorShown(True);
162
        header.setSortIndicatorShown(True)
162
  header.setSortIndicator(-1, QtCore.Qt.AscendingOrder);
163
        header.setSortIndicator(-1, QtCore.Qt.AscendingOrder)
163
        self.ui.resTable.setSortingEnabled(True)
164
        self.ui.resTable.setSortingEnabled(True)
164
        self.currentindex = -1
165
        self.currentindex = -1
165
        self.currentdoc = None
166
        self.currentdoc = None
166
        
167
        
167
    def on_searchEntry_returnPressed(self):
168
    def on_searchEntry_returnPressed(self):
...
...
170
    def on_resTable_clicked(self, index):
171
    def on_resTable_clicked(self, index):
171
        doc = self.qmodel.getdoc(index)
172
        doc = self.qmodel.getdoc(index)
172
        self.currentindex = index
173
        self.currentindex = index
173
        self.currentdoc = doc
174
        self.currentdoc = doc
174
        if doc is None:
175
        if doc is None:
175
            print "NO DoC"
176
            print("NO DoC")
176
            return
177
            return
177
        query = self.qmodel.query
178
        query = self.qmodel.query
178
        groups = query.getgroups()
179
        groups = query.getgroups()
179
        meths = HlMeths(groups)
180
        meths = HlMeths(groups)
180
        abs = query.makedocabstract(doc, methods=meths)
181
        abs = query.makedocabstract(doc, methods=meths)
181
        self.ui.resDetail.setText(abs)
182
        self.ui.resDetail.setText(abs)
182
        if hasextract:
183
        if hasextract:
183
            ipath = doc.get('ipath')
184
            ipath = doc.get('ipath')
184
            #print "ipath[%s]" % (ipath,)
185
            #print("ipath[%s]" % ipath)
185
            self.ui.previewPB.setEnabled(True)
186
            self.ui.previewPB.setEnabled(True)
186
            if ipath:
187
            if ipath:
187
                self.ui.savePB.setEnabled(True)
188
                self.ui.savePB.setEnabled(True)
188
            else:
189
            else:
189
                self.ui.savePB.setEnabled(False)
190
                self.ui.savePB.setEnabled(False)
190
191
191
    @pyqtSlot()
192
    @pyqtSlot()
192
    def on_previewPB_clicked(self):
193
    def on_previewPB_clicked(self):
193
        print "on_previewPB_clicked(self)"
194
        print("on_previewPB_clicked(self)")
194
        newdoc = textextract(self.currentdoc)
195
        newdoc = textextract(self.currentdoc)
195
        query = self.qmodel.query;
196
        query = self.qmodel.query;
196
        groups = query.getgroups()
197
        groups = query.getgroups()
197
        meths = HlMeths(groups)
198
        meths = HlMeths(groups)
198
        #print "newdoc.mimetype:", newdoc.mimetype
199
        #print("newdoc.mimetype:", newdoc.mimetype)
199
        if newdoc.mimetype == 'text/html':
200
        if newdoc.mimetype == 'text/html':
200
            ishtml = True
201
            ishtml = True
201
        else:
202
        else:
202
            ishtml = False
203
            ishtml = False
203
        text = '<qt><head></head><body>' + \
204
        text = '<qt><head></head><body>' + \
...
...
208
        text += '</body></qt>'
209
        text += '</body></qt>'
209
        self.ui.resDetail.setText(text)
210
        self.ui.resDetail.setText(text)
210
211
211
    @pyqtSlot()
212
    @pyqtSlot()
212
    def on_savePB_clicked(self):
213
    def on_savePB_clicked(self):
213
        print "on_savePB_clicked(self)"
214
        print("on_savePB_clicked(self)")
214
        doc = self.currentdoc
215
        doc = self.currentdoc
215
        ipath = doc.ipath
216
        ipath = doc.ipath
216
        if not ipath:
217
        if not ipath:
217
            return
218
            return
218
        fn = QFileDialog.getSaveFileName(self)
219
        fn = QFileDialog.getSaveFileName(self)
219
        if fn:
220
        if fn:
220
            docitems = doc.items()
221
            docitems = doc.items()
221
            fn = extractofile(doc, str(fn.toLocal8Bit()))
222
            fn = extractofile(doc, str(fn.toLocal8Bit()))
222
            print "Saved as", fn
223
            print("Saved as %s" % fn)
223
        else:
224
        else:
224
            print >> sys.stderr, "Canceled"
225
            print("Canceled", file=sys.stderr)
225
                
226
                
226
    def startQuery(self):
227
    def startQuery(self):
227
        self.qmodel.setquery(self.db, self.ui.searchEntry.text())
228
        self.qmodel.setquery(self.db, self.ui.searchEntry.text())
228
        self.ui.resTable.setModel(self.qmodel)
229
        self.ui.resTable.setModel(self.qmodel)
229
230
230
    def onexit(self):
231
    def onexit(self):
231
        self.close()
232
        self.close()
232
233
233
234
234
def Usage():
235
def Usage():
235
    print >> sys.stderr, '''Usage: qt.py [<qword1> [<qword2> ...]]'''
236
    print('''Usage: qt.py [<qword1> [<qword2> ...]]''', file=sys.stderr)
236
    sys.exit(1)
237
    sys.exit(1)
237
238
238
239
239
def main(args):
240
def main(args):
240
241
...
...
252
        if opt == "-c":
253
        if opt == "-c":
253
            confdir = val
254
            confdir = val
254
        elif opt == "-i":
255
        elif opt == "-i":
255
            extra_dbs.append(val)
256
            extra_dbs.append(val)
256
        else:
257
        else:
257
            print >> sys.stderr, "Bad opt: ", opt
258
            print("Bad opt: %s"% opt, file=sys.stderr) 
258
            Usage()
259
            Usage()
259
260
260
    # The query should be in the remaining arg(s)
261
    # The query should be in the remaining arg(s)
261
    q = None
262
    q = None
262
    if len(args) > 0:
263
    if len(args) > 0: