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