akregator/src

viewer.cpp

00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2004 Teemu Rytilahti <tpr@d5k.net>
00005                   2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <kaction.h>
00027 #include <kapplication.h>
00028 #include <kfiledialog.h>
00029 #include <khtmlview.h>
00030 #include <kiconloader.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <kpopupmenu.h>
00034 #include <kprocess.h>
00035 #include <krun.h>
00036 #include <kshell.h>
00037 #include <kurl.h>
00038 #include <kparts/browserextension.h>
00039 
00040 #include <qaccel.h>
00041 #include <qclipboard.h>
00042 #include <qpaintdevicemetrics.h>
00043 
00044 #include "viewer.h"
00045 #include "akregator_run.h"
00046 #include "akregatorconfig.h"
00047 
00048 namespace Akregator {
00049 
00050 Viewer::Viewer(QWidget *parent, const char *name)
00051     : KHTMLPart(parent, name), m_url(0)
00052 {
00053     setZoomFactor(100);
00054     setJScriptEnabled(false);
00055     setJavaEnabled(true);
00056     setMetaRefreshEnabled(true);
00057     setPluginsEnabled(true);
00058     setDNDEnabled(true);
00059     setAutoloadImages(true);
00060     setStatusMessagesEnabled(true);
00061 
00062     // change the cursor when loading stuff...
00063     connect( this, SIGNAL(started(KIO::Job *)),
00064              this, SLOT(slotStarted(KIO::Job *)));
00065     connect( this, SIGNAL(completed()),
00066              this, SLOT(slotCompleted()));
00067 
00068     connect( browserExtension(), SIGNAL(popupMenu (KXMLGUIClient*, const QPoint&, const KURL&, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags, mode_t)), this, SLOT(slotPopupMenu(KXMLGUIClient*, const QPoint&, const KURL&, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags, mode_t)));
00069 
00070     KStdAction::print(this, SLOT(slotPrint()), actionCollection(), "viewer_print");
00071     KStdAction::copy(this, SLOT(slotCopy()), actionCollection(), "viewer_copy");
00072     
00073     new KAction( i18n("&Increase Font Sizes"), "viewmag+", "Ctrl+Plus", this, SLOT(slotZoomIn()), actionCollection(), "incFontSizes" );
00074     new KAction( i18n("&Decrease Font Sizes"), "viewmag-", "Ctrl+Minus", this, SLOT(slotZoomOut()), actionCollection(), "decFontSizes" );
00075 
00076     connect(this, SIGNAL(selectionChanged()), this, SLOT(slotSelectionChanged()));
00077 
00078     connect( browserExtension(), SIGNAL(openURLRequestDelayed(const KURL&, const KParts::URLArgs&)), this, SLOT(slotOpenURLRequest(const KURL&, const KParts::URLArgs& )) );
00079 
00080     new KAction(i18n("Copy &Link Address"), "", 0,
00081                                  this, SLOT(slotCopyLinkAddress()),
00082                                  actionCollection(), "copylinkaddress");
00083     new KAction(i18n("&Save Link As..."), "", 0,
00084                                  this, SLOT(slotSaveLinkAs()),
00085                                  actionCollection(), "savelinkas");
00086 }
00087 
00088 Viewer::~Viewer()
00089 {}
00090 
00091 bool Viewer::closeURL()
00092 {
00093     emit browserExtension()->loadingProgress(-1);
00094     emit canceled(QString::null);
00095     return KHTMLPart::closeURL();
00096 }
00097 
00098 int Viewer::pointsToPixel(int pointSize) const
00099 {
00100     const QPaintDeviceMetrics metrics(view());
00101     return ( pointSize * metrics.logicalDpiY() + 36 ) / 72 ;
00102 }
00103 
00104 void Viewer::displayInExternalBrowser(const KURL &url, const QString &mimetype)
00105 {
00106    if (!url.isValid()) return;
00107    if (Settings::externalBrowserUseKdeDefault())
00108    {
00109        if (mimetype.isEmpty()) 
00110            kapp->invokeBrowser(url.url(), "0");
00111        else
00112            KRun::runURL(url, mimetype, false, false);
00113    }
00114    else
00115    {
00116        QString cmd = Settings::externalBrowserCustomCommand();
00117        QString urlStr = url.url();
00118        cmd.replace(QRegExp("%u"), urlStr);
00119        KProcess *proc = new KProcess;
00120        QStringList cmdAndArgs = KShell::splitArgs(cmd);
00121        *proc << cmdAndArgs;
00122        proc->start(KProcess::DontCare);
00123        delete proc;
00124    }
00125 }
00126 
00127 void Viewer::slotOpenURLRequest(const KURL& /*url*/, const KParts::URLArgs& /*args*/)
00128 {
00129 
00130 }
00131 
00132 void Viewer::urlSelected(const QString &url, int button, int state, const QString &_target, KParts::URLArgs args)
00133 {
00134     m_url = completeURL(url);
00135     browserExtension()->setURLArgs(args);
00136     if (button == LeftButton)
00137     {
00138         switch (Settings::lMBBehaviour())
00139         {
00140             case Settings::EnumLMBBehaviour::OpenInExternalBrowser:
00141                 slotOpenLinkInBrowser();
00142                 break;
00143             case Settings::EnumLMBBehaviour::OpenInBackground:
00144                 slotOpenLinkInBackgroundTab();
00145                 break;
00146             default:
00147                 slotOpenLinkInForegroundTab();
00148                 break;
00149         }
00150         return;
00151     }
00152     else if (button == MidButton)
00153     {
00154         switch (Settings::mMBBehaviour())
00155         {
00156             case Settings::EnumMMBBehaviour::OpenInExternalBrowser:
00157                 slotOpenLinkInBrowser();
00158                 break;
00159             case Settings::EnumMMBBehaviour::OpenInBackground:
00160                 slotOpenLinkInBackgroundTab();
00161                 break;
00162             default:
00163                 slotOpenLinkInForegroundTab();
00164                 break;
00165         }
00166         return;
00167     }
00168     KHTMLPart::urlSelected(url,button,state,_target,args);
00169 }
00170 
00171 void Viewer::slotPopupMenu(KXMLGUIClient*, const QPoint& p, const KURL& kurl, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags kpf, mode_t)
00172 {
00173    const bool isLink = (kpf & (KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowTextSelectionItems)) == 0;
00174    const bool isSelection = (kpf & KParts::BrowserExtension::ShowTextSelectionItems) != 0;
00175     
00176    QString url = kurl.url();
00177    
00178    m_url = url;
00179    KPopupMenu popup;
00180    
00181    if (isLink && !isSelection)
00182    {
00183         popup.insertItem(SmallIcon("tab_new"), i18n("Open Link in New &Tab"), this, SLOT(slotOpenLinkInForegroundTab()));
00184         popup.insertItem(SmallIcon("window_new"), i18n("Open Link in External &Browser"), this, SLOT(slotOpenLinkInBrowser()));
00185         popup.insertSeparator();
00186         action("savelinkas")->plug(&popup);
00187         action("copylinkaddress")->plug(&popup);
00188    }
00189    else
00190    {
00191        if (isSelection)
00192        {
00193             action("viewer_copy")->plug(&popup);
00194             popup.insertSeparator();
00195        }
00196        action("viewer_print")->plug(&popup);
00197        //KAction *ac = action("setEncoding");
00198        //if (ac)
00199        //     ac->plug(&popup);
00200    }
00201    popup.exec(p);
00202 }
00203 
00204 // taken from KDevelop
00205 void Viewer::slotCopy()
00206 {
00207     QString text = selectedText();
00208     text.replace( QChar( 0xa0 ), ' ' );
00209     QClipboard *cb = QApplication::clipboard();
00210     disconnect( cb, SIGNAL( selectionChanged() ), this, SLOT( slotClearSelection() ) );
00211     cb->setText(text);
00212     connect( cb, SIGNAL( selectionChanged() ), this, SLOT( slotClearSelection() ) );
00213 }
00214 
00215 void Viewer::slotCopyLinkAddress()
00216 {
00217    if(m_url.isEmpty()) return;
00218    QClipboard *cb = QApplication::clipboard();
00219    cb->setText(m_url.prettyURL(), QClipboard::Clipboard);
00220    cb->setText(m_url.prettyURL(), QClipboard::Selection);
00221 }
00222 
00223 void Viewer::slotSelectionChanged()
00224 {
00225     action("viewer_copy")->setEnabled(!selectedText().isEmpty());
00226 }
00227 
00228 void Viewer::slotOpenLinkInternal()
00229 {
00230    openURL(m_url);
00231 }
00232 
00233 void Viewer::slotOpenLinkInForegroundTab()
00234 {
00235     emit urlClicked(m_url, this, true, false);
00236 }
00237 
00238 void Viewer::slotOpenLinkInBackgroundTab()
00239 {
00240     emit urlClicked(m_url, this, true, true);
00241 }
00242 
00243 void Viewer::slotOpenLinkInThisTab()
00244 {
00245     emit urlClicked(m_url, this, false, false);
00246 }
00247 
00248 void Viewer::slotOpenLinkInBrowser()
00249 {
00250     displayInExternalBrowser(m_url, QString::null);
00251 }
00252 
00253 void Viewer::slotSaveLinkAs()
00254 {
00255     KURL tmp( m_url );
00256 
00257     if ( tmp.fileName(false).isEmpty() )
00258         tmp.setFileName( "index.html" );
00259     KParts::BrowserRun::simpleSave(tmp, tmp.fileName());
00260 }
00261 
00262 void Viewer::slotStarted(KIO::Job *)
00263 {
00264    widget()->setCursor( waitCursor );
00265 }
00266 
00267 void Viewer::slotCompleted()
00268 {
00269    widget()->unsetCursor();
00270 }
00271 
00272 void Viewer::slotScrollUp()
00273 {
00274     view()->scrollBy(0,-10);
00275 }
00276 
00277 void Viewer::slotScrollDown()
00278 {
00279     view()->scrollBy(0,10);
00280 }
00281 
00282 void Viewer::slotZoomIn()
00283 {
00284     int zf = zoomFactor();
00285     if (zf < 100)
00286     {
00287         zf = zf - (zf % 20) + 20;
00288         setZoomFactor(zf);
00289     }
00290     else
00291     {
00292         zf = zf - (zf % 50) + 50;
00293         setZoomFactor(zf < 300 ? zf : 300);
00294     }
00295 }
00296 
00297 void Viewer::slotZoomOut()
00298 {
00299     int zf = zoomFactor();
00300     if (zf <= 100)
00301     {
00302         zf = zf - (zf % 20) - 20;
00303         setZoomFactor(zf > 20 ? zf : 20);
00304     }
00305     else
00306     {
00307         zf = zf - (zf % 50) - 50;
00308         setZoomFactor(zf);
00309     }
00310 }
00311 
00312 void Viewer::slotSetZoomFactor(int percent)
00313 {
00314     setZoomFactor(percent);
00315 }
00316 
00317 // some code taken from KDevelop (lib/widgets/kdevhtmlpart.cpp)
00318 void Viewer::slotPrint( )
00319 {
00320     view()->print();
00321 }
00322 
00323 
00324 void Viewer::setSafeMode()
00325 {
00326     //setJScriptEnabled(false);
00327     setJavaEnabled(false);
00328     setMetaRefreshEnabled(false);
00329     setPluginsEnabled(false);
00330     setDNDEnabled(true);
00331     setAutoloadImages(true);
00332     setStatusMessagesEnabled(false);
00333 }
00334 
00335 } // namespace Akregator
00336 
00337 #include "viewer.moc"
00338 
00339 // vim: set et ts=4 sts=4 sw=4:
KDE Home | KDE Accessibility Home | Description of Access Keys