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     KParts::URLArgs args;
00251     args.frameName = "_blank";
00252 
00253     emit m_part->browserExtension()->openURLRequest( data.uri(), args );
00254 
00255 }
00256 
00257 void KHTMLPartBrowserExtension::paste()
00258 {
00259     if ( m_extensionProxy )
00260     {
00261         callExtensionProxyMethod( "paste()" );
00262         return;
00263     }
00264 
00265     if ( !m_editableFormWidget )
00266         return;
00267 
00268     if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00269         static_cast<QLineEdit *>( &(*m_editableFormWidget) )->paste();
00270     else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00271         static_cast<QTextEdit *>( &(*m_editableFormWidget) )->paste();
00272 }
00273 
00274 void KHTMLPartBrowserExtension::callExtensionProxyMethod( const char *method )
00275 {
00276     if ( !m_extensionProxy )
00277         return;
00278 
00279     int slot = m_extensionProxy->metaObject()->findSlot( method );
00280     if ( slot == -1 )
00281         return;
00282 
00283     QUObject o[ 1 ];
00284     m_extensionProxy->qt_invoke( slot, o );
00285 }
00286 
00287 void KHTMLPartBrowserExtension::updateEditActions()
00288 {
00289     if ( !m_editableFormWidget )
00290     {
00291         enableAction( "cut", false );
00292         enableAction( "copy", false );
00293         enableAction( "paste", false );
00294         return;
00295     }
00296 
00297     // ### duplicated from KonqMainWindow::slotClipboardDataChanged
00298 #ifndef QT_NO_MIMECLIPBOARD // Handle minimalized versions of Qt Embedded
00299     QMimeSource *data = QApplication::clipboard()->data();
00300     enableAction( "paste", data->provides( "text/plain" ) );
00301 #else
00302     QString data=QApplication::clipboard()->text();
00303     enableAction( "paste", data.contains("://"));
00304 #endif
00305     bool hasSelection = false;
00306 
00307     if( m_editableFormWidget) {
00308         if ( ::qt_cast<QLineEdit*>(m_editableFormWidget))
00309             hasSelection = static_cast<QLineEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00310         else if(::qt_cast<QTextEdit*>(m_editableFormWidget))
00311             hasSelection = static_cast<QTextEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00312     }
00313 
00314     enableAction( "copy", hasSelection );
00315     enableAction( "cut", hasSelection );
00316 }
00317 
00318 void KHTMLPartBrowserExtension::extensionProxyEditableWidgetFocused() {
00319     editableWidgetFocused();
00320 }
00321 
00322 void KHTMLPartBrowserExtension::extensionProxyEditableWidgetBlurred() {
00323     editableWidgetBlurred();
00324 }
00325 
00326 void KHTMLPartBrowserExtension::extensionProxyActionEnabled( const char *action, bool enable )
00327 {
00328     // only forward enableAction calls for actions we actually do forward
00329     if ( strcmp( action, "cut" ) == 0 ||
00330          strcmp( action, "copy" ) == 0 ||
00331          strcmp( action, "paste" ) == 0 ) {
00332         enableAction( action, enable );
00333     }
00334 }
00335 
00336 void KHTMLPartBrowserExtension::reparseConfiguration()
00337 {
00338   m_part->reparseConfiguration();
00339 }
00340 
00341 void KHTMLPartBrowserExtension::print()
00342 {
00343   m_part->view()->print();
00344 }
00345 
00346 class KHTMLPopupGUIClient::KHTMLPopupGUIClientPrivate
00347 {
00348 public:
00349   KHTMLPart *m_khtml;
00350   KURL m_url;
00351   KURL m_imageURL;
00352   QString m_suggestedFilename;
00353 };
00354 
00355 
00356 KHTMLPopupGUIClient::KHTMLPopupGUIClient( KHTMLPart *khtml, const QString &doc, const KURL &url )
00357   : QObject( khtml )
00358 {
00359   d = new KHTMLPopupGUIClientPrivate;
00360   d->m_khtml = khtml;
00361   d->m_url = url;
00362   bool isImage = false;
00363   bool hasSelection = khtml->hasSelection();
00364   setInstance( khtml->instance() );
00365 
00366   DOM::Element e;
00367   e = khtml->nodeUnderMouse();
00368 
00369   if ( !e.isNull() && (e.elementId() == ID_IMG ||
00370                        (e.elementId() == ID_INPUT && !static_cast<DOM::HTMLInputElement>(e).src().isEmpty())))
00371   {
00372     if (e.elementId() == ID_IMG) {
00373       DOM::HTMLImageElementImpl *ie = static_cast<DOM::HTMLImageElementImpl*>(e.handle());
00374       khtml::RenderImage *ri = dynamic_cast<khtml::RenderImage*>(ie->renderer());
00375       if (ri && ri->contentObject()) {
00376         d->m_suggestedFilename = static_cast<khtml::CachedImage*>(ri->contentObject())->suggestedFilename();
00377       }
00378     }
00379     isImage=true;
00380   }
00381 
00382   if ( url.isEmpty() && !isImage )
00383   {
00384     if (hasSelection)
00385     {
00386       KAction* copyAction = KStdAction::copy( d->m_khtml->browserExtension(), SLOT( copy() ), actionCollection(), "copy" );
00387       copyAction->setText(i18n("&Copy Text"));
00388       copyAction->setEnabled(d->m_khtml->browserExtension()->isActionEnabled( "copy" ));
00389       actionCollection()->insert( khtml->actionCollection()->action( "selectAll" ) );
00390 
00391       KConfig config("kuriikwsfilterrc");
00392       config.setGroup("General");
00393       QString engine = config.readEntry("DefaultSearchEngine");
00394 
00395       // search text
00396       QString selectedText = khtml->selectedText();
00397       if ( selectedText.length()>18 ) {
00398         selectedText.truncate(15);
00399         selectedText+="...";
00400       }
00401 
00402       // search provider name
00403       KDesktopFile file("searchproviders/" + engine + ".desktop", true, "services");
00404 
00405       // search provider icon
00406       QPixmap icon;
00407       KURIFilterData data;
00408       QStringList list;
00409       data.setData( QString("some keyword") );
00410       list << "kurisearchfilter" << "kuriikwsfilter";
00411 
00412       QString name;
00413       if ( KURIFilter::self()->filterURI(data, list) )
00414       {
00415         QString iconPath = locate("cache", KMimeType::favIconForURL(data.uri()) + ".png");
00416         if ( iconPath.isEmpty() )
00417           icon = SmallIcon("find");
00418         else
00419           icon = QPixmap( iconPath );
00420         name = file.readName();
00421       }
00422       else
00423       {
00424         icon = SmallIcon("google");
00425         name = "Google";
00426       }
00427 
00428       new KAction( i18n( "Search '%1' at %2" ).arg( selectedText ).arg( name ), icon, 0, d->m_khtml->browserExtension(),
00429                      SLOT( searchProvider() ), actionCollection(), "searchProvider" );
00430     }
00431     else
00432     {
00433       actionCollection()->insert( khtml->actionCollection()->action( "security" ) );
00434       actionCollection()->insert( khtml->actionCollection()->action( "setEncoding" ) );
00435       new KAction( i18n( "Stop Animations" ), 0, this, SLOT( slotStopAnimations() ),
00436                    actionCollection(), "stopanimations" );
00437     }
00438   }
00439 
00440   if ( !url.isEmpty() )
00441   {
00442     if (url.protocol() == "mailto")
00443     {
00444       new KAction( i18n( "Copy Email Address" ), 0, this, SLOT( slotCopyLinkLocation() ),
00445                  actionCollection(), "copylinklocation" );
00446     }
00447     else
00448     {
00449       new KAction( i18n( "&Save Link As..." ), 0, this, SLOT( slotSaveLinkAs() ),
00450                  actionCollection(), "savelinkas" );
00451       new KAction( i18n( "Copy Link Address" ), 0, this, SLOT( slotCopyLinkLocation() ),
00452                  actionCollection(), "copylinklocation" );
00453     }
00454   }
00455 
00456   // frameset? -> add "Reload Frame" etc.
00457   if (!hasSelection)
00458   {
00459     if ( khtml->parentPart() )
00460     {
00461       new KAction( i18n( "Open in New &Window" ), "window_new", 0, this, SLOT( slotFrameInWindow() ),
00462                                           actionCollection(), "frameinwindow" );
00463       new KAction( i18n( "Open in &This Window" ), 0, this, SLOT( slotFrameInTop() ),
00464                                           actionCollection(), "frameintop" );
00465       new KAction( i18n( "Open in &New Tab" ), "tab_new", 0, this, SLOT( slotFrameInTab() ),
00466                                        actionCollection(), "frameintab" );
00467       new KAction( i18n( "Reload Frame" ), 0, this, SLOT( slotReloadFrame() ),
00468                                         actionCollection(), "reloadframe" );
00469       new KAction( i18n( "View Frame Source" ), 0, d->m_khtml, SLOT( slotViewDocumentSource() ),
00470                                           actionCollection(), "viewFrameSource" );
00471       new KAction( i18n( "View Frame Information" ), 0, d->m_khtml, SLOT( slotViewPageInfo() ), actionCollection(), "viewFrameInfo" );
00472       // This one isn't in khtml_popupmenu.rc anymore, because Print isn't either,
00473       // and because print frame is already in the toolbar and the menu.
00474       // But leave this here, so that it's easy to readd it.
00475       new KAction( i18n( "Print Frame..." ), "frameprint", 0, d->m_khtml->browserExtension(), SLOT( print() ), actionCollection(), "printFrame" );
00476 
00477       actionCollection()->insert( khtml->parentPart()->actionCollection()->action( "viewDocumentSource" ) );
00478       actionCollection()->insert( khtml->parentPart()->actionCollection()->action( "viewPageInfo" ) );
00479     } else {
00480       actionCollection()->insert( khtml->actionCollection()->action( "viewDocumentSource" ) );
00481       actionCollection()->insert( khtml->actionCollection()->action( "viewPageInfo" ) );
00482     }
00483   } else if (isImage || !url.isEmpty()) {
00484     actionCollection()->insert( khtml->actionCollection()->action( "viewDocumentSource" ) );
00485     actionCollection()->insert( khtml->actionCollection()->action( "viewPageInfo" ) );
00486     new KAction( i18n( "Stop Animations" ), 0, this, SLOT( slotStopAnimations() ),
00487                  actionCollection(), "stopanimations" );
00488   }
00489 
00490   if (isImage)
00491   {
00492     if ( e.elementId() == ID_IMG )
00493       d->m_imageURL = KURL( static_cast<DOM::HTMLImageElement>( e ).src().string() );
00494     else
00495       d->m_imageURL = KURL( static_cast<DOM::HTMLInputElement>( e ).src().string() );
00496     new KAction( i18n( "Save Image As..." ), 0, this, SLOT( slotSaveImageAs() ),
00497                  actionCollection(), "saveimageas" );
00498     new KAction( i18n( "Send Image" ), 0, this, SLOT( slotSendImage() ),
00499                  actionCollection(), "sendimage" );
00500 
00501 
00502     new KAction( i18n( "Copy Image Location" ), 0, this, SLOT( slotCopyImageLocation() ),
00503                  actionCollection(), "copyimagelocation" );
00504     QString name = KStringHandler::csqueeze(d->m_imageURL.fileName()+d->m_imageURL.query(), 25);
00505     new KAction( i18n( "View Image (%1)" ).arg(d->m_suggestedFilename.isEmpty() ? name.replace("&", "&&") : d->m_suggestedFilename.replace("&", "&&")), 0, this, SLOT( slotViewImage() ),
00506                  actionCollection(), "viewimage" );
00507   }
00508 
00509   setXML( doc );
00510   setDOMDocument( QDomDocument(), true ); // ### HACK
00511 
00512   QDomElement menu = domDocument().documentElement().namedItem( "Menu" ).toElement();
00513 
00514   if ( actionCollection()->count() > 0 )
00515     menu.insertBefore( domDocument().createElement( "separator" ), menu.firstChild() );
00516 }
00517 
00518 KHTMLPopupGUIClient::~KHTMLPopupGUIClient()
00519 {
00520   delete d;
00521 }
00522 
00523 void KHTMLPopupGUIClient::slotSaveLinkAs()
00524 {
00525   KIO::MetaData metaData;
00526   metaData["referrer"] = d->m_khtml->referrer();
00527   saveURL( d->m_khtml->widget(), i18n( "Save Link As" ), d->m_url, metaData );
00528 }
00529 
00530 void KHTMLPopupGUIClient::slotSendImage()
00531 {
00532     QStringList urls;
00533     urls.append( d->m_imageURL.url());
00534     QString subject = d->m_imageURL.url();
00535     kapp->invokeMailer(QString::null, QString::null, QString::null, subject,
00536                        QString::null, //body
00537                        QString::null,
00538                        urls); // attachments
00539 
00540 
00541 }
00542 
00543 void KHTMLPopupGUIClient::slotSaveImageAs()
00544 {
00545   KIO::MetaData metaData;
00546   metaData["referrer"] = d->m_khtml->referrer();
00547   saveURL( d->m_khtml->widget(), i18n( "Save Image As" ), d->m_imageURL, metaData, QString::null, 0, d->m_suggestedFilename );
00548 }
00549 
00550 void KHTMLPopupGUIClient::slotCopyLinkLocation()
00551 {
00552   KURL safeURL(d->m_url);
00553   safeURL.setPass(QString::null);
00554 #ifndef QT_NO_MIMECLIPBOARD
00555   // Set it in both the mouse selection and in the clipboard
00556   KURL::List lst;
00557   lst.append( safeURL );
00558   QApplication::clipboard()->setSelectionMode(true);
00559   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00560   QApplication::clipboard()->setSelectionMode(false);
00561   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00562 #else
00563   QApplication::clipboard()->setText( safeURL.url() ); //FIXME(E): Handle multiple entries
00564 #endif
00565 }
00566 
00567 void KHTMLPopupGUIClient::slotStopAnimations()
00568 {
00569   d->m_khtml->stopAnimations();
00570 }
00571 
00572 void KHTMLPopupGUIClient::slotCopyImageLocation()
00573 {
00574   KURL safeURL(d->m_imageURL);
00575   safeURL.setPass(QString::null);
00576 #ifndef QT_NO_MIMECLIPBOARD
00577   // Set it in both the mouse selection and in the clipboard
00578   KURL::List lst;
00579   lst.append( safeURL );
00580   QApplication::clipboard()->setSelectionMode(true);
00581   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00582   QApplication::clipboard()->setSelectionMode(false);
00583   QApplication::clipboard()->setData( new KURLDrag( lst ) );
00584 #else
00585   QApplication::clipboard()->setText( safeURL.url() ); //FIXME(E): Handle multiple entries
00586 #endif
00587 }
00588 
00589 void KHTMLPopupGUIClient::slotViewImage()
00590 {
00591   d->m_khtml->browserExtension()->createNewWindow(d->m_imageURL);
00592 }
00593 
00594 void KHTMLPopupGUIClient::slotReloadFrame()
00595 {
00596   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00597   args.reload = true;
00598   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00599   // reload document
00600   d->m_khtml->closeURL();
00601   d->m_khtml->browserExtension()->setURLArgs( args );
00602   d->m_khtml->openURL( d->m_khtml->url() );
00603 }
00604 
00605 void KHTMLPopupGUIClient::slotFrameInWindow()
00606 {
00607   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00608   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00609   args.metaData()["forcenewwindow"] = "true";
00610   emit d->m_khtml->browserExtension()->createNewWindow( d->m_khtml->url(), args );
00611 }
00612 
00613 void KHTMLPopupGUIClient::slotFrameInTop()
00614 {
00615   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00616   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00617   args.frameName = "_top";
00618   emit d->m_khtml->browserExtension()->openURLRequest( d->m_khtml->url(), args );
00619 }
00620 
00621 void KHTMLPopupGUIClient::slotFrameInTab()
00622 {
00623   KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00624   args.metaData()["referrer"] = d->m_khtml->pageReferrer();
00625   args.setNewTab(true);
00626   emit d->m_khtml->browserExtension()->createNewWindow( d->m_khtml->url(), args );
00627 }
00628 
00629 void KHTMLPopupGUIClient::saveURL( QWidget *parent, const QString &caption,
00630                                    const KURL &url,
00631                                    const QMap<QString, QString> &metadata,
00632                                    const QString &filter, long cacheId,
00633                                    const QString & suggestedFilename )
00634 {
00635   QString name = QString::fromLatin1( "index.html" );
00636   if ( !suggestedFilename.isEmpty() )
00637     name = suggestedFilename;
00638   else if ( !url.fileName().isEmpty() )
00639     name = url.fileName();
00640 
00641   KURL destURL;
00642   int query;
00643   do {
00644     query = KMessageBox::Yes;
00645     destURL = KFileDialog::getSaveURL( name, filter, parent, caption );
00646       if( destURL.isLocalFile() )
00647       {
00648         QFileInfo info( destURL.path() );
00649         if( info.exists() ) {
00650           // TODO: use KIO::RenameDlg (shows more information)
00651           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() );
00652         }
00653        }
00654    } while ( query == KMessageBox::No );
00655 
00656   if ( destURL.isValid() )
00657     saveURL(url, destURL, metadata, cacheId);
00658 }
00659 
00660 void KHTMLPopupGUIClient::saveURL( const KURL &url, const KURL &destURL,
00661                                    const QMap<QString, QString> &metadata,
00662                                    long cacheId )
00663 {
00664     if ( destURL.isValid() )
00665     {
00666         bool saved = false;
00667         if (KHTMLPageCache::self()->isComplete(cacheId))
00668         {
00669             if (destURL.isLocalFile())
00670             {
00671                 KSaveFile destFile(destURL.path());
00672                 if (destFile.status() == 0)
00673                 {
00674                     KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00675                     saved = true;
00676                 }
00677             }
00678             else
00679             {
00680                 // save to temp file, then move to final destination.
00681                 KTempFile destFile;
00682                 if (destFile.status() == 0)
00683                 {
00684                     KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00685                     destFile.close();
00686                     KURL url2 = KURL();
00687                     url2.setPath(destFile.name());
00688                     KIO::file_move(url2, destURL, -1, true /*overwrite*/);
00689                     saved = true;
00690                 }
00691             }
00692         }
00693         if(!saved)
00694         {
00695           // DownloadManager <-> konqueror integration
00696           // find if the integration is enabled
00697           // the empty key  means no integration
00698           // only use download manager for non-local urls!
00699           bool downloadViaKIO = true;
00700           if ( !url.isLocalFile() )
00701           {
00702             KConfig cfg("konquerorrc", false, false);
00703             cfg.setGroup("HTML Settings");
00704             QString downloadManger = cfg.readPathEntry("DownloadManager");
00705             if (!downloadManger.isEmpty())
00706             {
00707                 // then find the download manager location
00708                 kdDebug(1000) << "Using: "<<downloadManger <<" as Download Manager" <<endl;
00709                 QString cmd = KStandardDirs::findExe(downloadManger);
00710                 if (cmd.isEmpty())
00711                 {
00712                     QString errMsg=i18n("The Download Manager (%1) could not be found in your $PATH ").arg(downloadManger);
00713                     QString errMsgEx= i18n("Try to reinstall it  \n\nThe integration with Konqueror will be disabled!");
00714                     KMessageBox::detailedSorry(0,errMsg,errMsgEx);
00715                     cfg.writePathEntry("DownloadManager",QString::null);
00716                     cfg.sync ();
00717                 }
00718                 else
00719                 {
00720                     downloadViaKIO = false;
00721                     KURL cleanDest = destURL;
00722                     cleanDest.setPass( QString::null ); // don't put password into commandline
00723                     cmd += " " + KProcess::quote(url.url()) + " " +
00724                            KProcess::quote(cleanDest.url());
00725                     kdDebug(1000) << "Calling command  "<<cmd<<endl;
00726                     KRun::runCommand(cmd);
00727                 }
00728             }
00729           }
00730 
00731           if ( downloadViaKIO )
00732           {
00733               KIO::Job *job = KIO::file_copy( url, destURL, -1, true /*overwrite*/ );
00734               job->setMetaData(metadata);
00735               job->addMetaData("MaxCacheSize", "0"); // Don't store in http cache.
00736               job->addMetaData("cache", "cache"); // Use entry from cache if available.
00737               job->setAutoErrorHandlingEnabled( true );
00738           }
00739         } //end if(!saved)
00740     }
00741 }
00742 
00743 KHTMLPartBrowserHostExtension::KHTMLPartBrowserHostExtension( KHTMLPart *part )
00744 : KParts::BrowserHostExtension( part )
00745 {
00746   m_part = part;
00747 }
00748 
00749 KHTMLPartBrowserHostExtension::~KHTMLPartBrowserHostExtension()
00750 {
00751 }
00752 
00753 QStringList KHTMLPartBrowserHostExtension::frameNames() const
00754 {
00755   return m_part->frameNames();
00756 }
00757 
00758 const QPtrList<KParts::ReadOnlyPart> KHTMLPartBrowserHostExtension::frames() const
00759 {
00760   return m_part->frames();
00761 }
00762 
00763 bool KHTMLPartBrowserHostExtension::openURLInFrame( const KURL &url, const KParts::URLArgs &urlArgs )
00764 {
00765   return m_part->openURLInFrame( url, urlArgs );
00766 }
00767 
00768 void KHTMLPartBrowserHostExtension::virtual_hook( int id, void *data )
00769 {
00770   if (id == VIRTUAL_FIND_FRAME_PARENT)
00771   {
00772     FindFrameParentParams *param = static_cast<FindFrameParentParams*>(data);
00773     KHTMLPart *parentPart = m_part->findFrameParent(param->callingPart, param->frame);
00774     if (parentPart)
00775        param->parent = parentPart->browserHostExtension();
00776     return;
00777   }
00778   BrowserHostExtension::virtual_hook( id, data );
00779 }
00780 
00781 
00782 // defined in khtml_part.cpp
00783 extern const int KDE_NO_EXPORT fastZoomSizes[];
00784 extern const int KDE_NO_EXPORT fastZoomSizeCount;
00785 
00786 // BCI: remove in KDE 4
00787 KHTMLZoomFactorAction::KHTMLZoomFactorAction( KHTMLPart *part, bool direction, const QString &text, const QString &icon, const QObject *receiver, const char *slot, QObject *parent, const char *name )
00788     : KAction( text, icon, 0, receiver, slot, parent, name )
00789 {
00790     init(part, direction);
00791 }
00792 
00793 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 )
00794     : KAction( text, icon, cut, receiver, slot, parent, name )
00795 {
00796     init(part, direction);
00797 }
00798 
00799 void KHTMLZoomFactorAction::init(KHTMLPart *part, bool direction)
00800 {
00801     m_direction = direction;
00802     m_part = part;
00803 
00804     m_popup = new QPopupMenu;
00805     m_popup->insertItem( i18n( "Default Font Size (100%)" ) );
00806 
00807     int m = m_direction ? 1 : -1;
00808     int ofs = fastZoomSizeCount / 2;       // take index of 100%
00809 
00810     // this only works if there is an odd number of elements in fastZoomSizes[]
00811     for ( int i = m; i != m*(ofs+1); i += m )
00812     {
00813         int num = i * m;
00814         QString numStr = QString::number( num );
00815         if ( num > 0 ) numStr.prepend( '+' );
00816 
00817         m_popup->insertItem( i18n( "%1%" ).arg( fastZoomSizes[ofs + i] ) );
00818     }
00819 
00820     connect( m_popup, SIGNAL( activated( int ) ), this, SLOT( slotActivated( int ) ) );
00821 }
00822 
00823 KHTMLZoomFactorAction::~KHTMLZoomFactorAction()
00824 {
00825     delete m_popup;
00826 }
00827 
00828 int KHTMLZoomFactorAction::plug( QWidget *w, int index )
00829 {
00830     int containerId = KAction::plug( w, index );
00831     if ( containerId == -1 || !w->inherits( "KToolBar" ) )
00832         return containerId;
00833 
00834     KToolBarButton *button = static_cast<KToolBar *>( w )->getButton( itemId( containerId ) );
00835     if ( !button )
00836         return containerId;
00837 
00838     button->setDelayedPopup( m_popup );
00839     return containerId;
00840 }
00841 
00842 void KHTMLZoomFactorAction::slotActivated( int id )
00843 {
00844     int idx = m_popup->indexOf( id );
00845 
00846     if (idx == 0)
00847         m_part->setZoomFactor(100);
00848     else
00849         m_part->setZoomFactor(fastZoomSizes[fastZoomSizeCount/2 + (m_direction ? 1 : -1)*idx]);
00850 }
00851 
00852 #include "khtml_ext.moc"
00853 
KDE Logo
This file is part of the documentation for khtml Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 30 10:22:18 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003