linuxdcpp-team team mailing list archive
-
linuxdcpp-team team
-
Mailing list archive
-
Message #04866
[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2699: user matching: better rule matching method selection; add "Hub address"
------------------------------------------------------------
revno: 2699
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Mon 2011-12-05 20:20:06 +0100
message:
user matching: better rule matching method selection; add "Hub address"
modified:
dcpp/Client.cpp
dcpp/UserMatch.cpp
dcpp/UserMatch.h
dcpp/UserMatchManager.cpp
dcpp/UserMatchManager.h
win32/UserMatchDlg.cpp
--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk
Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/Client.cpp'
--- dcpp/Client.cpp 2011-12-03 21:53:57 +0000
+++ dcpp/Client.cpp 2011-12-05 19:20:06 +0000
@@ -216,13 +216,13 @@
}
void Client::updated(OnlineUser& user) {
- UserMatchManager::getInstance()->match(user.getIdentity());
+ UserMatchManager::getInstance()->match(user);
fire(ClientListener::UserUpdated(), this, user);
}
void Client::updated(OnlineUserList& users) {
- std::for_each(users.begin(), users.end(), [](OnlineUser* user) { UserMatchManager::getInstance()->match(user->getIdentity()); });
+ std::for_each(users.begin(), users.end(), [](OnlineUser* user) { UserMatchManager::getInstance()->match(*user); });
fire(ClientListener::UsersUpdated(), this, users);
}
=== modified file 'dcpp/UserMatch.cpp'
--- dcpp/UserMatch.cpp 2011-12-04 14:56:09 +0000
+++ dcpp/UserMatch.cpp 2011-12-05 19:20:06 +0000
@@ -19,6 +19,7 @@
#include "stdinc.h"
#include "UserMatch.h"
+#include "Client.h"
#include "FavoriteManager.h"
#include "format.h"
#include "LogManager.h"
@@ -26,15 +27,16 @@
namespace dcpp {
-bool UserMatch::Rule::isRegEx() const {
- return boost::get<boost::regex>(&search);
+UserMatch::Rule::Method UserMatch::Rule::getMethod() const {
+ return boost::get<StringSearch>(&search) ? PARTIAL : boost::get<string>(&search) ? EXACT : REGEX;
}
-void UserMatch::Rule::setRegEx(bool b) {
- if(b)
- search = boost::regex();
- else
- search = StringSearch();
+void UserMatch::Rule::setMethod(Method method) {
+ switch(method) {
+ case PARTIAL: search = StringSearch(); break;
+ case EXACT: search = string(); break;
+ case REGEX: search = boost::regex(); break;
+ }
}
struct Prepare : boost::static_visitor<bool> {
@@ -45,6 +47,11 @@
return true;
}
+ bool operator()(string& s) const {
+ s = pattern;
+ return true;
+ }
+
bool operator()(boost::regex& r) const {
try {
r.assign(pattern);
@@ -70,6 +77,10 @@
return s.match(str);
}
+ bool operator()(const string& s) const {
+ return str == s;
+ }
+
bool operator()(const boost::regex& r) const {
try {
return !r.empty() && boost::regex_search(str, r);
@@ -97,7 +108,9 @@
return !isSet(FAVS) && !isSet(OPS) && !isSet(BOTS) && rules.empty();
}
-bool UserMatch::match(Identity& identity) const {
+bool UserMatch::match(OnlineUser& user) const {
+ const auto& identity = user.getIdentity();
+
if(isSet(FAVS) && !FavoriteManager::getInstance()->isFavoriteUser(identity.getUser())) {
return false;
}
@@ -116,13 +129,13 @@
case UserMatch::Rule::NICK: str = identity.getNick(); break;
case UserMatch::Rule::CID: str = identity.getUser()->getCID().toBase32(); break;
case UserMatch::Rule::IP: str = identity.getIp(); break;
+ case UserMatch::Rule::HUB_ADDRESS: str = user.getClient().getHubUrl(); break;
}
if(!i->match(str)) {
return false;
}
}
- identity.setMatch(props);
return true;
}
=== modified file 'dcpp/UserMatch.h'
--- dcpp/UserMatch.h 2011-12-03 21:53:57 +0000
+++ dcpp/UserMatch.h 2011-12-05 19:20:06 +0000
@@ -50,20 +50,29 @@
NICK,
CID,
IP,
+ HUB_ADDRESS,
FIELD_LAST
} field;
string pattern;
- bool isRegEx() const;
- void setRegEx(bool b);
+ enum Method {
+ PARTIAL,
+ EXACT,
+ REGEX,
+
+ METHOD_LAST
+ };
+
+ Method getMethod() const;
+ void setMethod(Method method);
private:
friend struct UserMatch;
bool prepare();
bool match(const string& str) const;
- boost::variant<StringSearch, boost::regex> search;
+ boost::variant<StringSearch, string, boost::regex> search;
};
vector<Rule> rules;
@@ -73,7 +82,7 @@
void addRule(Rule&& rule);
bool empty() const;
- bool match(Identity& identity) const;
+ bool match(OnlineUser& user) const;
};
/** Stores properties to be applied to matched users. */
=== modified file 'dcpp/UserMatchManager.cpp'
--- dcpp/UserMatchManager.cpp 2011-12-04 15:13:29 +0000
+++ dcpp/UserMatchManager.cpp 2011-12-05 19:20:06 +0000
@@ -45,13 +45,8 @@
auto cm = ClientManager::getInstance();
auto lock = cm->lock();
- // swap the new list.
-#ifdef __GNUC__ /// @todo GCC doesn't seem to support vector swapping to an rvalue ref...
- auto& lvalueList = newList;
- const_cast<UserMatches&>(list).swap(lvalueList);
-#else
- const_cast<UserMatches&>(list).swap(std::forward<UserMatches>(newList));
-#endif
+ // assign the new list.
+ const_cast<UserMatches&>(list) = std::forward<UserMatches>(newList);
// refresh user matches.
auto& users = cm->getOnlineUsers();
@@ -60,13 +55,14 @@
}
}
-void UserMatchManager::match(Identity& identity) const {
+void UserMatchManager::match(OnlineUser& user) const {
for(auto i = list.cbegin(), iend = list.cend(); i != iend; ++i) {
- if(i->match(identity)) {
+ if(i->match(user)) {
+ user.getIdentity().setMatch(i->props);
return;
}
}
- identity.setMatch(nullptr);
+ user.getIdentity().setMatch(nullptr);
}
void UserMatchManager::on(SettingsManagerListener::Load, SimpleXML& xml) noexcept {
@@ -97,10 +93,9 @@
while(xml.findChild("Rule")) {
UserMatch::Rule rule;
- if(xml.getBoolChildAttrib("Regex")) { rule.setRegEx(true); }
-
rule.field = static_cast<decltype(rule.field)>(xml.getIntChildAttrib("Field"));
rule.pattern = xml.getChildData();
+ rule.setMethod(static_cast<UserMatch::Rule::Method>(xml.getIntChildAttrib("Method")));
match.addRule(std::move(rule));
}
@@ -136,10 +131,8 @@
xml.stepIn();
for(auto rule = i->rules.cbegin(), rule_end = i->rules.cend(); rule != rule_end; ++rule) {
xml.addTag("Rule", rule->pattern);
-
- if(rule->isRegEx()) { xml.addChildAttrib("Regex", true); }
-
xml.addChildAttrib("Field", rule->field);
+ xml.addChildAttrib("Method", rule->getMethod());
}
xml.stepOut();
}
=== modified file 'dcpp/UserMatchManager.h'
--- dcpp/UserMatchManager.h 2011-12-03 21:53:57 +0000
+++ dcpp/UserMatchManager.h 2011-12-05 19:20:06 +0000
@@ -36,12 +36,12 @@
public:
/// Retrieve a copy of the list of user matching definitions.
UserMatches getList() const;
- /// Swap a new list of user matching definitions. All current users will be re-matched.
+ /// Assign a new list of user matching definitions. All current users will be re-matched.
void setList(UserMatches&& newList);
- /** Match the given identity against current user matching definitions. The "match" member of
- the identity object will point to the properties of the matched definition on success. */
- void match(Identity& identity) const;
+ /** Match the given user against current user matching definitions. The "match" member of the
+ user's identity object will point to the properties of the matched definition on success. */
+ void match(OnlineUser& user) const;
private:
friend class Singleton<UserMatchManager>;
=== modified file 'win32/UserMatchDlg.cpp'
--- win32/UserMatchDlg.cpp 2011-12-03 21:53:57 +0000
+++ win32/UserMatchDlg.cpp 2011-12-05 19:20:06 +0000
@@ -38,7 +38,7 @@
using dwt::Label;
UserMatchDlg::UserMatchDlg(dwt::Widget* parent, const UserMatch* initialMatcher) :
-GridDialog(parent, 500, DS_CONTEXTHELP),
+GridDialog(parent, 700, DS_CONTEXTHELP),
name(0),
favs(0),
ops(0),
@@ -143,7 +143,7 @@
int8_t counter = -1;
std::unique_ptr<UserMatch::Rule> rule;
boost::for_each(controls, [this, &counter, &rule](Control* control) {
- enum { RuleField, RuleSearch, RuleRegex, RuleRemove };
+ enum { RuleField, RuleSearch, RuleMethod, RuleRemove };
switch(++counter) {
case RuleField:
{
@@ -156,10 +156,9 @@
rule->pattern = Text::fromT(static_cast<TextBoxPtr>(control)->getText());
break;
}
- case RuleRegex:
+ case RuleMethod:
{
- if(static_cast<CheckBoxPtr>(control)->getChecked())
- rule->setRegEx(true);
+ rule->setMethod(static_cast<UserMatch::Rule::Method>(static_cast<ComboBoxPtr>(control)->getSelected()));
break;
}
case RuleRemove:
@@ -200,7 +199,7 @@
auto search = rules->addChild(WinUtil::Seeds::Dialog::textBox);
- auto regex = rules->addChild(CheckBox::Seed(_T("RE")));
+ auto method = rules->addChild(WinUtil::Seeds::Dialog::comboBox);
{
auto seed = Button::Seed(_T("X"));
@@ -212,12 +211,15 @@
}); });
}
- tstring fields[UserMatch::Rule::FIELD_LAST] = { T_("Nick"), T_("CID"), T_("IP") };
+ tstring fields[UserMatch::Rule::FIELD_LAST] = { T_("Nick"), T_("CID"), T_("IP"), T_("Hub address") };
std::for_each(fields, fields + UserMatch::Rule::FIELD_LAST, [field](const tstring& str) { field->addValue(str); });
field->setSelected(rule ? rule->field : 0);
+ tstring methods[UserMatch::Rule::METHOD_LAST] = { T_("Partial match"), T_("Exact match"), T_("Regular Expression") };
+ std::for_each(methods, methods + UserMatch::Rule::METHOD_LAST, [method](const tstring& str) { method->addValue(str); });
+ method->setSelected(rule ? rule->getMethod() : 0);
+
if(rule) {
search->setText(Text::toT(rule->pattern));
- if(rule->isRegEx()) { regex->setChecked(true); }
}
}