2 // C++ Implementation: ksayitbookmarkhandler
7 // Author: Robert Vogl <voglrobe@web.de>, (C) 2005
9 // Copyright: See COPYING file that comes with this distribution
14 #include <QtCore/QRegExp>
19 #include <kmessagebox.h>
22 // App specific includes
23 #include "ksayitbookmarkhandler.h"
26 KSayItBookmarkHandler::KSayItBookmarkHandler(KBookmarkManager
*bkManager
, KSayItApp
* parent
)
27 : KBookmarkOwner(), m_bkManager(bkManager
), m_parent(parent
)
33 KSayItBookmarkHandler::~KSayItBookmarkHandler()
38 void KSayItBookmarkHandler::notifyBookmarkHandler(const QString
&ID
, const QString
&title
)
40 kDebug(100200) << "KSayItBookmarkHandler::notifyBookmarkManager()";
47 void KSayItBookmarkHandler::openBookmarkURL(const QString
&url
)
49 kDebug(100200) << "KSayItBookmarkHandler::openBookmarkURL(" << url
<< ")";
52 QString title
= QString();
53 QString type
= l_url
.section( "://", 0, 0 );
54 QString ID
= l_url
.section( QRegExp("/+"), 1, 1 );
55 QString err
=QString();
58 if ( type
!= "ksayit" ){
59 err
+= i18n("This is not a KSayIt bookmark.\n");
63 KBookmarkGroup bkRoot
= m_bkManager
->root();
64 if ( bkRoot
.isNull() )
69 bool found
= recursiveGetBkByURL( bookmark
, group
, bkRoot
, url
);
71 title
= bookmark
.text();
74 QString result
= QString();
75 result
= m_parent
->setItemByBookmark( ID
, title
);
76 if ( !result
.isNull() ){
77 KMessageBox::sorry( 0, result
, i18n("Bookmark not found") );
82 QString
KSayItBookmarkHandler::currentTitle() const
84 kDebug(100200) << "KSayItBookmarkHandler::currentTitle()";
87 if ( m_title
.isEmpty()){
88 result
= i18n("untitled");
97 QString
KSayItBookmarkHandler::currentUrl() const
99 kDebug(100200) << "KSayItBookmarkHandler::currentUrl()";
102 url
= "ksayit://" + m_ID
;
108 void KSayItBookmarkHandler::deleteBookmark(const QString
&url
, const QString
&title
)
110 kDebug(100200) << "KSayItBookmarkHandler::deleteBookmark()";
112 KBookmarkGroup bkRoot
= m_bkManager
->root();
113 if ( bkRoot
.isNull() )
116 // search bookmark by URL
118 KBookmarkGroup group
;
120 found
= recursiveGetBkByURL( bookmark
, group
, bkRoot
, url
);
122 group
.deleteBookmark( bookmark
);
123 m_bkManager
->emitChanged( group
);
124 m_bkManager
->save(); // make persistent
128 // if not found, search bookmark by title
130 qty
= recursiveGetBkByTitle( bookmark
, group
, bkRoot
, title
);
132 QString url
= bookmark
.url().url();
133 QString title
= bookmark
.text();
134 group
.deleteBookmark( bookmark
);
135 m_bkManager
->emitChanged( group
);
136 m_bkManager
->save(); // make persistent
141 bool KSayItBookmarkHandler::recursiveGetBkByURL(
143 KBookmarkGroup
&group
,
144 const KBookmarkGroup
&bkGroup
,
150 KBookmark bk
= bkGroup
.first();
151 while ( !bk
.isNull() && !bk
.isSeparator() ){
154 found
= recursiveGetBkByURL( bookmark
, group
, bk
.toGroup(), url
);
157 bkNext
= bkGroup
.next( bk
);
159 QString l_url
= bk
.url().url();
165 bkNext
= bkGroup
.next( bk
);
173 int KSayItBookmarkHandler::recursiveGetBkByTitle(
175 KBookmarkGroup
&group
,
176 const KBookmarkGroup
&bkGroup
,
177 const QString
&title
)
182 KBookmark bk
= bkGroup
.first();
183 while ( !bk
.isNull() && !bk
.isSeparator() ){
186 qty
+= recursiveGetBkByTitle( bookmark
, group
, bk
.toGroup(), title
);
187 bkNext
= bkGroup
.next( bk
);
189 QString l_title
= bk
.text();
190 if ( l_title
== title
){
195 bkNext
= bkGroup
.next( bk
);
203 void KSayItBookmarkHandler::traverseBookmarks(KBookmarkGroup bkGroup
)
205 kDebug(100200) << "### KSayItBookmarkHandler::traverseBookmarks()";
207 if( bkGroup
.isNull() )
212 KBookmark bkNext
, bkPrev
, bkNew
;
214 KBookmark bk
= bkGroup
.first();
215 while ( !bk
.isNull() && !bk
.isSeparator() ){
217 traverseBookmarks( bk
.toGroup() ); // recursive call
218 bkNext
= bkGroup
.next( bk
);
222 bkNext
= bkGroup
.next( bk
);
223 bkPrev
= bkGroup
.previous( bk
);
224 if ( bkPrev
.isNull() ) // no predecessor
227 // Modifications on URL/Title BEGIN
229 // Modifications on URL/Title END
231 bkNew
= bkGroup
.addBookmark( m_bkManager
, title
, url
, QString(), false );
232 bkGroup
.moveItem( bkNew
, bkPrev
);
233 bkGroup
.deleteBookmark( bk
);
237 m_bkManager
->save(); // make persistent