15 #include <kbookmarkmanager.h>
16 #include <kbookmarkmenu.h>
19 #include <kstandarddirs.h>
25 using namespace Okular;
27 #define foreachObserver( cmd ) {\
28 QSet< DocumentObserver * >::const_iterator it = d->document->m_observers.constBegin(), end = d->document->m_observers.constEnd();\
29 for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
31 #define foreachObserverD( cmd ) {\
32 QSet< DocumentObserver * >::const_iterator it = document->m_observers.constBegin(), end = document->m_observers.constEnd();\
33 for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
35 class OkularBookmarkAction :
public KBookmarkAction
39 : KBookmarkAction( bk, owner, parent )
44 setProperty(
"htmlRef", bk.url().htmlRef());
47 inline int pageNumber()
const
49 return property(
"pageNumber").toInt();
54 return property(
"htmlRef").toString();
86 DocumentViewport vp1( static_cast< OkularBookmarkAction * >( a1 )->htmlRef() );
87 DocumentViewport vp2( static_cast< OkularBookmarkAction * >( a2 )->htmlRef() );
96 : KBookmarkOwner(), q( qq ), document( 0 ), manager( 0 )
107 virtual QString currentUrl()
const;
108 virtual QString currentTitle()
const;
109 virtual bool enableOption(BookmarkOption option)
const;
115 void _o_changed(
const QString & groupAddress,
const QString & caller );
122 KBookmarkManager * manager;
128 if ( group.url().isValid() )
return group.url();
129 else return KUrl( group.fullText() );
133 :
QObject( document->m_parent ), d( new Private( this ) )
137 d->document = document;
139 d->file = KStandardDirs::locateLocal(
"data",
"okular/bookmarks.xml" );
141 d->manager = KBookmarkManager::managerForFile( d->file,
"okular" );
142 d->manager->setEditorOptions( KGlobal::caption(),
false );
143 d->manager->setUpdate(
true );
148 BookmarkManager::~BookmarkManager()
154 QString BookmarkManager::Private::currentUrl()
const
156 return url.prettyUrl();
159 QString BookmarkManager::Private::currentTitle()
const
161 return url.isLocalFile() ? url.toLocalFile() : url.prettyUrl();
164 bool BookmarkManager::Private::enableOption(BookmarkOption option)
const
170 void BookmarkManager::Private::openBookmark( const KBookmark & bm, Qt::MouseButtons, Qt::KeyboardModifiers )
176 void BookmarkManager::Private::_o_changed(
const QString & groupAddress,
const QString & caller )
185 for ( ; it != itEnd; ++it )
187 if ( it.
value() == groupAddress )
190 knownFiles.erase( it );
194 if ( !referurl.isValid() )
196 const KBookmark bm = manager->findByAddress( groupAddress );
200 Q_ASSERT( bm.isGroup() );
203 Q_ASSERT( referurl.isValid() );
204 emit q->bookmarksChanged( referurl );
208 if ( referurl == url )
213 q->setUrl( referurl );
215 for (
int i = 0; i < qMax( oldUrlBookmarks.
size(), urlBookmarks.size() ); i++ )
217 bool oldContains = oldUrlBookmarks.
contains(i) && oldUrlBookmarks[i] > 0;
218 bool curContains = urlBookmarks.
contains(i) && urlBookmarks[i] > 0;
220 if ( oldContains != curContains )
224 else if ( oldContains && oldUrlBookmarks[i] != urlBookmarks[i] )
237 KBookmarkGroup group = d->manager->root();
238 for ( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) )
240 if ( bm.isSeparator() || !bm.isGroup() )
251 KBookmarkGroup group = d->manager->root();
252 for ( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) )
257 KBookmarkGroup group = bm.toGroup();
258 for ( KBookmark b = group.first(); !b.isNull(); b = group.next( b ) )
260 if ( b.isSeparator() || b.isGroup() )
278 const KBookmark::List bmarks =
bookmarks();
280 foreach(
const KBookmark &bm, bmarks )
283 if ( vp.isValid() && vp.pageNumber == page )
294 const KBookmark::List bmarks =
bookmarks();
295 foreach(
const KBookmark &bm, bmarks )
298 if ( vp.isValid() && vp.pageNumber == page )
311 KBookmarkGroup thebg;
313 if ( it == d->knownFiles.end() )
316 for ( KBookmark bm = thebg.first(); !bm.isNull(); bm = thebg.next( bm ) )
318 if ( bm.isSeparator() || bm.isGroup() )
333 d->manager->emitChanged();
340 if ( it == knownFiles.
end() )
346 KBookmarkGroup root = manager->root();
347 for ( KBookmark bm = root.first(); !found && !bm.isNull(); bm = root.next( bm ) )
349 if ( bm.isSeparator() || !bm.isGroup() )
356 KBookmarkGroup bg = bm.toGroup();
357 it = knownFiles.
insert( url, bg.address() );
364 if ( !found && doCreate )
368 QString purl = url.isLocalFile() ? url.toLocalFile() : url.prettyUrl();
369 KBookmarkGroup newbg = root.createNewFolder( purl );
371 it = knownFiles.
insert( url, newbg.address() );
378 const KBookmark bm = manager->findByAddress( it.
value() );
379 Q_ASSERT( bm.isGroup() );
380 *result = bm.toGroup();
387 if ( n >= 0 && n < (
int)d->document->m_pagesVector.count() )
389 if ( setPageBookmark( n ) )
401 if ( !referurl.isValid() || !vp.
isValid() )
407 KBookmarkGroup thebg;
409 Q_ASSERT( it != d->knownFiles.end() );
414 for ( KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next( bm ) )
416 if ( bm.isSeparator() || bm.isGroup() )
420 if ( bmViewport.isValid() && bmViewport.pageNumber == vp.
pageNumber )
447 KUrl newurl = referurl;
449 thebg.addBookmark( newtitle, newurl,
QString() );
450 if ( referurl == d->document->m_url )
455 d->manager->emitChanged( thebg );
461 if ( n >= 0 && n < (
int)d->document->m_pagesVector.count() )
463 if ( removePageBookmark( n ) )
471 if ( page >= 0 && page < d->document->
m_pagesVector.count() )
479 KBookmarkGroup thebg;
481 Q_ASSERT ( it != d->knownFiles.end() );
482 if ( it == d->knownFiles.end() )
485 bm->setFullText( newName );
486 d->manager->emitChanged( thebg );
491 if ( !referurl.isValid() )
494 KBookmarkGroup thebg;
496 Q_ASSERT ( it != d->knownFiles.end() );
497 if ( it == d->knownFiles.end() )
500 thebg.setFullText( newName );
501 d->manager->emitChanged( thebg );
506 KBookmarkGroup thebg;
508 Q_ASSERT( it != d->knownFiles.end() );
510 return thebg.fullText();
515 if ( !referurl.isValid() || bm.isNull() || bm.isGroup() || bm.isSeparator() )
522 KBookmarkGroup thebg;
524 if ( it == d->knownFiles.end() )
527 thebg.deleteBookmark( bm );
529 if ( referurl == d->document->m_url )
531 d->urlBookmarks[ vp.pageNumber ]--;
534 d->manager->emitChanged( thebg );
536 return vp.pageNumber;
541 if ( !referurl.isValid() || list.isEmpty() )
544 KBookmarkGroup thebg;
546 if ( it == d->knownFiles.end() )
550 bool deletedAny =
false;
551 foreach (
const KBookmark & bm, list )
553 if ( bm.parentGroup() == thebg )
555 thebg.deleteBookmark( bm );
559 if ( referurl == d->document->m_url )
561 d->urlBookmarks[ vp.pageNumber ]--;
566 if ( referurl == d->document->m_url )
568 for (
int i = 0; i < qMax( oldUrlBookmarks.
size(), d->urlBookmarks.size() ); i++ )
570 bool oldContains = oldUrlBookmarks.
contains(i) && oldUrlBookmarks[i] > 0;
571 bool curContains = d->urlBookmarks.contains(i) && d->urlBookmarks[i] > 0;
573 if ( oldContains != curContains )
577 else if ( oldContains && oldUrlBookmarks[i] != d->urlBookmarks[i] )
584 d->manager->emitChanged( thebg );
590 KBookmarkGroup group = d->manager->root();
591 for ( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) )
596 KBookmarkGroup group = bm.toGroup();
597 for ( KBookmark b = group.first(); !b.isNull(); b = group.next( b ) )
599 if ( b.isSeparator() || b.isGroup() )
610 void BookmarkManager::setUrl(
const KUrl& url )
613 d->urlBookmarks.clear();
614 KBookmarkGroup thebg;
616 if ( it != d->knownFiles.end() )
618 for ( KBookmark bm = thebg.first(); !bm.isNull(); bm = thebg.next( bm ) )
620 if ( bm.isSeparator() || bm.isGroup() )
627 d->urlBookmarks[ vp.pageNumber ]++;
632 bool BookmarkManager::setPageBookmark(
int page )
634 KBookmarkGroup thebg;
636 Q_ASSERT( it != d->knownFiles.end() );
640 for ( KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next( bm ) )
642 if ( bm.isSeparator() || bm.isGroup() )
646 if ( vp.isValid() && vp.pageNumber == page )
652 d->urlBookmarks[ page ]++;
655 KUrl newurl = d->url;
659 d->manager->emitChanged( thebg );
664 bool BookmarkManager::removePageBookmark(
int page )
666 KBookmarkGroup thebg;
668 if ( it == d->knownFiles.end() )
672 for ( KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next( bm ) )
674 if ( bm.isSeparator() || bm.isGroup() )
681 thebg.deleteBookmark( bm );
682 d->urlBookmarks[ page ]--;
683 d->manager->emitChanged( thebg );
691 return d->urlBookmarks.contains( page ) && d->urlBookmarks[ page ] > 0;
696 KBookmark bm =
bookmark( viewport );
707 foreach (
const KBookmark &bm, bmarks )
726 for ( KBookmark::List::const_iterator it = bmarks.
constEnd(); it != bmarks.
constBegin(); --it )
728 KBookmark bm = *(it-1);
740 #undef foreachObserver
741 #undef foreachObserverD
743 #include "bookmarkmanager.moc"
void removeBookmark(int page)
Remove a bookmark for the given page.
iterator insert(const Key &key, const T &value)
static bool bookmarkLessThan(const KBookmark &b1, const KBookmark &b2)
const Key key(const T &value) const
KBookmark bookmark(int page) const
Returns the bookmark for the given page of the document.
KBookmark nextBookmark(const DocumentViewport &viewport) const
Given a viewport, returns the next bookmark.
#define foreachObserverD(cmd)
KBookmark::List bookmarks() const
Returns the list of bookmarks for document.
Bookmarks has been changed.
void openUrl(const KUrl &url)
The bookmark manager is requesting to open the specified url.
QVector< Page * > m_pagesVector
static KUrl urlForGroup(const KBookmark &group)
static bool documentViewportFuzzyCompare(const DocumentViewport &vp1, const DocumentViewport &vp2)
QString number(int n, int base)
void append(const T &value)
const_iterator constEnd() const
static bool okularBookmarkActionLessThan(QAction *a1, QAction *a2)
int pageNumber
The number of the page nearest the center of the viewport.
struct Okular::DocumentViewport::@0 rePos
If 'rePos.enabled == true' then this structure contains the viewport center or top left depending on ...
const T value(const Key &key) const
iterator find(const Key &key)
QString titleForUrl(const KUrl &referurl) const
Returns title for the referurl.
#define foreachObserver(cmd)
bool isBookmarked(int page) const
Returns whether the given page is bookmarked.
const_iterator constBegin() const
QList< QAction * > actionsForUrl(const KUrl &url) const
Returns a list of actions for the bookmarks of the specified url.
void save() const
Forces to save the list of bookmarks.
void addBookmark(int page)
Adds a bookmark for the given page.
KUrl::List files() const
Returns the list of documents with bookmarks.
void saved()
This signal is emitted whenever bookmarks have been saved.
QString fromLatin1(const char *str, int size)
bool contains(const Key &key) const
QString toString() const
Returns the viewport as xml description.
KBookmark previousBookmark(const DocumentViewport &viewport) const
Given a viewport, returns the previous bookmark.
void renameBookmark(KBookmark *bm, const QString &newName)
Returns the bookmark given bookmark of the document.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void removeBookmarks(const KUrl &referurl, const KBookmark::List &list)
Removes the bookmarks in list for the referurl specified.
bool isValid() const
Returns whether the viewport is valid.
Bookmarks manager utility.
typedef KeyboardModifiers