File indexing completed on 2024-12-08 04:29:44

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "filtersettings.h"
0010 
0011 #include <QStringList>
0012 
0013 #include <KConfigGroup>
0014 #include <KLocalizedString>
0015 #include <KSharedConfig>
0016 
0017 #include "filter.h"
0018 
0019 FilterSettings *FilterSettings::_self = nullptr;
0020 QMap<Filter::FilterField, QString> FilterSettings::_filterFieldName;
0021 QMap<Filter::FilterType, QString> FilterSettings::_filterTypeName;
0022 QMap<Filter::FilterAction, QString> FilterSettings::_filterActionName;
0023 bool FilterSettings::_hideNoneFriendsReplies = false;
0024 bool FilterSettings::_hideRepliesNotRelatedToMe = false;
0025 
0026 FilterSettings *FilterSettings::self()
0027 {
0028     if (!_self) {
0029         _self = new FilterSettings;
0030         _filterFieldName[Filter::AuthorUsername] = i18n("Author Username");
0031         _filterFieldName[Filter::Content] = i18n("Post Text");
0032         _filterFieldName[Filter::Source] = i18n("Author Client");
0033         _filterFieldName[Filter::ReplyToUsername] = i18n("Reply to User");
0034 
0035         _filterTypeName[Filter::Contain] = i18n("Contain");
0036         _filterTypeName[Filter::DoesNotContain] = i18n("Does Not Contain");
0037         _filterTypeName[Filter::ExactMatch] = i18n("Exact Match");
0038         _filterTypeName[Filter::RegExp] = i18n("Regular Expression");
0039 
0040         _filterActionName[Filter::Remove] = i18nc("@action", "Hide Posts");
0041         _filterActionName[Filter::Highlight] = i18nc("@action", "Highlight Posts");
0042     }
0043     return _self;
0044 }
0045 
0046 FilterSettings::FilterSettings(): QObject()
0047 {
0048     conf = new KConfigGroup(KSharedConfig::openConfig(), QLatin1String("Filter Plugin"));
0049     readConfig();
0050 }
0051 
0052 FilterSettings::~FilterSettings()
0053 {
0054 
0055 }
0056 
0057 QList< Filter * > FilterSettings::filters() const
0058 {
0059     return _filters;
0060 }
0061 
0062 void FilterSettings::readConfig()
0063 {
0064     _filters.clear();
0065     //Filter group names are start with Filter_%Text%%Field%%Type%
0066     KSharedConfig::openConfig()->sync();
0067     QStringList groups = KSharedConfig::openConfig()->groupList();
0068     for (const QString &grp: groups) {
0069         if (grp.startsWith(QLatin1String("Filter_"))) {
0070             Filter *f = new Filter(KSharedConfig::openConfig()->group(grp), this);
0071             if (f->filterText().isEmpty()) {
0072                 continue;
0073             }
0074             _filters << f;
0075             //qDebug() << "REEADING A FILTER";
0076         }
0077     }
0078     //qDebug() << filters().count();
0079 
0080     _hideNoneFriendsReplies = conf->readEntry("hideNoneFriendsReplies", false);
0081     _hideRepliesNotRelatedToMe = conf->readEntry("hideRepliesNotRelatedToMe", false);
0082 }
0083 
0084 void FilterSettings::setFilters(const QList< Filter * > &filters)
0085 {
0086     _filters = filters;
0087 }
0088 
0089 void FilterSettings::writeConfig()
0090 {
0091     QStringList groups = KSharedConfig::openConfig()->groupList();
0092     for (const QString &grp: groups) {
0093         if (grp.startsWith(QLatin1String("Filter_"))) {
0094             KSharedConfig::openConfig()->deleteGroup(grp);
0095         }
0096     }
0097     conf->writeEntry("hideNoneFriendsReplies", _hideNoneFriendsReplies);
0098     conf->writeEntry("hideRepliesNotRelatedToMe", _hideRepliesNotRelatedToMe);
0099     KSharedConfig::openConfig()->sync();
0100 
0101     for (Filter *f: _filters) {
0102         f->writeConfig();
0103     }
0104 
0105     readConfig();
0106 }
0107 
0108 QString FilterSettings::filterFieldName(Filter::FilterField field)
0109 {
0110     return _filterFieldName.value(field);
0111 }
0112 
0113 QString FilterSettings::filterTypeName(Filter::FilterType type)
0114 {
0115     return _filterTypeName.value(type);
0116 }
0117 
0118 Filter::FilterField FilterSettings::filterFieldFromName(const QString &name)
0119 {
0120     return _filterFieldName.key(name);
0121 }
0122 
0123 Filter::FilterType FilterSettings::filterTypeFromName(const QString &name)
0124 {
0125     return _filterTypeName.key(name);
0126 }
0127 
0128 Filter::FilterAction FilterSettings::filterActionFromName(const QString &name)
0129 {
0130     return _filterActionName.key(name);
0131 }
0132 
0133 QString FilterSettings::filterActionName(Filter::FilterAction action)
0134 {
0135     return _filterActionName.value(action);
0136 }
0137 
0138 QMap< Filter::FilterField, QString > FilterSettings::filterFieldsMap()
0139 {
0140     return _filterFieldName;
0141 }
0142 
0143 QMap< Filter::FilterType, QString > FilterSettings::filterTypesMap()
0144 {
0145     return _filterTypeName;
0146 }
0147 
0148 QMap< Filter::FilterAction, QString > FilterSettings::filterActionsMap()
0149 {
0150     return _filterActionName;
0151 }
0152 
0153 bool FilterSettings::hideNoneFriendsReplies()
0154 {
0155     return _hideNoneFriendsReplies;
0156 }
0157 
0158 void FilterSettings::setHideNoneFriendsReplies(bool enable)
0159 {
0160     _hideNoneFriendsReplies = enable;
0161 }
0162 
0163 bool FilterSettings::hideRepliesNotRelatedToMe()
0164 {
0165     return _hideRepliesNotRelatedToMe;
0166 }
0167 
0168 void FilterSettings::setHideRepliesNotRelatedToMe(bool enable)
0169 {
0170     _hideRepliesNotRelatedToMe = enable;
0171 }
0172 
0173 #include "moc_filtersettings.cpp"