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 )
42 setText( QString::number( vp.
pageNumber + 1 ) +
" - " + text() );
44 setProperty(
"htmlRef", bk.url().htmlRef());
47 inline int pageNumber()
const
49 return property(
"pageNumber").toInt();
52 inline QString htmlRef()
const
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;
110 virtual void openBookmark(
const KBookmark & bm, Qt::MouseButtons, Qt::KeyboardModifiers );
112 QHash<KUrl, QString>::iterator bookmarkFind(
const KUrl& url,
bool doCreate, KBookmarkGroup *result = 0);
115 void _o_changed(
const QString & groupAddress,
const QString & caller );
119 QHash<int,int> urlBookmarks;
122 KBookmarkManager * manager;
123 QHash<KUrl, QString> knownFiles;
128 if ( group.url().isValid() )
return group.url();
129 else return KUrl( group.fullText() );
133 :
QObject( document->m_parent ), d( new Private( this ) )
135 setObjectName( QLatin1String(
"Okular::BookmarkManager" ) );
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 );
144 connect( d->manager, SIGNAL(changed(QString,QString)),
145 this, SLOT(_o_changed(QString,QString)) );
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 )
179 if ( groupAddress.isEmpty() )
184 QHash<KUrl, QString>::iterator it = knownFiles.begin(), itEnd = knownFiles.end();
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 )
211 const QHash<int,int> oldUrlBookmarks = urlBookmarks;
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;
312 QHash<KUrl, QString>::iterator it = d->bookmarkFind( d->url,
false, &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();
337 QHash<KUrl, QString>::iterator BookmarkManager::Private::bookmarkFind(
const KUrl& url,
bool doCreate, KBookmarkGroup *result )
339 QHash<KUrl, QString>::iterator it = knownFiles.find( url );
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;
408 QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl,
true, &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 )
433 if ( title.isEmpty() )
440 newtitle = QString(
"#%1-%2" ).arg( vp.
pageNumber + 1 ).arg( count );
442 newtitle = QString(
"#%1" ).arg( vp.
pageNumber + 1 );
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;
480 QHash<KUrl, QString>::iterator it = d->bookmarkFind( d->url,
false, &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;
495 QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl,
false, &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;
507 QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl,
false, &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;
523 QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl,
false, &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;
545 QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl,
false, &thebg );
546 if ( it == d->knownFiles.end() )
549 const QHash<int,int> oldUrlBookmarks = d->urlBookmarks;
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 );
589 QList< QAction * > ret;
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() )
602 ret.append(
new OkularBookmarkAction(
DocumentViewport( b.url().htmlRef() ), b, d, 0 ) );
610 void BookmarkManager::setUrl(
const KUrl& url )
613 d->urlBookmarks.clear();
614 KBookmarkGroup thebg;
615 QHash<KUrl, QString>::iterator it = d->bookmarkFind( url,
false, &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;
635 QHash<KUrl, QString>::iterator it = d->bookmarkFind( d->url,
true, &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;
657 thebg.addBookmark( QString::fromLatin1(
"#" ) + QString::number( vp.
pageNumber + 1 ), newurl, QString() );
659 d->manager->emitChanged( thebg );
664 bool BookmarkManager::removePageBookmark(
int page )
666 KBookmarkGroup thebg;
667 QHash<KUrl, QString>::iterator it = d->bookmarkFind( d->url,
false, &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.
static bool bookmarkLessThan(const KBookmark &b1, const KBookmark &b2)
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)
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 ...
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.
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 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.
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.