File indexing completed on 2024-04-28 04:57:35
0001 /*************************************************************************** 0002 * Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * 0018 ***************************************************************************/ 0019 0020 #include "kget_sortfilterproxymodel.h" 0021 0022 static const QString ARCHIVES = 0023 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"); 0024 static const QString WEB_CONTENT = QString("/html,/x-asp,/xhtml+xml,/x-php,"); 0025 0026 KGetSortFilterProxyModel::KGetSortFilterProxyModel(int column, QObject *parent) 0027 : QSortFilterProxyModel(parent) 0028 , m_filterType(NoFilter) 0029 , m_filterMode(Contain) 0030 , m_column(column) 0031 , m_showWebContent(false) 0032 { 0033 m_mimeTypes.insert(NoFilter, ""); 0034 m_mimeTypes.insert(VideoFiles, "video/"); 0035 m_mimeTypes.insert(AudioFiles, "audio/"); 0036 m_mimeTypes.insert(CompressedFiles, "archive/"); 0037 m_mimeTypes.insert(ImageFiles, "image/"); 0038 } 0039 0040 KGetSortFilterProxyModel::~KGetSortFilterProxyModel() 0041 { 0042 } 0043 0044 bool KGetSortFilterProxyModel::showWebContent() const 0045 { 0046 return m_showWebContent; 0047 } 0048 0049 void KGetSortFilterProxyModel::setFilterType(int filterType) 0050 { 0051 m_filterType = filterType; 0052 invalidateFilter(); 0053 } 0054 0055 void KGetSortFilterProxyModel::setFilterMode(int filterMode) 0056 { 0057 switch (filterMode) { 0058 case DoesNotContain: 0059 m_filterMode = DoesNotContain; 0060 break; 0061 case Contain: 0062 default: 0063 m_filterMode = Contain; 0064 } 0065 0066 invalidateFilter(); 0067 } 0068 0069 void KGetSortFilterProxyModel::setShowWebContent(bool show) 0070 { 0071 m_showWebContent = show; 0072 invalidateFilter(); 0073 } 0074 0075 void KGetSortFilterProxyModel::setShowWebContent(int show) 0076 { 0077 m_showWebContent = show; 0078 invalidateFilter(); 0079 } 0080 0081 void KGetSortFilterProxyModel::setFilterColumn(int column) 0082 { 0083 m_column = column; 0084 invalidateFilter(); 0085 } 0086 0087 bool KGetSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const 0088 { 0089 const QModelIndex index = sourceModel()->index(sourceRow, 1, sourceParent); 0090 if (!index.isValid() || index.data(Qt::UserRole).toString().isEmpty()) { 0091 return false; 0092 } 0093 0094 const QString meta = index.data(Qt::UserRole).toString(); 0095 const QString text = columnText(sourceRow, sourceParent); 0096 bool show = false; 0097 0098 // do not show entries if their text is empty when not using NoFilter and m_showWebContent 0099 if (!text.isEmpty() && (m_filterType != NoFilter)) { 0100 show = meta.startsWith(m_mimeTypes[m_filterType]); 0101 0102 if (m_filterType == CompressedFiles) { 0103 show = ARCHIVES.contains(meta.mid(meta.indexOf('/'))); 0104 } 0105 } else if (m_filterType == NoFilter) { 0106 if (m_showWebContent) { 0107 show = true; 0108 } else { 0109 show = !text.isEmpty() && !WEB_CONTENT.contains(meta.mid(meta.indexOf('/'))); 0110 } 0111 } 0112 0113 if (show) { 0114 show = acceptText(text); 0115 } 0116 0117 return show; 0118 } 0119 0120 QString KGetSortFilterProxyModel::columnText(int row, const QModelIndex &sourceParent) const 0121 { 0122 const QModelIndex index = sourceModel()->index(row, m_column, sourceParent); 0123 return (index.isValid() ? index.data(Qt::DisplayRole).toString() : QString()); 0124 } 0125 0126 bool KGetSortFilterProxyModel::acceptText(const QString &text) const 0127 { 0128 // look if the text-filter matches 0129 const auto re = filterRegularExpression(); 0130 const auto match = re.match(text); 0131 bool accept = (match.hasMatch() != -1) ? true : false; 0132 if ((m_filterMode == DoesNotContain) && !re.pattern().isEmpty()) { 0133 accept = !accept; 0134 } 0135 0136 return accept; 0137 } 0138 0139 #include "moc_kget_sortfilterproxymodel.cpp"