1 /***************************************************************************
2 *   Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>                     *
3 *                                                                         *
4 *   This program is free software; you can redistribute it and/or modify  *
5 *   it under the terms of the GNU General Public License as published by  *
6 *   the Free Software Foundation; either version 2 of the License, or     *
7 *   (at your option) any later version.                                   *
8 *                                                                         *
9 *   This program is distributed in the hope that it will be useful,       *
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 *   GNU General Public License for more details.                          *
13 *                                                                         *
14 *   You should have received a copy of the GNU General Public License     *
15 *   along with this program; if not, write to the                         *
16 *   Free Software Foundation, Inc.,                                       *
17 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
18 ***************************************************************************/
19 
20 #include "kget_sortfilterproxymodel.h"
21 
22 
23 static const QString ARCHIVES = QString("/x-7z-compressed,/x-ace,/x-archive,/x-arj,/x-bzip,/x-bzip-compressed-tar,/x-compressed-tar,/x-deb/,/x-rar,/x-tar,/x-rpm,/x-tarz,/zip");
24 static const QString WEB_CONTENT = QString("/html,/x-asp,/xhtml+xml,/x-php,");
25 
KGetSortFilterProxyModel(int column,QObject * parent)26 KGetSortFilterProxyModel::KGetSortFilterProxyModel(int column, QObject *parent)
27   : QSortFilterProxyModel(parent),
28     m_filterType(NoFilter),
29     m_filterMode(Contain),
30     m_column(column),
31     m_showWebContent(false)
32 {
33     m_mimeTypes.insert(NoFilter, "");
34     m_mimeTypes.insert(VideoFiles, "video/");
35     m_mimeTypes.insert(AudioFiles, "audio/");
36     m_mimeTypes.insert(CompressedFiles, "archive/");
37     m_mimeTypes.insert(ImageFiles, "image/");
38 }
39 
~KGetSortFilterProxyModel()40 KGetSortFilterProxyModel::~KGetSortFilterProxyModel()
41 {
42 }
43 
showWebContent() const44 bool KGetSortFilterProxyModel::showWebContent() const
45 {
46     return m_showWebContent;
47 }
48 
setFilterType(int filterType)49 void KGetSortFilterProxyModel::setFilterType(int filterType)
50 {
51     m_filterType = filterType;
52     invalidateFilter();
53 }
54 
setFilterMode(int filterMode)55 void KGetSortFilterProxyModel::setFilterMode(int filterMode)
56 {
57     switch (filterMode)
58     {
59         case DoesNotContain:
60             m_filterMode = DoesNotContain;
61             break;
62         case Contain:
63         default:
64             m_filterMode = Contain;
65     }
66 
67     invalidateFilter();
68 }
69 
setShowWebContent(bool show)70 void KGetSortFilterProxyModel::setShowWebContent(bool show)
71 {
72     m_showWebContent = show;
73     invalidateFilter();
74 }
75 
setShowWebContent(int show)76 void KGetSortFilterProxyModel::setShowWebContent(int show)
77 {
78     m_showWebContent = show;
79     invalidateFilter();
80 }
81 
setFilterColumn(int column)82 void KGetSortFilterProxyModel::setFilterColumn(int column)
83 {
84     m_column = column;
85     invalidateFilter();
86 }
87 
filterAcceptsRow(int sourceRow,const QModelIndex & sourceParent) const88 bool KGetSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
89 {
90     const QModelIndex index = sourceModel()->index(sourceRow, 1, sourceParent);
91     if (!index.isValid() || index.data(Qt::UserRole).toString().isEmpty())
92     {
93         return false;
94     }
95 
96     const QString meta = index.data(Qt::UserRole).toString();
97     const QString text = columnText(sourceRow, sourceParent);
98     bool show = false;
99 
100 
101     //do not show entries if their text is empty when not using NoFilter and m_showWebContent
102     if (!text.isEmpty() && (m_filterType != NoFilter))
103     {
104         show = meta.startsWith(m_mimeTypes[m_filterType]);
105 
106         if (m_filterType == CompressedFiles)
107         {
108             show = ARCHIVES.contains(meta.mid(meta.indexOf('/')));
109         }
110     }
111     else if (m_filterType == NoFilter)
112     {
113         if (m_showWebContent)
114         {
115             show = true;
116         }
117         else
118         {
119             show = !text.isEmpty() && !WEB_CONTENT.contains(meta.mid(meta.indexOf('/')));
120         }
121     }
122 
123     if (show) {
124         show = acceptText(text);
125     }
126 
127     return show;
128 }
129 
columnText(int row,const QModelIndex & sourceParent) const130 QString KGetSortFilterProxyModel::columnText(int row, const QModelIndex &sourceParent) const
131 {
132     const QModelIndex index = sourceModel()->index(row, m_column, sourceParent);
133     return (index.isValid() ? index.data(Qt::DisplayRole).toString() : QString());
134 }
135 
acceptText(const QString & text) const136 bool KGetSortFilterProxyModel::acceptText(const QString &text) const
137 {
138     //look if the text-filter matches
139     const QRegExp re = filterRegExp();
140     bool accept = (re.indexIn(text) != -1) ? true : false;
141     if ((m_filterMode == DoesNotContain) && !re.isEmpty()) {
142         accept = !accept;
143     }
144 
145     return accept;
146 }
147