26 #include "akregatorconfig.h"
27 #include "akregator_run.h"
28 #include "feediconmanager.h"
29 #include "pageviewer.h"
32 #include <tdeaction.h>
33 #include <tdeapplication.h>
34 #include <kbookmark.h>
35 #include <kbookmarkmanager.h>
36 #include <tdeconfig.h>
37 #include <tdeglobalsettings.h>
38 #include <tdehtml_settings.h>
39 #include <tdehtmlview.h>
40 #include <kiconloader.h>
41 #include <tdelocale.h>
42 #include <tdepopupmenu.h>
43 #include <kstandarddirs.h>
44 #include <tdestdaccel.h>
45 #include <tdeparts/browserinterface.h>
47 #include <tqclipboard.h>
48 #include <tqcstring.h>
49 #include <tqdatastream.h>
50 #include <tqdatetime.h>
52 #include <tqmetaobject.h>
53 #include <tqscrollview.h>
55 #include <tqvaluelist.h>
56 #include <private/tqucomextra_p.h>
65 class PageViewer::HistoryEntry
75 HistoryEntry(
const KURL& u,
const TQString& t=TQString()): url(u), title(t)
77 id = abs( TQTime::currentTime().msecsTo( TQTime() ) );
82 class PageViewer::PageViewerPrivate
86 TQValueList<HistoryEntry> history;
87 TQValueList<HistoryEntry>::Iterator current;
89 TDEToolBarPopupAction* backAction;
90 TDEToolBarPopupAction* forwardAction;
91 TDEAction* reloadAction;
92 TDEAction* stopAction;
98 PageViewer::PageViewer(TQWidget *parent,
const char *name)
99 : Viewer(parent, name), d(new PageViewerPrivate)
103 TDEHTMLSettings* s =
const_cast<TDEHTMLSettings*
> (settings());
104 s->init(Settings::self()->config());
106 setXMLFile(locate(
"data",
"akregator/pageviewer.rc"),
true);
108 TQPair< KGuiItem, KGuiItem > backForward = KStdGuiItem::backAndForward();
110 d->backAction =
new TDEToolBarPopupAction(backForward.first,
111 TDEStdAccel::shortcut(TDEStdAccel::Back),
this,
112 TQ_SLOT(slotBack()), actionCollection(),
115 connect(d->backAction->popupMenu(), TQ_SIGNAL(aboutToShow()),
116 this, TQ_SLOT(slotBackAboutToShow()));
117 connect(d->backAction->popupMenu(), TQ_SIGNAL(activated(
int)),
118 this, TQ_SLOT(slotPopupActivated(
int)));
121 d->forwardAction =
new TDEToolBarPopupAction(backForward.second,
122 TDEStdAccel::shortcut(TDEStdAccel::Forward),
this,
123 TQ_SLOT(slotForward()), actionCollection(),
124 "pageviewer_forward");
126 connect(d->forwardAction->popupMenu(), TQ_SIGNAL(aboutToShow()),
127 this, TQ_SLOT(slotForwardAboutToShow()));
128 connect(d->forwardAction->popupMenu(), TQ_SIGNAL(activated(
int)),
129 this, TQ_SLOT(slotPopupActivated(
int)));
131 d->reloadAction =
new TDEAction(i18n(
"Reload"),
"reload", 0,
132 this, TQ_SLOT(slotReload()),
133 actionCollection(),
"pageviewer_reload");
134 d->stopAction =
new TDEAction(KStdGuiItem::guiItem(KStdGuiItem::Stop), 0,
135 this, TQ_SLOT(slotStop()),
136 actionCollection(),
"pageviewer_stop");
140 d->backAction->setEnabled(
false);
141 d->forwardAction->setEnabled(
false);
142 d->stopAction->setEnabled(
false);
144 connect(
this, TQ_SIGNAL(setWindowCaption (
const TQString &)),
145 this, TQ_SLOT(slotSetCaption (
const TQString &)) );
147 connect(
this, TQ_SIGNAL(started(TDEIO::Job *)),
this, TQ_SLOT(slotStarted(TDEIO::Job* )));
148 connect(
this, TQ_SIGNAL(completed()),
this, TQ_SLOT(slotCompleted()));
149 connect(
this, TQ_SIGNAL(canceled(
const TQString &)),
this, TQ_SLOT(slotCancelled(
const TQString &)));
151 d->current = d->history.end();
158 PageViewer::~PageViewer()
165 void PageViewer::slotBack()
167 if ( d->current != d->history.begin() )
169 TQValueList<HistoryEntry>::Iterator tmp = d->current;
171 restoreHistoryEntry(tmp);
176 void PageViewer::slotForward()
178 if ( d->current != d->history.fromLast() && d->current != d->history.end() )
180 TQValueList<HistoryEntry>::Iterator tmp = d->current;
182 restoreHistoryEntry(tmp);
186 void PageViewer::slotBackAboutToShow()
188 TDEPopupMenu *popup = d->backAction->popupMenu();
191 if ( d->current == d->history.begin() )
194 TQValueList<HistoryEntry>::Iterator it = d->current;
200 if ( it == d->history.begin() )
202 popup->insertItem( (*it).title, (*it).id );
206 popup->insertItem( (*it).title, (*it).id );
212 void PageViewer::slotForwardAboutToShow()
214 TDEPopupMenu *popup = d->forwardAction->popupMenu();
217 if ( d->current == d->history.fromLast() )
220 TQValueList<HistoryEntry>::Iterator it = d->current;
226 if ( it == d->history.fromLast() )
228 popup->insertItem( (*it).title, (*it).id );
232 popup->insertItem( (*it).title, (*it).id );
239 void PageViewer::slotReload()
244 void PageViewer::slotStop()
249 bool PageViewer::openURL(
const KURL& url)
251 updateHistoryEntry();
254 bool val = TDEHTMLPart::openURL(url);
256 addHistoryEntry(url);
258 d->backAction->setEnabled( d->current != d->history.begin() );
259 d->forwardAction->setEnabled( d->current != d->history.fromLast() );
261 TQString favicon = FeedIconManager::self()->iconLocation(url);
262 if (!favicon.isEmpty())
263 emit setTabIcon(TQPixmap(TDEGlobal::dirs()->findResource(
"cache", favicon+
".png")));
265 emit setTabIcon(SmallIcon(
"text-html"));
271 void PageViewer::slotOpenURLRequest(
const KURL& url,
const KParts::URLArgs& args)
273 updateHistoryEntry();
276 browserExtension()->setURLArgs(args);
282 void PageViewer::slotPopupActivated(
int id )
284 TQValueList<HistoryEntry>::Iterator it = d->history.begin();
285 while( it != d->history.end() )
287 if ( (*it).id ==
id )
289 restoreHistoryEntry(it);
296 void PageViewer::updateHistoryEntry()
298 (*d->current).title = d->caption;
299 (*d->current).state = TQByteArray();
300 TQDataStream stream( (*d->current).state, IO_WriteOnly);
301 browserExtension()->saveState(stream);
304 void PageViewer::restoreHistoryEntry(
const TQValueList<HistoryEntry>::Iterator& entry)
306 updateHistoryEntry();
308 TQDataStream stream( (*entry).state, IO_ReadOnly );
309 browserExtension()->restoreState( stream );
311 d->backAction->setEnabled( d->current != d->history.begin() );
312 d->forwardAction->setEnabled( d->current != d->history.fromLast() );
319 void PageViewer::addHistoryEntry(
const KURL& url)
321 TQValueList<HistoryEntry>::Iterator it = d->current;
324 if ( it != d->history.end() && it != d->history.fromLast() )
326 d->history.erase( ++it, d->history.end() );
328 HistoryEntry newEntry( url, url.url() );
331 if ( newEntry.url != (*d->current).url )
333 d->history.append( newEntry );
334 d->current = d->history.fromLast();
336 updateHistoryEntry();
340 void PageViewer::slotStarted( TDEIO::Job * )
342 d->stopAction->setEnabled(
true);
346 void PageViewer::slotCompleted( )
348 d->stopAction->setEnabled(
false);
352 void PageViewer::slotCancelled(
const TQString & )
354 d->stopAction->setEnabled(
false);
357 void PageViewer::urlSelected(
const TQString &url,
int button,
int state,
const TQString &_target, KParts::URLArgs args)
359 if (url.startsWith(TQString::fromLatin1(
"javascript:" ),
false) )
361 TDEHTMLPart::urlSelected(url,button,state,_target,args);
365 if (button == TQt::LeftButton)
367 m_url = completeURL(url);
368 browserExtension()->setURLArgs(args);
369 slotOpenLinkInThisTab();
373 Viewer::urlSelected(url,button,state,_target,args);
378 void PageViewer::slotSetCaption(
const TQString& cap)
381 (*d->current).title = cap;
384 void PageViewer::slotPaletteOrFontChanged()
386 kdDebug() <<
"PageViewer::slotPaletteOrFontChanged()" << endl;
389 TQObject *obj = KParts::BrowserExtension::childObject(
this);
393 int id = obj->metaObject()->findSlot(
"reparseConfiguration()");
398 obj->tqt_invoke(
id, o);
403 TDEHTMLSettings* s =
const_cast<TDEHTMLSettings*
> (settings());
404 s->init(Settings::self()->config());
407 void PageViewer::slotGlobalBookmarkArticle()
409 KBookmarkManager *mgr = KBookmarkManager::userBookmarksManager();
410 KBookmarkGroup grp = mgr->root();
411 grp.addBookmark(mgr, d->caption, toplevelURL());
412 mgr->emitChanged(grp);
417 void PageViewer::slotPopupMenu(KXMLGUIClient*,
const TQPoint& p,
const KURL& kurl,
const KParts::URLArgs&, KParts::BrowserExtension::PopupFlags kpf, mode_t)
420 TQString url = kurl.url();
422 const bool showReload = (kpf & KParts::BrowserExtension::ShowReload) != 0;
423 const bool showNavigationItems = (kpf & KParts::BrowserExtension::ShowNavigationItems) != 0;
424 const bool isLink = (kpf & (KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowTextSelectionItems)) == 0;
425 const bool isSelection = (kpf & KParts::BrowserExtension::ShowTextSelectionItems) != 0;
427 TDEPopupMenu popup(this->widget());
429 int idNewWindow = -2;
432 idNewWindow = popup.insertItem(SmallIcon(
"tab_new"),i18n(
"Open Link in New &Tab"),
this, TQ_SLOT(slotOpenLinkInForegroundTab()));
433 popup.setWhatsThis(idNewWindow, i18n(
"<b>Open Link in New Tab</b><p>Opens current link in a new tab."));
434 popup.insertItem(SmallIcon(
"window-new"), i18n(
"Open Link in External &Browser"),
this, TQ_SLOT(slotOpenLinkInBrowser()));
436 popup.insertSeparator();
437 action(
"savelinkas")->plug(&popup);
438 TDEAction* copylinkaddress = action(
"copylinkaddress");
441 copylinkaddress->plug( &popup);
447 if (showNavigationItems)
449 d->backAction->plug( &popup );
450 d->forwardAction->plug( &popup );
454 d->reloadAction->plug(&popup);
456 d->stopAction->plug(&popup);
458 popup.insertSeparator();
462 action(
"viewer_copy")->plug(&popup);
463 popup.insertSeparator();
466 TDEAction* incFontAction = this->action(
"incFontSizes");
467 TDEAction* decFontAction = this->action(
"decFontSizes");
468 if ( incFontAction && decFontAction )
470 incFontAction->plug( &popup );
471 decFontAction->plug( &popup );
472 popup.insertSeparator();
475 popup.insertItem(SmallIcon(
"window-new"), i18n(
"Open Page in External Browser"),
this, TQ_SLOT(slotOpenLinkInBrowser()));
477 action(
"viewer_print")->plug(&popup);
478 popup.insertSeparator();
480 TDEAction *ac = action(
"setEncoding");
483 popup.insertItem(SmallIcon(
"bookmark_add"),i18n(
"Add to Konqueror Bookmarks"),
this, TQ_SLOT(slotGlobalBookmarkArticle()));
486 int r = popup.exec(p);
488 if (r == idNewWindow)
491 if (!KURL(url).path().startsWith(
"/"))
493 kdDebug() <<
"processing relative url: " << url << endl;
494 if (url.startsWith(
"#"))
496 kurl = KURL(PageViewer::url());
497 kurl.setRef(url.mid(1));
500 kurl = KURL(PageViewer::url().upURL().url(
true)+url);
505 if (kurl.isValid()) {
514 #include "pageviewer.moc"