2006-12-27 16:04:49 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Pino Toscano <pino@kde.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include "bookmarkmanager.h"
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
// qt/kde includes
|
|
|
|
#include <qhash.h>
|
|
|
|
#include <qset.h>
|
2007-01-13 15:26:05 +00:00
|
|
|
#include <kbookmarkmanager.h>
|
|
|
|
#include <kbookmarkmenu.h>
|
2006-12-27 16:04:49 +00:00
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kglobal.h>
|
|
|
|
#include <kstandarddirs.h>
|
|
|
|
|
|
|
|
// local includes
|
2007-10-28 18:31:33 +00:00
|
|
|
#include "document_p.h"
|
|
|
|
#include "observer.h"
|
2006-12-27 16:04:49 +00:00
|
|
|
|
|
|
|
using namespace Okular;
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
#define foreachObserver( cmd ) {\
|
2008-11-11 18:48:40 +00:00
|
|
|
QMap< int, DocumentObserver * >::const_iterator it = d->document->m_observers.constBegin(), end = d->document->m_observers.constEnd();\
|
2007-10-28 18:31:33 +00:00
|
|
|
for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
#define foreachObserverD( cmd ) {\
|
|
|
|
QMap< int, DocumentObserver * >::const_iterator it = document->m_observers.constBegin(), end = document->m_observers.constEnd();\
|
|
|
|
for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
|
|
|
|
|
2007-01-18 21:20:08 +00:00
|
|
|
class OkularBookmarkAction : public KBookmarkAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OkularBookmarkAction( const Okular::DocumentViewport& vp, const KBookmark& bk, KBookmarkOwner* owner, QObject *parent )
|
|
|
|
: KBookmarkAction( bk, owner, parent )
|
|
|
|
{
|
|
|
|
if ( vp.isValid() )
|
|
|
|
setText( QString::number( vp.pageNumber + 1 ) + " - " + text() );
|
2011-10-25 22:10:42 +00:00
|
|
|
setProperty("pageNumber", vp.pageNumber + 1);
|
2007-01-18 21:20:08 +00:00
|
|
|
}
|
2011-10-25 22:10:42 +00:00
|
|
|
|
2010-05-22 17:44:26 +00:00
|
|
|
inline int pageNumber() const
|
|
|
|
{
|
2011-10-25 22:10:42 +00:00
|
|
|
return property("pageNumber").toInt();
|
2010-05-22 17:44:26 +00:00
|
|
|
}
|
2007-01-18 21:20:08 +00:00
|
|
|
};
|
|
|
|
|
2010-05-22 17:44:26 +00:00
|
|
|
inline bool okularBookmarkActionLessThan( QAction * a1, QAction * a2 )
|
|
|
|
{
|
|
|
|
return static_cast< OkularBookmarkAction * >( a1 )->pageNumber()
|
|
|
|
< static_cast< OkularBookmarkAction * >( a2 )->pageNumber();
|
|
|
|
}
|
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
class BookmarkManager::Private : public KBookmarkOwner
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2007-01-13 15:26:05 +00:00
|
|
|
Private( BookmarkManager * qq )
|
|
|
|
: KBookmarkOwner(), q( qq ), document( 0 ), manager( 0 )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
knownFiles.clear();
|
2007-07-14 12:44:08 +00:00
|
|
|
// no need to delete the manager, it's automatically done by KBookmarkManager
|
|
|
|
// delete manager;
|
2007-01-13 15:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual QString currentUrl() const;
|
|
|
|
virtual QString currentTitle() const;
|
2007-08-13 10:51:17 +00:00
|
|
|
virtual bool enableOption(BookmarkOption option) const;
|
2007-01-13 15:26:05 +00:00
|
|
|
virtual void openBookmark( const KBookmark & bm, Qt::MouseButtons, Qt::KeyboardModifiers );
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
QHash<KUrl, QString>::iterator bookmarkFind( const KUrl& url, bool doCreate, KBookmarkGroup *result = 0);
|
|
|
|
|
|
|
|
// slots
|
|
|
|
void _o_changed( const QString & groupAddress, const QString & caller );
|
2008-02-26 21:27:19 +00:00
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
BookmarkManager * q;
|
2006-12-27 16:04:49 +00:00
|
|
|
KUrl url;
|
|
|
|
QSet<int> urlBookmarks;
|
2007-10-28 18:31:33 +00:00
|
|
|
DocumentPrivate * document;
|
2006-12-27 16:04:49 +00:00
|
|
|
QString file;
|
|
|
|
KBookmarkManager * manager;
|
2009-12-26 16:57:09 +00:00
|
|
|
QHash<KUrl, QString> knownFiles;
|
2006-12-27 16:04:49 +00:00
|
|
|
};
|
|
|
|
|
2010-03-27 18:00:48 +00:00
|
|
|
static inline KUrl urlForGroup(const KBookmark &group)
|
|
|
|
{
|
|
|
|
if ( group.url().isValid() ) return group.url();
|
|
|
|
else return KUrl( group.fullText() );
|
|
|
|
}
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
BookmarkManager::BookmarkManager( DocumentPrivate * document )
|
|
|
|
: QObject( document->m_parent ), d( new Private( this ) )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2010-09-22 11:52:46 +00:00
|
|
|
setObjectName( QLatin1String( "Okular::BookmarkManager" ) );
|
2006-12-27 16:04:49 +00:00
|
|
|
|
|
|
|
d->document = document;
|
|
|
|
|
|
|
|
d->file = KStandardDirs::locateLocal( "data", "okular/bookmarks.xml" );
|
|
|
|
|
2007-06-25 21:43:25 +00:00
|
|
|
d->manager = KBookmarkManager::managerForFile( d->file, "okular" );
|
2007-01-29 20:18:39 +00:00
|
|
|
d->manager->setEditorOptions( KGlobal::caption(), false );
|
2006-12-27 16:04:49 +00:00
|
|
|
d->manager->setUpdate( true );
|
2011-07-31 19:22:04 +00:00
|
|
|
connect( d->manager, SIGNAL(changed(QString,QString)),
|
|
|
|
this, SLOT(_o_changed(QString,QString)) );
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkManager::~BookmarkManager()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
//BEGIN Reimplementations from KBookmarkOwner
|
2007-01-13 15:26:05 +00:00
|
|
|
QString BookmarkManager::Private::currentUrl() const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2007-01-13 15:26:05 +00:00
|
|
|
return url.prettyUrl();
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
QString BookmarkManager::Private::currentTitle() const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2010-01-08 15:29:38 +00:00
|
|
|
return url.isLocalFile() ? url.toLocalFile() : url.prettyUrl();
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2007-08-13 10:51:17 +00:00
|
|
|
bool BookmarkManager::Private::enableOption(BookmarkOption option) const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2007-08-14 12:26:08 +00:00
|
|
|
Q_UNUSED( option )
|
2007-01-13 15:26:05 +00:00
|
|
|
return false;
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
void BookmarkManager::Private::openBookmark( const KBookmark & bm, Qt::MouseButtons, Qt::KeyboardModifiers )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2007-01-13 15:26:05 +00:00
|
|
|
emit q->openUrl( bm.url() );
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
//END Reimplementations from KBookmarkOwner
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
void BookmarkManager::Private::_o_changed( const QString & groupAddress, const QString & caller )
|
|
|
|
{
|
|
|
|
Q_UNUSED( caller );
|
|
|
|
if ( groupAddress.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
KUrl referurl;
|
|
|
|
// first, try to find the bookmark group whom change notification was just received
|
|
|
|
QHash<KUrl, QString>::iterator it = knownFiles.begin(), itEnd = knownFiles.end();
|
|
|
|
for ( ; it != itEnd; ++it )
|
|
|
|
{
|
|
|
|
if ( it.value() == groupAddress )
|
|
|
|
{
|
|
|
|
referurl = it.key();
|
|
|
|
knownFiles.erase( it );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !referurl.isValid() )
|
|
|
|
{
|
|
|
|
const KBookmark bm = manager->findByAddress( groupAddress );
|
|
|
|
// better be safe than sorry
|
|
|
|
if ( bm.isNull() )
|
|
|
|
return;
|
|
|
|
Q_ASSERT( bm.isGroup() );
|
2010-03-27 18:00:48 +00:00
|
|
|
referurl = urlForGroup( bm );
|
2009-12-26 16:57:09 +00:00
|
|
|
}
|
|
|
|
Q_ASSERT( referurl.isValid() );
|
|
|
|
emit q->bookmarksChanged( referurl );
|
|
|
|
// case for the url representing the current document
|
|
|
|
// (this might happen if the same document is open in another place;
|
|
|
|
// in such case, make really sure to be in sync)
|
|
|
|
if ( referurl == url )
|
|
|
|
{
|
|
|
|
// save the old bookmarks for the current url
|
|
|
|
const QSet<int> oldUrlBookmarks = urlBookmarks;
|
|
|
|
// set the same url again, so we reload the information we have about it
|
|
|
|
q->setUrl( referurl );
|
|
|
|
// then notify the observers about the changes in the bookmarks
|
|
|
|
foreach ( int p, ( oldUrlBookmarks + urlBookmarks ) - ( oldUrlBookmarks & urlBookmarks ) )
|
|
|
|
{
|
|
|
|
foreachObserverD( notifyPageChanged( p, DocumentObserver::Bookmark ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit q->saved();
|
|
|
|
}
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
KUrl::List BookmarkManager::files() const
|
|
|
|
{
|
|
|
|
KUrl::List ret;
|
|
|
|
KBookmarkGroup group = d->manager->root();
|
|
|
|
for ( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) )
|
|
|
|
{
|
|
|
|
if ( bm.isSeparator() || !bm.isGroup() )
|
|
|
|
continue;
|
|
|
|
|
2010-03-27 18:00:48 +00:00
|
|
|
ret.append( urlForGroup( bm ) );
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
KBookmark::List BookmarkManager::bookmarks( const KUrl& url ) const
|
|
|
|
{
|
|
|
|
KBookmark::List ret;
|
|
|
|
KBookmarkGroup group = d->manager->root();
|
|
|
|
for ( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) )
|
|
|
|
{
|
2010-03-27 18:00:48 +00:00
|
|
|
if ( !bm.isGroup() || urlForGroup( bm ) != url )
|
2006-12-27 16:04:49 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
KBookmarkGroup group = bm.toGroup();
|
|
|
|
for ( KBookmark b = group.first(); !b.isNull(); b = group.next( b ) )
|
|
|
|
{
|
|
|
|
if ( b.isSeparator() || b.isGroup() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret.append( b );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-25 20:21:22 +00:00
|
|
|
KBookmark::List BookmarkManager::bookmarks() const
|
|
|
|
{
|
|
|
|
return bookmarks( d->url );
|
|
|
|
}
|
|
|
|
|
|
|
|
KBookmark BookmarkManager::bookmark( int page ) const
|
|
|
|
{
|
|
|
|
const KBookmark::List bmarks = bookmarks();
|
|
|
|
foreach( const KBookmark &bm, bmarks )
|
|
|
|
{
|
|
|
|
DocumentViewport vp( bm.url().htmlRef() );
|
|
|
|
if ( vp.isValid() && vp.pageNumber == page )
|
|
|
|
{
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return KBookmark();
|
|
|
|
}
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
void BookmarkManager::save() const
|
|
|
|
{
|
2009-12-26 16:57:09 +00:00
|
|
|
d->manager->emitChanged();
|
2008-02-05 17:36:04 +00:00
|
|
|
emit const_cast<BookmarkManager*>( this )->saved();
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
QHash<KUrl, QString>::iterator BookmarkManager::Private::bookmarkFind( const KUrl& url, bool doCreate, KBookmarkGroup *result )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2009-12-26 16:57:09 +00:00
|
|
|
QHash<KUrl, QString>::iterator it = knownFiles.find( url );
|
2008-02-26 21:27:19 +00:00
|
|
|
if ( it == knownFiles.end() )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
// if the url we want to add a new entry for is not in the hash of the
|
|
|
|
// known files, then first try to find the file among the top-level
|
|
|
|
// "folder" names
|
|
|
|
bool found = false;
|
|
|
|
KBookmarkGroup root = manager->root();
|
|
|
|
for ( KBookmark bm = root.first(); !found && !bm.isNull(); bm = root.next( bm ) )
|
|
|
|
{
|
|
|
|
if ( bm.isSeparator() || !bm.isGroup() )
|
|
|
|
continue;
|
|
|
|
|
2010-03-27 18:00:48 +00:00
|
|
|
KUrl tmpurl( urlForGroup( bm ) );
|
2006-12-27 16:04:49 +00:00
|
|
|
if ( tmpurl == url )
|
|
|
|
{
|
|
|
|
// got it! place it the hash of known files
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup bg = bm.toGroup();
|
|
|
|
it = knownFiles.insert( url, bg.address() );
|
2006-12-27 16:04:49 +00:00
|
|
|
found = true;
|
2009-12-26 16:57:09 +00:00
|
|
|
if ( result )
|
|
|
|
*result = bg;
|
2006-12-27 16:04:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !found && doCreate )
|
|
|
|
{
|
|
|
|
// folder not found :(
|
|
|
|
// then, in a single step create a new folder and add it in our cache :)
|
2010-01-08 15:29:38 +00:00
|
|
|
QString purl = url.isLocalFile() ? url.toLocalFile() : url.prettyUrl();
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup newbg = root.createNewFolder( purl );
|
2010-03-27 18:00:48 +00:00
|
|
|
newbg.setUrl( url );
|
2009-12-26 16:57:09 +00:00
|
|
|
it = knownFiles.insert( url, newbg.address() );
|
|
|
|
if ( result )
|
|
|
|
*result = newbg;
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-26 16:57:09 +00:00
|
|
|
else if ( result )
|
|
|
|
{
|
|
|
|
const KBookmark bm = manager->findByAddress( it.value() );
|
|
|
|
Q_ASSERT( bm.isGroup() );
|
|
|
|
*result = bm.toGroup();
|
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
void BookmarkManager::addBookmark( int n )
|
|
|
|
{
|
|
|
|
if ( n >= 0 && n < (int)d->document->m_pagesVector.count() )
|
|
|
|
{
|
|
|
|
if ( setPageBookmark( n ) )
|
|
|
|
foreachObserver( notifyPageChanged( n, DocumentObserver::Bookmark ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
bool BookmarkManager::addBookmark( const KUrl& referurl, const Okular::DocumentViewport& vp, const QString& title )
|
|
|
|
{
|
|
|
|
if ( !referurl.isValid() || !vp.isValid() )
|
|
|
|
return false;
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
|
|
|
QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl, true, &thebg );
|
2006-12-27 16:04:49 +00:00
|
|
|
Q_ASSERT( it != d->knownFiles.end() );
|
|
|
|
|
|
|
|
QString newtitle;
|
|
|
|
if ( title.isEmpty() )
|
|
|
|
{
|
|
|
|
// if we have no title specified for the new bookmark, then give it the
|
|
|
|
// name '#n' where n is the index of this bookmark among the ones of
|
|
|
|
// its file
|
|
|
|
int count = 0;
|
2009-12-26 16:57:09 +00:00
|
|
|
for ( KBookmark bm = thebg.first(); !bm.isNull(); bm = thebg.next( bm ) )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
if ( !bm.isSeparator() && !bm.isGroup() )
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
newtitle = QString( "#%1" ).arg( count + 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
newtitle = title;
|
|
|
|
KUrl newurl = referurl;
|
2006-12-27 17:02:45 +00:00
|
|
|
newurl.setHTMLRef( vp.toString() );
|
2009-12-26 16:57:09 +00:00
|
|
|
thebg.addBookmark( newtitle, newurl, QString() );
|
2008-04-04 09:50:41 +00:00
|
|
|
if ( referurl == d->document->m_url )
|
|
|
|
{
|
|
|
|
d->urlBookmarks.insert( vp.pageNumber );
|
|
|
|
foreachObserver( notifyPageChanged( vp.pageNumber, DocumentObserver::Bookmark ) );
|
|
|
|
}
|
2009-12-26 16:57:09 +00:00
|
|
|
d->manager->emitChanged( thebg );
|
2006-12-27 16:04:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
void BookmarkManager::removeBookmark( int n )
|
|
|
|
{
|
|
|
|
if ( n >= 0 && n < (int)d->document->m_pagesVector.count() )
|
|
|
|
{
|
|
|
|
if ( removePageBookmark( n ) )
|
|
|
|
foreachObserver( notifyPageChanged( n, DocumentObserver::Bookmark ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-25 20:21:22 +00:00
|
|
|
void BookmarkManager::renameBookmark( KBookmark* bm, const QString& newName)
|
|
|
|
{
|
|
|
|
KBookmarkGroup thebg;
|
|
|
|
QHash<KUrl, QString>::iterator it = d->bookmarkFind( d->url, false, &thebg );
|
|
|
|
Q_ASSERT ( it != d->knownFiles.end() );
|
|
|
|
if ( it == d->knownFiles.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
bm->setFullText( newName );
|
|
|
|
d->manager->emitChanged( thebg );
|
|
|
|
}
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
int BookmarkManager::removeBookmark( const KUrl& referurl, const KBookmark& bm )
|
|
|
|
{
|
|
|
|
if ( !referurl.isValid() || bm.isNull() || bm.isGroup() || bm.isSeparator() )
|
|
|
|
return -1;
|
|
|
|
|
2006-12-27 17:02:45 +00:00
|
|
|
DocumentViewport vp( bm.url().htmlRef() );
|
2006-12-27 16:04:49 +00:00
|
|
|
if ( !vp.isValid() )
|
|
|
|
return -1;
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
|
|
|
QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl, false, &thebg );
|
2006-12-27 16:04:49 +00:00
|
|
|
if ( it == d->knownFiles.end() )
|
|
|
|
return -1;
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
thebg.deleteBookmark( bm );
|
2008-04-04 09:50:41 +00:00
|
|
|
if ( referurl == d->document->m_url )
|
|
|
|
{
|
|
|
|
d->urlBookmarks.remove( vp.pageNumber );
|
|
|
|
foreachObserver( notifyPageChanged( vp.pageNumber, DocumentObserver::Bookmark ) );
|
|
|
|
}
|
2009-12-26 16:57:09 +00:00
|
|
|
d->manager->emitChanged( thebg );
|
2007-10-28 18:31:33 +00:00
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
return vp.pageNumber;
|
|
|
|
}
|
|
|
|
|
2010-01-09 13:13:43 +00:00
|
|
|
void BookmarkManager::removeBookmarks( const KUrl& referurl, const KBookmark::List& list )
|
|
|
|
{
|
|
|
|
if ( !referurl.isValid() || list.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
KBookmarkGroup thebg;
|
|
|
|
QHash<KUrl, QString>::iterator it = d->bookmarkFind( referurl, false, &thebg );
|
|
|
|
if ( it == d->knownFiles.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QSet<int> oldUrlBookmarks = d->urlBookmarks;
|
|
|
|
bool deletedAny = false;
|
|
|
|
foreach ( const KBookmark & bm, list )
|
|
|
|
{
|
|
|
|
if ( bm.parentGroup() == thebg )
|
|
|
|
{
|
|
|
|
thebg.deleteBookmark( bm );
|
|
|
|
deletedAny = true;
|
|
|
|
if ( referurl == d->document->m_url )
|
|
|
|
{
|
|
|
|
d->urlBookmarks.remove( DocumentViewport( bm.url().htmlRef() ).pageNumber );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( referurl == d->document->m_url )
|
|
|
|
{
|
|
|
|
foreach ( int p, oldUrlBookmarks - d->urlBookmarks )
|
|
|
|
{
|
|
|
|
foreachObserver( notifyPageChanged( p, DocumentObserver::Bookmark ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( deletedAny )
|
|
|
|
d->manager->emitChanged( thebg );
|
|
|
|
}
|
|
|
|
|
2007-01-13 17:28:54 +00:00
|
|
|
QList< QAction * > BookmarkManager::actionsForUrl( const KUrl& url ) const
|
|
|
|
{
|
|
|
|
QList< QAction * > ret;
|
|
|
|
KBookmarkGroup group = d->manager->root();
|
|
|
|
for ( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) )
|
|
|
|
{
|
2010-03-27 18:00:48 +00:00
|
|
|
if ( !bm.isGroup() || urlForGroup( bm ) != url )
|
2007-01-13 17:28:54 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
KBookmarkGroup group = bm.toGroup();
|
|
|
|
for ( KBookmark b = group.first(); !b.isNull(); b = group.next( b ) )
|
|
|
|
{
|
|
|
|
if ( b.isSeparator() || b.isGroup() )
|
|
|
|
continue;
|
|
|
|
|
2007-01-18 21:20:08 +00:00
|
|
|
ret.append( new OkularBookmarkAction( DocumentViewport( b.url().htmlRef() ), b, d, 0 ) );
|
2007-01-13 17:28:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-05-22 17:44:26 +00:00
|
|
|
qSort( ret.begin(), ret.end(), okularBookmarkActionLessThan );
|
2007-01-13 17:28:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
void BookmarkManager::setUrl( const KUrl& url )
|
|
|
|
{
|
|
|
|
d->url = url;
|
|
|
|
d->urlBookmarks.clear();
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
|
|
|
QHash<KUrl, QString>::iterator it = d->bookmarkFind( url, false, &thebg );
|
2006-12-27 16:04:49 +00:00
|
|
|
if ( it != d->knownFiles.end() )
|
|
|
|
{
|
2009-12-26 16:57:09 +00:00
|
|
|
for ( KBookmark bm = thebg.first(); !bm.isNull(); bm = thebg.next( bm ) )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
if ( bm.isSeparator() || bm.isGroup() )
|
|
|
|
continue;
|
|
|
|
|
2006-12-27 17:02:45 +00:00
|
|
|
DocumentViewport vp( bm.url().htmlRef() );
|
2006-12-27 16:04:49 +00:00
|
|
|
if ( !vp.isValid() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
d->urlBookmarks.insert( vp.pageNumber );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BookmarkManager::setPageBookmark( int page )
|
|
|
|
{
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
|
|
|
QHash<KUrl, QString>::iterator it = d->bookmarkFind( d->url, true, &thebg );
|
2006-12-27 16:04:49 +00:00
|
|
|
Q_ASSERT( it != d->knownFiles.end() );
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
bool added = false;
|
2009-12-26 16:57:09 +00:00
|
|
|
for ( KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next( bm ) )
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
if ( bm.isSeparator() || bm.isGroup() )
|
|
|
|
continue;
|
|
|
|
|
2006-12-27 17:02:45 +00:00
|
|
|
DocumentViewport vp( bm.url().htmlRef() );
|
2006-12-27 16:04:49 +00:00
|
|
|
if ( vp.isValid() && vp.pageNumber == page )
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
if ( !found )
|
|
|
|
{
|
|
|
|
d->urlBookmarks.insert( page );
|
|
|
|
DocumentViewport vp;
|
|
|
|
vp.pageNumber = page;
|
|
|
|
KUrl newurl = d->url;
|
2006-12-27 17:02:45 +00:00
|
|
|
newurl.setHTMLRef( vp.toString() );
|
2009-12-26 16:57:09 +00:00
|
|
|
thebg.addBookmark( QString::fromLatin1( "#" ) + QString::number( vp.pageNumber + 1 ), newurl, QString() );
|
2006-12-27 16:04:49 +00:00
|
|
|
added = true;
|
2009-12-26 16:57:09 +00:00
|
|
|
d->manager->emitChanged( thebg );
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
2007-01-13 14:45:51 +00:00
|
|
|
bool BookmarkManager::removePageBookmark( int page )
|
|
|
|
{
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
|
|
|
QHash<KUrl, QString>::iterator it = d->bookmarkFind( d->url, false, &thebg );
|
2007-01-13 14:45:51 +00:00
|
|
|
if ( it == d->knownFiles.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool found = false;
|
2009-12-26 16:57:09 +00:00
|
|
|
for ( KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next( bm ) )
|
2007-01-13 14:45:51 +00:00
|
|
|
{
|
|
|
|
if ( bm.isSeparator() || bm.isGroup() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DocumentViewport vp( bm.url().htmlRef() );
|
|
|
|
if ( vp.isValid() && vp.pageNumber == page )
|
|
|
|
{
|
|
|
|
found = true;
|
2009-12-26 16:57:09 +00:00
|
|
|
thebg.deleteBookmark( bm );
|
2007-01-13 14:45:51 +00:00
|
|
|
d->urlBookmarks.remove( page );
|
2009-12-26 16:57:09 +00:00
|
|
|
d->manager->emitChanged( thebg );
|
2007-01-13 14:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
bool BookmarkManager::isBookmarked( int page ) const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
return d->urlBookmarks.contains( page );
|
|
|
|
}
|
|
|
|
|
2009-01-10 11:32:54 +00:00
|
|
|
#undef foreachObserver
|
2009-12-26 16:57:09 +00:00
|
|
|
#undef foreachObserverD
|
2009-01-10 11:32:54 +00:00
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
#include "bookmarkmanager.moc"
|
2010-03-27 18:00:48 +00:00
|
|
|
|
|
|
|
/* kate: replace-tabs on; indent-width 4; */
|