Switch to unified view

a b/src/qtgui/webcache.cpp
1
/* Copyright (C) 2016 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#include "autoconfig.h"
18
19
#include <sstream>
20
#include <iostream>
21
#include MEMORY_INCLUDE
22
#include UNORDERED_MAP_INCLUDE
23
24
#ifndef USING_STD_REGEX
25
#include <sys/types.h>
26
#include <regex.h>
27
#else
28
#include <regex>
29
#endif
30
31
#include <QDebug>
32
33
#include "recoll.h"
34
#include "webcache.h"
35
#include "beaglequeuecache.h"
36
#include "circache.h"
37
#include "conftree.h"
38
39
using namespace std;
40
41
class CEnt {
42
public:
43
    CEnt(const string& ud, const string& ur, const string& mt)
44
        : udi(ud), url(ur), mimetype(mt) {
45
    }
46
    string udi;
47
    string url;
48
    string mimetype;
49
};
50
51
class WebcacheModelInternal {
52
public:
53
    STD_SHARED_PTR<BeagleQueueCache> cache;
54
    vector<CEnt> all;
55
    vector<CEnt> disp;
56
};
57
58
WebcacheModel::WebcacheModel(QObject *parent)
59
    : QAbstractTableModel(parent), m(new WebcacheModelInternal())
60
{
61
    qDebug() << "WebcacheModel::WebcacheModel()";
62
    m->cache =
63
        STD_SHARED_PTR<BeagleQueueCache>(new BeagleQueueCache(theconfig));
64
65
    if (m->cache) {
66
        bool eof;
67
        m->cache->cc()->rewind(eof);
68
        while (!eof) {
69
            string udi, sdic;
70
            m->cache->cc()->getCurrent(udi, sdic);
71
            ConfSimple dic(sdic);
72
            string mime, url;
73
            dic.get("mimetype", mime);
74
            dic.get("url", url);
75
            if (!udi.empty()) {
76
                m->all.push_back(CEnt(udi, url, mime));
77
                m->disp.push_back(CEnt(udi, url, mime));
78
            }
79
            if (!m->cache->cc()->next(eof))
80
                break;
81
        }
82
    }
83
}
84
85
int WebcacheModel::rowCount(const QModelIndex&) const
86
{
87
    //qDebug() << "WebcacheModel::rowCount(): " << m->disp.size();
88
    return int(m->disp.size());
89
}
90
91
int WebcacheModel::columnCount(const QModelIndex&) const
92
{
93
    //qDebug() << "WebcacheModel::columnCount()";
94
    return 2;
95
}
96
97
QVariant WebcacheModel::headerData (int col, Qt::Orientation orientation, 
98
                                    int role) const
99
{
100
    // qDebug() << "WebcacheModel::headerData()";
101
    if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
102
        return QVariant();
103
    }
104
    switch (col) {
105
    case 0: return QVariant(tr("MIME"));
106
    case 1: return QVariant(tr("Url"));
107
    default: return QVariant();
108
    }
109
}
110
111
QVariant WebcacheModel::data(const QModelIndex& index, int role) const
112
{
113
    //qDebug() << "WebcacheModel::data()";
114
    Q_UNUSED(index);
115
    if (role != Qt::DisplayRole) {
116
        return QVariant();
117
    }
118
    int row = index.row();
119
    if (row < 0 || row >= int(m->disp.size())) {
120
        return QVariant();
121
    }
122
123
    /* We now read the data on init */
124
#if 0
125
    string sdic;
126
    if (!m->cache->cc()->get(m->disp[row].udi, sdic)) {
127
        return QVariant();
128
    }
129
    ConfSimple dic(sdic);
130
    //ostringstream os; dic.write(os);  cerr << "DIC: " << os.str() << endl;
131
    string mime, url;
132
    dic.get("mimetype", mime);
133
    dic.get("url", url);
134
#else
135
    const string& mime = m->disp[row].mimetype;
136
    const string& url = m->disp[row].url;
137
#endif
138
    
139
    switch (index.column()) {
140
    case 0: return QVariant(QString::fromUtf8(mime.c_str()));
141
    case 1: return QVariant(QString::fromUtf8(url.c_str()));
142
    default: return QVariant();
143
    }
144
}
145
146
#ifndef USING_STD_REGEX
147
#define M_regexec(A,B,C,D,E) regexec(&(A),B,C,D,E)
148
#else
149
#define M_regexec(A,B,C,D,E) (!regex_match(B,A))
150
#endif
151
152
void WebcacheModel::setSearchFilter(const QString& _txt)
153
{
154
    string txt = qs2utf8s(_txt);
155
    
156
#ifndef USING_STD_REGEX
157
    regex_t exp;
158
    if (regcomp(&exp, txt.c_str(), REG_NOSUB|REG_EXTENDED)) {
159
        //qDebug() << "regcomp failed for " << _txt;
160
        return;
161
    }
162
#else
163
    basic_regex exp;
164
    try {
165
        exp = basic_regexp(txt, std::regex_constants::nosubs |
166
                           std::regex_constants::extended);
167
    } catch(...) {
168
        return;
169
    }
170
#endif
171
    
172
    m->disp.clear();
173
    for (unsigned int i = 0; i < m->all.size(); i++) {
174
        if (!M_regexec(exp, m->all[i].url.c_str(), 0, 0, 0)) {
175
            m->disp.push_back(m->all[i]);
176
        } else {
177
            //qDebug() << "match failed. exp" << _txt << "data" <<
178
            // m->all[i].url.c_str();
179
        }
180
    }
181
    emit dataChanged(createIndex(0,0,0), createIndex(1, m->all.size(),0));
182
}
183
184
185
WebcacheEdit::WebcacheEdit(QWidget *parent)
186
    : QDialog(parent)
187
{
188
    qDebug() << "WebcacheEdit::WebcacheEdit()";
189
    setupUi(this);
190
    m_model = new WebcacheModel(this);
191
    webcacheTV->setModel(m_model);
192
    connect(searchLE, SIGNAL(textEdited(const QString&)),
193
            m_model, SLOT(setSearchFilter(const QString&)));
194
}
195