PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/adblock/adblockdialog.cpp

https://github.com/8jack/arora
C++ | 150 lines | 98 code | 24 blank | 28 comment | 6 complexity | 0da10ab6cc58ea77643ba75537c5126a MD5 | raw file
  1. /**
  2. * Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
  13. * may be used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include "adblockdialog.h"
  29. #include "adblockmodel.h"
  30. #include "adblockmanager.h"
  31. #include "adblocksubscription.h"
  32. #include "treesortfilterproxymodel.h"
  33. #include <qdesktopservices.h>
  34. #include <qmenu.h>
  35. #include <qurl.h>
  36. #include <qdebug.h>
  37. AdBlockDialog::AdBlockDialog(QWidget *parent)
  38. : QDialog(parent)
  39. {
  40. setupUi(this);
  41. m_adBlockModel = new AdBlockModel(this);
  42. m_proxyModel = new TreeSortFilterProxyModel(this);
  43. m_proxyModel->setSourceModel(m_adBlockModel);
  44. treeView->setModel(m_proxyModel);
  45. connect(search, SIGNAL(textChanged(QString)),
  46. m_proxyModel, SLOT(setFilterFixedString(QString)));
  47. AdBlockManager *manager = AdBlockManager::instance();
  48. adblockGroupBox->setChecked(manager->isEnabled());
  49. connect(adblockGroupBox, SIGNAL(toggled(bool)),
  50. AdBlockManager::instance(), SLOT(setEnabled(bool)));
  51. QMenu *menu = new QMenu(this);
  52. connect(menu, SIGNAL(aboutToShow()),
  53. this, SLOT(aboutToShowActionMenu()));
  54. actionToolButton->setMenu(menu);
  55. actionToolButton->setIcon(QIcon(QLatin1String(":128x128/run.png")));
  56. actionToolButton->setPopupMode(QToolButton::InstantPopup);
  57. AdBlockSubscription *subscription = manager->customRules();
  58. QModelIndex subscriptionIndex = m_adBlockModel->index(subscription);
  59. treeView->expand(m_proxyModel->mapFromSource(subscriptionIndex));
  60. }
  61. void AdBlockDialog::aboutToShowActionMenu()
  62. {
  63. QMenu *menu = actionToolButton->menu();
  64. menu->clear();
  65. QAction *addRule = menu->addAction(tr("Add Custom Rule"));
  66. connect(addRule, SIGNAL(triggered()), this, SLOT(addCustomRule()));
  67. QAction *learnRule = menu->addAction(tr("Learn more about writing rules..."));
  68. connect(learnRule, SIGNAL(triggered()), this, SLOT(learnAboutWritingFilters()));
  69. menu->addSeparator();
  70. QModelIndex idx = m_proxyModel->mapToSource(treeView->currentIndex());
  71. QAction *updateSubscription = menu->addAction(tr("Update Subscription"));
  72. connect(updateSubscription, SIGNAL(triggered()), this, SLOT(updateSubscription()));
  73. if (!idx.isValid())
  74. updateSubscription->setEnabled(false);
  75. QAction *addSubscription = menu->addAction(tr("Browse Subscriptions..."));
  76. connect(addSubscription, SIGNAL(triggered()), this, SLOT(browseSubscriptions()));
  77. menu->addSeparator();
  78. QAction *removeSubscription = menu->addAction(tr("Remove Subscription"));
  79. connect(removeSubscription, SIGNAL(triggered()), this, SLOT(removeSubscription()));
  80. if (!idx.isValid())
  81. removeSubscription->setEnabled(false);
  82. }
  83. void AdBlockDialog::addCustomRule(const QString &rule)
  84. {
  85. AdBlockManager *manager = AdBlockManager::instance();
  86. AdBlockSubscription *subscription = manager->customRules();
  87. Q_ASSERT(subscription);
  88. subscription->addRule(AdBlockRule(rule));
  89. // reset the model
  90. qApp->processEvents();
  91. QModelIndex parent = m_adBlockModel->index(subscription);
  92. int x = m_adBlockModel->rowCount(parent);
  93. QModelIndex ruleIndex = m_adBlockModel->index(x - 1, 0, parent);
  94. treeView->expand(m_proxyModel->mapFromSource(parent));
  95. treeView->edit(m_proxyModel->mapFromSource(ruleIndex));
  96. }
  97. void AdBlockDialog::updateSubscription()
  98. {
  99. QModelIndex idx = m_proxyModel->mapToSource(treeView->currentIndex());
  100. if (!idx.isValid())
  101. return;
  102. if (idx.parent().isValid())
  103. idx = idx.parent();
  104. AdBlockSubscription *subscription = (AdBlockSubscription*)m_adBlockModel->subscription(idx);
  105. subscription->updateNow();
  106. }
  107. void AdBlockDialog::browseSubscriptions()
  108. {
  109. QUrl url(QLatin1String("http://adblockplus.org/en/subscriptions"));
  110. QDesktopServices::openUrl(url);
  111. }
  112. void AdBlockDialog::learnAboutWritingFilters()
  113. {
  114. QUrl url(QLatin1String("http://adblockplus.org/en/filters"));
  115. QDesktopServices::openUrl(url);
  116. }
  117. void AdBlockDialog::removeSubscription()
  118. {
  119. QModelIndex idx = m_proxyModel->mapToSource(treeView->currentIndex());
  120. if (!idx.isValid())
  121. return;
  122. if (idx.parent().isValid())
  123. idx = idx.parent();
  124. AdBlockSubscription *subscription = (AdBlockSubscription*)m_adBlockModel->subscription(idx);
  125. AdBlockManager::instance()->removeSubscription(subscription);
  126. }