void AdBlockSubscription::subscriptionDownloaded() {
  if (m_reply != qobject_cast<QNetworkReply*>(sender())) {
    return;
  }

  bool error = false;
  const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();

  if (m_reply->error() != QNetworkReply::NoError || !response.startsWith(QByteArray("[Adblock")) || !saveDownloadedData(response)) {
    error = true;
  }

  m_reply->manager()->deleteLater();
  m_reply->deleteLater();
  m_reply = 0;

  if (error) {
    emit subscriptionError(tr("Cannot load subscription!"));

    return;
  }

  loadSubscription(AdBlockManager::instance()->disabledRules());
  emit subscriptionUpdated();
  emit subscriptionChanged();
}
示例#2
0
AdBlockSubscription* AdBlockManager::addSubscription(const QString &title, const QString &url)
{
    if (title.isEmpty() || url.isEmpty()) {
        return 0;
    }

    QString fileName = QzTools::filterCharsFromFilename(title.toLower()) + ".txt";
    QString filePath = QzTools::ensureUniqueFilename(DataPaths::currentProfilePath() + "/adblock/" + fileName);

    QByteArray data = QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1();

    QSaveFile file(filePath);
    if (!file.open(QFile::WriteOnly)) {
        qWarning() << "AdBlockManager: Cannot write to file" << filePath;
        return 0;
    }
    file.write(data);
    file.commit();

    AdBlockSubscription* subscription = new AdBlockSubscription(title, this);
    subscription->setUrl(QUrl(url));
    subscription->setFilePath(filePath);
    subscription->loadSubscription(m_disabledRules);

    m_subscriptions.insert(m_subscriptions.count() - 1, subscription);
    connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
    connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));

    return subscription;
}
示例#3
0
AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent)
    : TreeWidget(parent)
    , m_subscription(subscription)
    , m_topItem(0)
    , m_itemChangingBlock(false)
{
    setContextMenuPolicy(Qt::CustomContextMenu);
    setDefaultItemShowMode(TreeWidget::ItemsExpanded);
    setHeaderHidden(true);
    setAlternatingRowColors(true);
    setLayoutDirection(Qt::LeftToRight);

    connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequested(QPoint)));
    connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(itemChanged(QTreeWidgetItem*)));
    connect(m_subscription, SIGNAL(subscriptionUpdated()), this, SLOT(subscriptionUpdated()));
    connect(m_subscription, SIGNAL(subscriptionError(QString)), this, SLOT(subscriptionError(QString)));
}
void AdBlockSubscription::subscriptionDownloaded()
{
    if (m_reply != qobject_cast<FollowRedirectReply*>(sender())) {
        return;
    }

    QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();

    if (m_reply->error() == QNetworkReply::NoError && response.startsWith("[Adblock")) {
        // Prepend subscription info
        response.prepend(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8());

        saveDownloadedData(response);

        loadSubscription(AdBlockManager::instance()->disabledRules());
        emit subscriptionUpdated();
    }

    m_reply->deleteLater();
    m_reply = 0;
}