00001
00002 #include <assert.h>
00003 #include "khtml_ext.h"
00004 #include "khtmlview.h"
00005 #include "khtml_pagecache.h"
00006 #include "rendering/render_form.h"
00007 #include "dom/html_form.h"
00008 #include "dom/html_image.h"
00009 #include <qclipboard.h>
00010 #include <qfileinfo.h>
00011 #include <qpopupmenu.h>
00012 #include <qmetaobject.h>
00013 #include <private/qucomextra_p.h>
00014
00015 #include <kdebug.h>
00016 #include <klocale.h>
00017 #include <kfiledialog.h>
00018 #include <kio/job.h>
00019 #include <kprocess.h>
00020 #include <ktoolbarbutton.h>
00021 #include <ktoolbar.h>
00022 #include <ksavefile.h>
00023 #include <kurldrag.h>
00024 #include <kstringhandler.h>
00025 #include <kapplication.h>
00026 #include <kmessagebox.h>
00027 #include <kstandarddirs.h>
00028 #include <krun.h>
00029
00030 #include "dom/dom_element.h"
00031 #include "misc/htmltags.h"
00032
00033 KHTMLPartBrowserExtension::KHTMLPartBrowserExtension( KHTMLPart *parent, const char *name )
00034 : KParts::BrowserExtension( parent, name )
00035 {
00036 m_part = parent;
00037 setURLDropHandlingEnabled( true );
00038
00039 enableAction( "cut", false );
00040 enableAction( "copy", false );
00041 enableAction( "paste", false );
00042
00043 m_connectedToClipboard = false;
00044 }
00045
00046 int KHTMLPartBrowserExtension::xOffset()
00047 {
00048 return m_part->view()->contentsX();
00049 }
00050
00051 int KHTMLPartBrowserExtension::yOffset()
00052 {
00053 return m_part->view()->contentsY();
00054 }
00055
00056 void KHTMLPartBrowserExtension::saveState( QDataStream &stream )
00057 {
00058 kdDebug( 6050 ) << "saveState!" << endl;
00059 m_part->saveState( stream );
00060 }
00061
00062 void KHTMLPartBrowserExtension::restoreState( QDataStream &stream )
00063 {
00064 kdDebug( 6050 ) << "restoreState!" << endl;
00065 m_part->restoreState( stream );
00066 }
00067
00068 void KHTMLPartBrowserExtension::editableWidgetFocused( QWidget *widget )
00069 {
00070 m_editableFormWidget = widget;
00071 updateEditActions();
00072
00073 if ( !m_connectedToClipboard && m_editableFormWidget )
00074 {
00075 connect( QApplication::clipboard(), SIGNAL( dataChanged() ),
00076 this, SLOT( updateEditActions() ) );
00077
00078 if ( m_editableFormWidget->inherits( "QLineEdit" ) || m_editableFormWidget->inherits( "QTextEdit" ) )
00079 connect( m_editableFormWidget, SIGNAL( selectionChanged() ),
00080 this, SLOT( updateEditActions() ) );
00081
00082 m_connectedToClipboard = true;
00083 }
00084 }
00085
00086 void KHTMLPartBrowserExtension::editableWidgetBlurred( QWidget *widget )
00087 {
00088 QWidget *oldWidget = m_editableFormWidget;
00089
00090 m_editableFormWidget = widget;
00091 enableAction( "cut", false );
00092 enableAction( "paste", false );
00093 m_part->emitSelectionChanged();
00094
00095 if ( m_connectedToClipboard )
00096 {
00097 disconnect( QApplication::clipboard(), SIGNAL( dataChanged() ),
00098 this, SLOT( updateEditActions() ) );
00099
00100 if ( oldWidget )
00101 {
00102 if ( oldWidget->inherits( "QLineEdit" ) || oldWidget->inherits( "QTextEdit" ) )
00103 disconnect( oldWidget, SIGNAL( selectionChanged() ),
00104 this, SLOT( updateEditActions() ) );
00105 }
00106
00107 m_connectedToClipboard = false;
00108 }
00109 }
00110
00111 void KHTMLPartBrowserExtension::setExtensionProxy( KParts::BrowserExtension *proxy )
00112 {
00113 if ( m_extensionProxy )
00114 disconnect( m_extensionProxy, SIGNAL( enableAction( const char *, bool ) ),
00115 this, SLOT( extensionProxyActionEnabled( const char *, bool ) ) );
00116
00117 m_extensionProxy = proxy;
00118
00119 if ( m_extensionProxy )
00120 {
00121 connect( m_extensionProxy, SIGNAL( enableAction( const char *, bool ) ),
00122 this, SLOT( extensionProxyActionEnabled( const char *, bool ) ) );
00123
00124 enableAction( "cut", m_extensionProxy->isActionEnabled( "cut" ) );
00125 enableAction( "copy", m_extensionProxy->isActionEnabled( "copy" ) );
00126 enableAction( "paste", m_extensionProxy->isActionEnabled( "paste" ) );
00127 }
00128 else
00129 {
00130 updateEditActions();
00131 enableAction( "copy", false );
00132 }
00133 }
00134
00135 void KHTMLPartBrowserExtension::cut()
00136 {
00137 if ( m_extensionProxy )
00138 {
00139 callExtensionProxyMethod( "cut()" );
00140 return;
00141 }
00142
00143 if ( !m_editableFormWidget )
00144 return;
00145
00146 if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00147 static_cast<QLineEdit *>( &(*m_editableFormWidget) )->cut();
00148 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00149 static_cast<QTextEdit *>( &(*m_editableFormWidget) )->cut();
00150 }
00151
00152 void KHTMLPartBrowserExtension::copy()
00153 {
00154 if ( m_extensionProxy )
00155 {
00156 callExtensionProxyMethod( "copy()" );
00157 return;
00158 }
00159
00160 kdDebug( 6050 ) << "************! KHTMLPartBrowserExtension::copy()" << endl;
00161 if ( !m_editableFormWidget )
00162 {
00163
00164 QString text = m_part->selectedText();
00165 QClipboard *cb = QApplication::clipboard();
00166 disconnect( cb, SIGNAL( selectionChanged() ), m_part, SLOT( slotClearSelection() ) );
00167 cb->setText(text);
00168 connect( cb, SIGNAL( selectionChanged() ), m_part, SLOT( slotClearSelection() ) );
00169 }
00170 else
00171 {
00172 if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00173 static_cast<QLineEdit *>( &(*m_editableFormWidget) )->copy();
00174 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00175 static_cast<QTextEdit *>( &(*m_editableFormWidget) )->copy();
00176 }
00177 }
00178
00179 void KHTMLPartBrowserExtension::paste()
00180 {
00181 if ( m_extensionProxy )
00182 {
00183 callExtensionProxyMethod( "paste()" );
00184 return;
00185 }
00186
00187 if ( !m_editableFormWidget )
00188 return;
00189
00190 if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00191 static_cast<QLineEdit *>( &(*m_editableFormWidget) )->paste();
00192 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00193 static_cast<QTextEdit *>( &(*m_editableFormWidget) )->paste();
00194 }
00195
00196 void KHTMLPartBrowserExtension::callExtensionProxyMethod( const char *method )
00197 {
00198 if ( !m_extensionProxy )
00199 return;
00200
00201 int slot = m_extensionProxy->metaObject()->findSlot( method );
00202 if ( slot == -1 )
00203 return;
00204
00205 QUObject o[ 1 ];
00206 m_extensionProxy->qt_invoke( slot, o );
00207 }
00208
00209 void KHTMLPartBrowserExtension::updateEditActions()
00210 {
00211 if ( !m_editableFormWidget )
00212 {
00213 enableAction( "cut", false );
00214 enableAction( "paste", false );
00215 return;
00216 }
00217
00218
00219 #ifndef QT_NO_MIMECLIPBOARD // Handle minimalized versions of Qt Embedded
00220 QMimeSource *data = QApplication::clipboard()->data();
00221 enableAction( "paste", data->provides( "text/plain" ) );
00222 #else
00223 QString data=QApplication::clipboard()->text();
00224 enableAction( "paste", data.contains("://"));
00225 #endif
00226
00227 bool hasSelection = false;
00228
00229 if ( m_editableFormWidget->inherits( "QLineEdit" ) ) {
00230 hasSelection = static_cast<QLineEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00231 }
00232 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00233 {
00234 hasSelection = static_cast<QTextEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00235 }
00236
00237 enableAction( "copy", hasSelection );
00238 enableAction( "cut", hasSelection );
00239 }
00240
00241 void KHTMLPartBrowserExtension::extensionProxyActionEnabled( const char *action, bool enable )
00242 {
00243
00244 if ( strcmp( action, "cut" ) == 0 ||
00245 strcmp( action, "copy" ) == 0 ||
00246 strcmp( action, "paste" ) == 0 ) {
00247 enableAction( action, enable );
00248 }
00249 }
00250
00251 void KHTMLPartBrowserExtension::reparseConfiguration()
00252 {
00253 m_part->reparseConfiguration();
00254 }
00255
00256 void KHTMLPartBrowserExtension::print()
00257 {
00258 m_part->view()->print();
00259 }
00260
00261 class KHTMLPopupGUIClient::KHTMLPopupGUIClientPrivate
00262 {
00263 public:
00264 KHTMLPart *m_khtml;
00265 KURL m_url;
00266 KURL m_imageURL;
00267 KAction *m_paPrintFrame;
00268 KAction *m_paSaveLinkAs;
00269 KAction *m_paSaveImageAs;
00270 KAction *m_paCopyLinkLocation;
00271 KAction *m_paStopAnimations;
00272 KAction *m_paCopyImageLocation;
00273 KAction *m_paViewImage;
00274 KAction *m_paReloadFrame;
00275 KAction *m_paViewFrameSource;
00276 KAction *m_paViewFrameInfo;
00277 };
00278
00279
00280 KHTMLPopupGUIClient::KHTMLPopupGUIClient( KHTMLPart *khtml, const QString &doc, const KURL &url )
00281 {
00282 d = new KHTMLPopupGUIClientPrivate;
00283 d->m_khtml = khtml;
00284 d->m_url = url;
00285
00286 setInstance( khtml->instance() );
00287
00288 actionCollection()->insert( khtml->actionCollection()->action( "selectAll" ) );
00289 actionCollection()->insert( khtml->actionCollection()->action( "security" ) );
00290 actionCollection()->insert( khtml->actionCollection()->action( "viewDocumentSource" ) );
00291
00292
00293 if ( khtml->parentPart() )
00294 {
00295 d->m_paReloadFrame = new KAction( i18n( "Reload Frame" ), 0, this, SLOT( slotReloadFrame() ),
00296 actionCollection(), "reloadframe" );
00297 d->m_paViewFrameSource = new KAction( i18n( "View Frame Source" ), 0, d->m_khtml, SLOT( slotViewDocumentSource() ),
00298 actionCollection(), "viewFrameSource" );
00299 d->m_paViewFrameInfo = new KAction( i18n( "View Frame Information" ), 0, d->m_khtml, SLOT( slotViewPageInfo() ), actionCollection(), "viewPageInfo" );
00300
00301
00302
00303 d->m_paPrintFrame = new KAction( i18n( "Print Frame..." ), "fileprint", 0, d->m_khtml->browserExtension(), SLOT( print() ), actionCollection(), "printFrame" );
00304 } else {
00305 actionCollection()->insert( khtml->actionCollection()->action( "viewPageInfo" ) );
00306 }
00307
00308 actionCollection()->insert( khtml->actionCollection()->action( "setEncoding" ) );
00309
00310 if ( !url.isEmpty() )
00311 {
00312 d->m_paSaveLinkAs = new KAction( i18n( "&Save Link As..." ), 0, this, SLOT( slotSaveLinkAs() ),
00313 actionCollection(), "savelinkas" );
00314 d->m_paCopyLinkLocation = new KAction( i18n( "Copy Link Location" ), 0, this, SLOT( slotCopyLinkLocation() ),
00315 actionCollection(), "copylinklocation" );
00316 }
00317
00318 d->m_paStopAnimations = new KAction( i18n( "Stop Animations" ), 0, this, SLOT( slotStopAnimations() ),
00319 actionCollection(), "stopanimations" );
00320
00321 DOM::Element e;
00322 e = khtml->nodeUnderMouse();
00323
00324 if ( !e.isNull() && (e.elementId() == ID_IMG ||
00325 (e.elementId() == ID_INPUT && !static_cast<DOM::HTMLInputElement>(e).src().isEmpty())))
00326 {
00327 if ( e.elementId() == ID_IMG )
00328 d->m_imageURL = KURL( static_cast<DOM::HTMLImageElement>( e ).src().string() );
00329 else
00330 d->m_imageURL = KURL( static_cast<DOM::HTMLInputElement>( e ).src().string() );
00331 d->m_paSaveImageAs = new KAction( i18n( "Save Image As..." ), 0, this, SLOT( slotSaveImageAs() ),
00332 actionCollection(), "saveimageas" );
00333 d->m_paCopyImageLocation = new KAction( i18n( "Copy Image Location" ), 0, this, SLOT( slotCopyImageLocation() ),
00334 actionCollection(), "copyimagelocation" );
00335 QString name = KStringHandler::csqueeze(d->m_imageURL.fileName()+d->m_imageURL.query(), 25);
00336 d->m_paViewImage = new KAction( i18n( "View Image (%1)" ).arg(name.replace(QRegExp("&"), "&&")), 0, this, SLOT( slotViewImage() ),
00337 actionCollection(), "viewimage" );
00338 }
00339
00340 setXML( doc );
00341 setDOMDocument( QDomDocument(), true );
00342
00343 QDomElement menu = domDocument().documentElement().namedItem( "Menu" ).toElement();
00344
00345 if ( actionCollection()->count() > 0 )
00346 menu.insertBefore( domDocument().createElement( "separator" ), menu.firstChild() );
00347 }
00348
00349 KHTMLPopupGUIClient::~KHTMLPopupGUIClient()
00350 {
00351 delete d;
00352 }
00353
00354 void KHTMLPopupGUIClient::slotSaveLinkAs()
00355 {
00356 KIO::MetaData metaData;
00357 metaData["referrer"] = d->m_khtml->referrer();
00358 saveURL( d->m_khtml->widget(), i18n( "Save Link As" ), d->m_url, metaData );
00359 }
00360
00361 void KHTMLPopupGUIClient::slotSaveImageAs()
00362 {
00363 KIO::MetaData metaData;
00364 metaData["referrer"] = d->m_khtml->referrer();
00365 saveURL( d->m_khtml->widget(), i18n( "Save Image As" ), d->m_imageURL, metaData );
00366 }
00367
00368 void KHTMLPopupGUIClient::slotCopyLinkLocation()
00369 {
00370 KURL::List lst;
00371 lst.append( d->m_url );
00372 #ifndef QT_NO_MIMECLIPBOARD
00373
00374 QApplication::clipboard()->setSelectionMode(true);
00375 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00376 QApplication::clipboard()->setSelectionMode(false);
00377 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00378 #else
00379 QApplication::clipboard()->setText( d->m_url.url() );
00380 #endif
00381 }
00382
00383 void KHTMLPopupGUIClient::slotStopAnimations()
00384 {
00385 d->m_khtml->stopAnimations();
00386 }
00387
00388 void KHTMLPopupGUIClient::slotCopyImageLocation()
00389 {
00390 KURL::List lst;
00391 lst.append( d->m_imageURL );
00392 #ifndef QT_NO_MIMECLIPBOARD
00393
00394 QApplication::clipboard()->setSelectionMode(true);
00395 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00396 QApplication::clipboard()->setSelectionMode(false);
00397 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00398 #else
00399 QApplication::clipboard()->setText(d->m_imageURL.url());
00400 #endif
00401 }
00402
00403 void KHTMLPopupGUIClient::slotViewImage()
00404 {
00405 d->m_khtml->browserExtension()->createNewWindow(d->m_imageURL.url());
00406 }
00407
00408 void KHTMLPopupGUIClient::slotReloadFrame()
00409 {
00410 KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00411 args.reload = true;
00412
00413 d->m_khtml->closeURL();
00414 d->m_khtml->browserExtension()->setURLArgs( args );
00415 d->m_khtml->openURL( d->m_khtml->url() );
00416 }
00417
00418 void KHTMLPopupGUIClient::saveURL( QWidget *parent, const QString &caption,
00419 const KURL &url,
00420 const QMap<QString, QString> &metadata,
00421 const QString &filter, long cacheId,
00422 const QString & suggestedFilename )
00423 {
00424 QString name = QString::fromLatin1( "index.html" );
00425 if ( !suggestedFilename.isEmpty() )
00426 name = suggestedFilename;
00427 else if ( !url.fileName().isEmpty() )
00428 name = url.fileName();
00429
00430 KURL destURL;
00431 int query;
00432 do {
00433 query = KMessageBox::Yes;
00434 destURL = KFileDialog::getSaveURL( name, filter, parent, caption );
00435 if( destURL.isLocalFile() )
00436 {
00437 QFileInfo info( destURL.path() );
00438 if( info.exists() )
00439 query = KMessageBox::warningContinueCancel( parent, i18n( "A file named \"%1\" already exists. " "Are you sure you want to overwrite it?" ).arg( info.fileName() ), i18n( "Overwrite File?" ), i18n( "Overwrite" ) );
00440 }
00441 } while ( query == KMessageBox::Cancel );
00442
00443 if ( !destURL.isMalformed() )
00444 saveURL(url, destURL, metadata, cacheId);
00445 }
00446
00447 void KHTMLPopupGUIClient::saveURL( const KURL &url, const KURL &destURL,
00448 const QMap<QString, QString> &metadata,
00449 long cacheId )
00450 {
00451 if ( !destURL.isMalformed() )
00452 {
00453 bool saved = false;
00454 if (KHTMLPageCache::self()->isValid(cacheId))
00455 {
00456 if (destURL.isLocalFile())
00457 {
00458 KSaveFile destFile(destURL.path());
00459 if (destFile.status() == 0)
00460 {
00461 KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00462 saved = true;
00463 }
00464 }
00465 else
00466 {
00467
00468 KTempFile destFile;
00469 if (destFile.status() == 0)
00470 {
00471 KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00472 destFile.close();
00473 KURL url2 = KURL();
00474 url2.setPath(destFile.name());
00475 KIO::move(url2, destURL);
00476 saved = true;
00477 }
00478 }
00479 }
00480 if(!saved)
00481 {
00482
00483
00484
00485 KConfig *cfg = new KConfig("konquerorrc", false, false);
00486 cfg->setGroup("HTML Settings");
00487 QString downloadManger=cfg->readPathEntry("DownloadManager");
00488 if (!downloadManger.isEmpty())
00489 {
00490
00491 kdDebug(1000) << "Using: "<<downloadManger <<" as Download Manager" <<endl;
00492 QString cmd=KStandardDirs::findExe(downloadManger);
00493 if (cmd.isEmpty())
00494 {
00495 QString errMsg=i18n("The Download Manager (%1) could not be found in your $PATH ").arg(downloadManger);
00496 QString errMsgEx= i18n("Try to reinstall it \n\nThe integration with Konqueror will be disabled!");
00497 KMessageBox::detailedSorry(0,errMsg,errMsgEx);
00498 cfg->writeEntry("DownloadManager",QString::null);
00499 cfg->sync ();
00500 }
00501 else
00502 {
00503 KURL cleanDest = destURL;
00504 cleanDest.setPass( QString::null );
00505 cmd += " " + KProcess::quote(url.url()) + " " +
00506 KProcess::quote(cleanDest.url());
00507 kdDebug(1000) << "Calling command "<<cmd<<endl;
00508 KRun::runCommand(cmd);
00509 }
00510 }
00511 else
00512 {
00513 KIO::Job *job = KIO::copy( url, destURL );
00514 job->setMetaData(metadata);
00515 job->addMetaData("MaxCacheSize", "0");
00516 job->addMetaData("cache", "cache");
00517 job->setAutoErrorHandlingEnabled( true );
00518 }
00519 delete cfg;
00520 }
00521 }
00522 }
00523
00524 KHTMLPartBrowserHostExtension::KHTMLPartBrowserHostExtension( KHTMLPart *part )
00525 : KParts::BrowserHostExtension( part )
00526 {
00527 m_part = part;
00528 }
00529
00530 KHTMLPartBrowserHostExtension::~KHTMLPartBrowserHostExtension()
00531 {
00532 }
00533
00534 QStringList KHTMLPartBrowserHostExtension::frameNames() const
00535 {
00536 return m_part->frameNames();
00537 }
00538
00539 const QPtrList<KParts::ReadOnlyPart> KHTMLPartBrowserHostExtension::frames() const
00540 {
00541 return m_part->frames();
00542 }
00543
00544 bool KHTMLPartBrowserHostExtension::openURLInFrame( const KURL &url, const KParts::URLArgs &urlArgs )
00545 {
00546 return m_part->openURLInFrame( url, urlArgs );
00547 }
00548
00549 KHTMLZoomFactorAction::KHTMLZoomFactorAction( KHTMLPart *part, bool direction, const QString &text, const QString &icon, const QObject *receiver, const char *slot, QObject *parent, const char *name )
00550 : KAction( text, icon, 0, receiver, slot, parent, name )
00551 {
00552 m_direction = direction;
00553 m_part = part;
00554
00555 m_popup = new QPopupMenu;
00556 m_popup->insertItem( i18n( "Default Font Size" ) );
00557
00558 int m = m_direction ? 1 : -1;
00559
00560 for ( int i = 1; i < 5; ++i )
00561 {
00562 int num = i * m;
00563 QString numStr = QString::number( num );
00564 if ( num > 0 ) numStr.prepend( '+' );
00565
00566 m_popup->insertItem( i18n( "Font Size %1" ).arg( numStr ) );
00567 }
00568
00569 connect( m_popup, SIGNAL( activated( int ) ), this, SLOT( slotActivated( int ) ) );
00570 }
00571
00572 KHTMLZoomFactorAction::~KHTMLZoomFactorAction()
00573 {
00574 delete m_popup;
00575 }
00576
00577 int KHTMLZoomFactorAction::plug( QWidget *w, int index )
00578 {
00579 int containerId = KAction::plug( w, index );
00580 if ( containerId == -1 || !w->inherits( "KToolBar" ) )
00581 return containerId;
00582
00583 KToolBarButton *button = static_cast<KToolBar *>( w )->getButton( menuId( containerId ) );
00584 if ( !button )
00585 return containerId;
00586
00587 button->setDelayedPopup( m_popup );
00588 return containerId;
00589 }
00590
00591 void KHTMLZoomFactorAction::slotActivated( int id )
00592 {
00593 int idx = m_popup->indexOf( id );
00594
00595 if (idx == 0)
00596 m_part->setZoomFactor(100);
00597 else
00598 m_part->setZoomFactor(m_part->zoomFactor() + (m_direction ? 10 : -10) * idx);
00599 }
00600
00601 #include "khtml_ext.moc"
00602