From ee202b721dae35c2752c1ced62006b6654b559fa Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 26 May 2018 13:48:35 +0800 Subject: [PATCH] Refactor function --- src/gui/downloadfromurldialog.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/gui/downloadfromurldialog.cpp b/src/gui/downloadfromurldialog.cpp index 8502e7000..78b879a8e 100644 --- a/src/gui/downloadfromurldialog.cpp +++ b/src/gui/downloadfromurldialog.cpp @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include @@ -39,6 +39,20 @@ #include "ui_downloadfromurldialog.h" #include "utils.h" +namespace +{ + bool isDownloadable(const QString &str) + { + return (str.startsWith("http://", Qt::CaseInsensitive) + || str.startsWith("https://", Qt::CaseInsensitive) + || str.startsWith("ftp://", Qt::CaseInsensitive) + || str.startsWith("magnet:", Qt::CaseInsensitive) + || str.startsWith("bc://bt/", Qt::CaseInsensitive) + || ((str.size() == 40) && !str.contains(QRegularExpression("[^0-9A-Fa-f]"))) + || ((str.size() == 32) && !str.contains(QRegularExpression("[^2-7A-Za-z]")))); + } +} + DownloadFromURLDialog::DownloadFromURLDialog(QWidget *parent) : QDialog(parent) , m_ui(new Ui::DownloadFromURLDialog) @@ -61,15 +75,8 @@ DownloadFromURLDialog::DownloadFromURLDialog(QWidget *parent) str = str.trimmed(); if (str.isEmpty()) continue; - if (str.startsWith("http://", Qt::CaseInsensitive) - || str.startsWith("https://", Qt::CaseInsensitive) - || str.startsWith("ftp://", Qt::CaseInsensitive) - || str.startsWith("magnet:", Qt::CaseInsensitive) - || str.startsWith("bc://bt/", Qt::CaseInsensitive) - || ((str.size() == 40) && !str.contains(QRegExp("[^0-9A-Fa-f]"))) - || ((str.size() == 32) && !str.contains(QRegExp("[^2-7A-Za-z]")))) { + if (isDownloadable(str)) uniqueURLs << str; - } } m_ui->textUrls->setText(uniqueURLs.toList().join('\n'));