~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to konqueror/keditbookmarks/search.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode:cperl; cperl-indent-level:4; cperl-continued-statement-offset:4; indent-tabs-mode:nil -*-
 
2
// vim: set ts=4 sts=4 sw=4 et:
 
3
/* This file is part of the KDE project
 
4
   Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
 
5
 
 
6
   This program is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU General Public
 
8
   License version 2 as published by the Free Software Foundation.
 
9
 
 
10
   This program is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU General Public License
 
16
   along with this program; see the file COPYING.  If not, write to
 
17
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
#include "toplevel.h"
 
22
#include "listview.h"
 
23
#include "search.h"
 
24
#include "commands.h"
 
25
 
 
26
#include <qregexp.h>
 
27
#include <qtimer.h>
 
28
 
 
29
#include <kdebug.h>
 
30
#include <klocale.h>
 
31
#include <kfind.h>
 
32
 
 
33
#include <kbookmarkmanager.h>
 
34
 
 
35
MagicKLineEdit::MagicKLineEdit(
 
36
   const QString &text, QWidget *parent, const char *name
 
37
) : KLineEdit(text, parent, name), m_grayedText(text) {
 
38
    setPaletteForegroundColor(gray);
 
39
}
 
40
 
 
41
void MagicKLineEdit::focusInEvent(QFocusEvent *ev) {
 
42
    if (text() == m_grayedText)
 
43
        setText(QString::null);
 
44
    QLineEdit::focusInEvent(ev);
 
45
}
 
46
 
 
47
void MagicKLineEdit::focusOutEvent(QFocusEvent *ev) {
 
48
    if (text().isEmpty()) {
 
49
        setText(m_grayedText);
 
50
        setPaletteForegroundColor(gray); 
 
51
    }
 
52
    QLineEdit::focusOutEvent(ev);
 
53
}
 
54
 
 
55
void MagicKLineEdit::mousePressEvent(QMouseEvent *ev) {
 
56
    setPaletteForegroundColor(parentWidget()->paletteForegroundColor()); 
 
57
    QLineEdit::mousePressEvent(ev);
 
58
}
 
59
 
 
60
class KBookmarkTextMap : private KBookmarkGroupTraverser {
 
61
public:
 
62
    KBookmarkTextMap(KBookmarkManager *);
 
63
    void update();
 
64
    QValueList<KBookmark> find(const QString &text) const;
 
65
private:
 
66
    virtual void visit(const KBookmark &);
 
67
    virtual void visitEnter(const KBookmarkGroup &);
 
68
    virtual void visitLeave(const KBookmarkGroup &) { ; }
 
69
private:
 
70
    typedef QValueList<KBookmark> KBookmarkList;
 
71
    QMap<QString, KBookmarkList> m_bk_map;
 
72
    KBookmarkManager *m_manager;
 
73
};
 
74
 
 
75
KBookmarkTextMap::KBookmarkTextMap( KBookmarkManager *manager ) {
 
76
    m_manager = manager;
 
77
}
 
78
 
 
79
void KBookmarkTextMap::update()
 
80
{
 
81
    m_bk_map.clear();
 
82
    KBookmarkGroup root = m_manager->root();
 
83
    traverse(root);
 
84
}
 
85
 
 
86
void KBookmarkTextMap::visit(const KBookmark &bk) {
 
87
    if (!bk.isSeparator()) {
 
88
        QString text = bk.url().url() + " " + bk.text() + NodeEditCommand::getNodeText(bk, "desc");
 
89
        m_bk_map[text].append(bk);
 
90
    }
 
91
}
 
92
 
 
93
void KBookmarkTextMap::visitEnter(const KBookmarkGroup &grp) {
 
94
    visit(grp);
 
95
}
 
96
 
 
97
QValueList<KBookmark> KBookmarkTextMap::find(const QString &text) const
 
98
{
 
99
    QValueList<KBookmark> matches;
 
100
    QValueList<QString> keys = m_bk_map.keys();
 
101
    for (QValueList<QString>::iterator it = keys.begin();
 
102
            it != keys.end(); ++it 
 
103
        ) {
 
104
        if ((*it).find(text,0,false) != -1)
 
105
            matches += m_bk_map[(*it)];
 
106
    }
 
107
    return matches;
 
108
}
 
109
 
 
110
Searcher* Searcher::s_self = 0;
 
111
 
 
112
static unsigned int m_currentResult;
 
113
 
 
114
class Address
 
115
{
 
116
public:
 
117
    Address() { ; }
 
118
    Address(const QString &str) { m_string = str; }
 
119
    virtual ~Address() { ; }
 
120
    QString string() const { return m_string; }
 
121
    bool operator< ( const Address & s2 ) const {
 
122
        bool ret = Address::addressStringCompare(string(), s2.string());
 
123
        // kdDebug() << string() << " < " << s2.string() << " == " << ret << endl;
 
124
        return ret;
 
125
    }
 
126
    static bool addressStringCompare(const QString & s1, const QString & s2) {
 
127
        QStringList s1s = QStringList::split("/", s1);
 
128
        QStringList s2s = QStringList::split("/", s2);
 
129
        for (unsigned int n = 0; ; n++) {
 
130
            if (n >= s1s.count())  // "/0/*5" < "/0"
 
131
                return true;
 
132
            if (n >= s2s.count())  // "/0" > "/0/*5"
 
133
                return false;
 
134
            int i1 = s1s[n].toInt();
 
135
            int i2 = s2s[n].toInt();
 
136
            if (i1 != i2)          // "/*0/2" == "/*0/3"
 
137
                return (i1 < i2);   // "/*2" < "/*3"
 
138
        }
 
139
        // fall through, probably never hit...
 
140
        return false;
 
141
    }
 
142
private:
 
143
    QString m_string;
 
144
};
 
145
 
 
146
static QValueList<Address> m_foundAddrs;
 
147
 
 
148
void Searcher::slotSearchNext()
 
149
{
 
150
    if (m_foundAddrs.empty())
 
151
        return;
 
152
    QString addr = m_foundAddrs[m_currentResult].string();
 
153
    KEBListViewItem *item = ListView::self()->getItemAtAddress(addr);
 
154
    m_currentResult = m_currentResult+1 > m_foundAddrs.count()-1
 
155
        ? 0 : m_currentResult+1;
 
156
    ListView::self()->clearSelection();
 
157
    ListView::self()->setCurrent(item);
 
158
    item->setSelected(true);
 
159
    ListView::self()->emitSlotSelectionChanged();
 
160
}
 
161
 
 
162
void Searcher::slotSearchTextChanged(const QString & text)
 
163
{
 
164
    if (text.isEmpty() || text == i18n("Click here and type to search..."))
 
165
        return;
 
166
    if (!m_bktextmap)
 
167
        m_bktextmap = new KBookmarkTextMap(CurrentMgr::self()->mgr());
 
168
    m_bktextmap->update(); // FIXME - make this public and use it!!!
 
169
    QValueList<KBookmark> results = m_bktextmap->find(text);
 
170
    m_foundAddrs.clear();
 
171
    for (QValueList<KBookmark>::iterator it = results.begin(); it != results.end(); ++it )
 
172
        m_foundAddrs << Address((*it).address());
 
173
    // sort the addr list so we don't go "next" in a random order
 
174
    qHeapSort(m_foundAddrs.begin(), m_foundAddrs.end());
 
175
    m_currentResult = 0;
 
176
    slotSearchNext();
 
177
}
 
178
 
 
179
#include "search.moc"