kcookiewin.cpp
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 // $Id$ 00028 00029 // The purpose of the QT_NO_TOOLTIP and QT_NO_WHATSTHIS ifdefs is because 00030 // this file is also used in Konqueror/Embedded. One of the aims of 00031 // Konqueror/Embedded is to be a small as possible to fit on embedded 00032 // devices. For this it's also useful to strip out unneeded features of 00033 // Qt, like for example TQToolTip or TQWhatsThis. The availability (or the 00034 // lack thereof) can be determined using these preprocessor defines. 00035 // The same applies to the QT_NO_ACCEL ifdef below. I hope it doesn't make 00036 // too much trouble... (Simon) 00037 00038 #include <tqhbox.h> 00039 #include <tqvbox.h> 00040 #include <tqaccel.h> 00041 #include <tqlabel.h> 00042 #include <tqwidget.h> 00043 #include <tqlayout.h> 00044 #include <tqgroupbox.h> 00045 #include <tqdatetime.h> 00046 #include <tqmessagebox.h> 00047 #include <tqpushbutton.h> 00048 #include <tqradiobutton.h> 00049 #include <tqvbuttongroup.h> 00050 00051 #ifndef QT_NO_TOOLTIP 00052 #include <tqtooltip.h> 00053 #endif 00054 00055 #ifndef QT_NO_WHATSTHIS 00056 #include <tqwhatsthis.h> 00057 #endif 00058 00059 #include <kidna.h> 00060 #include <kwin.h> 00061 #include <klocale.h> 00062 #include <kglobal.h> 00063 #include <kurllabel.h> 00064 #include <klineedit.h> 00065 #include <kiconloader.h> 00066 #include <kapplication.h> 00067 00068 #ifdef Q_WS_X11 00069 #include <X11/Xlib.h> 00070 #endif 00071 00072 #include "kcookiejar.h" 00073 #include "kcookiewin.h" 00074 00075 KCookieWin::KCookieWin( TQWidget *parent, KHttpCookieList cookieList, 00076 int defaultButton, bool showDetails ) 00077 :KDialog( parent, "cookiealert", true ) 00078 { 00079 #ifndef Q_WS_QWS //FIXME(E): Implement for Qt Embedded 00080 setCaption( i18n("Cookie Alert") ); 00081 setIcon( SmallIcon("cookie") ); 00082 // all cookies in the list should have the same window at this time, so let's take the first 00083 # ifdef Q_WS_X11 00084 if( cookieList.first()->windowIds().count() > 0 ) 00085 { 00086 XSetTransientForHint( qt_xdisplay(), winId(), cookieList.first()->windowIds().first()); 00087 } 00088 else 00089 { 00090 // No window associated... make sure the user notices our dialog. 00091 KWin::setState( winId(), NET::KeepAbove ); 00092 kapp->updateUserTimestamp(); 00093 } 00094 # endif 00095 #endif 00096 // Main widget's layout manager... 00097 TQVBoxLayout* vlayout = new TQVBoxLayout( this, KDialog::marginHint(), KDialog::spacingHint() ); 00098 vlayout->setResizeMode( TQLayout::Fixed ); 00099 00100 // Cookie image and message to user 00101 TQHBox* hBox = new TQHBox( this ); 00102 hBox->setSpacing( KDialog::spacingHint() ); 00103 TQLabel* icon = new TQLabel( hBox ); 00104 icon->setPixmap( TQMessageBox::standardIcon(TQMessageBox::Warning) ); 00105 icon->setAlignment( Qt::AlignCenter ); 00106 icon->setFixedSize( 2*icon->sizeHint() ); 00107 00108 int count = cookieList.count(); 00109 00110 TQVBox* vBox = new TQVBox( hBox ); 00111 TQString txt = i18n("You received a cookie from", 00112 "You received %n cookies from", count); 00113 TQLabel* lbl = new TQLabel( txt, vBox ); 00114 lbl->setAlignment( Qt::AlignCenter ); 00115 KHttpCookiePtr cookie = cookieList.first(); 00116 00117 TQString host (cookie->host()); 00118 int pos = host.find(':'); 00119 if ( pos > 0 ) 00120 { 00121 TQString portNum = host.left(pos); 00122 host.remove(0, pos+1); 00123 host += ':'; 00124 host += portNum; 00125 } 00126 00127 txt = TQString("<b>%1</b>").arg( KIDNA::toUnicode(host) ); 00128 if (cookie->isCrossDomain()) 00129 txt += i18n(" <b>[Cross Domain!]</b>"); 00130 lbl = new TQLabel( txt, vBox ); 00131 lbl->setAlignment( Qt::AlignCenter ); 00132 lbl = new TQLabel( i18n("Do you want to accept or reject?"), vBox ); 00133 lbl->setAlignment( Qt::AlignCenter ); 00134 vlayout->addWidget( hBox, 0, Qt::AlignLeft ); 00135 00136 // Cookie Details dialog... 00137 m_detailView = new KCookieDetail( cookieList, count, this ); 00138 vlayout->addWidget( m_detailView ); 00139 m_showDetails = showDetails; 00140 m_showDetails ? m_detailView->show():m_detailView->hide(); 00141 00142 // Cookie policy choice... 00143 m_btnGrp = new TQVButtonGroup( i18n("Apply Choice To"), this ); 00144 m_btnGrp->setRadioButtonExclusive( true ); 00145 00146 txt = (count == 1)? i18n("&Only this cookie") : i18n("&Only these cookies"); 00147 TQRadioButton* rb = new TQRadioButton( txt, m_btnGrp ); 00148 #ifndef QT_NO_WHATSTHIS 00149 TQWhatsThis::add( rb, i18n("Select this option to accept/reject only this cookie. " 00150 "You will be prompted if another cookie is received. " 00151 "<em>(see WebBrowsing/Cookies in the Control Center)</em>." ) ); 00152 #endif 00153 m_btnGrp->insert( rb ); 00154 rb = new TQRadioButton( i18n("All cookies from this do&main"), m_btnGrp ); 00155 #ifndef QT_NO_WHATSTHIS 00156 TQWhatsThis::add( rb, i18n("Select this option to accept/reject all cookies from " 00157 "this site. Choosing this option will add a new policy for " 00158 "the site this cookie originated from. This policy will be " 00159 "permanent until you manually change it from the Control Center " 00160 "<em>(see WebBrowsing/Cookies in the Control Center)</em>.") ); 00161 #endif 00162 m_btnGrp->insert( rb ); 00163 rb = new TQRadioButton( i18n("All &cookies"), m_btnGrp ); 00164 #ifndef QT_NO_WHATSTHIS 00165 TQWhatsThis::add( rb, i18n("Select this option to accept/reject all cookies from " 00166 "anywhere. Choosing this option will change the global " 00167 "cookie policy set in the Control Center for all cookies " 00168 "<em>(see WebBrowsing/Cookies in the Control Center)</em>.") ); 00169 #endif 00170 m_btnGrp->insert( rb ); 00171 vlayout->addWidget( m_btnGrp ); 00172 00173 if ( defaultButton > -1 && defaultButton < 3 ) 00174 m_btnGrp->setButton( defaultButton ); 00175 else 00176 m_btnGrp->setButton( 1 ); 00177 00178 // Accept/Reject buttons 00179 TQWidget* bbox = new TQWidget( this ); 00180 TQBoxLayout* bbLay = new TQHBoxLayout( bbox ); 00181 bbLay->setSpacing( KDialog::spacingHint() ); 00182 TQPushButton* btn = new TQPushButton( i18n("&Accept"), bbox ); 00183 btn->setDefault( true ); 00184 btn->setFocus(); 00185 connect( btn, TQT_SIGNAL(clicked()), TQT_SLOT(accept()) ); 00186 bbLay->addWidget( btn ); 00187 btn = new TQPushButton( i18n("&Reject"), bbox ); 00188 connect( btn, TQT_SIGNAL(clicked()), TQT_SLOT(reject()) ); 00189 bbLay->addWidget( btn ); 00190 bbLay->addStretch( 1 ); 00191 #ifndef QT_NO_ACCEL 00192 TQAccel* a = new TQAccel( this ); 00193 a->connectItem( a->insertItem(Qt::Key_Escape), btn, TQT_SLOT(animateClick()) ); 00194 #endif 00195 00196 m_button = new TQPushButton( bbox ); 00197 m_button->setText( m_showDetails ? i18n("&Details <<"):i18n("&Details >>") ); 00198 connect( m_button, TQT_SIGNAL(clicked()), TQT_SLOT(slotCookieDetails()) ); 00199 bbLay->addWidget( m_button ); 00200 #ifndef QT_NO_WHATSTHIS 00201 TQWhatsThis::add( m_button, i18n("See or modify the cookie information") ); 00202 #endif 00203 00204 00205 vlayout->addWidget( bbox ); 00206 setFixedSize( sizeHint() ); 00207 } 00208 00209 KCookieWin::~KCookieWin() 00210 { 00211 } 00212 00213 void KCookieWin::slotCookieDetails() 00214 { 00215 if ( m_detailView->isVisible() ) 00216 { 00217 m_detailView->setMaximumSize( 0, 0 ); 00218 m_detailView->adjustSize(); 00219 m_detailView->hide(); 00220 m_button->setText( i18n( "&Details >>" ) ); 00221 m_showDetails = false; 00222 } 00223 else 00224 { 00225 m_detailView->setMaximumSize( 1000, 1000 ); 00226 m_detailView->adjustSize(); 00227 m_detailView->show(); 00228 m_button->setText( i18n( "&Details <<" ) ); 00229 m_showDetails = true; 00230 } 00231 } 00232 00233 KCookieAdvice KCookieWin::advice( KCookieJar *cookiejar, KHttpCookie* cookie ) 00234 { 00235 int result = exec(); 00236 00237 cookiejar->setShowCookieDetails ( m_showDetails ); 00238 00239 KCookieAdvice advice = (result==TQDialog::Accepted) ? KCookieAccept:KCookieReject; 00240 00241 int preferredPolicy = m_btnGrp->id( m_btnGrp->selected() ); 00242 cookiejar->setPreferredDefaultPolicy( preferredPolicy ); 00243 00244 switch ( preferredPolicy ) 00245 { 00246 case 2: 00247 cookiejar->setGlobalAdvice( advice ); 00248 break; 00249 case 1: 00250 cookiejar->setDomainAdvice( cookie, advice ); 00251 break; 00252 case 0: 00253 default: 00254 break; 00255 } 00256 return advice; 00257 } 00258 00259 KCookieDetail::KCookieDetail( KHttpCookieList cookieList, int cookieCount, 00260 TQWidget* parent, const char* name ) 00261 :TQGroupBox( parent, name ) 00262 { 00263 setTitle( i18n("Cookie Details") ); 00264 TQGridLayout* grid = new TQGridLayout( this, 9, 2, 00265 KDialog::spacingHint(), 00266 KDialog::marginHint() ); 00267 grid->addRowSpacing( 0, fontMetrics().lineSpacing() ); 00268 grid->setColStretch( 1, 3 ); 00269 00270 TQLabel* label = new TQLabel( i18n("Name:"), this ); 00271 grid->addWidget( label, 1, 0 ); 00272 m_name = new KLineEdit( this ); 00273 m_name->setReadOnly( true ); 00274 m_name->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00275 grid->addWidget( m_name, 1 ,1 ); 00276 00277 //Add the value 00278 label = new TQLabel( i18n("Value:"), this ); 00279 grid->addWidget( label, 2, 0 ); 00280 m_value = new KLineEdit( this ); 00281 m_value->setReadOnly( true ); 00282 m_value->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00283 grid->addWidget( m_value, 2, 1); 00284 00285 label = new TQLabel( i18n("Expires:"), this ); 00286 grid->addWidget( label, 3, 0 ); 00287 m_expires = new KLineEdit( this ); 00288 m_expires->setReadOnly( true ); 00289 m_expires->setMaximumWidth(fontMetrics().maxWidth() * 25 ); 00290 grid->addWidget( m_expires, 3, 1); 00291 00292 label = new TQLabel( i18n("Path:"), this ); 00293 grid->addWidget( label, 4, 0 ); 00294 m_path = new KLineEdit( this ); 00295 m_path->setReadOnly( true ); 00296 m_path->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00297 grid->addWidget( m_path, 4, 1); 00298 00299 label = new TQLabel( i18n("Domain:"), this ); 00300 grid->addWidget( label, 5, 0 ); 00301 m_domain = new KLineEdit( this ); 00302 m_domain->setReadOnly( true ); 00303 m_domain->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00304 grid->addWidget( m_domain, 5, 1); 00305 00306 label = new TQLabel( i18n("Exposure:"), this ); 00307 grid->addWidget( label, 6, 0 ); 00308 m_secure = new KLineEdit( this ); 00309 m_secure->setReadOnly( true ); 00310 m_secure->setMaximumWidth( fontMetrics().maxWidth() * 25 ); 00311 grid->addWidget( m_secure, 6, 1 ); 00312 00313 if ( cookieCount > 1 ) 00314 { 00315 TQPushButton* btnNext = new TQPushButton( i18n("Next cookie","&Next >>"), this ); 00316 btnNext->setFixedSize( btnNext->sizeHint() ); 00317 grid->addMultiCellWidget( btnNext, 8, 8, 0, 1 ); 00318 connect( btnNext, TQT_SIGNAL(clicked()), TQT_SLOT(slotNextCookie()) ); 00319 #ifndef QT_NO_TOOLTIP 00320 TQToolTip::add( btnNext, i18n("Show details of the next cookie") ); 00321 #endif 00322 } 00323 m_cookieList = cookieList; 00324 m_cookie = 0; 00325 slotNextCookie(); 00326 } 00327 00328 KCookieDetail::~KCookieDetail() 00329 { 00330 } 00331 00332 void KCookieDetail::slotNextCookie() 00333 { 00334 KHttpCookiePtr cookie = m_cookieList.first(); 00335 if (m_cookie) while(cookie) 00336 { 00337 if (cookie == m_cookie) 00338 { 00339 cookie = m_cookieList.next(); 00340 break; 00341 } 00342 cookie = m_cookieList.next(); 00343 } 00344 m_cookie = cookie; 00345 if (!m_cookie) 00346 m_cookie = m_cookieList.first(); 00347 00348 if ( m_cookie ) 00349 { 00350 m_name->setText( m_cookie->name() ); 00351 m_value->setText( ( m_cookie->value() ) ); 00352 if ( m_cookie->domain().isEmpty() ) 00353 m_domain->setText( i18n("Not specified") ); 00354 else 00355 m_domain->setText( m_cookie->domain() ); 00356 m_path->setText( m_cookie->path() ); 00357 TQDateTime cookiedate; 00358 cookiedate.setTime_t( m_cookie->expireDate() ); 00359 if ( m_cookie->expireDate() ) 00360 m_expires->setText( KGlobal::locale()->formatDateTime(cookiedate) ); 00361 else 00362 m_expires->setText( i18n("End of Session") ); 00363 TQString sec; 00364 if (m_cookie->isSecure()) 00365 { 00366 if (m_cookie->isHttpOnly()) 00367 sec = i18n("Secure servers only"); 00368 else 00369 sec = i18n("Secure servers, page scripts"); 00370 } 00371 else 00372 { 00373 if (m_cookie->isHttpOnly()) 00374 sec = i18n("Servers"); 00375 else 00376 sec = i18n("Servers, page scripts"); 00377 } 00378 m_secure->setText( sec ); 00379 } 00380 } 00381 00382 #include "kcookiewin.moc"