~ps-jenkins/ubuntu-ui-extras/trusty-proposed

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/Extras/plugin/browser/history-matches-model.cpp

  • Committer: Ugo Riboni
  • Date: 2013-07-02 19:40:38 UTC
  • Revision ID: ugo.riboni@canonical.com-20130702194038-v9l2q3v2xxjjxn8v
Remove all browser work and leave only an example component and skeleton build system

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2013 Canonical Ltd.
3
 
 *
4
 
 * This file is part of webbrowser-app.
5
 
 *
6
 
 * webbrowser-app is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; version 3.
9
 
 *
10
 
 * webbrowser-app is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
#include "history-matches-model.h"
20
 
#include "history-model.h"
21
 
 
22
 
// Qt
23
 
#include <QtCore/QRegExp>
24
 
 
25
 
/*!
26
 
    \class HistoryMatchesModel
27
 
    \brief Proxy model that filters the contents of the history model
28
 
           based on a query string
29
 
 
30
 
    HistoryMatchesModel is a proxy model that filters the contents of a
31
 
    HistoryModel based on a query string.
32
 
 
33
 
    The query string may contain several terms (or words).
34
 
 
35
 
    An entry in the history model matches if all the terms are contained in
36
 
    either its URL or its title (inclusive OR).
37
 
*/
38
 
HistoryMatchesModel::HistoryMatchesModel(QObject* parent)
39
 
    : QSortFilterProxyModel(parent)
40
 
{
41
 
}
42
 
 
43
 
HistoryModel* HistoryMatchesModel::sourceModel() const
44
 
{
45
 
    return qobject_cast<HistoryModel*>(QSortFilterProxyModel::sourceModel());
46
 
}
47
 
 
48
 
void HistoryMatchesModel::setSourceModel(HistoryModel* sourceModel)
49
 
{
50
 
    if (sourceModel != this->sourceModel()) {
51
 
        QSortFilterProxyModel::setSourceModel(sourceModel);
52
 
        Q_EMIT sourceModelChanged();
53
 
    }
54
 
}
55
 
 
56
 
const QString& HistoryMatchesModel::query() const
57
 
{
58
 
    return m_query;
59
 
}
60
 
 
61
 
void HistoryMatchesModel::setQuery(const QString& query)
62
 
{
63
 
    if (query != m_query) {
64
 
        m_query = query;
65
 
        m_terms = query.split(QRegExp("\\s+"), QString::SkipEmptyParts);
66
 
        invalidateFilter();
67
 
        Q_EMIT queryChanged();
68
 
        Q_EMIT termsChanged();
69
 
    }
70
 
}
71
 
 
72
 
const QStringList& HistoryMatchesModel::terms() const
73
 
{
74
 
    return m_terms;
75
 
}
76
 
 
77
 
bool HistoryMatchesModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
78
 
{
79
 
    if (m_terms.isEmpty()) {
80
 
        return false;
81
 
    }
82
 
    QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
83
 
    QString url = sourceModel()->data(index, HistoryModel::Url).toUrl().toString();
84
 
    QString title = sourceModel()->data(index, HistoryModel::Title).toString();
85
 
    Q_FOREACH (const QString& term, m_terms) {
86
 
        if (!url.contains(term, Qt::CaseInsensitive) &&
87
 
            !title.contains(term, Qt::CaseInsensitive)) {
88
 
            return false;
89
 
        }
90
 
    }
91
 
    return true;
92
 
}