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