khtml Library API Documentation

khtml_ext.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /* This file is part of the KDE project
00003  *
00004  * Copyright (C) 2000-2003 Simon Hausmann <hausmann@kde.org>
00005  *               2001-2003 George Staikos <staikos@kde.org>
00006  *               2001-2003 Laurent Montel <montel@kde.org>
00007  *               2001-2003 Dirk Mueller <mueller@kde.org>
00008  *               2001-2003 Waldo Bastian <bastian@kde.org>
00009  *               2001-2003 David Faure <faure@kde.org>
00010  *               2001-2003 Daniel Naber <dnaber@kde.org>
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Library General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Library General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Library General Public License
00023  * along with this library; see the file COPYING.LIB.  If not, write to
00024  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00025  * Boston, MA 02111-1307, USA.
00026  */
00027 
00028 #include <assert.h>
00029 #include "khtml_ext.h"
00030 #include "khtmlview.h"
00031 #include "khtml_pagecache.h"
00032 #include "rendering/render_form.h"
00033 #include "rendering/render_image.h"
00034 #include "html/html_imageimpl.h"
00035 #include "misc/loader.h"
00036 #include "dom/html_form.h"
00037 #include "dom/html_image.h"
00038 #include <qclipboard.h>
00039 #include <qfileinfo.h>
00040 #include <qpopupmenu.h>
00041 #include <qmetaobject.h>
00042 #include <private/qucomextra_p.h>
00043 
00044 #include <kdebug.h>
00045 #include <klocale.h>
00046 #include <kfiledialog.h>
00047 #include <kio/job.h>
00048 #include <kprocess.h>
00049 #include <ktoolbarbutton.h>
00050 #include <ktoolbar.h>
00051 #include <ksavefile.h>
00052 #include <kurldrag.h>
00053 #include <kstringhandler.h>
00054 #include <kapplication.h>
00055 #include <kmessagebox.h>
00056 #include <kstandarddirs.h>
00057 #include <krun.h>
00058 #include <kurifilter.h>
00059 #include <kiconloader.h>
00060 #include <kdesktopfile.h>
00061 
00062 
00063 #include "dom/dom_element.h"
00064 #include "misc/htmltags.h"
00065 
00066 KHTMLPartBrowserExtension::KHTMLPartBrowserExtension( KHTMLPart *parent, const char *name )
00067 : KParts::BrowserExtension( parent, name )
00068 {
00069     m_part = parent;
00070     setURLDropHandlingEnabled( true );
00071 
00072     enableAction( "cut", false );
00073     enableAction( "copy", false );
00074     enableAction( "paste", false );
00075 
00076     m_connectedToClipboard = false;
00077 }
00078 
00079 int KHTMLPartBrowserExtension::xOffset()
00080 {
00081     return m_part->view()->contentsX();
00082 }
00083 
00084 int KHTMLPartBrowserExtension::yOffset()
00085 {
00086   return m_part->view()->contentsY();
00087 }
00088 
00089 void KHTMLPartBrowserExtension::saveState( QDataStream &stream )
00090 {
00091   //kdDebug( 6050 ) << "saveState!" << endl;
00092   m_part->saveState( stream );
00093 }
00094 
00095 void KHTMLPartBrowserExtension::restoreState( QDataStream &stream )
00096 {
00097   //kdDebug( 6050 ) << "restoreState!" << endl;
00098   m_part->restoreState( stream );
00099 }
00100 
00101 void KHTMLPartBrowserExtension::editableWidgetFocused( QWidget *widget )
00102 {
00103     m_editableFormWidget = widget;
00104     updateEditActions();
00105 
00106     if ( !m_connectedToClipboard && m_editableFormWidget )
00107     {
00108         connect( QApplication::clipboard(), SIGNAL( dataChanged() ),
00109                  this, SLOT( updateEditActions() ) );
00110 
00111         if ( m_editableFormWidget->inherits( "QLineEdit" ) || m_editableFormWidget->inherits( "QTextEdit" ) )
00112             connect( m_editableFormWidget, SIGNAL( selectionChanged() ),
00113                      this, SLOT( updateEditActions() ) );
00114 
00115         m_connectedToClipboard = true;
00116     }
00117     editableWidgetFocused();
00118 }
00119 
00120 void KHTMLPartBrowserExtension::editableWidgetBlurred( QWidget * /*widget*/ )
00121 {
00122     QWidget *oldWidget = m_editableFormWidget;
00123 
00124     m_editableFormWidget = 0;
00125     enableAction( "cut", false );
00126     enableAction( "paste", false );
00127     m_part->emitSelectionChanged();
00128 
00129     if ( m_connectedToClipboard )
00130     {
00131         disconnect( QApplication::clipboard(), SIGNAL( dataChanged() ),
00132                     this, SLOT( updateEditActions() ) );
00133 
00134         if ( oldWidget )
00135         {
00136             if ( oldWidget->inherits( "QLineEdit" ) || oldWidget->inherits( "QTextEdit" ) )
00137                 disconnect( oldWidget, SIGNAL( selectionChanged() ),
00138                             this, SLOT( updateEditActions() ) );
00139         }
00140 
00141         m_connectedToClipboard = false;
00142     }
00143     editableWidgetBlurred();
00144 }
00145 
00146 void KHTMLPartBrowserExtension::setExtensionProxy( KParts::BrowserExtension *proxy )
00147 {
00148     if ( m_extensionProxy )
00149     {
00150         disconnect( m_extensionProxy, SIGNAL( enableAction( const char *, bool ) ),
00151                     this, SLOT( extensionProxyActionEnabled( const char *, bool ) ) );
00152         if ( m_extensionProxy->inherits( "KHTMLPartBrowserExtension" ) )
00153         {
00154             disconnect( m_extensionProxy, SIGNAL( editableWidgetFocused() ),
00155                         this, SLOT( extensionProxyEditableWidgetFocused() ) );
00156             disconnect( m_extensionProxy, SIGNAL( editableWidgetBlurred() ),
00157                         this, SLOT( extensionProxyEditableWidgetBlurred() ) );
00158         }
00159     }
00160 
00161     m_extensionProxy = proxy;
00162 
00163     if ( m_extensionProxy )
00164     {
00165         connect( m_extensionProxy, SIGNAL( enableAction( const char *, bool ) ),
00166                  this, SLOT( extensionProxyActionEnabled( const char *, bool ) ) );
00167         if ( m_extensionProxy->inherits( "KHTMLPartBrowserExtension" ) )
00168         {
00169             connect( m_extensionProxy, SIGNAL( editableWidgetFocused() ),
00170                      this, SLOT( extensionProxyEditableWidgetFocused() ) );
00171             connect( m_extensionProxy, SIGNAL( editableWidgetBlurred() ),
00172                      this, SLOT( extensionProxyEditableWidgetBlurred() ) );
00173         }
00174 
00175         enableAction( "cut", m_extensionProxy->isActionEnabled( "cut" ) );
00176         enableAction( "copy", m_extensionProxy->isActionEnabled( "copy" ) );
00177         enableAction( "paste", m_extensionProxy->isActionEnabled( "paste" ) );
00178     }
00179     else
00180     {
00181         updateEditActions();
00182         enableAction( "copy", false ); // ### re-check this
00183     }
00184 }
00185 
00186 void KHTMLPartBrowserExtension::cut()
00187 {
00188     if ( m_extensionProxy )
00189     {
00190         callExtensionProxyMethod( "cut()" );
00191         return;
00192     }
00193 
00194     if ( !m_editableFormWidget )
00195         return;
00196 
00197     if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00198         static_cast<QLineEdit *>( &(*m_editableFormWidget) )->cut();
00199     else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00200         static_cast<QTextEdit *>( &(*m_editableFormWidget) )->cut();
00201 }
00202 
00203 void KHTMLPartBrowserExtension::copy()
00204 {
00205     if ( m_extensionProxy )
00206     {
00207         callExtensionProxyMethod( "copy()" );
00208         return;
00209     }
00210 
00211     kdDebug( 6050 ) << "************! KHTMLPartBrowserExtension::copy()" << endl;
00212     if ( !m_editableFormWidget )
00213     {
00214         // get selected text and paste to the clipboard
00215         QString text = m_part->selectedText();
00216     text.replace( QChar( 0xa0 ), ' ' );
00217         QClipboard *cb = QApplication::clipboard();
00218         disconnect( cb, SIGNAL( selectionChanged() ), m_part, SLOT( slotClearSelection() ) );
00219         cb->setText(text);
00220         connect( cb, SIGNAL( selectionChanged() ), m_part, SLOT( slotClearSelection() ) );
00221     }
00222     else
00223     {
00224         if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00225             static_cast<QLineEdit *>( &(*m_editableFormWidget) )->copy();
00226         else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00227             static_cast<QTextEdit *>( &(*m_editableFormWidget) )->copy();
00228     }
00229 }
00230 
00231 void KHTMLPartBrowserExtension::searchProvider()
00232 {
00233     if ( m_extensionProxy )
00234     {
00235         callExtensionProxyMethod( "searchProvider()" );
00236         return;
00237     }
00238 
00239     KURIFilterData data;
00240     QStringList list;
00241     data.setData( m_part->selectedText() );
00242     list << "kurisearchfilter" << "kuriikwsfilter";
00243 
00244     if( !KURIFilter::self()->filterURI(data, list) )
00245     {
00246         KDesktopFile file("searchproviders/google.desktop", true, "services");
00247         data.setData(file.readEntry("Query").replace("\\{@}", m_part->selectedText()));
00248     }
00249 
00250     emit m_part->browserExtension()->openURLRequest( data.uri() );
00251 }
00252 
00253 void KHTMLPartBrowserExtension::paste()
00254 {
00255     if ( m_extensionProxy )
00256     {
00257         callExtensionProxyMethod( "paste()" );
00258         return;
00259     }
00260 
00261     if ( !m_editableFormWidget )
00262         return;
00263 
00264     if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00265         static_cast<QLineEdit *>( &(*m_editableFormWidget) )->paste();
00266     else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00267         static_cast<QTextEdit *>( &(*m_editableFormWidget) )->paste();
00268 }
00269 
00270 void KHTMLPartBrowserExtension::callExtensionProxyMethod( const char *method )
00271 {
00272     if ( !m_extensionProxy )
00273         return;
00274 
00275     int slot = m_extensionProxy->metaObject()->findSlot( method );
00276     if ( slot == -1 )
00277         return;
00278 
00279     QUObject o[ 1 ];
00280     m_extensionProxy->qt_invoke( slot, o );
00281 }
00282 
00283 void KHTMLPartBrowserExtension::updateEditActions()
00284 {
00285     if ( !m_editableFormWidget )
00286     {
00287         enableAction( "cut", false );
00288         enableAction( "copy", false );
00289         enableAction( "paste", false );
00290         return;
00291     }
00292 
00293     // ### duplicated from KonqMainWindow::slotClipboardDataChanged
00294 #ifndef QT_NO_MIMECLIPBOARD // Handle minimalized versions of Qt Embedded
00295     QMimeSource *data = QApplication::clipboard()->data();
00296     enableAction( "paste", data->provides( "text/plain" ) );
00297 #else
00298     QString data=QApplication::clipboard()->text();
00299     enableAction( "paste", data.contains("://"));
00300 #endif
00301     bool hasSelection = false;
00302 
00303     if( m_editableFormWidget) {
00304         if ( ::qt_cast<QLineEdit*>(m_editableFormWidget))
00305             hasSelection = static_cast<QLineEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00306         else if(::qt_cast<QTextEdit*>(m_editableFormWidget))
00307             hasSelection = static_cast<QTextEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00308     }
00309 
00310     enableAction( "copy", hasSelection );
00311     enableAction( "cut", hasSelection );
00312 }
00313 
00314 void KHTMLPartBrowserExtension::extensionProxyEditableWidgetFocused() {
00315     editableWidgetFocused();
00316 }
00317 
00318 void KHTMLPartBrowserExtension::extensionProxyEditableWidgetBlurred() {
00319     editableWidgetBlurred();
00320 }
00321 
00322 void KHTMLPartBrowserExtension::extensionProxyActionEnabled( const char *action, bool enable )
00323 {
00324     // only forward enableAction calls for actions we actually do forward
00325     if ( strcmp( action, "cut" ) == 0 ||
00326          strcmp( action, "copy" ) == 0 ||
00327          strcmp( action, "paste" ) == 0 ) {
00328         enableAction( action, enable );
00329     }
00330 }
00331 
00332 void KHTMLPartBrowserExtension::reparseConfiguration()
00333 {
00334   m_part->reparseConfiguration();
00335 }
00336 
00337 void KHTMLPartBrowserExtension::print()
00338 {
00339   m_part->view()->print();
00340 }
00341 
00342 class KHTMLPopupGUIClient::KHTMLPopupGUIClientPrivate
00343 {
00344 public:
00345   KHTMLPart *m_khtml;
00346   KURL m_url;
00347   KURL m_imageURL;
00348   QString m_suggestedFilename;
00349 };
00350 
00351 
00352 KHTMLPopupGUIClient::KHTMLPopupGUIClient( KHTMLPart *khtml, const QString &doc, const KURL &url )
00353   : QObject( khtml )
00354 {
00355   d = new KHTMLPopupGUIClientPrivate;
00356   d->m_khtml = khtml;
00357   d->m_url = url;
00358   bool isImage = false;
00359   bool hasSelection = khtml->hasSelection();
00360   setInstance( khtml->instance() );
00361 
00362   DOM::Element e;
00363   e = khtml->nodeUnderMouse();
00364 
00365   if ( !e.isNull() && (e.elementId() == ID_IMG ||
00366                        (e.elementId() == ID_INPUT && !static_cast<DOM::HTMLInputElement>(e).src().isEmpty())))
00367   {
00368     if (e.elementId() == ID_IMG) {
00369       DOM::HTMLImageElementImpl *ie = static_cast<DOM::HTMLImageElementImpl*>(e.handle());
00370       khtml::RenderImage *ri = dynamic_cast<khtml::RenderImage*>(ie->renderer());
00371       if (ri && ri->contentObject()) {
00372         d->m_suggestedFilename = static_cast<khtml::CachedImage*>(ri->contentObject())->suggestedFilename();
00373       }
00374     }
00375     isImage=true;
00376   }
00377 
00378   if ( url.isEmpty() && !isImage )
00379   {
00380     if (hasSelection)
00381     {
00382       KAction* copyAction = KStdAction::copy( d->m_khtml->browserExtension(), SLOT( copy() ), actionCollection(), "copy" );
00383       copyAction->setText(i18n("&Copy Text"));
00384       copyAction->setEnabled(d->m_khtml->browserExtension()->isActionEnabled( "copy" ));
00385       actionCollection()->insert( khtml->actionCollection()->action( "selectAll" ) );
00386 
00387       KConfig config("kuriikwsfilterrc");
00388       config.setGroup("General");
00389       QString engine = config.readEntry("DefaultSearchEngine");
00390 
00391       // search text
00392       QString selectedText = khtml->selectedText();
00393       if ( selectedText.length()>18 ) {
00394         selectedText.truncate(15);
00395         selectedText+="...";
00396       }
00397 
00398       // search provider name
00399       KDesktopFile file("searchproviders/" + engine + ".desktop", true, "services");
00400 
00401       // search provider icon
00402       QPixmap icon;
00403       KURIFilterData data;
00404       QStringList list;
00405       data.setData( QString("some keyword") );
00406       list << "kurisearchfilter" << "kuriikwsfilter";
00407 
00408       QString name;
00409       if ( KURIFilter::self()->filterURI(data, list) )
00410       {
00411         QString iconPath = locate("cache", KMimeType::favIconForURL(data.uri()) + ".png");
00412         if ( iconPath.isEmpty() )
00413           icon = SmallIcon("find");
00414         else
00415           icon = QPixmap( iconPath );
00416         name = file.readName();
00417       }
00418       else
00419       {
00420         icon = SmallIcon("google");
00421         name = "Google";
00422       }
00423 
00424       new KAction( i18n( "Search '%1' at %2" ).arg( selectedText ).arg( name ), icon, 0, d->m_khtml->browserExtension(),
00425                      SLOT( searchProvider() ), actionCollection(), "searchProvider" );
00426     }
00427     else
00428     {
00429       actionCollection()->insert( khtml->actionCollection()->action( "security" ) );
00430       actionCollection()->insert( khtml->actionCollection()->action( "setEncoding" ) );
00431       new KAction( i18n( "Stop Animations" ), 0, this, SLOT( slotStopAnimations() ),
00432                    actionCollection(), "stopanimations" );
00433     }
00434   }
00435 
00436   if ( !url.isEmpty() )
00437   {
00438     if (url.protocol() == "mailto")
00439     {
00440       new KAction( i18n( "Copy Email Address" ), 0, this, SLOT( slotCopyLinkLocation() ),
00441                  actionCollection(), "copylinklocation" );
00442     }
00443     else
00444     {
00445       new KAction( i18n( "&Save Link As..." ), 0, this, SLOT( slotSaveLinkAs() ),
00446                  actionCollection(), "savelinkas" );
00447       new KAction( i18n( "Copy Link Address" ), 0, this, SLOT( slotCopyLinkLocation() ),
00448                  actionCollection(), "copylinklocation" );
00449     }
00450   }
00451 
00452   // frameset? -> add "Reload Frame" etc.
00453   if (!hasSelection)
00454   {
00455     if ( khtml->parentPart() )
00456     {
00457       new KAction( i18n( "Open in New &Window" ), "window_new", 0, this, SLOT( slotFrameInWindow() ),
00458                                           actionCollection(), "frameinwindow" );
00459       new KAction( i18n( "Open in &This Window" ), 0, this, SLOT( slotFrameInTop() ),
00460                                           actionCollection(), "frameintop" );
00461       new KAction( i18n( "Open in &New Tab" ), "tab_new", 0, this, SLOT( slotFrameInTab() ),
00462                                        actionCollection(), "frameintab" );
00463       new KAction( i18n( "Reload Frame" ), 0, this, SLOT( slotReloadFrame() ),
00464                                         actionCollection(), "reloadframe" );
00465       new KAction( i18n( "View Frame Source" ), 0, d->m_khtml, SLOT( slotViewDocumentSource() ),
00466                                           actionCollection(), "viewFrameSource" );
00467       new KAction( i18n( "View Frame Information" ), 0, d->m_khtml, SLOT( slotViewPageInfo() ), actionCollection(), "viewFrameInfo" );
00468       // This one isn't in khtml_popupmenu.rc anymore, because Print isn't either,
00469       // and because print frame is already in the toolbar and the menu.
00470       // But leave this here, so that it's easy to readd it.
00471       new KAction( i18n( "Print Frame..." ), "frameprint", 0, d->m_khtml->browserExtension(), SLOT( print() ), actionCollection(), "printFrame" );
00472 
00473       actionCollection()->insert( khtml->parentPart()->actionCollection()->action( "viewDocumentSource" ) );
00474       actionCollection()->insert( khtml->parentPart()->actionCollection()->action( "viewPageInfo" ) );
00475     } else {
00476       actionCollection()->insert( khtml->actionCollection()->action( "viewDocumentSource" ) );
00477       actionCollection()->insert( khtml->actionCollection()->action( "viewPageInfo" ) );
00478     }
00479   } else if (isImage || !url.isEmpty()) {
00480     actionCollection()->insert( khtml->actionCollection()->action( "viewDocumentSource" ) );
00481     actionCollection()->insert( khtml->actionCollection()->action( "viewPageInfo" ) );
00482     new KAction( i18n( "Stop Animations" ), 0, this, SLOT( slotStopAnimations() ),
00483                  actionCollection(), "stopanimations" );
00484   }
00485 
00486   if (isImage)
00487   {
00488     if ( e.elementId() == ID_IMG )
00489       d->m_imageURL = KURL( static_cast<DOM::HTMLImageElement>( e ).src().string() );
00490     else
00491       d->m_imageURL = KURL( static_cast<DOM::HTMLInputElement>( e ).src().string() );
00492     new KAction( i18n( "Save Image As..." ), 0, this, SLOT( slotSaveImageAs() ),
00493                  actionCollection(), "saveimageas" );
00494     new KAction( i18n( "Send Image" ), 0, this, SLOT( slotSendImage() ),
00495                  actionCollection(), "sendimage" );
00496 
00497 
00498     new KAction( i18n( "Copy Image Location" ), 0, this, SLOT( slotCopyImageLocation() ),
00499                  actionCollection(), "copyimagelocation" );
00500     QString name = KStringHandler::csqueeze(d->m_imageURL.fileName()+d->m_imageURL.query(), 25);
00501     new KAction( i18n( "View Image (%1)" ).arg(d->m_suggestedFilename.isEmpty() ? name.replace("&", "&&") : d->m_suggestedFilename.replace("&", "&&")), 0, this, SLOT( slotViewImage() ),
00502                  actionCollection(), "viewimage" );
00503   }
00504 
00505   setXML( doc );
00506   setDOMDocument( QDomDocument(), true ); // ### HACK
00507 
00508   QDomElement menu = domDocument().documentElement().namedItem( "Menu" ).toElement();
00509 
00510   if ( actionCollection()->count() > 0 )
00511     menu.insertBefore( domDocument().createElement( "separator" ), menu.firstChild() );
00512 }
00513 
00514 KHTMLPopupGUIClient::~KHTMLPopupGUIClient()
00515 {
00516   delete d;
00517 }
00518 
00519 void KHTMLPopupGUIClient::slotSaveLinkAs()
00520 {
00521   KIO::MetaData metaData;
00522   metaData["referrer"] = d->m_khtml->referrer();
00523   saveURL( d->m_khtml->widget(), i18n( "Save Link As" ), d->m_url, metaData );
00524 }
00525 
00526 void KHTMLPopupGUIClient::slotSendImage()
00527 {
00528     QStringList urls;
00529     urls.append( d->m_imageURL.url());
00530     QString subject = d->m_imageURL.url();
00531     kapp->invokeMailer(QString::null, QString::null, QString::null, subject,
00532                        QString::null, //body
00533                        QString::null,
00534                        urls); // attachments
00535 
00536 
00537 }
00538 
00539 void KHTMLPopupGUIClient::slotSaveImageAs()
00540 {
00541   KIO::MetaData metaData;
00542   metaData["referrer"] = d->m_khtml->referrer();
00543   saveURL( d->m_khtml->widget(), i18n( "Save Image As" ), d->m_imageURL, metaData, QString::null, 0, d->m_suggestedFilename );
00544 }
00545 
00546 void KHTMLPopupGUIClient::slotCopyLinkLocation()
00547 {
00548   KURL safeURL(d->m_url);
00549   safeURL.setPass(QString::null);
00550 #ifndef QT_NO_MIMECLIPBOARD
00551   // Set it in both the mouse selection and in the clipboard
00552   KURL::List lst;
00553   lst.append( safeURL );
00554   QApplication::clipboard()->setSelectionMode(true);
00555   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00556   QApplication::clipboard()->setSelectionMode(false);
00557   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00558 #else
00559   QApplication::clipboard()->setText( safeURL.url() ); //FIXME(E): Handle multiple entries
00560 #endif
00561 }
00562 
00563 void KHTMLPopupGUIClient::slotStopAnimations()
00564 {
00565   d->m_khtml->stopAnimations();
00566 }
00567 
00568 void KHTMLPopupGUIClient::slotCopyImageLocation()
00569 {
00570   KURL safeURL(d->m_imageURL);
00571   safeURL.setPass(QString::null);
00572 #ifndef QT_NO_MIMECLIPBOARD
00573   // Set it in both the mouse selection and in the clipboard
00574   KURL::List lst;
00575   lst.append( safeURL );
00576   QApplication::clipboard()->setSelectionMode(true);
00577   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00578   QApplication::clipboard()->setSelectionMode(false);
00579   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00580 #else
00581   QApplication::clipboard()->setText( safeURL.url() ); //FIXME(E): Handle multiple entries
00582 #endif
00583 }
00584 
00585 void KHTMLPopupGUIClient::slotViewImage()
00586 {
00587   d->m_khtml->browserExtension()->createNewWindow(d->m_imageURL);
00588 }
00589 
00590 void KHTMLPopupGUIClient::slotReloadFrame()
00591 {
00592   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00593   args.reload = true;
00594   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00595   // reload document
00596   d->m_khtml->closeURL();
00597   d->m_khtml->browserExtension()->setURLArgs( args );
00598   d->m_khtml->openURL( d->m_khtml->url() );
00599 }
00600 
00601 void KHTMLPopupGUIClient::slotFrameInWindow()
00602 {
00603   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00604   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00605   args.metaData()["forcenewwindow"] = "true";
00606   emit d->m_khtml->browserExtension()->createNewWindow( d->m_khtml->url(), args );
00607 }
00608 
00609 void KHTMLPopupGUIClient::slotFrameInTop()
00610 {
00611   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00612   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00613   args.frameName = "_top";
00614   emit d->m_khtml->browserExtension()->openURLRequest( d->m_khtml->url(), args );
00615 }
00616 
00617 void KHTMLPopupGUIClient::slotFrameInTab()
00618 {
00619   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00620   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00621   args.setNewTab(true);
00622   emit d->m_khtml->browserExtension()->createNewWindow( d->m_khtml->url(), args );
00623 }
00624 
00625 void KHTMLPopupGUIClient::saveURL( QWidget *parent, const QString &caption,
00626                                    const KURL &url,
00627                                    const QMap<QString, QString> &metadata,
00628                                    const QString &filter, long cacheId,
00629                                    const QString & suggestedFilename )
00630 {
00631   QString name = QString::fromLatin1( "index.html" );
00632   if ( !suggestedFilename.isEmpty() )
00633     name = suggestedFilename;
00634   else if ( !url.fileName().isEmpty() )
00635     name = url.fileName();
00636 
00637   KURL destURL;
00638   int query;
00639   do {
00640     query = KMessageBox::Yes;
00641     destURL = KFileDialog::getSaveURL( name, filter, parent, caption );
00642       if( destURL.isLocalFile() )
00643       {
00644         QFileInfo info( destURL.path() );
00645         if( info.exists() ) {
00646           // TODO: use KIO::RenameDlg (shows more information)
00647           query = KMessageBox::warningYesNo( parent, i18n( "A file named \"%1\" already exists. " "Are you sure you want to overwrite it?" ).arg( info.fileName() ), i18n( "Overwrite File?" ), i18n( "Overwrite" ), KStdGuiItem::cancel() );
00648         }
00649        }
00650    } while ( query == KMessageBox::No );
00651 
00652   if ( destURL.isValid() )
00653     saveURL(url, destURL, metadata, cacheId);
00654 }
00655 
00656 void KHTMLPopupGUIClient::saveURL( const KURL &url, const KURL &destURL,
00657                                    const QMap<QString, QString> &metadata,
00658                                    long cacheId )
00659 {
00660     if ( destURL.isValid() )
00661     {
00662         bool saved = false;
00663         if (KHTMLPageCache::self()->isComplete(cacheId))
00664         {
00665             if (destURL.isLocalFile())
00666             {
00667                 KSaveFile destFile(destURL.path());
00668                 if (destFile.status() == 0)
00669                 {
00670                     KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00671                     saved = true;
00672                 }
00673             }
00674             else
00675             {
00676                 // save to temp file, then move to final destination.
00677                 KTempFile destFile;
00678                 if (destFile.status() == 0)
00679                 {
00680                     KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00681                     destFile.close();
00682                     KURL url2 = KURL();
00683                     url2.setPath(destFile.name());
00684                     KIO::file_move(url2, destURL, -1, true /*overwrite*/);
00685                     saved = true;
00686                 }
00687             }
00688         }
00689         if(!saved)
00690         {
00691           // DownloadManager <-> konqueror integration
00692           // find if the integration is enabled
00693           // the empty key  means no integration
00694           // only use download manager for non-local urls!
00695           bool downloadViaKIO = true;
00696           if ( !url.isLocalFile() )
00697           {
00698             KConfig cfg("konquerorrc", false, false);
00699             cfg.setGroup("HTML Settings");
00700             QString downloadManger = cfg.readPathEntry("DownloadManager");
00701             if (!downloadManger.isEmpty())
00702             {
00703                 // then find the download manager location
00704                 kdDebug(1000) << "Using: "<<downloadManger <<" as Download Manager" <<endl;
00705                 QString cmd = KStandardDirs::findExe(downloadManger);
00706                 if (cmd.isEmpty())
00707                 {
00708                     QString errMsg=i18n("The Download Manager (%1) could not be found in your $PATH ").arg(downloadManger);
00709                     QString errMsgEx= i18n("Try to reinstall it  \n\nThe integration with Konqueror will be disabled!");
00710                     KMessageBox::detailedSorry(0,errMsg,errMsgEx);
00711                     cfg.writePathEntry("DownloadManager",QString::null);
00712                     cfg.sync ();
00713                 }
00714                 else
00715                 {
00716                     downloadViaKIO = false;
00717                     KURL cleanDest = destURL;
00718                     cleanDest.setPass( QString::null ); // don't put password into commandline
00719                     cmd += " " + KProcess::quote(url.url()) + " " +
00720                            KProcess::quote(cleanDest.url());
00721                     kdDebug(1000) << "Calling command  "<<cmd<<endl;
00722                     KRun::runCommand(cmd);
00723                 }
00724             }
00725           }
00726 
00727           if ( downloadViaKIO )
00728           {
00729               KIO::Job *job = KIO::file_copy( url, destURL, -1, true /*overwrite*/ );
00730               job->setMetaData(metadata);
00731               job->addMetaData("MaxCacheSize", "0"); // Don't store in http cache.
00732               job->addMetaData("cache", "cache"); // Use entry from cache if available.
00733               job->setAutoErrorHandlingEnabled( true );
00734           }
00735         } //end if(!saved)
00736     }
00737 }
00738 
00739 KHTMLPartBrowserHostExtension::KHTMLPartBrowserHostExtension( KHTMLPart *part )
00740 : KParts::BrowserHostExtension( part )
00741 {
00742   m_part = part;
00743 }
00744 
00745 KHTMLPartBrowserHostExtension::~KHTMLPartBrowserHostExtension()
00746 {
00747 }
00748 
00749 QStringList KHTMLPartBrowserHostExtension::frameNames() const
00750 {
00751   return m_part->frameNames();
00752 }
00753 
00754 const QPtrList<KParts::ReadOnlyPart> KHTMLPartBrowserHostExtension::frames() const
00755 {
00756   return m_part->frames();
00757 }
00758 
00759 bool KHTMLPartBrowserHostExtension::openURLInFrame( const KURL &url, const KParts::URLArgs &urlArgs )
00760 {
00761   return m_part->openURLInFrame( url, urlArgs );
00762 }
00763 
00764 void KHTMLPartBrowserHostExtension::virtual_hook( int id, void *data )
00765 {
00766   if (id == VIRTUAL_FIND_FRAME_PARENT)
00767   {
00768     FindFrameParentParams *param = static_cast<FindFrameParentParams*>(data);
00769     KHTMLPart *parentPart = m_part->findFrameParent(param->callingPart, param->frame);
00770     if (parentPart)
00771        param->parent = parentPart->browserHostExtension();
00772     return;
00773   }
00774   BrowserHostExtension::virtual_hook( id, data );
00775 }
00776 
00777 
00778 // defined in khtml_part.cpp
00779 extern const int KDE_NO_EXPORT fastZoomSizes[];
00780 extern const int KDE_NO_EXPORT fastZoomSizeCount;
00781 
00782 // BCI: remove in KDE 4
00783 KHTMLZoomFactorAction::KHTMLZoomFactorAction( KHTMLPart *part, bool direction, const QString &text, const QString &icon, const QObject *receiver, const char *slot, QObject *parent, const char *name )
00784     : KAction( text, icon, 0, receiver, slot, parent, name )
00785 {
00786     init(part, direction);
00787 }
00788 
00789 KHTMLZoomFactorAction::KHTMLZoomFactorAction( KHTMLPart *part, bool direction, const QString &text, const QString &icon, const KShortcut &cut, const QObject *receiver, const char *slot, QObject *parent, const char *name )
00790     : KAction( text, icon, cut, receiver, slot, parent, name )
00791 {
00792     init(part, direction);
00793 }
00794 
00795 void KHTMLZoomFactorAction::init(KHTMLPart *part, bool direction)
00796 {
00797     m_direction = direction;
00798     m_part = part;
00799 
00800     m_popup = new QPopupMenu;
00801     m_popup->insertItem( i18n( "Default Font Size (100%)" ) );
00802 
00803     int m = m_direction ? 1 : -1;
00804     int ofs = fastZoomSizeCount / 2;       // take index of 100%
00805 
00806     // this only works if there is an odd number of elements in fastZoomSizes[]
00807     for ( int i = m; i != m*(ofs+1); i += m )
00808     {
00809         int num = i * m;
00810         QString numStr = QString::number( num );
00811         if ( num > 0 ) numStr.prepend( '+' );
00812 
00813         m_popup->insertItem( i18n( "%1%" ).arg( fastZoomSizes[ofs + i] ) );
00814     }
00815 
00816     connect( m_popup, SIGNAL( activated( int ) ), this, SLOT( slotActivated( int ) ) );
00817 }
00818 
00819 KHTMLZoomFactorAction::~KHTMLZoomFactorAction()
00820 {
00821     delete m_popup;
00822 }
00823 
00824 int KHTMLZoomFactorAction::plug( QWidget *w, int index )
00825 {
00826     int containerId = KAction::plug( w, index );
00827     if ( containerId == -1 || !w->inherits( "KToolBar" ) )
00828         return containerId;
00829 
00830     KToolBarButton *button = static_cast<KToolBar *>( w )->getButton( itemId( containerId ) );
00831     if ( !button )
00832         return containerId;
00833 
00834     button->setDelayedPopup( m_popup );
00835     return containerId;
00836 }
00837 
00838 void KHTMLZoomFactorAction::slotActivated( int id )
00839 {
00840     int idx = m_popup->indexOf( id );
00841 
00842     if (idx == 0)
00843         m_part->setZoomFactor(100);
00844     else
00845         m_part->setZoomFactor(fastZoomSizes[fastZoomSizeCount/2 + (m_direction ? 1 : -1)*idx]);
00846 }
00847 
00848 #include "khtml_ext.moc"
00849 
KDE Logo
This file is part of the documentation for khtml Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:34:40 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003