summaryrefslogtreecommitdiffstats
path: root/ksayit/src/ksayitbookmarkhandler.cpp
blob: b52cdd70a1e3d608af7ba54f89b81961ec8a088c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// C++ Implementation: ksayitbookmarkhandler
//
// Description:
//
//
// Author: Robert Vogl <voglrobe@web.de>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//

// Qt include
#include <qregexp.h>

// KDE includes
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kurl.h>

// 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
}