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 TQt, and distribute the resulting executable, 00023 without including the source code for TQt in the source distribution. 00024 */ 00025 00026 #include <tdeaction.h> 00027 #include <tdeapplication.h> 00028 #include <tdefiledialog.h> 00029 #include <tdehtmlview.h> 00030 #include <kiconloader.h> 00031 #include <tdelocale.h> 00032 #include <tdemessagebox.h> 00033 #include <tdepopupmenu.h> 00034 #include <kprocess.h> 00035 #include <krun.h> 00036 #include <kshell.h> 00037 #include <kurl.h> 00038 #include <tdeparts/browserextension.h> 00039 00040 #include <tqaccel.h> 00041 #include <tqclipboard.h> 00042 #include <tqpaintdevicemetrics.h> 00043 00044 #include "viewer.h" 00045 #include "akregator_run.h" 00046 #include "akregatorconfig.h" 00047 00048 namespace Akregator { 00049 00050 Viewer::Viewer(TQWidget *parent, const char *name) 00051 : TDEHTMLPart(parent, name), m_url(0) 00052 { 00053 setZoomFactor(100); 00054 setMetaRefreshEnabled(true); 00055 setDNDEnabled(true); 00056 setAutoloadImages(true); 00057 setStatusMessagesEnabled(true); 00058 00059 // change the cursor when loading stuff... 00060 connect( this, TQT_SIGNAL(started(TDEIO::Job *)), 00061 this, TQT_SLOT(slotStarted(TDEIO::Job *))); 00062 connect( this, TQT_SIGNAL(completed()), 00063 this, TQT_SLOT(slotCompleted())); 00064 00065 connect( browserExtension(), TQT_SIGNAL(popupMenu (KXMLGUIClient*, const TQPoint&, const KURL&, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags, mode_t)), this, TQT_SLOT(slotPopupMenu(KXMLGUIClient*, const TQPoint&, const KURL&, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags, mode_t))); 00066 00067 KStdAction::print(this, TQT_SLOT(slotPrint()), actionCollection(), "viewer_print"); 00068 KStdAction::copy(this, TQT_SLOT(slotCopy()), actionCollection(), "viewer_copy"); 00069 00070 new TDEAction( i18n("&Increase Font Sizes"), "zoom-in", "Ctrl+Plus", this, TQT_SLOT(slotZoomIn()), actionCollection(), "incFontSizes" ); 00071 new TDEAction( i18n("&Decrease Font Sizes"), "zoom-out", "Ctrl+Minus", this, TQT_SLOT(slotZoomOut()), actionCollection(), "decFontSizes" ); 00072 00073 connect(this, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged())); 00074 00075 connect( browserExtension(), TQT_SIGNAL(openURLRequestDelayed(const KURL&, const KParts::URLArgs&)), this, TQT_SLOT(slotOpenURLRequest(const KURL&, const KParts::URLArgs& )) ); 00076 00077 new TDEAction(i18n("Copy &Link Address"), "", 0, 00078 this, TQT_SLOT(slotCopyLinkAddress()), 00079 actionCollection(), "copylinkaddress"); 00080 new TDEAction(i18n("&Save Link As..."), "", 0, 00081 this, TQT_SLOT(slotSaveLinkAs()), 00082 actionCollection(), "savelinkas"); 00083 } 00084 00085 Viewer::~Viewer() 00086 {} 00087 00088 bool Viewer::closeURL() 00089 { 00090 emit browserExtension()->loadingProgress(-1); 00091 emit canceled(TQString()); 00092 return TDEHTMLPart::closeURL(); 00093 } 00094 00095 int Viewer::pointsToPixel(int pointSize) const 00096 { 00097 const TQPaintDeviceMetrics metrics(view()); 00098 return ( pointSize * metrics.logicalDpiY() + 36 ) / 72 ; 00099 } 00100 00101 void Viewer::displayInExternalBrowser(const KURL &url, const TQString &mimetype) 00102 { 00103 if (!url.isValid()) return; 00104 if (Settings::externalBrowserUseTdeDefault()) 00105 { 00106 if (mimetype.isEmpty()) 00107 kapp->invokeBrowser(url.url(), "0"); 00108 else 00109 KRun::runURL(url, mimetype, false, false); 00110 } 00111 else 00112 { 00113 TQString cmd = Settings::externalBrowserCustomCommand(); 00114 TQString urlStr = url.url(); 00115 cmd.replace(TQRegExp("%u"), urlStr); 00116 TDEProcess *proc = new TDEProcess; 00117 TQStringList cmdAndArgs = KShell::splitArgs(cmd); 00118 *proc << cmdAndArgs; 00119 proc->start(TDEProcess::DontCare); 00120 delete proc; 00121 } 00122 } 00123 00124 void Viewer::slotOpenURLRequest(const KURL& /*url*/, const KParts::URLArgs& /*args*/) 00125 { 00126 00127 } 00128 00129 void Viewer::urlSelected(const TQString &url, int button, int state, const TQString &_target, KParts::URLArgs args) 00130 { 00131 m_url = completeURL(url); 00132 browserExtension()->setURLArgs(args); 00133 if (button == Qt::LeftButton) 00134 { 00135 switch (Settings::lMBBehaviour()) 00136 { 00137 case Settings::EnumLMBBehaviour::OpenInExternalBrowser: 00138 slotOpenLinkInBrowser(); 00139 break; 00140 case Settings::EnumLMBBehaviour::OpenInBackground: 00141 slotOpenLinkInBackgroundTab(); 00142 break; 00143 default: 00144 slotOpenLinkInForegroundTab(); 00145 break; 00146 } 00147 return; 00148 } 00149 else if (button == Qt::MidButton) 00150 { 00151 switch (Settings::mMBBehaviour()) 00152 { 00153 case Settings::EnumMMBBehaviour::OpenInExternalBrowser: 00154 slotOpenLinkInBrowser(); 00155 break; 00156 case Settings::EnumMMBBehaviour::OpenInBackground: 00157 slotOpenLinkInBackgroundTab(); 00158 break; 00159 default: 00160 slotOpenLinkInForegroundTab(); 00161 break; 00162 } 00163 return; 00164 } 00165 TDEHTMLPart::urlSelected(url,button,state,_target,args); 00166 } 00167 00168 void Viewer::slotPopupMenu(KXMLGUIClient*, const TQPoint& p, const KURL& kurl, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags kpf, mode_t) 00169 { 00170 const bool isLink = (kpf & (KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowTextSelectionItems)) == 0; 00171 const bool isSelection = (kpf & KParts::BrowserExtension::ShowTextSelectionItems) != 0; 00172 00173 TQString url = kurl.url(); 00174 00175 m_url = url; 00176 TDEPopupMenu popup; 00177 00178 if (isLink && !isSelection) 00179 { 00180 popup.insertItem(SmallIcon("tab_new"), i18n("Open Link in New &Tab"), this, TQT_SLOT(slotOpenLinkInForegroundTab())); 00181 popup.insertItem(SmallIcon("window-new"), i18n("Open Link in External &Browser"), this, TQT_SLOT(slotOpenLinkInBrowser())); 00182 popup.insertSeparator(); 00183 action("savelinkas")->plug(&popup); 00184 action("copylinkaddress")->plug(&popup); 00185 } 00186 else 00187 { 00188 if (isSelection) 00189 { 00190 action("viewer_copy")->plug(&popup); 00191 popup.insertSeparator(); 00192 } 00193 action("viewer_print")->plug(&popup); 00194 //TDEAction *ac = action("setEncoding"); 00195 //if (ac) 00196 // ac->plug(&popup); 00197 } 00198 popup.exec(p); 00199 } 00200 00201 // taken from KDevelop 00202 void Viewer::slotCopy() 00203 { 00204 TQString text = selectedText(); 00205 text.replace( TQChar( 0xa0 ), ' ' ); 00206 TQClipboard *cb = TQApplication::clipboard(); 00207 disconnect( cb, TQT_SIGNAL( selectionChanged() ), this, TQT_SLOT( slotClearSelection() ) ); 00208 cb->setText(text); 00209 connect( cb, TQT_SIGNAL( selectionChanged() ), this, TQT_SLOT( slotClearSelection() ) ); 00210 } 00211 00212 void Viewer::slotCopyLinkAddress() 00213 { 00214 if(m_url.isEmpty()) return; 00215 TQClipboard *cb = TQApplication::clipboard(); 00216 cb->setText(m_url.prettyURL(), TQClipboard::Clipboard); 00217 cb->setText(m_url.prettyURL(), TQClipboard::Selection); 00218 } 00219 00220 void Viewer::slotSelectionChanged() 00221 { 00222 action("viewer_copy")->setEnabled(!selectedText().isEmpty()); 00223 } 00224 00225 void Viewer::slotOpenLinkInternal() 00226 { 00227 openURL(m_url); 00228 } 00229 00230 void Viewer::slotOpenLinkInForegroundTab() 00231 { 00232 emit urlClicked(m_url, this, true, false); 00233 } 00234 00235 void Viewer::slotOpenLinkInBackgroundTab() 00236 { 00237 emit urlClicked(m_url, this, true, true); 00238 } 00239 00240 void Viewer::slotOpenLinkInThisTab() 00241 { 00242 emit urlClicked(m_url, this, false, false); 00243 } 00244 00245 void Viewer::slotOpenLinkInBrowser() 00246 { 00247 displayInExternalBrowser(m_url, TQString()); 00248 } 00249 00250 void Viewer::slotSaveLinkAs() 00251 { 00252 KURL tmp( m_url ); 00253 00254 if ( tmp.fileName(false).isEmpty() ) 00255 tmp.setFileName( "index.html" ); 00256 KParts::BrowserRun::simpleSave(tmp, tmp.fileName()); 00257 } 00258 00259 void Viewer::slotStarted(TDEIO::Job *) 00260 { 00261 widget()->setCursor( waitCursor ); 00262 } 00263 00264 void Viewer::slotCompleted() 00265 { 00266 widget()->unsetCursor(); 00267 } 00268 00269 void Viewer::slotScrollUp() 00270 { 00271 view()->scrollBy(0,-10); 00272 } 00273 00274 void Viewer::slotScrollDown() 00275 { 00276 view()->scrollBy(0,10); 00277 } 00278 00279 void Viewer::slotZoomIn() 00280 { 00281 int zf = zoomFactor(); 00282 if (zf < 100) 00283 { 00284 zf = zf - (zf % 20) + 20; 00285 setZoomFactor(zf); 00286 } 00287 else 00288 { 00289 zf = zf - (zf % 50) + 50; 00290 setZoomFactor(zf < 300 ? zf : 300); 00291 } 00292 } 00293 00294 void Viewer::slotZoomOut() 00295 { 00296 int zf = zoomFactor(); 00297 if (zf <= 100) 00298 { 00299 zf = zf - (zf % 20) - 20; 00300 setZoomFactor(zf > 20 ? zf : 20); 00301 } 00302 else 00303 { 00304 zf = zf - (zf % 50) - 50; 00305 setZoomFactor(zf); 00306 } 00307 } 00308 00309 void Viewer::slotSetZoomFactor(int percent) 00310 { 00311 setZoomFactor(percent); 00312 } 00313 00314 // some code taken from KDevelop (lib/widgets/kdevhtmlpart.cpp) 00315 void Viewer::slotPrint( ) 00316 { 00317 view()->print(); 00318 } 00319 00320 00321 void Viewer::setSafeMode() 00322 { 00323 //setJScriptEnabled(false); 00324 setJavaEnabled(false); 00325 setMetaRefreshEnabled(false); 00326 setPluginsEnabled(false); 00327 setDNDEnabled(true); 00328 setAutoloadImages(true); 00329 setStatusMessagesEnabled(false); 00330 } 00331 00332 } // namespace Akregator 00333 00334 #include "viewer.moc" 00335 00336 // vim: set et ts=4 sts=4 sw=4: