From 00bb99ac80741fc50ef8a289719373032f2391eb Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdeaccessibility@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- ksayit/src/ksayitbookmarkhandler.cpp | 238 +++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 ksayit/src/ksayitbookmarkhandler.cpp (limited to 'ksayit/src/ksayitbookmarkhandler.cpp') diff --git a/ksayit/src/ksayitbookmarkhandler.cpp b/ksayit/src/ksayitbookmarkhandler.cpp new file mode 100644 index 0000000..b52cdd7 --- /dev/null +++ b/ksayit/src/ksayitbookmarkhandler.cpp @@ -0,0 +1,238 @@ +// +// C++ Implementation: ksayitbookmarkhandler +// +// Description: +// +// +// Author: Robert Vogl , (C) 2005 +// +// Copyright: See COPYING file that comes with this distribution +// +// + +// Qt include +#include + +// KDE includes +#include +#include +#include +#include + +// App specific includes +#include "ksayitbookmarkhandler.h" +#include "ksayit.h" + +KSayItBookmarkHandler::KSayItBookmarkHandler(KBookmarkManager *bkManager, KSayItApp* parent) + : KBookmarkOwner(), m_bkManager(bkManager), m_parent(parent) +{ + m_ID = QString::null; + m_title = QString::null; +} + +KSayItBookmarkHandler::~KSayItBookmarkHandler() +{ +} + + +void KSayItBookmarkHandler::notifyBookmarkHandler(const QString &ID, const QString &title) +{ + kdDebug(100200) << "KSayItBookmarkHandler::notifyBookmarkManager()" << endl; + + m_ID = ID; + m_title = title; +} + + +void KSayItBookmarkHandler::openBookmarkURL(const QString &url) +{ + kdDebug(100200) << "KSayItBookmarkHandler::openBookmarkURL(" << url << ")" << endl; + + QString l_url = url; + QString title = QString::null; + QString type = l_url.section( "://", 0, 0 ); + QString ID = l_url.section( QRegExp("/+"), 1, 1 ); + QString err = QString::null; + + // Some checks + if ( type != "ksayit" ){ + err += i18n("This is not a KSayIt bookmark.\n"); + } + + // get title + KBookmarkGroup bkRoot = m_bkManager->root(); + if ( bkRoot.isNull() ) + return; + + KBookmark bookmark; + KBookmarkGroup group; + bool found = recursiveGetBkByURL( bookmark, group, bkRoot, url ); + if ( found ){ + title = bookmark.text(); + } + + QString result = QString::null; + result = m_parent->setItemByBookmark( ID, title ); + if ( !result.isNull() ){ + KMessageBox::sorry( 0, result, i18n("Bookmark not found") ); + } +} + + +QString KSayItBookmarkHandler::currentTitle() const +{ + kdDebug(100200) << "KSayItBookmarkHandler::currentTitle()" << endl; + + QString result; + if ( m_title.isEmpty()){ + result = i18n("untitled"); + } else { + result = m_title; + } + + return result; +} + + +QString KSayItBookmarkHandler::currentURL() const +{ + kdDebug(100200) << "KSayItBookmarkHandler::currentURL()" << endl; + + QString url; + url = "ksayit://" + m_ID; + + return url; +} + + +void KSayItBookmarkHandler::deleteBookmark(const QString &url, const QString &title) +{ + kdDebug(100200) << "KSayItBookmarkHandler::deleteBookmark()" << endl; + + KBookmarkGroup bkRoot = m_bkManager->root(); + if ( bkRoot.isNull() ) + return; + + // search bookmark by URL + KBookmark bookmark; + KBookmarkGroup group; + bool found = false; + found = recursiveGetBkByURL( bookmark, group, bkRoot, url ); + if ( found ){ + group.deleteBookmark( bookmark ); + m_bkManager->emitChanged( group ); + m_bkManager->save(); // make persistent + return; + } + + // if not found, search bookmark by title + int qty = 0; + qty = recursiveGetBkByTitle( bookmark, group, bkRoot, title ); + if ( qty == 1 ){ + QString url = bookmark.url().url(); + QString title = bookmark.text(); + group.deleteBookmark( bookmark ); + m_bkManager->emitChanged( group ); + m_bkManager->save(); // make persistent + } +} + + +bool KSayItBookmarkHandler::recursiveGetBkByURL( + KBookmark &bookmark, + KBookmarkGroup &group, + const KBookmarkGroup &bkGroup, + const QString &url) +{ + KBookmark bkNext; + bool found = false; + + KBookmark bk = bkGroup.first(); + while ( !bk.isNull() && !bk.isSeparator() ){ + if ( bk.isGroup() ){ + // recursive call + found = recursiveGetBkByURL( bookmark, group, bk.toGroup(), url ); + if ( found ) + return true; + bkNext = bkGroup.next( bk ); + } else { + QString l_url = bk.url().url(); + if ( l_url == url ){ + bookmark = bk; + group = bkGroup; + return true; + } + bkNext = bkGroup.next( bk ); + } + bk = bkNext; + } + return false; +} + + +int KSayItBookmarkHandler::recursiveGetBkByTitle( + KBookmark &bookmark, + KBookmarkGroup &group, + const KBookmarkGroup &bkGroup, + const QString &title) +{ + KBookmark bkNext; + int qty = 0; + + KBookmark bk = bkGroup.first(); + while ( !bk.isNull() && !bk.isSeparator() ){ + if ( bk.isGroup() ){ + // recursive call + qty += recursiveGetBkByTitle( bookmark, group, bk.toGroup(), title ); + bkNext = bkGroup.next( bk ); + } else { + QString l_title = bk.text(); + if ( l_title == title ){ + bookmark = bk; + group = bkGroup; + qty++; + } + bkNext = bkGroup.next( bk ); + } + bk = bkNext; + } + return qty; +} + + +void KSayItBookmarkHandler::traverseBookmarks(KBookmarkGroup bkGroup) +{ + kdDebug(100200) << "### KSayItBookmarkHandler::traverseBookmarks()" << endl; + + if( bkGroup.isNull() ) + return; + + KURL url; + QString title; + KBookmark bkNext, bkPrev, bkNew; + + KBookmark bk = bkGroup.first(); + while ( !bk.isNull() && !bk.isSeparator() ){ + if ( bk.isGroup() ){ + traverseBookmarks( bk.toGroup() ); // recursive call + bkNext = bkGroup.next( bk ); + } else { + url = bk.url(); + title = bk.text(); + bkNext = bkGroup.next( bk ); + bkPrev = bkGroup.previous( bk ); + if ( bkPrev.isNull() ) // no predecessor + bkPrev = bk; + + // Modifications on URL/Title BEGIN + // + // Modifications on URL/Title END + + bkNew = bkGroup.addBookmark( m_bkManager, title, url, QString::null, false ); + bkGroup.moveItem( bkNew, bkPrev ); + bkGroup.deleteBookmark( bk ); + } + bk = bkNext; + } + m_bkManager->save(); // make persistent +} -- cgit v1.2.1