From f6210d58570ccf0e4ebb4af9836dc2cb06a76ce2 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 17 May 2008 17:01:03 +0000 Subject: [PATCH] - Removed IP filter list and buttons to add/remove ranges, I don't think people handle this manually anyway. - Prepare code for P2P/P2B filter files (soon to be coded) --- src/options.ui | 350 +++++++++++++------------------------------- src/options_imp.cpp | 262 ++++++++------------------------- src/options_imp.h | 2 - 3 files changed, 165 insertions(+), 449 deletions(-) diff --git a/src/options.ui b/src/options.ui index 1c26fc2df..c8ce7fafd 100644 --- a/src/options.ui +++ b/src/options.ui @@ -22,16 +22,7 @@ 6 - - 9 - - - 9 - - - 9 - - + 9 @@ -83,11 +74,20 @@ Qt::ElideLeft + + + 0 + 0 + 641 + 457 + + General - :/Icons/star.png + + :/Icons/star.png:/Icons/star.png @@ -120,7 +120,7 @@ Qt::Horizontal - + 40 20 @@ -189,7 +189,7 @@ Qt::Horizontal - + 40 20 @@ -250,7 +250,7 @@ Qt::Horizontal - + 40 20 @@ -318,7 +318,7 @@ Qt::Vertical - + 623 20 @@ -329,11 +329,20 @@ + + + 0 + 0 + 641 + 457 + + Downloads - :/Icons/download.png + + :/Icons/download.png:/Icons/download.png @@ -360,16 +369,7 @@ 6 - - 0 - - - 0 - - - 0 - - + 0 @@ -377,16 +377,7 @@ 6 - - 0 - - - 0 - - - 0 - - + 0 @@ -473,16 +464,7 @@ 6 - - 0 - - - 0 - - - 0 - - + 0 @@ -490,16 +472,7 @@ 6 - - 0 - - - 0 - - - 0 - - + 0 @@ -580,7 +553,7 @@ Qt::Horizontal - + 40 20 @@ -623,7 +596,7 @@ Qt::Horizontal - + 40 20 @@ -639,11 +612,20 @@ + + + 0 + 0 + 641 + 457 + + Connection - :/Icons/connection.png + + :/Icons/connection.png:/Icons/connection.png @@ -706,7 +688,7 @@ Qt::Horizontal - + 20 20 @@ -954,7 +936,7 @@ Qt::Horizontal - + 21 29 @@ -1043,7 +1025,7 @@ Qt::Horizontal - + 40 20 @@ -1145,11 +1127,20 @@ + + + 0 + 0 + 641 + 457 + + Bittorrent - :/Icons/bt_settings.png + + :/Icons/bt_settings.png:/Icons/bt_settings.png @@ -1196,7 +1187,7 @@ Qt::Horizontal - + 40 20 @@ -1241,7 +1232,7 @@ Qt::Horizontal - + 40 20 @@ -1283,7 +1274,7 @@ Qt::Horizontal - + 40 20 @@ -1365,7 +1356,7 @@ Qt::Horizontal - + 40 20 @@ -1428,7 +1419,7 @@ Qt::Horizontal - + 40 20 @@ -1482,7 +1473,7 @@ Qt::Horizontal - + 40 20 @@ -1500,7 +1491,7 @@ Qt::Vertical - + 20 40 @@ -1511,11 +1502,20 @@ + + + 0 + 0 + 641 + 457 + + Misc - :/Icons/configure.png + + :/Icons/configure.png:/Icons/configure.png @@ -1526,170 +1526,20 @@ Filter Settings - - - 6 - - - 9 - - - 9 - - - 9 - - - 9 - + Activate IP Filtering - :/Icons/filter.png + + :/Icons/filter.png:/Icons/filter.png - - - false - - - - 0 - 171 - - - - - Sans Serif - 8 - 50 - false - false - false - false - - - - QAbstractItemView::ExtendedSelection - - - false - - - - Start IP - - - - - End IP - - - - - Origin - - - - - Comment - - - - - - - - 6 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - false - - - Add Range - - - :/Icons/skin/add.png - - - - - - - false - - - Remove Range - - - :/Icons/skin/remove.png - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - 6 - - - 0 - - - 0 - - - 0 - - - 0 - + @@ -1791,7 +1641,7 @@ Qt::Horizontal - + 40 20 @@ -1825,7 +1675,7 @@ Qt::Horizontal - + 40 20 @@ -1842,14 +1692,36 @@ + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + 0 + 0 + 641 + 457 + + Web UI - :/Icons/password.png + + :/Icons/password.png:/Icons/password.png @@ -1902,7 +1774,7 @@ Qt::Horizontal - + 21 29 @@ -1987,7 +1859,7 @@ Qt::Horizontal - + 198 57 @@ -2003,7 +1875,7 @@ Qt::Vertical - + 623 41 @@ -2020,16 +1892,7 @@ 6 - - 0 - - - 0 - - - 0 - - + 0 @@ -2038,7 +1901,7 @@ Qt::Horizontal - QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok true @@ -2104,9 +1967,6 @@ checkRatioRemove spinMaxRatio checkIPFilter - filtersList - addFilterRangeButton - delFilterRangeButton textFilterPath browseFilterButton spinRSSRefresh diff --git a/src/options_imp.cpp b/src/options_imp.cpp index e27516473..84d43cd88 100644 --- a/src/options_imp.cpp +++ b/src/options_imp.cpp @@ -200,8 +200,6 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){ connect(spinMaxRatio, SIGNAL(valueChanged(QString)), this, SLOT(enableApplyButton())); // Misc tab connect(checkIPFilter, SIGNAL(stateChanged(int)), this, SLOT(enableApplyButton())); - connect(addFilterRangeButton, SIGNAL(clicked()), this, SLOT(enableApplyButton())); - connect(delFilterRangeButton, SIGNAL(clicked()), this, SLOT(enableApplyButton())); connect(textFilterPath, SIGNAL(textChanged(QString)), this, SLOT(enableApplyButton())); connect(spinRSSRefresh, SIGNAL(valueChanged(QString)), this, SLOT(enableApplyButton())); connect(spinRSSMaxArticlesPerFeed, SIGNAL(valueChanged(QString)), this, SLOT(enableApplyButton())); @@ -845,17 +843,11 @@ void options_imp::enableMaxUploadsLimitPerTorrent(int checkBoxValue){ void options_imp::enableFilter(int checkBoxValue){ if(checkBoxValue!=2){ //Disable - filtersList->setEnabled(false); - addFilterRangeButton->setEnabled(false); - delFilterRangeButton->setEnabled(false); lblFilterPath->setEnabled(false); textFilterPath->setEnabled(false); browseFilterButton->setEnabled(false); }else{ //enable - filtersList->setEnabled(true); - addFilterRangeButton->setEnabled(true); - delFilterRangeButton->setEnabled(true); lblFilterPath->setEnabled(true); textFilterPath->setEnabled(true); browseFilterButton->setEnabled(true); @@ -1060,114 +1052,74 @@ void options_imp::on_browseSaveDirButton_clicked(){ } } -// look for ipfilter.dat file -// reads emule ipfilter files. -// with the following format: -// -// - , , -// -// first-ip is an ip address that defines the first -// address of the range -// last-ip is the last ip address in the range -// access is a number specifying the access control -// for this ip-range. Right now values > 127 = allowed -// and numbers <= 127 = blocked -// the rest of the line is ignored -// -// Lines may be commented using '#' or '//' +// Process ip filter file +// Supported formats: +// * eMule IP list (DAT): http://wiki.phoenixlabs.org/wiki/DAT_Format +// * PeerGuardian Text (P2P): http://wiki.phoenixlabs.org/wiki/P2P_Format (TODO) +// * PeerGuardian Binary (P2B): http://wiki.phoenixlabs.org/wiki/P2B_Format (TODO) void options_imp::processFilterFile(QString filePath){ qDebug("Processing filter files"); - filtersList->clear(); - QString manualFilters= misc::qBittorrentPath() + QString::fromUtf8("ipfilter.dat"); - QStringList filterFiles(manualFilters); - filterFiles.append(filePath); - for(int i=0; i<2; ++i){ - QFile file(filterFiles.at(i)); - QStringList IP; - if (file.exists()){ - if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ - QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath)); + QFile file(filePath); + QStringList IP; + if (file.exists()){ + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ + QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath)); + return; + } + unsigned int nbLine = 0; + while (!file.atEnd()) { + ++nbLine; + QByteArray line = file.readLine(); + if(line.startsWith('#') || line.startsWith("//")) continue; + // Line is not commented + QList partsList = line.split(','); + unsigned int nbElem = partsList.size(); + if(nbElem < 2){ + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); continue; } - unsigned int nbLine = 0; - while (!file.atEnd()) { - ++nbLine; - QByteArray line = file.readLine(); - if(line.startsWith('#') || line.startsWith("//")) continue; - // Line is not commented - QList partsList = line.split(','); - unsigned int nbElem = partsList.size(); - if(nbElem < 2){ - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - continue; - } - bool ok; - int nbAccess = partsList.at(1).trimmed().toInt(&ok); - if(!ok){ - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - continue; + bool ok; + int nbAccess = partsList.at(1).trimmed().toInt(&ok); + if(!ok){ + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + continue; + } + if(nbAccess <= 127){ + QString strComment; + QString strStartIP = partsList.at(0).split('-').at(0).trimmed(); + QString strEndIP = partsList.at(0).split('-').at(1).trimmed(); + if(nbElem > 2){ + strComment = partsList.at(2).trimmed(); + }else{ + strComment = QString(); } - if(nbAccess <= 127){ - QString strComment; - QString strStartIP = partsList.at(0).split('-').at(0).trimmed(); - QString strEndIP = partsList.at(0).split('-').at(1).trimmed(); - if(nbElem > 2){ - strComment = partsList.at(2).trimmed(); - }else{ - strComment = QString(); - } - // Split IP - - QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp); - QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp); - - if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) { - // IPv4 - IP = strStartIP.split('.'); - address_v4 start((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt()); - IP = strEndIP.split('.'); - address_v4 last((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt()); - - // add it to list - QStringList item(QString::fromUtf8(start.to_string().c_str())); - item.append(QString::fromUtf8(last.to_string().c_str())); - if(!i){ - item.append(QString::fromUtf8("Manual")); - }else{ - item.append(QString::fromUtf8("ipfilter.dat")); - } - item.append(strComment); - new QTreeWidgetItem(filtersList, item); - // Apply to bittorrent session - filter.add_rule(start, last, ip_filter::blocked); - } else if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) { - // IPv6, ex : 1fff:0000:0a88:85a3:0000:0000:ac1f:8001 - IP = strStartIP.split(':'); - address_v6 start = address_v6::from_string(strStartIP.remove(':', 0).toUtf8().data()); - IP = strEndIP.split(':'); - address_v6 last = address_v6::from_string(strEndIP.remove(':', 0).toUtf8().data()); - - // add it to list - QStringList item(QString::fromUtf8(start.to_string().c_str())); - item.append(QString::fromUtf8(last.to_string().c_str())); - if(!i){ - item.append(QString::fromUtf8("Manual")); - }else{ - item.append(QString::fromUtf8("ipfilter.dat")); - } - item.append(strComment); - new QTreeWidgetItem(filtersList, item); - // Apply to bittorrent session - filter.add_rule(start, last, ip_filter::blocked); - } else { - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - continue; - } - + // Split IP + QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp); + QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp); + + if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) { + // IPv4 + IP = strStartIP.split('.'); + address_v4 start((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt()); + IP = strEndIP.split('.'); + address_v4 last((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt()); + // Apply to bittorrent session + filter.add_rule(start, last, ip_filter::blocked); + } else if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) { + // IPv6, ex : 1fff:0000:0a88:85a3:0000:0000:ac1f:8001 + IP = strStartIP.split(':'); + address_v6 start = address_v6::from_string(strStartIP.remove(':', 0).toUtf8().data()); + IP = strEndIP.split(':'); + address_v6 last = address_v6::from_string(strEndIP.remove(':', 0).toUtf8().data()); + // Apply to bittorrent session + filter.add_rule(start, last, ip_filter::blocked); + } else { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + continue; } } - file.close(); } + file.close(); } } @@ -1176,100 +1128,6 @@ ip_filter options_imp::getFilter() const{ return filter; } -// Add an IP Range to ipFilter -void options_imp::on_addFilterRangeButton_clicked(){ - bool ok; - // Ask user for start ip - QString startIP = QInputDialog::getText(this, tr("Range Start IP"), - tr("Start IP:"), QLineEdit::Normal, - QString::fromUtf8("0.0.0.0"), &ok); - QStringList IP1 = startIP.split('.'); - // Check IP - bool ipv4 = true; - QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp); - QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp); - - - - if(!ok) { - return; - } else if(startIP.isEmpty() - || (!startIP.contains(is_ipv4) && !startIP.contains(is_ipv6))){ - QMessageBox::critical(0, tr("Invalid IP"), tr("This IP is invalid.")); - return; - } else if(startIP.contains(is_ipv4)) { - ipv4 = true; - } else if(startIP.contains(is_ipv6)) { - ipv4 = false; - } - - // Ask user for last ip - QString lastIP = QInputDialog::getText(this, tr("Range End IP"), - tr("End IP:"), QLineEdit::Normal, - startIP, &ok); - // check IP - if (!ok) { - return; - } else if(lastIP.isEmpty() - || (!lastIP.contains(is_ipv4) && !lastIP.contains(is_ipv6)) - || (ipv4 == true && !lastIP.contains(is_ipv4)) - || (ipv4 == false && !lastIP.contains(is_ipv6))){ - QMessageBox::critical(0, tr("Invalid IP"), tr("This IP is invalid.")); - return; - } - - // Ask user for Comment - QString comment = QInputDialog::getText(this, tr("IP Range Comment"), - tr("Comment:"), QLineEdit::Normal, - QString::fromUtf8(""), &ok); - if (!ok){ - comment = QString::fromUtf8(""); - return; - } - QFile ipfilter(misc::qBittorrentPath() + QString::fromUtf8("ipfilter.dat")); - if (!ipfilter.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text)){ - std::cerr << "Error: Couldn't write in ipfilter.dat"; - return; - } - QTextStream out(&ipfilter); - out << startIP << " - " << lastIP << ", 0, " << comment << "\n"; - ipfilter.close(); - processFilterFile(textFilterPath->text()); - enableApplyButton(); -} - -// Delete selected IP range in list and ipfilter.dat file -// User can only delete IP added manually -void options_imp::on_delFilterRangeButton_clicked(){ - bool changed = false; - QList selectedItems = filtersList->selectedItems(); - // Delete from list - for(int i=0; itext(2) == QString::fromUtf8("Manual")){ - delete item; - changed = true; - } - if(changed){ - enableApplyButton(); - } - } - // Update ipfilter.dat - QFile ipfilter(misc::qBittorrentPath() + QString::fromUtf8("ipfilter.dat")); - if (!ipfilter.open(QIODevice::WriteOnly | QIODevice::Text)){ - std::cerr << "Error: Couldn't write in ipfilter.dat"; - return; - } - QTextStream out(&ipfilter); - for(int i=0; itopLevelItemCount();++i){ - QTreeWidgetItem *item = filtersList->topLevelItem(i); - if(item->text(2) == QString::fromUtf8("Manual")){ - out << item->text(0) << " - " << item->text(1) << ", 0, " << item->text(3) << "\n"; - } - } - ipfilter.close(); -} - // Web UI void options_imp::enableWebUi(bool checkBoxValue){ diff --git a/src/options_imp.h b/src/options_imp.h index d2b0cf837..31046805f 100644 --- a/src/options_imp.h +++ b/src/options_imp.h @@ -133,8 +133,6 @@ class options_imp : public QDialog, private Ui::Dialog { void closeEvent(QCloseEvent *e); void on_buttonBox_rejected(); void applySettings(QAbstractButton* button); - void on_addFilterRangeButton_clicked(); - void on_delFilterRangeButton_clicked(); void on_browseScanDirButton_clicked(); void on_browseFilterButton_clicked(); void on_browseSaveDirButton_clicked();