akregator/src

viewer.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2004 Teemu Rytilahti <tpr@d5k.net>
5  2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 #include <tdeaction.h>
27 #include <tdeapplication.h>
28 #include <tdefiledialog.h>
29 #include <tdehtmlview.h>
30 #include <kiconloader.h>
31 #include <tdelocale.h>
32 #include <tdemessagebox.h>
33 #include <tdepopupmenu.h>
34 #include <tdeprocess.h>
35 #include <krun.h>
36 #include <kshell.h>
37 #include <kurl.h>
38 #include <tdeparts/browserextension.h>
39 
40 #include <tqaccel.h>
41 #include <tqclipboard.h>
42 #include <tqpaintdevicemetrics.h>
43 
44 #include "viewer.h"
45 #include "akregator_run.h"
46 #include "akregatorconfig.h"
47 
48 namespace Akregator {
49 
50 Viewer::Viewer(TQWidget *parent, const char *name)
51  : TDEHTMLPart(parent, name), m_url(0)
52 {
53  setZoomFactor(100);
54  setMetaRefreshEnabled(true);
55  setDNDEnabled(true);
56  setAutoloadImages(true);
57  setStatusMessagesEnabled(true);
58 
59  // change the cursor when loading stuff...
60  connect( this, TQ_SIGNAL(started(TDEIO::Job *)),
61  this, TQ_SLOT(slotStarted(TDEIO::Job *)));
62  connect( this, TQ_SIGNAL(completed()),
63  this, TQ_SLOT(slotCompleted()));
64 
65  connect( browserExtension(), TQ_SIGNAL(popupMenu (KXMLGUIClient*, const TQPoint&, const KURL&, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags, mode_t)), this, TQ_SLOT(slotPopupMenu(KXMLGUIClient*, const TQPoint&, const KURL&, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags, mode_t)));
66 
67  KStdAction::print(this, TQ_SLOT(slotPrint()), actionCollection(), "viewer_print");
68  KStdAction::copy(this, TQ_SLOT(slotCopy()), actionCollection(), "viewer_copy");
69 
70  new TDEAction( i18n("&Increase Font Sizes"), "zoom-in", "Ctrl+Plus", this, TQ_SLOT(slotZoomIn()), actionCollection(), "incFontSizes" );
71  new TDEAction( i18n("&Decrease Font Sizes"), "zoom-out", "Ctrl+Minus", this, TQ_SLOT(slotZoomOut()), actionCollection(), "decFontSizes" );
72 
73  connect(this, TQ_SIGNAL(selectionChanged()), this, TQ_SLOT(slotSelectionChanged()));
74 
75  connect( browserExtension(), TQ_SIGNAL(openURLRequestDelayed(const KURL&, const KParts::URLArgs&)), this, TQ_SLOT(slotOpenURLRequest(const KURL&, const KParts::URLArgs& )) );
76 
77  new TDEAction(i18n("Copy &Link Address"), "", 0,
78  this, TQ_SLOT(slotCopyLinkAddress()),
79  actionCollection(), "copylinkaddress");
80  new TDEAction(i18n("&Save Link As..."), "", 0,
81  this, TQ_SLOT(slotSaveLinkAs()),
82  actionCollection(), "savelinkas");
83 }
84 
85 Viewer::~Viewer()
86 {}
87 
88 bool Viewer::closeURL()
89 {
90  emit browserExtension()->loadingProgress(-1);
91  emit canceled(TQString());
92  return TDEHTMLPart::closeURL();
93 }
94 
95 int Viewer::pointsToPixel(int pointSize) const
96 {
97  const TQPaintDeviceMetrics metrics(view());
98  return ( pointSize * metrics.logicalDpiY() + 36 ) / 72 ;
99 }
100 
101 void Viewer::displayInExternalBrowser(const KURL &url, const TQString &mimetype)
102 {
103  if (!url.isValid()) return;
104  if (Settings::externalBrowserUseTdeDefault())
105  {
106  if (mimetype.isEmpty())
107  kapp->invokeBrowser(url.url(), "0");
108  else
109  KRun::runURL(url, mimetype, false, false);
110  }
111  else
112  {
113  TQString cmd = Settings::externalBrowserCustomCommand();
114  TQString urlStr = url.url();
115  cmd.replace(TQRegExp("%u"), urlStr);
116  TDEProcess *proc = new TDEProcess;
117  TQStringList cmdAndArgs = KShell::splitArgs(cmd);
118  *proc << cmdAndArgs;
119  proc->start(TDEProcess::DontCare);
120  delete proc;
121  }
122 }
123 
124 void Viewer::slotOpenURLRequest(const KURL& /*url*/, const KParts::URLArgs& /*args*/)
125 {
126 
127 }
128 
129 void Viewer::urlSelected(const TQString &url, int button, int state, const TQString &_target, KParts::URLArgs args)
130 {
131  m_url = completeURL(url);
132  browserExtension()->setURLArgs(args);
133  if (button == TQt::LeftButton)
134  {
135  switch (Settings::lMBBehaviour())
136  {
137  case Settings::EnumLMBBehaviour::OpenInExternalBrowser:
138  slotOpenLinkInBrowser();
139  break;
140  case Settings::EnumLMBBehaviour::OpenInBackground:
141  slotOpenLinkInBackgroundTab();
142  break;
143  default:
144  slotOpenLinkInForegroundTab();
145  break;
146  }
147  return;
148  }
149  else if (button == TQt::MidButton)
150  {
151  switch (Settings::mMBBehaviour())
152  {
153  case Settings::EnumMMBBehaviour::OpenInExternalBrowser:
154  slotOpenLinkInBrowser();
155  break;
156  case Settings::EnumMMBBehaviour::OpenInBackground:
157  slotOpenLinkInBackgroundTab();
158  break;
159  default:
160  slotOpenLinkInForegroundTab();
161  break;
162  }
163  return;
164  }
165  TDEHTMLPart::urlSelected(url,button,state,_target,args);
166 }
167 
168 void Viewer::slotPopupMenu(KXMLGUIClient*, const TQPoint& p, const KURL& kurl, const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags kpf, mode_t)
169 {
170  const bool isLink = (kpf & (KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowTextSelectionItems)) == 0;
171  const bool isSelection = (kpf & KParts::BrowserExtension::ShowTextSelectionItems) != 0;
172 
173  TQString url = kurl.url();
174 
175  m_url = url;
176  TDEPopupMenu popup;
177 
178  if (isLink && !isSelection)
179  {
180  popup.insertItem(SmallIcon("tab_new"), i18n("Open Link in New &Tab"), this, TQ_SLOT(slotOpenLinkInForegroundTab()));
181  popup.insertItem(SmallIcon("window-new"), i18n("Open Link in External &Browser"), this, TQ_SLOT(slotOpenLinkInBrowser()));
182  popup.insertSeparator();
183  action("savelinkas")->plug(&popup);
184  action("copylinkaddress")->plug(&popup);
185  }
186  else
187  {
188  if (isSelection)
189  {
190  action("viewer_copy")->plug(&popup);
191  popup.insertSeparator();
192  }
193  action("viewer_print")->plug(&popup);
194  //TDEAction *ac = action("setEncoding");
195  //if (ac)
196  // ac->plug(&popup);
197  }
198  popup.exec(p);
199 }
200 
201 // taken from KDevelop
202 void Viewer::slotCopy()
203 {
204  TQString text = selectedText();
205  text.replace( TQChar( 0xa0 ), ' ' );
206  TQClipboard *cb = TQApplication::clipboard();
207  disconnect( cb, TQ_SIGNAL( selectionChanged() ), this, TQ_SLOT( slotClearSelection() ) );
208  cb->setText(text);
209  connect( cb, TQ_SIGNAL( selectionChanged() ), this, TQ_SLOT( slotClearSelection() ) );
210 }
211 
212 void Viewer::slotCopyLinkAddress()
213 {
214  if(m_url.isEmpty()) return;
215  TQClipboard *cb = TQApplication::clipboard();
216  cb->setText(m_url.prettyURL(), TQClipboard::Clipboard);
217  cb->setText(m_url.prettyURL(), TQClipboard::Selection);
218 }
219 
220 void Viewer::slotSelectionChanged()
221 {
222  action("viewer_copy")->setEnabled(!selectedText().isEmpty());
223 }
224 
225 void Viewer::slotOpenLinkInternal()
226 {
227  openURL(m_url);
228 }
229 
230 void Viewer::slotOpenLinkInForegroundTab()
231 {
232  emit urlClicked(m_url, this, true, false);
233 }
234 
235 void Viewer::slotOpenLinkInBackgroundTab()
236 {
237  emit urlClicked(m_url, this, true, true);
238 }
239 
240 void Viewer::slotOpenLinkInThisTab()
241 {
242  emit urlClicked(m_url, this, false, false);
243 }
244 
245 void Viewer::slotOpenLinkInBrowser()
246 {
247  displayInExternalBrowser(m_url, TQString());
248 }
249 
250 void Viewer::slotSaveLinkAs()
251 {
252  KURL tmp( m_url );
253 
254  if ( tmp.fileName(false).isEmpty() )
255  tmp.setFileName( "index.html" );
256  KParts::BrowserRun::simpleSave(tmp, tmp.fileName());
257 }
258 
259 void Viewer::slotStarted(TDEIO::Job *)
260 {
261  widget()->setCursor( waitCursor );
262 }
263 
264 void Viewer::slotCompleted()
265 {
266  widget()->unsetCursor();
267 }
268 
269 void Viewer::slotScrollUp()
270 {
271  view()->scrollBy(0,-10);
272 }
273 
274 void Viewer::slotScrollDown()
275 {
276  view()->scrollBy(0,10);
277 }
278 
279 void Viewer::slotZoomIn()
280 {
281  int zf = zoomFactor();
282  if (zf < 100)
283  {
284  zf = zf - (zf % 20) + 20;
285  setZoomFactor(zf);
286  }
287  else
288  {
289  zf = zf - (zf % 50) + 50;
290  setZoomFactor(zf < 300 ? zf : 300);
291  }
292 }
293 
294 void Viewer::slotZoomOut()
295 {
296  int zf = zoomFactor();
297  if (zf <= 100)
298  {
299  zf = zf - (zf % 20) - 20;
300  setZoomFactor(zf > 20 ? zf : 20);
301  }
302  else
303  {
304  zf = zf - (zf % 50) - 50;
305  setZoomFactor(zf);
306  }
307 }
308 
309 void Viewer::slotSetZoomFactor(int percent)
310 {
311  setZoomFactor(percent);
312 }
313 
314 // some code taken from KDevelop (lib/widgets/kdevhtmlpart.cpp)
315 void Viewer::slotPrint( )
316 {
317  view()->print();
318 }
319 
320 
321 void Viewer::setSafeMode()
322 {
323  //setJScriptEnabled(false);
324  setJavaEnabled(false);
325  setMetaRefreshEnabled(false);
326  setPluginsEnabled(false);
327  setDNDEnabled(true);
328  setAutoloadImages(true);
329  setStatusMessagesEnabled(false);
330 }
331 
332 } // namespace Akregator
333 
334 #include "viewer.moc"