fix i18nc typo in last commit
[kdeaccessibility.git] / ksayit / src / ksayitbookmarkhandler.cpp
blobadf1585d8a7ccbfdaba71f9cbd8938ab567ef00e
1 //
2 // C++ Implementation: ksayitbookmarkhandler
3 //
4 // Description:
5 //
6 //
7 // Author: Robert Vogl <voglrobe@web.de>, (C) 2005
8 //
9 // Copyright: See COPYING file that comes with this distribution
13 // Qt include
14 #include <QtCore/QRegExp>
16 // KDE includes
17 #include <kdebug.h>
18 #include <klocale.h>
19 #include <kmessagebox.h>
20 #include <kurl.h>
22 // App specific includes
23 #include "ksayitbookmarkhandler.h"
24 #include "ksayit.h"
26 KSayItBookmarkHandler::KSayItBookmarkHandler(KBookmarkManager *bkManager, KSayItApp* parent)
27 : KBookmarkOwner(), m_bkManager(bkManager), m_parent(parent)
29 m_ID .clear();
30 m_title .clear();
33 KSayItBookmarkHandler::~KSayItBookmarkHandler()
38 void KSayItBookmarkHandler::notifyBookmarkHandler(const QString &ID, const QString &title)
40 kDebug(100200) << "KSayItBookmarkHandler::notifyBookmarkManager()";
42 m_ID = ID;
43 m_title = title;
47 void KSayItBookmarkHandler::openBookmarkURL(const QString &url)
49 kDebug(100200) << "KSayItBookmarkHandler::openBookmarkURL(" << url << ")";
51 QString l_url = 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();
57 // Some checks
58 if ( type != "ksayit" ){
59 err += i18n("This is not a KSayIt bookmark.\n");
62 // get title
63 KBookmarkGroup bkRoot = m_bkManager->root();
64 if ( bkRoot.isNull() )
65 return;
67 KBookmark bookmark;
68 KBookmarkGroup group;
69 bool found = recursiveGetBkByURL( bookmark, group, bkRoot, url );
70 if ( found ){
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()";
86 QString result;
87 if ( m_title.isEmpty()){
88 result = i18n("untitled");
89 } else {
90 result = m_title;
93 return result;
97 QString KSayItBookmarkHandler::currentUrl() const
99 kDebug(100200) << "KSayItBookmarkHandler::currentUrl()";
101 QString url;
102 url = "ksayit://" + m_ID;
104 return url;
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() )
114 return;
116 // search bookmark by URL
117 KBookmark bookmark;
118 KBookmarkGroup group;
119 bool found = false;
120 found = recursiveGetBkByURL( bookmark, group, bkRoot, url );
121 if ( found ){
122 group.deleteBookmark( bookmark );
123 m_bkManager->emitChanged( group );
124 m_bkManager->save(); // make persistent
125 return;
128 // if not found, search bookmark by title
129 int qty = 0;
130 qty = recursiveGetBkByTitle( bookmark, group, bkRoot, title );
131 if ( qty == 1 ){
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(
142 KBookmark &bookmark,
143 KBookmarkGroup &group,
144 const KBookmarkGroup &bkGroup,
145 const QString &url)
147 KBookmark bkNext;
148 bool found = false;
150 KBookmark bk = bkGroup.first();
151 while ( !bk.isNull() && !bk.isSeparator() ){
152 if ( bk.isGroup() ){
153 // recursive call
154 found = recursiveGetBkByURL( bookmark, group, bk.toGroup(), url );
155 if ( found )
156 return true;
157 bkNext = bkGroup.next( bk );
158 } else {
159 QString l_url = bk.url().url();
160 if ( l_url == url ){
161 bookmark = bk;
162 group = bkGroup;
163 return true;
165 bkNext = bkGroup.next( bk );
167 bk = bkNext;
169 return false;
173 int KSayItBookmarkHandler::recursiveGetBkByTitle(
174 KBookmark &bookmark,
175 KBookmarkGroup &group,
176 const KBookmarkGroup &bkGroup,
177 const QString &title)
179 KBookmark bkNext;
180 int qty = 0;
182 KBookmark bk = bkGroup.first();
183 while ( !bk.isNull() && !bk.isSeparator() ){
184 if ( bk.isGroup() ){
185 // recursive call
186 qty += recursiveGetBkByTitle( bookmark, group, bk.toGroup(), title );
187 bkNext = bkGroup.next( bk );
188 } else {
189 QString l_title = bk.text();
190 if ( l_title == title ){
191 bookmark = bk;
192 group = bkGroup;
193 qty++;
195 bkNext = bkGroup.next( bk );
197 bk = bkNext;
199 return qty;
203 void KSayItBookmarkHandler::traverseBookmarks(KBookmarkGroup bkGroup)
205 kDebug(100200) << "### KSayItBookmarkHandler::traverseBookmarks()";
207 if( bkGroup.isNull() )
208 return;
210 KUrl url;
211 QString title;
212 KBookmark bkNext, bkPrev, bkNew;
214 KBookmark bk = bkGroup.first();
215 while ( !bk.isNull() && !bk.isSeparator() ){
216 if ( bk.isGroup() ){
217 traverseBookmarks( bk.toGroup() ); // recursive call
218 bkNext = bkGroup.next( bk );
219 } else {
220 url = bk.url();
221 title = bk.text();
222 bkNext = bkGroup.next( bk );
223 bkPrev = bkGroup.previous( bk );
224 if ( bkPrev.isNull() ) // no predecessor
225 bkPrev = bk;
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 );
235 bk = bkNext;
237 m_bkManager->save(); // make persistent