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 assert( m_editableFormWidget );
00144 if ( !m_editableFormWidget )
00145 return;
00146
00147 if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00148 static_cast<QLineEdit *>( &(*m_editableFormWidget) )->cut();
00149 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00150 static_cast<QTextEdit *>( &(*m_editableFormWidget) )->cut();
00151 }
00152
00153 void KHTMLPartBrowserExtension::copy()
00154 {
00155 if ( m_extensionProxy )
00156 {
00157 callExtensionProxyMethod( "copy()" );
00158 return;
00159 }
00160
00161 kdDebug( 6050 ) << "************! KHTMLPartBrowserExtension::copy()" << endl;
00162 if ( !m_editableFormWidget )
00163 {
00164
00165 QString text = m_part->selectedText();
00166 QClipboard *cb = QApplication::clipboard();
00167 disconnect( cb, SIGNAL( selectionChanged() ), m_part, SLOT( slotClearSelection() ) );
00168 cb->setText(text);
00169 connect( cb, SIGNAL( selectionChanged() ), m_part, SLOT( slotClearSelection() ) );
00170 }
00171 else
00172 {
00173 if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00174 static_cast<QLineEdit *>( &(*m_editableFormWidget) )->copy();
00175 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00176 static_cast<QTextEdit *>( &(*m_editableFormWidget) )->copy();
00177 }
00178 }
00179
00180 void KHTMLPartBrowserExtension::paste()
00181 {
00182 if ( m_extensionProxy )
00183 {
00184 callExtensionProxyMethod( "paste()" );
00185 return;
00186 }
00187
00188 assert( m_editableFormWidget );
00189 if ( !m_editableFormWidget )
00190 return;
00191
00192 if ( m_editableFormWidget->inherits( "QLineEdit" ) )
00193 static_cast<QLineEdit *>( &(*m_editableFormWidget) )->paste();
00194 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00195 static_cast<QTextEdit *>( &(*m_editableFormWidget) )->paste();
00196 }
00197
00198 void KHTMLPartBrowserExtension::callExtensionProxyMethod( const char *method )
00199 {
00200 if ( !m_extensionProxy )
00201 return;
00202
00203 int slot = m_extensionProxy->metaObject()->findSlot( method );
00204 if ( slot == -1 )
00205 return;
00206
00207 QUObject o[ 1 ];
00208 m_extensionProxy->qt_invoke( slot, o );
00209 }
00210
00211 void KHTMLPartBrowserExtension::updateEditActions()
00212 {
00213 if ( !m_editableFormWidget )
00214 {
00215 enableAction( "cut", false );
00216 enableAction( "paste", false );
00217 return;
00218 }
00219
00220
00221 #ifndef QT_NO_MIMECLIPBOARD // Handle minimalized versions of Qt Embedded
00222 QMimeSource *data = QApplication::clipboard()->data();
00223 enableAction( "paste", data->provides( "text/plain" ) );
00224 #else
00225 QString data=QApplication::clipboard()->text();
00226 enableAction( "paste", data.contains("://"));
00227 #endif
00228
00229 bool hasSelection = false;
00230
00231 if ( m_editableFormWidget->inherits( "QLineEdit" ) ) {
00232 hasSelection = static_cast<QLineEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00233 }
00234 else if ( m_editableFormWidget->inherits( "QTextEdit" ) )
00235 {
00236 hasSelection = static_cast<QTextEdit *>( &(*m_editableFormWidget) )->hasSelectedText();
00237 }
00238
00239 enableAction( "copy", hasSelection );
00240 enableAction( "cut", hasSelection );
00241 }
00242
00243 void KHTMLPartBrowserExtension::extensionProxyActionEnabled( const char *action, bool enable )
00244 {
00245
00246 if ( strcmp( action, "cut" ) == 0 ||
00247 strcmp( action, "copy" ) == 0 ||
00248 strcmp( action, "paste" ) == 0 ) {
00249 enableAction( action, enable );
00250 }
00251 }
00252
00253 void KHTMLPartBrowserExtension::reparseConfiguration()
00254 {
00255 m_part->reparseConfiguration();
00256 }
00257
00258 void KHTMLPartBrowserExtension::print()
00259 {
00260 m_part->view()->print();
00261 }
00262
00263 class KHTMLPopupGUIClient::KHTMLPopupGUIClientPrivate
00264 {
00265 public:
00266 KHTMLPart *m_khtml;
00267 KURL m_url;
00268 KURL m_imageURL;
00269 KAction *m_paPrintFrame;
00270 KAction *m_paSaveLinkAs;
00271 KAction *m_paSaveImageAs;
00272 KAction *m_paCopyLinkLocation;
00273 KAction *m_paStopAnimations;
00274 KAction *m_paCopyImageLocation;
00275 KAction *m_paViewImage;
00276 KAction *m_paReloadFrame;
00277 KAction *m_paViewFrameSource;
00278 KAction *m_paViewFrameInfo;
00279 };
00280
00281
00282 KHTMLPopupGUIClient::KHTMLPopupGUIClient( KHTMLPart *khtml, const QString &doc, const KURL &url )
00283 {
00284 d = new KHTMLPopupGUIClientPrivate;
00285 d->m_khtml = khtml;
00286 d->m_url = url;
00287
00288 setInstance( khtml->instance() );
00289
00290 actionCollection()->insert( khtml->actionCollection()->action( "selectAll" ) );
00291 actionCollection()->insert( khtml->actionCollection()->action( "security" ) );
00292 actionCollection()->insert( khtml->actionCollection()->action( "viewDocumentSource" ) );
00293
00294
00295 if ( khtml->parentPart() )
00296 {
00297 d->m_paReloadFrame = new KAction( i18n( "Reload Frame" ), 0, this, SLOT( slotReloadFrame() ),
00298 actionCollection(), "reloadframe" );
00299 d->m_paViewFrameSource = new KAction( i18n( "View Frame Source" ), 0, d->m_khtml, SLOT( slotViewDocumentSource() ),
00300 actionCollection(), "viewFrameSource" );
00301 d->m_paViewFrameInfo = new KAction( i18n( "View Frame Information" ), 0, d->m_khtml, SLOT( slotViewPageInfo() ), actionCollection(), "viewPageInfo" );
00302
00303
00304
00305 d->m_paPrintFrame = new KAction( i18n( "Print Frame..." ), "fileprint", 0, d->m_khtml->browserExtension(), SLOT( print() ), actionCollection(), "printFrame" );
00306 } else {
00307 actionCollection()->insert( khtml->actionCollection()->action( "viewPageInfo" ) );
00308 }
00309
00310 actionCollection()->insert( khtml->actionCollection()->action( "setEncoding" ) );
00311
00312 if ( !url.isEmpty() )
00313 {
00314 d->m_paSaveLinkAs = new KAction( i18n( "&Save Link As..." ), 0, this, SLOT( slotSaveLinkAs() ),
00315 actionCollection(), "savelinkas" );
00316 d->m_paCopyLinkLocation = new KAction( i18n( "Copy Link Location" ), 0, this, SLOT( slotCopyLinkLocation() ),
00317 actionCollection(), "copylinklocation" );
00318 }
00319
00320 d->m_paStopAnimations = new KAction( i18n( "Stop Animations" ), 0, this, SLOT( slotStopAnimations() ),
00321 actionCollection(), "stopanimations" );
00322
00323 DOM::Element e;
00324 e = khtml->nodeUnderMouse();
00325
00326 if ( !e.isNull() && (e.elementId() == ID_IMG ||
00327 (e.elementId() == ID_INPUT && !static_cast<DOM::HTMLInputElement>(e).src().isEmpty())))
00328 {
00329 if ( e.elementId() == ID_IMG )
00330 d->m_imageURL = KURL( static_cast<DOM::HTMLImageElement>( e ).src().string() );
00331 else
00332 d->m_imageURL = KURL( static_cast<DOM::HTMLInputElement>( e ).src().string() );
00333 d->m_paSaveImageAs = new KAction( i18n( "Save Image As..." ), 0, this, SLOT( slotSaveImageAs() ),
00334 actionCollection(), "saveimageas" );
00335 d->m_paCopyImageLocation = new KAction( i18n( "Copy Image Location" ), 0, this, SLOT( slotCopyImageLocation() ),
00336 actionCollection(), "copyimagelocation" );
00337 QString name = KStringHandler::csqueeze(d->m_imageURL.fileName()+d->m_imageURL.query(), 25);
00338 d->m_paViewImage = new KAction( i18n( "View Image (%1)" ).arg(name.replace(QRegExp("&"), "&&")), 0, this, SLOT( slotViewImage() ),
00339 actionCollection(), "viewimage" );
00340 }
00341
00342 setXML( doc );
00343 setDOMDocument( QDomDocument(), true );
00344
00345 QDomElement menu = domDocument().documentElement().namedItem( "Menu" ).toElement();
00346
00347 if ( actionCollection()->count() > 0 )
00348 menu.insertBefore( domDocument().createElement( "separator" ), menu.firstChild() );
00349 }
00350
00351 KHTMLPopupGUIClient::~KHTMLPopupGUIClient()
00352 {
00353 delete d;
00354 }
00355
00356 void KHTMLPopupGUIClient::slotSaveLinkAs()
00357 {
00358 KIO::MetaData metaData;
00359 metaData["referrer"] = d->m_khtml->referrer();
00360 saveURL( d->m_khtml->widget(), i18n( "Save Link As" ), d->m_url, metaData );
00361 }
00362
00363 void KHTMLPopupGUIClient::slotSaveImageAs()
00364 {
00365 KIO::MetaData metaData;
00366 metaData["referrer"] = d->m_khtml->referrer();
00367 saveURL( d->m_khtml->widget(), i18n( "Save Image As" ), d->m_imageURL, metaData );
00368 }
00369
00370 void KHTMLPopupGUIClient::slotCopyLinkLocation()
00371 {
00372 KURL::List lst;
00373 lst.append( d->m_url );
00374 #ifndef QT_NO_MIMECLIPBOARD
00375
00376 QApplication::clipboard()->setSelectionMode(true);
00377 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00378 QApplication::clipboard()->setSelectionMode(false);
00379 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00380 #else
00381 QApplication::clipboard()->setText( d->m_url.url() );
00382 #endif
00383 }
00384
00385 void KHTMLPopupGUIClient::slotStopAnimations()
00386 {
00387 d->m_khtml->stopAnimations();
00388 }
00389
00390 void KHTMLPopupGUIClient::slotCopyImageLocation()
00391 {
00392 KURL::List lst;
00393 lst.append( d->m_imageURL );
00394 #ifndef QT_NO_MIMECLIPBOARD
00395
00396 QApplication::clipboard()->setSelectionMode(true);
00397 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00398 QApplication::clipboard()->setSelectionMode(false);
00399 QApplication::clipboard()->setData( KURLDrag::newDrag( lst ) );
00400 #else
00401 QApplication::clipboard()->setText(d->m_imageURL.url());
00402 #endif
00403 }
00404
00405 void KHTMLPopupGUIClient::slotViewImage()
00406 {
00407 d->m_khtml->browserExtension()->createNewWindow(d->m_imageURL.url());
00408 }
00409
00410 void KHTMLPopupGUIClient::slotReloadFrame()
00411 {
00412 KParts::URLArgs args( d->m_khtml->browserExtension()->urlArgs() );
00413 args.reload = true;
00414
00415 d->m_khtml->closeURL();
00416 d->m_khtml->browserExtension()->setURLArgs( args );
00417 d->m_khtml->openURL( d->m_khtml->url() );
00418 }
00419
00420 void KHTMLPopupGUIClient::saveURL( QWidget *parent, const QString &caption,
00421 const KURL &url,
00422 const QMap<QString, QString> &metadata,
00423 const QString &filter, long cacheId,
00424 const QString & suggestedFilename )
00425 {
00426 QString name = QString::fromLatin1( "index.html" );
00427 if ( !suggestedFilename.isEmpty() )
00428 name = suggestedFilename;
00429 else if ( !url.fileName().isEmpty() )
00430 name = url.fileName();
00431
00432 KURL destURL;
00433 int query;
00434 do {
00435 query = KMessageBox::Yes;
00436 destURL = KFileDialog::getSaveURL( name, filter, parent, caption );
00437 if( destURL.isLocalFile() )
00438 {
00439 QFileInfo info( destURL.path() );
00440 if( info.exists() )
00441 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" ) );
00442 }
00443 } while ( query == KMessageBox::Cancel );
00444
00445 if ( !destURL.isMalformed() )
00446 saveURL(url, destURL, metadata, cacheId);
00447 }
00448
00449 void KHTMLPopupGUIClient::saveURL( const KURL &url, const KURL &destURL,
00450 const QMap<QString, QString> &metadata,
00451 long cacheId )
00452 {
00453 if ( !destURL.isMalformed() )
00454 {
00455 bool saved = false;
00456 if (KHTMLPageCache::self()->isValid(cacheId))
00457 {
00458 if (destURL.isLocalFile())
00459 {
00460 KSaveFile destFile(destURL.path());
00461 if (destFile.status() == 0)
00462 {
00463 KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00464 saved = true;
00465 }
00466 }
00467 else
00468 {
00469
00470 KTempFile destFile;
00471 if (destFile.status() == 0)
00472 {
00473 KHTMLPageCache::self()->saveData(cacheId, destFile.dataStream());
00474 destFile.close();
00475 KURL url2 = KURL();
00476 url2.setPath(destFile.name());
00477 KIO::move(url2, destURL);
00478 saved = true;
00479 }
00480 }
00481 }
00482 if(!saved)
00483 {
00484
00485
00486
00487 KConfig *cfg = new KConfig("konquerorrc", false, false);
00488 cfg->setGroup("HTML Settings");
00489 QString downloadManger=cfg->readEntry("DownloadManager");
00490 if (!downloadManger.isEmpty())
00491 {
00492
00493 kdDebug(1000) << "Using: "<<downloadManger <<" as Download Manager" <<endl;
00494 QString cmd=KStandardDirs::findExe(downloadManger);
00495 if (cmd.isEmpty())
00496 {
00497 QString errMsg=i18n("The Download Manager (%1) could not be found in your $PATH ").arg(downloadManger);
00498 QString errMsgEx= i18n("Try to reinstall it \n\nThe integration with Konqueror will be disabled!");
00499 KMessageBox::detailedSorry(0,errMsg,errMsgEx);
00500 cfg->writeEntry("DownloadManager",QString::null);
00501 cfg->sync ();
00502 }
00503 else
00504 {
00505 KURL cleanDest = destURL;
00506 cleanDest.setPass( QString::null );
00507 cmd += " " + KProcess::quote(url.url()) + " " +
00508 KProcess::quote(cleanDest.url());
00509 kdDebug(1000) << "Calling command "<<cmd<<endl;
00510 KRun::runCommand(cmd);
00511 }
00512 }
00513 else
00514 {
00515 KIO::Job *job = KIO::copy( url, destURL );
00516 job->setMetaData(metadata);
00517 job->addMetaData("MaxCacheSize", "0");
00518 job->setAutoErrorHandlingEnabled( true );
00519 }
00520 delete cfg;
00521 }
00522 }
00523 }
00524
00525 KHTMLPartBrowserHostExtension::KHTMLPartBrowserHostExtension( KHTMLPart *part )
00526 : KParts::BrowserHostExtension( part )
00527 {
00528 m_part = part;
00529 }
00530
00531 KHTMLPartBrowserHostExtension::~KHTMLPartBrowserHostExtension()
00532 {
00533 }
00534
00535 QStringList KHTMLPartBrowserHostExtension::frameNames() const
00536 {
00537 return m_part->frameNames();
00538 }
00539
00540 const QPtrList<KParts::ReadOnlyPart> KHTMLPartBrowserHostExtension::frames() const
00541 {
00542 return m_part->frames();
00543 }
00544
00545 bool KHTMLPartBrowserHostExtension::openURLInFrame( const KURL &url, const KParts::URLArgs &urlArgs )
00546 {
00547 return m_part->openURLInFrame( url, urlArgs );
00548 }
00549
00550 KHTMLZoomFactorAction::KHTMLZoomFactorAction( KHTMLPart *part, bool direction, const QString &text, const QString &icon, const QObject *receiver, const char *slot, QObject *parent, const char *name )
00551 : KAction( text, icon, 0, receiver, slot, parent, name )
00552 {
00553 m_direction = direction;
00554 m_part = part;
00555
00556 m_popup = new QPopupMenu;
00557 m_popup->insertItem( i18n( "Default Font Size" ) );
00558
00559 int m = m_direction ? 1 : -1;
00560
00561 for ( int i = 1; i < 5; ++i )
00562 {
00563 int num = i * m;
00564 QString numStr = QString::number( num );
00565 if ( num > 0 ) numStr.prepend( '+' );
00566
00567 m_popup->insertItem( i18n( "Font Size %1" ).arg( numStr ) );
00568 }
00569
00570 connect( m_popup, SIGNAL( activated( int ) ), this, SLOT( slotActivated( int ) ) );
00571 }
00572
00573 KHTMLZoomFactorAction::~KHTMLZoomFactorAction()
00574 {
00575 delete m_popup;
00576 }
00577
00578 int KHTMLZoomFactorAction::plug( QWidget *w, int index )
00579 {
00580 int containerId = KAction::plug( w, index );
00581 if ( containerId == -1 || !w->inherits( "KToolBar" ) )
00582 return containerId;
00583
00584 KToolBarButton *button = static_cast<KToolBar *>( w )->getButton( menuId( containerId ) );
00585 if ( !button )
00586 return containerId;
00587
00588 button->setDelayedPopup( m_popup );
00589 return containerId;
00590 }
00591
00592 void KHTMLZoomFactorAction::slotActivated( int id )
00593 {
00594 int idx = m_popup->indexOf( id );
00595
00596 if (idx == 0)
00597 m_part->setZoomFactor(100);
00598 else
00599 m_part->setZoomFactor(m_part->zoomFactor() + (m_direction ? 10 : -10) * idx);
00600 }
00601
00602 #include "khtml_ext.moc"
00603