23 #include <kbookmarkbar.h>
24 #include <kbookmarkdrag.h>
26 #include <kbookmarkmenu.h>
29 #include <tdetoolbar.h>
30 #include <tdetoolbarbutton.h>
32 #include <tdeconfig.h>
33 #include <tdepopupmenu.h>
35 #include "kbookmarkdrag.h"
36 #include "kbookmarkmenu_p.h"
37 #include "kbookmarkdombuilder.h"
39 #include "dptrtemplate.h"
41 #include <tqapplication.h>
43 class KBookmarkBarPrivate :
public dPtrTemplate<KBookmarkBar, KBookmarkBarPrivate>
46 TQPtrList<TDEAction> m_actions;
49 TDEToolBar* m_sepToolBar;
52 TQString m_dropAddress;
53 TQString m_highlightedAddress;
55 KBookmarkBarPrivate() {
63 template<> TQPtrDict<KBookmarkBarPrivate>* dPtrTemplate<KBookmarkBar, KBookmarkBarPrivate>::d_ptr = 0;
65 KBookmarkBarPrivate* KBookmarkBar::dptr()
const
67 return KBookmarkBarPrivate::d(
this );
74 ToolbarFilter() : m_visible(false) { ; }
77 virtual void visit(
const KBookmark & );
87 TDEActionCollection *coll,
88 TQObject *parent,
const char *name )
89 : TQObject( parent, name ), m_pOwner(_owner), m_toolBar(_toolBar),
90 m_actionCollection( coll ), m_pManager(mgr)
92 m_lstSubMenus.setAutoDelete(
true );
94 m_toolBar->setAcceptDrops(
true );
95 m_toolBar->installEventFilter(
this );
97 dptr()->m_actions.setAutoDelete(
true );
99 connect( mgr, TQT_SIGNAL( changed(
const TQString &,
const TQString &) ),
100 TQT_SLOT( slotBookmarksChanged(
const TQString &) ) );
103 fillBookmarkBar( toolbar );
108 return dptr()->m_filteredMgr ? TQString::null : m_pManager->
toolbar().address();
111 #define CURRENT_TOOLBAR() ( \
112 dptr()->m_filteredMgr ? dptr()->m_filteredMgr->root() \
113 : m_pManager->toolbar() )
115 #define CURRENT_MANAGER() ( \
116 dptr()->m_filteredMgr ? dptr()->m_filteredMgr \
121 if ( KBookmarkSettings::self()->m_filteredtoolbar )
123 if ( !dptr()->m_filteredMgr ) {
124 dptr()->m_filteredMgr = KBookmarkManager::createTempManager();
127 TQValueList<KBookmark> bks;
128 for (KBookmark bm = bkRoot.
first(); !bm.isNull(); bm = bkRoot.
next(bm))
130 for ( TQValueListConstIterator<KBookmark> it = bks.begin(); it != bks.end(); ++it )
133 ToolbarFilter filter;
135 dptr()->m_filteredMgr );
136 builder.connectImporter( &filter );
137 filter.filter( m_pManager->
root() );
140 return CURRENT_TOOLBAR();
143 KBookmarkBar::~KBookmarkBar()
146 KBookmarkBarPrivate::delete_d(
this);
149 void KBookmarkBar::clear()
151 TQPtrListIterator<TDEAction> it( dptr()->m_actions );
153 for (; it.current(); ++it ) {
156 dptr()->m_actions.clear();
157 m_lstSubMenus.clear();
160 void KBookmarkBar::slotBookmarksChanged(
const TQString & group )
163 kdDebug(7043) <<
"slotBookmarksChanged( " << group <<
" )" << endl;
168 if ( KBookmark::commonParent(group, tb.address()) == group
169 || KBookmarkSettings::self()->m_filteredtoolbar )
172 fillBookmarkBar( tb );
177 TQPtrListIterator<KBookmarkMenu> it( m_lstSubMenus );
178 for (; it.current(); ++it )
180 it.current()->slotBookmarksChanged( group );
190 for (KBookmark bm = parent.
first(); !bm.isNull(); bm = parent.
next(bm))
192 TQString text = bm.text();
193 text.replace(
'&',
"&&" );
196 if ( bm.isSeparator() )
197 m_toolBar->insertLineSeparator();
200 TDEAction *action =
new KBookmarkAction( text, bm.icon(), 0, m_actionCollection, 0 );
201 connect(action, TQT_SIGNAL( activated ( TDEAction::ActivationReason, TQt::ButtonState )),
202 this, TQT_SLOT( slotBookmarkSelected( TDEAction::ActivationReason, TQt::ButtonState ) ));
204 action->setProperty(
"url", bm.url().url() );
205 action->setProperty(
"address", bm.address() );
207 action->setToolTip( bm.url().pathOrURL() );
209 action->plug(m_toolBar);
211 dptr()->m_actions.append( action );
216 TDEActionMenu *action =
new KBookmarkActionMenu( text, bm.icon(),
218 "bookmarkbar-actionmenu");
219 action->setProperty(
"address", bm.address() );
220 action->setProperty(
"readOnly", dptr()->m_readOnly );
221 action->setDelayed(
false );
224 TDEGlobal::config()->setGroup(
"Settings" );
225 bool addEntriesBookmarkBar = TDEGlobal::config()->readBoolEntry(
"AddEntriesBookmarkBar",
true);
228 m_actionCollection,
false, addEntriesBookmarkBar,
232 connect(menu, TQT_SIGNAL(
openBookmark(
const TQString &, TQt::ButtonState) ),
233 this, TQT_SIGNAL(
openBookmark(
const TQString &, TQt::ButtonState) ));
235 action->plug(m_toolBar);
236 m_lstSubMenus.append( menu );
238 dptr()->m_actions.append( action );
245 dptr()->m_readOnly = readOnly;
250 return dptr()->m_readOnly;
253 void KBookmarkBar::slotBookmarkSelected( TDEAction::ActivationReason , TQt::ButtonState state )
255 if (!m_pOwner)
return;
257 const TDEAction* action =
dynamic_cast<const TDEAction *
>(sender());
260 const TQString & url = sender()->property(
"url").toString();
266 void KBookmarkBar::slotBookmarkSelected()
268 slotBookmarkSelected(TDEAction::ToolBarActivation, Qt::NoButton);
271 static const int const_sepId = -9999;
275 static void removeTempSep(KBookmarkBarPrivate* p)
277 if (p->m_sepToolBar) {
278 p->m_sepToolBar->removeItem(const_sepId);
283 static TDEAction* findPluggedAction(TQPtrList<TDEAction> actions, TDEToolBar *tb,
int id)
285 TQPtrListIterator<TDEAction> it( actions );
287 if ((*it)->isPlugged(tb,
id))
302 static TQString handleToolbarDragMoveEvent(
303 KBookmarkBarPrivate *p, TDEToolBar *tb, TQPoint pos, TQPtrList<TDEAction> actions,
307 Q_ASSERT( actions.isEmpty() || (tb ==
dynamic_cast<TDEToolBar*
>(actions.first()->container(0))) );
308 p->m_sepToolBar = tb;
309 p->m_sepToolBar->removeItemDelayed(const_sepId);
314 b =
dynamic_cast<TDEToolBarButton*
>(tb->childAt(pos));
321 index = tb->itemIndex(b->id());
322 TQRect r = b->geometry();
323 if (pos.x() < ((r.left() + r.right())/2))
331 b = tb->getButton(tb->idAt(index));
335 else if (actions.isEmpty())
349 index = actions.count() - 1;
350 b = tb->getButton(tb->idAt(index));
352 if (pos.x() <= b->geometry().left())
357 return TQString::null;
359 a = findPluggedAction(actions, tb, b->id());
361 address = a->property(
"address").toString();
362 p->m_sepIndex = index + (atFirst ? 0 : 1);
369 kdDebug() <<
"kbookmarkbar:: popping up " << bk.text() << endl;
370 KBookmarkActionMenu *menu =
dynamic_cast<KBookmarkActionMenu*
>(a);
372 menu->popup(tb->mapToGlobal(b->geometry().center()));
378 tb->insertLineSeparator(p->m_sepIndex, const_sepId);
383 static TDEAction* handleToolbarMouseButton(TQPoint pos, TQPtrList<TDEAction> actions,
386 TDEAction *act = actions.first();
391 TDEToolBar *tb =
dynamic_cast<TDEToolBar*
>(act->container(0));
395 b =
dynamic_cast<TDEToolBarButton*
>(tb->childAt(pos));
400 a = findPluggedAction(actions, tb, b->id());
402 pt = tb->mapToGlobal(pos);
413 class KBookmarkBarRMBAssoc :
public dPtrTemplate<KBookmarkBar, RMB> { };
414 template<> TQPtrDict<RMB>* dPtrTemplate<KBookmarkBar, RMB>::d_ptr = 0;
416 static RMB* rmbSelf(
KBookmarkBar *m) {
return KBookmarkBarRMBAssoc::d(m); }
420 RMB *s = rmbSelf(
self);
422 s->m_parentAddress =
self->parentAddress();
423 s->s_highlightedAddress =
self->dptr()->m_highlightedAddress;
424 s->m_pManager =
self->m_pManager;
425 s->m_pOwner =
self->m_pOwner;
430 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionEditAt( val ); }
433 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionProperties( val ); }
436 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionInsert( val ); }
439 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionRemove( val ); }
442 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionCopyLocation( val ); }
444 bool KBookmarkBar::eventFilter( TQObject *o, TQEvent *e )
446 if (dptr()->m_readOnly || dptr()->m_filteredMgr)
450 if ( (e->type() == TQEvent::MouseButtonRelease) || (e->type() == TQEvent::MouseButtonPress) )
452 TQMouseEvent *mev = (TQMouseEvent*)e;
458 _a = handleToolbarMouseButton( mev->pos(), dptr()->m_actions, m_pManager, pt );
459 if (_a && mev->button() == Qt::RightButton)
461 dptr()->m_highlightedAddress = _a->property(
"address").toString();
462 KBookmark bookmark = m_pManager->
findByAddress( dptr()->m_highlightedAddress );
463 RMB::begin_rmb_action(
this);
464 TDEPopupMenu *pm =
new TDEPopupMenu;
465 rmbSelf(
this)->fillContextMenu( pm, dptr()->m_highlightedAddress, 0 );
467 rmbSelf(
this)->fillContextMenu2( pm, dptr()->m_highlightedAddress, 0 );
474 else if ( e->type() == TQEvent::DragLeave )
476 removeTempSep(dptr());
477 dptr()->m_dropAddress = TQString::null;
479 else if ( e->type() == TQEvent::Drop )
481 removeTempSep(dptr());
482 TQDropEvent *dev = (TQDropEvent*)e;
483 if ( !KBookmarkDrag::canDecode( dev ) )
485 TQValueList<KBookmark> list = KBookmarkDrag::decode( dev );
486 if (list.count() > 1)
487 kdWarning(7043) <<
"Sorry, currently you can only drop one address "
488 "onto the bookmark bar!" << endl;
489 KBookmark toInsert = list.first();
490 KBookmark bookmark = m_pManager->
findByAddress( dptr()->m_dropAddress );
491 Q_ASSERT(!bookmark.isNull());
492 kdDebug(7043) <<
"inserting "
493 << TQString(dptr()->m_atFirst ?
"before" :
"after")
494 <<
" dptr()->m_dropAddress == " << dptr()->m_dropAddress << endl;
496 Q_ASSERT(!parentBookmark.isNull());
497 KBookmark newBookmark = parentBookmark.
addBookmark(
498 m_pManager, toInsert.fullText(),
500 parentBookmark.
moveItem( newBookmark, dptr()->m_atFirst ? KBookmark() : bookmark );
504 else if ( e->type() == TQEvent::DragMove )
506 TQDragMoveEvent *dme = (TQDragMoveEvent*)e;
507 if (!KBookmarkDrag::canDecode( dme ))
510 TQString dropAddress;
511 TDEToolBar *tb = (TDEToolBar*)o;
512 dropAddress = handleToolbarDragMoveEvent(dptr(), tb, dme->pos(), dptr()->m_actions, _atFirst, m_pManager);
513 if (!dropAddress.isNull())
515 dptr()->m_dropAddress = dropAddress;
516 dptr()->m_atFirst = _atFirst;
523 static bool showInToolbar(
const KBookmark &bk ) {
524 return (bk.internalElement().attributes().namedItem(
"showintoolbar").toAttr().value() ==
"yes");
527 void ToolbarFilter::visit(
const KBookmark &bk ) {
529 if ( m_visible || showInToolbar(bk) )
530 KXBELBookmarkImporterImpl::visit(bk);
535 if ( !m_visible && showInToolbar(grp) )
537 m_visibleStart = grp;
541 KXBELBookmarkImporterImpl::visitEnter(grp);
547 KXBELBookmarkImporterImpl::visitLeave(grp);
548 if ( m_visible && grp.address() == m_visibleStart.address() )
552 #include "kbookmarkbar.moc"