KIOSlave
kcookiewin.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of KDE 00003 00004 Copyright (C) 2000- Waldo Bastian <bastian@kde.org> 00005 Copyright (C) 2000- Dawit Alemayehu <adawit@kde.org> 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a copy 00008 of this software and associated documentation files (the "Software"), to deal 00009 in the Software without restriction, including without limitation the rights 00010 to use, copy, modify, merge, publish, distribute, and/or sell 00011 copies of the Software, and to permit persons to whom the Software is 00012 furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00023 */ 00024 //---------------------------------------------------------------------------- 00025 // 00026 // KDE File Manager -- HTTP Cookie Dialogs 00027 00028 // The purpose of the QT_NO_TOOLTIP and QT_NO_WHATSTHIS ifdefs is because 00029 // this file is also used in Konqueror/Embedded. One of the aims of 00030 // Konqueror/Embedded is to be a small as possible to fit on embedded 00031 // devices. For this it's also useful to strip out unneeded features of 00032 // Qt, like for example QToolTip or QWhatsThis. The availability (or the 00033 // lack thereof) can be determined using these preprocessor defines. 00034 // The same applies to the QT_NO_ACCEL ifdef below. I hope it doesn't make 00035 // too much trouble... (Simon) 00036 00037 #include "kcookiewin.h" 00038 #include "kcookiejar.h" 00039 00040 #include <QtGui/QLabel> 00041 #include <QtGui/QLayout> 00042 #include <QtGui/QGroupBox> 00043 #include <QtGui/QPushButton> 00044 #include <QtGui/QRadioButton> 00045 #include <QtGui/QShortcut> 00046 00047 #include <kwindowsystem.h> 00048 #include <klocale.h> 00049 #include <kglobal.h> 00050 #include <klineedit.h> 00051 #include <kiconloader.h> 00052 #include <kapplication.h> 00053 #include <kvbox.h> 00054 #include <kdatetime.h> 00055 00056 KCookieWin::KCookieWin( QWidget *parent, KHttpCookieList cookieList, 00057 int defaultButton, bool showDetails ) 00058 :KDialog( parent ) 00059 { 00060 setModal(true); 00061 setObjectName("cookiealert"); 00062 setButtons(Yes|No|Details); 00063 #ifndef Q_WS_QWS //FIXME(E): Implement for Qt Embedded 00064 setCaption( i18n("Cookie Alert") ); 00065 setWindowIcon( KIcon("preferences-web-browser-cookies") ); 00066 // all cookies in the list should have the same window at this time, so let's take the first 00067 if( cookieList.first().windowIds().count() > 0 ) 00068 { 00069 #ifdef Q_WS_WIN 00070 KWindowSystem::setMainWindow( this, reinterpret_cast<WId>( cookieList.first().windowIds().first() ) ); 00071 #else 00072 KWindowSystem::setMainWindow( this, cookieList.first().windowIds().first()); 00073 #endif 00074 } 00075 else 00076 { 00077 // No window associated... make sure the user notices our dialog. 00078 #ifdef Q_WS_X11 00079 KWindowSystem::setState( winId(), NET::KeepAbove ); 00080 #endif 00081 kapp->updateUserTimestamp(); 00082 } 00083 #endif 00084 KVBox* vBox1 = new KVBox( this ); 00085 vBox1->setSpacing( -1 ); 00086 setMainWidget(vBox1); 00087 // Cookie image and message to user 00088 KHBox* hBox = new KHBox( vBox1 ); 00089 QLabel* icon = new QLabel( hBox ); 00090 icon->setPixmap(KIcon("dialog-warning").pixmap(IconSize(KIconLoader::Desktop))); 00091 icon->setAlignment( Qt::AlignCenter ); 00092 icon->setFixedSize( 2*icon->sizeHint() ); 00093 00094 int count = cookieList.count(); 00095 00096 KVBox* vBox = new KVBox( hBox ); 00097 QString txt = i18np("You received a cookie from", 00098 "You received %1 cookies from", count); 00099 QLabel* lbl = new QLabel( txt, vBox ); 00100 lbl->setAlignment( Qt::AlignCenter ); 00101 const KHttpCookie& cookie = cookieList.first(); 00102 00103 QString host (cookie.host()); 00104 int pos = host.indexOf(':'); 00105 if ( pos > 0 ) 00106 { 00107 QString portNum = host.left(pos); 00108 host.remove(0, pos+1); 00109 host += ':'; 00110 host += portNum; 00111 } 00112 00113 txt = QString("<b>%1</b>").arg( QUrl::fromAce(host.toLatin1()) ); 00114 if (cookie.isCrossDomain()) 00115 txt += i18n(" <b>[Cross Domain]</b>"); 00116 lbl = new QLabel( txt, vBox ); 00117 lbl->setAlignment( Qt::AlignCenter ); 00118 lbl = new QLabel( i18n("Do you want to accept or reject?"), vBox ); 00119 lbl->setAlignment( Qt::AlignCenter ); 00120 00121 // Cookie Details dialog... 00122 m_detailView = new KCookieDetail( cookieList, count, vBox1 ); 00123 setDetailsWidget(m_detailView); 00124 00125 // Cookie policy choice... 00126 QGroupBox *m_btnGrp = new QGroupBox(i18n("Apply Choice To"),vBox1); 00127 QVBoxLayout *vbox = new QVBoxLayout; 00128 txt = (count == 1)? i18n("&Only this cookie") : i18n("&Only these cookies"); 00129 m_onlyCookies = new QRadioButton( txt, m_btnGrp ); 00130 vbox->addWidget(m_onlyCookies); 00131 #ifndef QT_NO_WHATSTHIS 00132 m_onlyCookies->setWhatsThis(i18n("Select this option to accept/reject only this cookie. " 00133 "You will be prompted if another cookie is received. " 00134 "<em>(see WebBrowsing/Cookies in the System Settings)</em>." ) ); 00135 #endif 00136 m_allCookiesDomain = new QRadioButton( i18n("All cookies from this do&main"), m_btnGrp ); 00137 vbox->addWidget(m_allCookiesDomain); 00138 #ifndef QT_NO_WHATSTHIS 00139 m_allCookiesDomain->setWhatsThis(i18n("Select this option to accept/reject all cookies from " 00140 "this site. Choosing this option will add a new policy for " 00141 "the site this cookie originated from. This policy will be " 00142 "permanent until you manually change it from the System Settings " 00143 "<em>(see WebBrowsing/Cookies in the System Settings)</em>.") ); 00144 #endif 00145 m_allCookies = new QRadioButton( i18n("All &cookies"), m_btnGrp); 00146 vbox->addWidget(m_allCookies); 00147 #ifndef QT_NO_WHATSTHIS 00148 m_allCookies->setWhatsThis(i18n("Select this option to accept/reject all cookies from " 00149 "anywhere. Choosing this option will change the global " 00150 "cookie policy set in the System Settings for all cookies " 00151 "<em>(see WebBrowsing/Cookies in the System Settings)</em>.") ); 00152 #endif 00153 m_btnGrp->setLayout(vbox); 00154 if (defaultButton == KCookieJar::ApplyToShownCookiesOnly ) 00155 m_onlyCookies->setChecked(true); 00156 else if (defaultButton == KCookieJar::ApplyToCookiesFromDomain) 00157 m_allCookiesDomain->setChecked(true); 00158 else if (defaultButton == KCookieJar::ApplyToAllCookies) 00159 m_allCookies->setChecked(true); 00160 else 00161 m_onlyCookies->setChecked(true); 00162 setButtonText(KDialog::Yes, i18n("&Accept")); 00163 setButtonText(KDialog::No, i18n("&Reject")); 00164 //QShortcut( Qt::Key_Escape, btn, SLOT(animateClick()) ); 00165 setButtonToolTip(Details, i18n("See or modify the cookie information") ); 00166 setDefaultButton(Yes); 00167 00168 setDetailsWidgetVisible(showDetails); 00169 } 00170 00171 KCookieWin::~KCookieWin() 00172 { 00173 } 00174 00175 KCookieAdvice KCookieWin::advice( KCookieJar *cookiejar, const KHttpCookie& cookie ) 00176 { 00177 int result = exec(); 00178 00179 cookiejar->setShowCookieDetails ( isDetailsWidgetVisible() ); 00180 00181 KCookieAdvice advice = (result==KDialog::Yes) ? KCookieAccept : KCookieReject; 00182 00183 KCookieJar::KCookieDefaultPolicy preferredPolicy = KCookieJar::ApplyToShownCookiesOnly; 00184 if (m_allCookiesDomain->isChecked()) { 00185 preferredPolicy = KCookieJar::ApplyToCookiesFromDomain; 00186 cookiejar->setDomainAdvice( cookie, advice ); 00187 } else if (m_allCookies->isChecked()) { 00188 preferredPolicy = KCookieJar::ApplyToAllCookies; 00189 cookiejar->setGlobalAdvice( advice ); 00190 } 00191 cookiejar->setPreferredDefaultPolicy( preferredPolicy ); 00192 00193 return advice; 00194 } 00195 00196 KCookieDetail::KCookieDetail( KHttpCookieList cookieList, int cookieCount, 00197 QWidget* parent ) 00198 :QGroupBox( parent ) 00199 { 00200 setTitle( i18n("Cookie Details") ); 00201 QGridLayout* grid = new QGridLayout( this ); 00202 grid->addItem( new QSpacerItem(0, fontMetrics().lineSpacing()), 0, 0 ); 00203 grid->setColumnStretch( 1, 3 ); 00204 00205 QLabel* label = new QLabel( i18n("Name:"), this ); 00206 grid->addWidget( label, 1, 0 ); 00207 m_name = new KLineEdit( this ); 00208 m_name->setReadOnly( true ); 00209 m_name->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00210 grid->addWidget( m_name, 1 ,1 ); 00211 00212 //Add the value 00213 label = new QLabel( i18n("Value:"), this ); 00214 grid->addWidget( label, 2, 0 ); 00215 m_value = new KLineEdit( this ); 00216 m_value->setReadOnly( true ); 00217 m_value->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00218 grid->addWidget( m_value, 2, 1); 00219 00220 label = new QLabel( i18n("Expires:"), this ); 00221 grid->addWidget( label, 3, 0 ); 00222 m_expires = new KLineEdit( this ); 00223 m_expires->setReadOnly( true ); 00224 m_expires->setMaximumWidth(fontMetrics().maxWidth() * 25 ); 00225 grid->addWidget( m_expires, 3, 1); 00226 00227 label = new QLabel( i18n("Path:"), this ); 00228 grid->addWidget( label, 4, 0 ); 00229 m_path = new KLineEdit( this ); 00230 m_path->setReadOnly( true ); 00231 m_path->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00232 grid->addWidget( m_path, 4, 1); 00233 00234 label = new QLabel( i18n("Domain:"), this ); 00235 grid->addWidget( label, 5, 0 ); 00236 m_domain = new KLineEdit( this ); 00237 m_domain->setReadOnly( true ); 00238 m_domain->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00239 grid->addWidget( m_domain, 5, 1); 00240 00241 label = new QLabel( i18n("Exposure:"), this ); 00242 grid->addWidget( label, 6, 0 ); 00243 m_secure = new KLineEdit( this ); 00244 m_secure->setReadOnly( true ); 00245 m_secure->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00246 grid->addWidget( m_secure, 6, 1 ); 00247 00248 if ( cookieCount > 1 ) 00249 { 00250 QPushButton* btnNext = new QPushButton( i18nc("Next cookie","&Next >>"), this ); 00251 btnNext->setFixedSize( btnNext->sizeHint() ); 00252 grid->addWidget( btnNext, 8, 0, 1, 2 ); 00253 connect( btnNext, SIGNAL(clicked()), SLOT(slotNextCookie()) ); 00254 #ifndef QT_NO_TOOLTIP 00255 btnNext->setToolTip(i18n("Show details of the next cookie") ); 00256 #endif 00257 } 00258 m_cookieList = cookieList; 00259 m_cookieNumber = 0; 00260 slotNextCookie(); 00261 } 00262 00263 KCookieDetail::~KCookieDetail() 00264 { 00265 } 00266 00267 void KCookieDetail::slotNextCookie() 00268 { 00269 if (m_cookieNumber == m_cookieList.count() - 1) 00270 m_cookieNumber = 0; 00271 else 00272 ++m_cookieNumber; 00273 displayCookieDetails(); 00274 } 00275 00276 void KCookieDetail::displayCookieDetails() 00277 { 00278 const KHttpCookie& cookie = m_cookieList.at(m_cookieNumber); 00279 m_name->setText(cookie.name()); 00280 m_value->setText((cookie.value())); 00281 if (cookie.domain().isEmpty()) 00282 m_domain->setText(i18n("Not specified")); 00283 else 00284 m_domain->setText(cookie.domain()); 00285 m_path->setText(cookie.path()); 00286 KDateTime cookiedate; 00287 cookiedate.setTime_t(cookie.expireDate()); 00288 if (cookie.expireDate()) 00289 m_expires->setText(KGlobal::locale()->formatDateTime(cookiedate)); 00290 else 00291 m_expires->setText(i18n("End of Session")); 00292 QString sec; 00293 if (cookie.isSecure()) 00294 { 00295 if (cookie.isHttpOnly()) 00296 sec = i18n("Secure servers only"); 00297 else 00298 sec = i18n("Secure servers, page scripts"); 00299 } 00300 else 00301 { 00302 if (cookie.isHttpOnly()) 00303 sec = i18n("Servers"); 00304 else 00305 sec = i18n("Servers, page scripts"); 00306 } 00307 m_secure->setText(sec); 00308 } 00309 00310 #include "kcookiewin.moc"
KDE 4.6 API Reference