[PATCH] per-domain popup policy settings

Sergio Visinoni piffio at arklinux.org
Sun Jun 8 17:38:12 BST 2003


Hi all,
attached you'll find two patches (one for kdelibs and one for kdebase,
generated against KDE_3_1_BRANCH from 20030520) that allows per-domain
web popups policy (as per cookies / javascript / etc).
here: http://www.piffio.org/kcontrol-popup.png you can see the kcontrol
tab (I've moved the default policy settings here as well from the 
javascript tab).

Comments and suggestions are welcome. Let me know if I should ask someone
to commit this patch (since I don't have a cvs account yet) or not.

greetings,
sergio visinoni
-------------- next part --------------
--- kdelibs/khtml/ecma/kjs_window.cpp.popup	2003-06-05 00:49:31.000000000 +0200
+++ kdelibs/khtml/ecma/kjs_window.cpp	2003-06-05 02:34:46.000000000 +0200
@@ -1069,20 +1069,22 @@
   config->setGroup( "Java/JavaScript Settings" );
   int policy = config->readUnsignedNumEntry(  "WindowOpenPolicy", 0 ); // 0=allow, 1=ask, 2=deny, 3=smart
   delete config;
-  if (  policy == 1 ) {
+  if (  m_part->d->m_popUpPolicy == KHTMLSettings::KPopUpAsk || ( policy == 1 && 
+			  m_part->d->m_popUpPolicy == -1 ) ) {
     if ( KMessageBox::questionYesNo(widget,
                                     i18n( "This site is trying to open up a new browser "
                                           "window using JavaScript.\n"
                                           "Do you want to allow this?" ),
                                     i18n( "Confirmation: JavaScript Popup" ) ) == KMessageBox::Yes )
       policy = 0;
-  } else if ( policy == 3 )
+  } else if ( m_part->d->m_popUpPolicy == -1 && policy == 3 )
   {
     // window.open disabled unless from a key/mouse event
     if (static_cast<ScriptInterpreter *>(exec->interpreter())->isWindowOpenAllowed())
       policy = 0;
   }
-  if ( policy != 0 ) {
+  if ( m_part->d->m_popUpPolicy == KHTMLSettings::KPopUpReject || ( policy != 0 &&
+			  m_part->d->m_popUpPolicy == -1 ) ) {
     return Undefined();
   } else {
     KParts::WindowArgs winargs;
--- kdelibs/khtml/khtml_settings.cc.popup	2003-03-11 01:02:58.000000000 +0100
+++ kdelibs/khtml/khtml_settings.cc	2003-06-05 02:24:45.000000000 +0200
@@ -31,6 +31,7 @@
 
 
 typedef QMap<QString,KHTMLSettings::KJavaScriptAdvice> PolicyMap;
+typedef QMap<QString,KHTMLSettings::KPopUpAdvice> PopUpPolicyMap;
 
 class KHTMLSettingsPrivate
 {
@@ -62,6 +63,7 @@
 
     QMap<QString,KHTMLSettings::KJavaScriptAdvice> javaDomainPolicy;
     QMap<QString,KHTMLSettings::KJavaScriptAdvice> javaScriptDomainPolicy;
+    QMap<QString,KHTMLSettings::KPopUpAdvice> popUpDomainPolicy;
     QStringList fonts;
     QStringList defaultFonts;
 };
@@ -122,6 +124,52 @@
     }
 }
 
+// PopUp
+KHTMLSettings::KPopUpAdvice KHTMLSettings::p_strToAdvice(const QString& _str)
+{
+  KPopUpAdvice ret = KPopUpDunno;
+
+  if (!_str)
+        ret = KPopUpDunno;
+
+  if (_str.lower() == QString::fromLatin1("accept"))
+        ret = KPopUpAccept;
+  else if (_str.lower() == QString::fromLatin1("reject"))
+        ret = KPopUpReject;
+  else if (_str.lower() == QString::fromLatin1("ask"))
+        ret = KPopUpAsk;
+
+  return ret;
+}
+
+const char* KHTMLSettings::adviceToStr(KPopUpAdvice _advice)
+{
+    switch( _advice ) {
+    case KPopUpAccept: return I18N_NOOP("Accept");
+    case KPopUpReject: return I18N_NOOP("Reject");
+    case KPopUpAsk: return I18N_NOOP("Ask");
+    default: return 0;
+    }
+    return 0;
+}
+
+void KHTMLSettings::splitDomainAdvice(const QString& configStr, QString &domain,
+                                      KPopUpAdvice& popUpAdvice)
+{
+    QString tmp(configStr);
+    int splitIndex = tmp.find(':');
+    if ( splitIndex == -1)
+    {
+        domain = configStr.lower();
+        popUpAdvice = KPopUpDunno;
+    }
+    else
+    {
+        domain = tmp.left(splitIndex).lower();
+        QString adviceString = tmp.mid( splitIndex+1, tmp.length() );
+        popUpAdvice = p_strToAdvice( adviceString );
+    }
+}
 
 KHTMLSettings::KHTMLSettings()
 {
@@ -359,6 +407,20 @@
         config->writeEntry( "ECMADomainSettings", domainConfig );
       }
     }
+    
+    // PopUp
+    if( reset || config->hasKey( "PopUpDomainSettings" ) )
+    {
+      QStringList domainList = config->readListEntry( "PopUpDomainSettings" );
+      for ( QStringList::ConstIterator it = domainList.begin();
+                it != domainList.end(); ++it)
+      {
+        QString domain;
+        KPopUpAdvice popUpAdvice;
+        splitDomainAdvice(*it, domain, popUpAdvice);
+        d->popUpDomainPolicy[domain] = popUpAdvice;
+      }
+    }
   }
   config->setGroup(group_save);
 }
@@ -406,6 +468,39 @@
   return default_retval;
 }
 
+// Local helper for getPopUpPolicy
+static int lookup_hostname_popup_policy(const QString& hostname,
+                                   const PopUpPolicyMap& policy)
+{
+  if (hostname.isEmpty()) {
+    return -1;
+  }
+
+  // First check whether there is a perfect match.
+  if( policy.contains( hostname ) ) {
+    // yes, use it (unless dunno)
+    KHTMLSettings::KPopUpAdvice adv = policy[ hostname ];
+    return adv;
+  }
+
+  // Now, check for partial match.  Chop host from the left until
+  // there's no dots left.
+  QString host_part = hostname;
+  int dot_idx = -1;
+  while( (dot_idx = host_part.find(QChar('.'))) >= 0 ) {
+    host_part.remove(0,dot_idx);
+    if( policy.contains( host_part ) ) {
+      KHTMLSettings::KPopUpAdvice adv = policy[ host_part ];
+      return adv;
+    }
+    // assert(host_part[0] == QChar('.'));
+    host_part.remove(0,1); // Chop off the dot.
+  }
+
+  // No domain-specific entry, or was dunno: use global setting
+  return -1;
+}
+
 bool KHTMLSettings::isBackRightClickEnabled()
 {
   return d->m_bBackRightClick;
@@ -433,6 +528,12 @@
   return d->m_bEnablePlugins;
 }
 
+// PopUp
+int KHTMLSettings::getPopUpPolicy( const QString& hostname )
+{
+  return lookup_hostname_popup_policy(hostname.lower(), d->popUpDomainPolicy);
+}
+
 int KHTMLSettings::mediumFontSize() const
 {
     return d->m_fontSize;
--- kdelibs/khtml/khtml_settings.h.popup	2003-03-11 01:02:58.000000000 +0100
+++ kdelibs/khtml/khtml_settings.h	2003-06-05 02:28:19.000000000 +0200
@@ -44,7 +44,15 @@
 	KJavaScriptAccept,
 	KJavaScriptReject
     };
-
+   
+    // PopUp 
+    enum KPopUpAdvice {
+	KPopUpDunno=0,
+	KPopUpAccept,
+	KPopUpReject,
+	KPopUpAsk
+    };
+    
     enum KAnimationAdvice {
         KAnimationDisabled=0,
         KAnimationLoopOnce,
@@ -112,12 +120,21 @@
     bool isJavaScriptDebugEnabled( const QString& hostname = QString::null );
     bool isPluginsEnabled( const QString& hostname = QString::null );
 
+    // PopUp
+    int getPopUpPolicy( const QString& hostname = QString::null );
+
     // helpers for parsing domain-specific configuration, used in KControl module as well
     static KJavaScriptAdvice strToAdvice(const QString& _str);
     static void splitDomainAdvice(const QString& configStr, QString &domain,
 				  KJavaScriptAdvice &javaAdvice, KJavaScriptAdvice& javaScriptAdvice);
     static const char* adviceToStr(KJavaScriptAdvice _advice);
 
+    // PopUp
+    static KPopUpAdvice p_strToAdvice(const QString& _str);
+    static void splitDomainAdvice(const QString& configStr, QString &domain,
+                                          KPopUpAdvice& popUpAdvice);
+    static const char* adviceToStr(KPopUpAdvice _advice);
+    
     QString settingsToCSS() const;
     static const QString &availableFamilies();
 
--- kdelibs/khtml/khtmlpart_p.h.popup	2003-06-05 01:49:10.000000000 +0200
+++ kdelibs/khtml/khtmlpart_p.h	2003-06-05 01:50:52.000000000 +0200
@@ -218,6 +218,7 @@
   bool m_restored :1;
   int m_frameNameId;
   int m_dcop_counter;
+  int m_popUpPolicy;
   DCOPObject *m_dcopobject;
 
 #ifndef Q_WS_QWS
--- kdelibs/khtml/khtml_part.cpp.popup	2003-06-05 01:26:10.000000000 +0200
+++ kdelibs/khtml/khtml_part.cpp	2003-06-05 01:56:22.000000000 +0200
@@ -222,6 +222,7 @@
   d->m_bJScriptDebugEnabled = d->m_settings->isJavaScriptDebugEnabled();
   d->m_bJavaEnabled = d->m_settings->isJavaEnabled();
   d->m_bPluginsEnabled = d->m_settings->isPluginsEnabled();
+  d->m_popUpPolicy = d->m_settings->getPopUpPolicy();
 
   // Set the meta-refresh flag...
   d->m_metaRefreshEnabled = d->m_settings->isAutoDelayedActionsEnabled ();
@@ -323,6 +324,7 @@
   d->m_bJScriptDebugEnabled = KHTMLFactory::defaultHTMLSettings()->isJavaScriptDebugEnabled();
   d->m_bJavaEnabled = KHTMLFactory::defaultHTMLSettings()->isJavaEnabled(url.host());
   d->m_bPluginsEnabled = KHTMLFactory::defaultHTMLSettings()->isPluginsEnabled(url.host());
+  d->m_popUpPolicy = KHTMLFactory::defaultHTMLSettings()->getPopUpPolicy(url.host());
 
   m_url = url;
 
@@ -470,6 +472,7 @@
   d->m_bJScriptDebugEnabled = KHTMLFactory::defaultHTMLSettings()->isJavaScriptDebugEnabled();
   d->m_bJavaEnabled = KHTMLFactory::defaultHTMLSettings()->isJavaEnabled(url.host());
   d->m_bPluginsEnabled = KHTMLFactory::defaultHTMLSettings()->isPluginsEnabled(url.host());
+  d->m_popUpPolicy = KHTMLFactory::defaultHTMLSettings()->getPopUpPolicy(url.host());
 
 
   kdDebug( 6050 ) << "KHTMLPart::openURL now (before started) m_url = " << m_url.url() << endl;
@@ -4211,6 +4214,7 @@
   d->m_bJavaEnabled = settings->isJavaEnabled(m_url.host());
   d->m_bPluginsEnabled = settings->isPluginsEnabled(m_url.host());
   d->m_metaRefreshEnabled = settings->isAutoDelayedActionsEnabled ();
+  d->m_popUpPolicy = settings->getPopUpPolicy();
 
   delete d->m_settings;
   d->m_settings = new KHTMLSettings(*KHTMLFactory::defaultHTMLSettings());
-------------- next part --------------
--- kdebase/kcontrol/konqhtml/jsopts.cpp.popup	2003-05-20 04:54:23.000000000 +0200
+++ kdebase/kcontrol/konqhtml/jsopts.cpp	2003-06-04 18:32:05.000000000 +0200
@@ -145,33 +145,33 @@
                                           "button allows you to easily share your policies with other people by allowing "
                                           "you to save and retrive them from a zipped file.") );
 
-  js_popup = new QButtonGroup(4, Horizontal, i18n( "JavaScript Web Popups Policy" ), this);
-  js_popup->setExclusive(TRUE);
+  //js_popup = new QButtonGroup(4, Horizontal, i18n( "JavaScript Web Popups Policy" ), this);
+  //js_popup->setExclusive(TRUE);
 
-  QRadioButton* popupMode = new QRadioButton(i18n( "Allow" ), js_popup);
-  QWhatsThis::add( popupMode,i18n("Accept all popup window requests.") );
+  //QRadioButton* popupMode = new QRadioButton(i18n( "Allow" ), js_popup);
+  //QWhatsThis::add( popupMode,i18n("Accept all popup window requests.") );
 
-  popupMode = new QRadioButton(i18n( "Ask" ), js_popup);
-  QWhatsThis::add( popupMode,i18n("Prompt every time a popup window is requested.") );
+  //popupMode = new QRadioButton(i18n( "Ask" ), js_popup);
+  //QWhatsThis::add( popupMode,i18n("Prompt every time a popup window is requested.") );
 
-  popupMode = new QRadioButton(i18n( "Deny" ), js_popup);
-  QWhatsThis::add( popupMode,i18n("Reject all popup window requests.") );
-
-  popupMode = new QRadioButton(i18n( "Smart" ), js_popup);
-  QWhatsThis::add( popupMode, i18n("Accept popup window requests only when "
-                                   "links are activated through an explicit "
-                                   "mouse click or keyboard operation.") );
-  toplevel->addWidget(js_popup);
-  QWhatsThis::add( js_popup, i18n("If you disable this, Konqueror will stop "
-                                  "interpreting the <i>window.open()</i> "
-                                  "JavaScript command. This is useful if you "
-                                  "regulary visit sites that make extensive use "
-                                  "of this command to pop up ad banners.<br>"
-                                  "<br><b>Note:</b> Disabling this option might "
-                                  "also break certain sites that require <i>"
-                                  "window.open()</i> for proper operation. Use "
-                                  "this feature carefully!") );
-  connect( js_popup, SIGNAL( clicked( int ) ), this, SLOT( slotChanged() ) );
+  //popupMode = new QRadioButton(i18n( "Deny" ), js_popup);
+  //QWhatsThis::add( popupMode,i18n("Reject all popup window requests.") );
+
+  //popupMode = new QRadioButton(i18n( "Smart" ), js_popup);
+  //QWhatsThis::add( popupMode, i18n("Accept popup window requests only when "
+  //                                 "links are activated through an explicit "
+  //                                 "mouse click or keyboard operation.") );
+  //toplevel->addWidget(js_popup);
+  //QWhatsThis::add( js_popup, i18n("If you disable this, Konqueror will stop "
+  //                                "interpreting the <i>window.open()</i> "
+  //                                "JavaScript command. This is useful if you "
+  //                                "regulary visit sites that make extensive use "
+  //                                "of this command to pop up ad banners.<br>"
+  //                                "<br><b>Note:</b> Disabling this option might "
+  //                                "also break certain sites that require <i>"
+  //                                "window.open()</i> for proper operation. Use "
+  //                                "this feature carefully!") );
+  //connect( js_popup, SIGNAL( clicked( int ) ), this, SLOT( slotChanged() ) );
 
 /*
   kdDebug() << "\"Show debugger window\" says: make me useful!" << endl;
@@ -212,7 +212,7 @@
     // *** apply to GUI ***
     enableJavaScriptGloballyCB->setChecked( m_pConfig->readBoolEntry("EnableJavaScript",true));
 //    enableJavaScriptDebugCB->setChecked( m_pConfig->readBoolEntry("EnableJavaScriptDebug",false));
-    js_popup->setButton( m_pConfig->readUnsignedNumEntry("WindowOpenPolicy", 0) );
+    //js_popup->setButton( m_pConfig->readUnsignedNumEntry("WindowOpenPolicy", 0) );
 
   // enableDebugOutputCB->setChecked( m_pConfig->readBoolEntry("EnableJSDebugOutput") );
 }
@@ -221,7 +221,7 @@
 {
   enableJavaScriptGloballyCB->setChecked( true );
 //  enableJavaScriptDebugCB->setChecked( false );
-  js_popup->setButton(0);
+  //js_popup->setButton(0);
  // enableDebugOutputCB->setChecked( false );
 }
 
@@ -231,11 +231,11 @@
     m_pConfig->writeEntry( "EnableJavaScript", enableJavaScriptGloballyCB->isChecked() );
 //    m_pConfig->writeEntry( "EnableJavaScriptDebug", enableJavaScriptDebugCB->isChecked() );
 
-    int js_policy = 0;
-    if ( js_popup->selected() )
-        js_policy = js_popup->id( js_popup->selected() );
+    //int js_policy = 0;
+    //if ( js_popup->selected() )
+    //    js_policy = js_popup->id( js_popup->selected() );
 
-    m_pConfig->writeEntry( "WindowOpenPolicy", js_policy);
+    //m_pConfig->writeEntry( "WindowOpenPolicy", js_policy);
 
 //    m_pConfig->writeEntry( "EnableJSDebugOutput", enableDebugOutputCB->isChecked() );
 
--- kdebase/kcontrol/konqhtml/jsopts.h.popup	2003-05-20 04:54:23.000000000 +0200
+++ kdebase/kcontrol/konqhtml/jsopts.h	2003-06-04 18:32:05.000000000 +0200
@@ -52,7 +52,7 @@
   QString m_groupname;
   QCheckBox *enableJavaScriptGloballyCB;
   QCheckBox *enableJavaScriptDebugCB;
-  QButtonGroup *js_popup;
+  //QButtonGroup *js_popup;
     QPushButton* changeDomainPB;
     QPushButton* deleteDomainPB;
   KListView* domainSpecificLV;
--- kdebase/kcontrol/konqhtml/main.cpp.popup	2002-08-23 18:07:51.000000000 +0200
+++ kdebase/kcontrol/konqhtml/main.cpp	2003-06-04 20:08:29.000000000 +0200
@@ -36,6 +36,7 @@
 
 #include "jsopts.h"
 #include "javaopts.h"
+#include "popupopts.h"
 #include "pluginopts.h"
 #include "appearance.h"
 #include "htmlopts.h"
@@ -88,6 +89,10 @@
   javascript = new KJavaScriptOptions( config, "Java/JavaScript Settings", this, name );
   tab->addTab( javascript, i18n( "Java&Script" ) );
   connect( javascript, SIGNAL( changed( bool ) ), SIGNAL( changed( bool ) ) );
+
+  popup = new KJSPopupOptions (config, "Java/JavaScript Settings", this, name );
+  tab->addTab( popup, i18n( "&Popup policy" ) );
+  connect( popup, SIGNAL ( changed( bool ) ), SIGNAL ( changed ( bool ) ) );
 }
 
 KJSParts::~KJSParts()
@@ -99,6 +104,7 @@
 {
   javascript->load();
   java->load();
+  popup->load();
 }
 
 
@@ -106,6 +112,7 @@
 {
   javascript->save();
   java->save();
+  popup->save();
 
   // Send signal to konqueror
   // Warning. In case something is added/changed here, keep kfmclient in sync
@@ -120,6 +127,7 @@
 {
   javascript->defaults();
   java->defaults();
+  popup->defaults();
 }
 
 QString KJSParts::quickHelp() const
--- kdebase/kcontrol/konqhtml/main.h.popup	2002-07-04 07:29:26.000000000 +0200
+++ kdebase/kcontrol/konqhtml/main.h	2003-06-04 20:05:43.000000000 +0200
@@ -30,6 +30,7 @@
 
 class KJavaOptions;
 class KJavaScriptOptions;
+class KJSPopupOptions;
 
 class QTabWidget;
 
@@ -54,6 +55,7 @@
 
   KJavaScriptOptions *javascript;
   KJavaOptions       *java;
+  KJSPopupOptions   *popup;
 
   KConfig *mConfig;
 };
--- kdebase/kcontrol/konqhtml/popupopts.cpp.popup	2003-06-04 18:32:05.000000000 +0200
+++ kdebase/kcontrol/konqhtml/popupopts.cpp	2003-06-04 21:11:14.000000000 +0200
@@ -0,0 +1,310 @@
+// (c) Martin R. Jones 1996
+// (c) Bernd Wuebben 1998
+// KControl port & modifications
+// (c) Torben Weis 1998
+// End of the KControl port, added 'kfmclient configure' call.
+// (c) David Faure 1998
+// New configuration scheme for JavaScript
+// (C) Kalle Dalheimer 2000
+// Major cleanup & Java/JS settings splitted
+// (c) Daniel Molkentin 2000
+
+#include <kfiledialog.h>
+#include <qbuttongroup.h>
+#include <qcheckbox.h>
+#include <qcolor.h>
+#include <qcombobox.h>
+#include <qlayout.h>
+#include <qpushbutton.h>
+#include <qradiobutton.h>
+#include <qmessagebox.h>
+#include <qwhatsthis.h>
+#include <qvgroupbox.h>
+#include <qhbox.h>
+#include <qvbox.h>
+#include <kglobal.h>
+#include <kglobalsettings.h>
+#include <kconfig.h>
+#include <klistview.h>
+#include <kmessagebox.h>
+#include <qlabel.h>
+#include <kcharsets.h>
+#include <qspinbox.h>
+#include <kdebug.h>
+#include <kurlrequester.h>
+#include <X11/Xlib.h>
+#include <klineedit.h>
+
+#include "htmlopts.h"
+#include "policydlg.h"
+
+#include <konq_defaults.h> // include default values directly from konqueror
+#include <klocale.h>
+#include <khtml_settings.h>
+#include <khtmldefaults.h>
+
+#include "popupopts.h"
+
+#include "popupopts.moc"
+
+KJSPopupOptions::KJSPopupOptions( KConfig* config, QString group, QWidget *parent,
+										const char *name ) :
+  KCModule( parent, name ), m_pConfig( config ), m_groupname( group )
+{
+  QVBoxLayout* toplevel = new QVBoxLayout( this, 10, 5 );
+  
+  defaultPolicy = new QButtonGroup(4, Horizontal, i18n( "Web Popups Default Policy" ), this);
+  defaultPolicy->setExclusive(TRUE);
+
+  QRadioButton* popupMode = new QRadioButton(i18n( "Allow" ), defaultPolicy);
+  QWhatsThis::add( popupMode,i18n("Accept all popup window requests.") );
+
+  popupMode = new QRadioButton(i18n( "Ask" ), defaultPolicy);
+  QWhatsThis::add( popupMode,i18n("Prompt every time a popup window is requested.") );
+
+  popupMode = new QRadioButton(i18n( "Deny" ), defaultPolicy);
+  QWhatsThis::add( popupMode,i18n("Reject all popup window requests.") );
+
+  popupMode = new QRadioButton(i18n( "Smart" ), defaultPolicy);
+  QWhatsThis::add( popupMode, i18n("Accept popup window requests only when "
+                                   "links are activated through an explicit "
+                                   "mouse click or keyboard operation.") );
+  toplevel->addWidget(defaultPolicy);
+  QWhatsThis::add( defaultPolicy, i18n("If you disable this, Konqueror will stop "
+                                  "interpreting the <i>window.open()</i> "
+                                  "JavaScript command. This is useful if you "
+                                  "regulary visit sites that make extensive use "
+                                  "of this command to pop up ad banners.<br>"
+                                  "<br><b>Note:</b> Disabling this option might "
+                                  "also break certain sites that require <i>"
+                                  "window.open()</i> for proper operation. Use "
+                                  "this feature carefully!") );
+  connect( defaultPolicy, SIGNAL( clicked( int ) ), this, SLOT( slotChanged() ) );
+
+  // the domain-specific listview (copied and modified from Cookies configuration)
+  QGroupBox* domainSpecificGB = new QGroupBox( i18n( "Do&main-Specific" ), this );
+  domainSpecificGB->setColumnLayout(0, Qt::Vertical );
+  domainSpecificGB->layout()->setSpacing( 0 );
+  domainSpecificGB->layout()->setMargin( 0 );
+  QGridLayout* domainSpecificGBLayout = new QGridLayout( domainSpecificGB->layout() );
+  domainSpecificGBLayout->setAlignment( Qt::AlignTop );
+  domainSpecificGBLayout->setSpacing( 6 );
+  domainSpecificGBLayout->setMargin( 11 );
+
+  domainSpecificLV = new KListView( domainSpecificGB );
+  domainSpecificLV->addColumn(i18n("Host/Domain"));
+  domainSpecificLV->addColumn(i18n("Policy"), 100);
+  QString wtstr = i18n("This box contains the domains and hosts you have set "
+                       "a specific Popup policy for. This policy will be used "
+                       "instead of the default policy for enabling or disabling Popups on pages sent by these "
+                       "domains or hosts. <p>Select a policy and use the controls on "
+                       "the right to modify it.");
+  QWhatsThis::add( domainSpecificLV, wtstr );
+  QWhatsThis::add( domainSpecificGB, wtstr );
+  connect(domainSpecificLV,SIGNAL(doubleClicked ( QListViewItem * )), this, SLOT( changePressed() ) );
+  connect(domainSpecificLV,SIGNAL(returnPressed ( QListViewItem * )), this, SLOT( changePressed() ) );
+  connect(domainSpecificLV, SIGNAL( executed( QListViewItem *)), SLOT( updateButton()));
+
+  domainSpecificGBLayout->addMultiCellWidget( domainSpecificLV, 0, 5, 0, 0 );
+  QPushButton* addDomainPB = new QPushButton( i18n("&New..."), domainSpecificGB );
+  domainSpecificGBLayout->addWidget( addDomainPB, 0, 1 );
+  QWhatsThis::add( addDomainPB, i18n("Click on this button to manually add a host or domain "
+                                     "specific policy.") );
+  connect( addDomainPB, SIGNAL(clicked()), SLOT( addPressed() ) );
+
+  changeDomainPB = new QPushButton( i18n("Chan&ge..."), domainSpecificGB );
+  domainSpecificGBLayout->addWidget( changeDomainPB, 1, 1 );
+  QWhatsThis::add( changeDomainPB, i18n("Click on this button to change the policy for the "
+                                        "host or domain selected in the list box.") );
+  connect( changeDomainPB, SIGNAL( clicked() ), this, SLOT( changePressed() ) );
+
+  deleteDomainPB = new QPushButton( i18n("De&lete"), domainSpecificGB );
+  domainSpecificGBLayout->addWidget( deleteDomainPB, 2, 1 );
+  QWhatsThis::add( deleteDomainPB, i18n("Click on this button to change the policy for the "
+                                        "host or domain selected in the list box.") );
+  connect( deleteDomainPB, SIGNAL( clicked() ), this, SLOT( deletePressed() ) );
+
+  // XXX IMPORT/EXPORT do we need this?
+  //QPushButton* importDomainPB = new QPushButton( i18n("&Import..."), domainSpecificGB );
+  //domainSpecificGBLayout->addWidget( importDomainPB, 3, 1 );
+  //QWhatsThis::add( importDomainPB, i18n("Click this button to choose the file that contains "
+  //                                      "the Popup policies. These policies will be merged "
+  //                                      "with the existing ones. Duplicate entries are ignored.") );
+  //connect( importDomainPB, SIGNAL( clicked() ), this, SLOT( importPressed() ) );
+  //importDomainPB->setEnabled( false );
+  //importDomainPB->hide();
+
+  //QPushButton* exportDomainPB = new QPushButton( i18n("&Export..."), domainSpecificGB );
+  //domainSpecificGBLayout->addWidget( exportDomainPB, 4, 1 );
+  //QWhatsThis::add( exportDomainPB, i18n("Click this button to save the JavaScript policy to a zipped "
+  //                                      "file. The file, named <b>javascript_policy.tgz</b>, will be "
+  //                                      "saved to a location of your choice." ) );
+
+  //connect( exportDomainPB, SIGNAL( clicked() ), this, SLOT( exportPressed() ) );
+  //exportDomainPB->setEnabled( false );
+  //exportDomainPB->hide();
+
+  QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
+  domainSpecificGBLayout->addItem( spacer, 5, 1 );
+  toplevel->addWidget( domainSpecificGB, 2 );
+
+  QWhatsThis::add( domainSpecificGB, i18n("Here you can set specific PopUp policies for any particular "
+                                          "host or domain. To add a new policy, simply click the <i>Add...</i> "
+                                          "button and supply the necessary information requested by the "
+                                          "dialog box. To change an existing policy, click on the <i>Change...</i> "
+                                          "button and choose the new policy from the policy dialog box. Clicking "
+                                          "on the <i>Delete</i> button will remove the selected policy causing the default "
+                                          "policy setting to be used for that domain.") ); /* The <i>Import</i> and <i>Export</i> "
+                                          "button allows you to easily share your policies with other people by allowing "
+                                          "you to save and retrive them from a zipped file.") ); */
+
+  // Finally do the loading
+  load();
+  updateButton();
+}
+
+void KJSPopupOptions::updateButton()
+{
+    QListViewItem *index = domainSpecificLV->currentItem();
+    bool enable = ( index != 0 );
+    changeDomainPB->setEnabled( enable );
+    deleteDomainPB->setEnabled( enable );
+}
+
+void KJSPopupOptions::load()
+{
+    // *** load ***
+    m_pConfig->setGroup(m_groupname);
+
+    //if( m_pConfig->hasKey( "PopUpDomainSettings" ) )
+        updateDomainList( m_pConfig->readListEntry( "PopUpDomainSettings" ) );
+    //else
+    //    updateDomainList(m_pConfig->readListEntry("JavaScriptDomainAdvice") );
+
+    // *** apply to GUI ***
+//    enableJavaScriptGloballyCB->setChecked( m_pConfig->readBoolEntry("EnableJavaScript",true));
+    defaultPolicy->setButton( m_pConfig->readUnsignedNumEntry("WindowOpenPolicy", 0) );
+}
+
+void KJSPopupOptions::defaults()
+{
+  //enableJavaScriptGloballyCB->setChecked( true );
+//  enableJavaScriptDebugCB->setChecked( false );
+  defaultPolicy->setButton(0);
+ // enableDebugOutputCB->setChecked( false );
+}
+
+void KJSPopupOptions::save()
+{
+    m_pConfig->setGroup(m_groupname);
+    
+    int pu_policy = 0;
+    if ( defaultPolicy->selected() )
+        pu_policy = defaultPolicy->id( defaultPolicy->selected() );
+    m_pConfig->writeEntry( "WindowOpenPolicy", pu_policy);
+
+    QStringList domainConfig;
+    QListViewItemIterator it( domainSpecificLV );
+    QListViewItem* current;
+    while( ( current = it.current() ) ) {
+        ++it;
+	// XXX This looks meaningless to me
+        //QCString javaPolicy = KHTMLSettings::adviceToStr( KHTMLSettings::KJavaScriptDunno );
+        QCString popUpPolicy = KHTMLSettings::adviceToStr(
+                         (KHTMLSettings::KPopUpAdvice) popUpDomainPolicy[current] );
+
+        //domainConfig.append(QString::fromLatin1("%1:%2:%3").arg(current->text(0)).arg(javaPolicy).arg(javaScriptPolicy));
+        domainConfig.append(QString::fromLatin1("%1:%2").arg(current->text(0)).arg(popUpPolicy));
+    }
+    m_pConfig->writeEntry("PopUpDomainSettings", domainConfig);
+
+    m_pConfig->sync();
+}
+
+void KJSPopupOptions::slotChanged()
+{
+  emit changed(true);
+}
+
+void KJSPopupOptions::addPressed()
+{
+    PolicyDialog pDlg( false, false, this );
+    //int def_javapolicy = KHTMLSettings::KJavaScriptDunno;
+    //int def_javascriptpolicy = KHTMLSettings::KJavaScriptReject;
+    // XXX is this needed?
+    //pDlg.setDefaultPolicy( def_javapolicy, def_javascriptpolicy );
+    pDlg.setCaption( i18n( "New PopUp Policy" ) );
+    if( pDlg.exec() ) {
+        QListViewItem* index = new QListViewItem( domainSpecificLV, pDlg.domain(),
+                                                  KHTMLSettings::adviceToStr( (KHTMLSettings::KPopUpAdvice)
+                                                                              pDlg.popUpPolicyAdvice() ) );
+        popUpDomainPolicy.insert( index, (KHTMLSettings::KPopUpAdvice)pDlg.popUpPolicyAdvice());
+        domainSpecificLV->setCurrentItem( index );
+        slotChanged();
+        updateButton();
+    }
+}
+
+void KJSPopupOptions::changePressed()
+{
+    QListViewItem *index = domainSpecificLV->currentItem();
+    if ( index == 0 )
+    {
+        KMessageBox::information( 0, i18n("You must first select a policy to be changed!" ) );
+        return;
+    }
+
+    int popUpAdvice = popUpDomainPolicy[index];
+
+    PolicyDialog pDlg( false, false, this );
+    pDlg.setDisableEdit( true, index->text(0) );
+    pDlg.setCaption( i18n( "Change PopUp Policy" ) );
+    // XXX
+    //pDlg.setDefaultPolicy( KHTMLSettings::KJavaScriptDunno, javaScriptAdvice );
+    if( pDlg.exec() )
+    {
+        popUpDomainPolicy[index] = pDlg.popUpPolicyAdvice();
+        index->setText(0, pDlg.domain() );
+        index->setText(1, i18n(KHTMLSettings::adviceToStr(
+                (KHTMLSettings::KPopUpAdvice)popUpDomainPolicy[index])));
+        slotChanged();
+    }
+}
+
+void KJSPopupOptions::deletePressed()
+{
+    QListViewItem *index = domainSpecificLV->currentItem();
+    if ( index == 0 )
+    {
+        KMessageBox::information( 0, i18n("You must first select a policy to delete!" ) );
+        return;
+    }
+    popUpDomainPolicy.remove(index);
+    delete index;
+    slotChanged();
+    updateButton();
+}
+
+/*
+void KJSPopupOptions::changeJavaScriptEnabled()
+{
+  bool enabled = enableJavaScriptGloballyCB->isChecked();
+  enableJavaScriptGloballyCB->setChecked( enabled );
+}
+*/
+
+void KJSPopupOptions::updateDomainList(const QStringList &domainConfig)
+{
+    domainSpecificLV->clear();
+    for (QStringList::ConstIterator it = domainConfig.begin();
+         it != domainConfig.end(); ++it) {
+      QString domain;
+      KHTMLSettings::KPopUpAdvice popUpAdvice;
+      KHTMLSettings::splitDomainAdvice(*it, domain, popUpAdvice);
+      QListViewItem *index =
+        new QListViewItem( domainSpecificLV, domain,
+                i18n(KHTMLSettings::adviceToStr(popUpAdvice)) );
+
+      popUpDomainPolicy[index] = popUpAdvice;
+    }
+}
--- kdebase/kcontrol/konqhtml/popupopts.h.popup	2003-06-04 18:32:05.000000000 +0200
+++ kdebase/kcontrol/konqhtml/popupopts.h	2003-06-04 18:32:05.000000000 +0200
@@ -0,0 +1,61 @@
+//-----------------------------------------------------------------------------
+//
+// PopUp Options
+//
+// (c) Sergio Visinoni <piffio at arklinux.org> 2003
+
+#ifndef __POPUPOPTS_H__
+#define __POPUPOPTS_H__
+
+#include <kcmodule.h>
+#include <qmap.h>
+
+class KColorButton;
+class KConfig;
+class KListView;
+class KURLRequester;
+class QCheckBox;
+class QComboBox;
+class QLineEdit;
+class QListViewItem;
+class QRadioButton;
+class QSpinBox;
+class QButtonGroup;
+class QPushButton;
+
+class KJSPopupOptions : public KCModule
+{
+  Q_OBJECT
+public:
+  KJSPopupOptions( KConfig* config, QString group, QWidget* parent = 0, const char* name = 0 );
+
+  virtual void load();
+  virtual void save();
+  virtual void defaults();
+
+private slots:
+  void slotChanged();
+  //void importPressed();
+  //void exportPressed();
+  void addPressed();
+  void changePressed();
+  void deletePressed();
+  void updateButton();
+private:
+  //void changeJavaScriptEnabled();
+  void updateDomainList(const QStringList &domainConfig);
+
+  KConfig *m_pConfig;
+  QString m_groupname;
+  //QCheckBox *enableJavaScriptGloballyCB;
+  //QCheckBox *enableJavaScriptDebugCB;
+  QButtonGroup *defaultPolicy;
+  QPushButton* changeDomainPB;
+  QPushButton* deleteDomainPB;
+  KListView* domainSpecificLV;
+  QMap<QListViewItem*, int> popUpDomainPolicy;
+};
+
+
+#endif		// __POPUPOPTS_H__
+
--- kdebase/kcontrol/konqhtml/policydlg.cpp.popup	2003-05-20 04:54:24.000000000 +0200
+++ kdebase/kcontrol/konqhtml/policydlg.cpp	2003-06-04 18:32:05.000000000 +0200
@@ -60,6 +60,18 @@
 
   QWhatsThis::add(cb_javascriptpolicy, i18n("Select a JavaScript policy for "
                                           "the above host or domain.") );
+  // PopUp
+  l_popuppolicy = new QLabel(i18n("PopUp policy:"), this);
+  grid->addWidget(l_popuppolicy, 2, 0);
+
+  cb_popuppolicy = new QComboBox(this);
+  policies << i18n( "Ask" );
+  cb_popuppolicy->insertStringList( policies );
+  l->setBuddy( cb_popuppolicy );
+  grid->addWidget(cb_popuppolicy, 2, 1);
+
+  QWhatsThis::add(cb_popuppolicy, i18n("Select a PopUp policy for "
+                                          "the above host or domain.") );
 
 
   KButtonBox *bbox = new KButtonBox(this);
@@ -86,6 +98,13 @@
     l_javascriptpolicy->hide();
   }
 
+  // Hack
+  if ( javascript || java )
+  {
+    cb_popuppolicy->hide();
+    l_popuppolicy->hide();
+  }
+  
   le_domain->setFocus();
   okButton->setEnabled( !le_domain->text().isEmpty());
 }
--- kdebase/kcontrol/konqhtml/policydlg.h.popup	2003-03-11 00:53:25.000000000 +0100
+++ kdebase/kcontrol/konqhtml/policydlg.h	2003-06-04 18:32:05.000000000 +0200
@@ -25,7 +25,12 @@
     * @return 1 for "Accept", 2 for "Reject"
     */
     int javaScriptPolicyAdvice() const { return cb_javascriptpolicy->currentItem() + 1; }
-
+    
+    /*
+    * @return 1 for "Accept", 2 for "Reject", 3 for "Ask"
+    */
+    int popUpPolicyAdvice() const { return cb_popuppolicy->currentItem() + 1; }
+    
     /*
     * @return the hostname for whom the policy is being set
     */
@@ -58,8 +63,10 @@
     QLineEdit *le_domain;
     QLabel *l_javapolicy;
     QLabel *l_javascriptpolicy;
+    QLabel *l_popuppolicy;
     QComboBox *cb_javapolicy;
     QComboBox *cb_javascriptpolicy;
+    QComboBox *cb_popuppolicy;
     QPushButton *okButton;
 };
 
--- kdebase/kcontrol/konqhtml/Makefile.am.popup	2003-06-04 21:13:13.000000000 +0200
+++ kdebase/kcontrol/konqhtml/Makefile.am	2003-06-04 20:19:56.000000000 +0200
@@ -6,7 +6,7 @@
 kde_module_LTLIBRARIES = kcm_konqhtml.la
 
 kcm_konqhtml_la_SOURCES = htmlopts.cpp jsopts.cpp \
-			     javaopts.cpp pluginopts.cpp appearance.cpp \
+			     javaopts.cpp pluginopts.cpp appearance.cpp popupopts.cpp \
 			     khttpoptdlg.cpp policydlg.cpp main.cpp nsconfigwidget.ui
 
 kcm_konqhtml_la_LDFLAGS  = $(all_libraries) -module -avoid-version -no-undefined


More information about the kde-core-devel mailing list