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>
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.
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.
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.
33
#include <kbookmarkmanager.h>
35
MagicKLineEdit::MagicKLineEdit(
36
const QString &text, QWidget *parent, const char *name
37
) : KLineEdit(text, parent, name), m_grayedText(text) {
38
setPaletteForegroundColor(gray);
41
void MagicKLineEdit::focusInEvent(QFocusEvent *ev) {
42
if (text() == m_grayedText)
43
setText(QString::null);
44
QLineEdit::focusInEvent(ev);
47
void MagicKLineEdit::focusOutEvent(QFocusEvent *ev) {
48
if (text().isEmpty()) {
49
setText(m_grayedText);
50
setPaletteForegroundColor(gray);
52
QLineEdit::focusOutEvent(ev);
55
void MagicKLineEdit::mousePressEvent(QMouseEvent *ev) {
56
setPaletteForegroundColor(parentWidget()->paletteForegroundColor());
57
QLineEdit::mousePressEvent(ev);
60
class KBookmarkTextMap : private KBookmarkGroupTraverser {
62
KBookmarkTextMap(KBookmarkManager *);
64
QValueList<KBookmark> find(const QString &text) const;
66
virtual void visit(const KBookmark &);
67
virtual void visitEnter(const KBookmarkGroup &);
68
virtual void visitLeave(const KBookmarkGroup &) { ; }
70
typedef QValueList<KBookmark> KBookmarkList;
71
QMap<QString, KBookmarkList> m_bk_map;
72
KBookmarkManager *m_manager;
75
KBookmarkTextMap::KBookmarkTextMap( KBookmarkManager *manager ) {
79
void KBookmarkTextMap::update()
82
KBookmarkGroup root = m_manager->root();
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);
93
void KBookmarkTextMap::visitEnter(const KBookmarkGroup &grp) {
97
QValueList<KBookmark> KBookmarkTextMap::find(const QString &text) const
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
104
if ((*it).find(text,0,false) != -1)
105
matches += m_bk_map[(*it)];
110
Searcher* Searcher::s_self = 0;
112
static unsigned int m_currentResult;
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;
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"
132
if (n >= s2s.count()) // "/0" > "/0/*5"
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"
139
// fall through, probably never hit...
146
static QValueList<Address> m_foundAddrs;
148
void Searcher::slotSearchNext()
150
if (m_foundAddrs.empty())
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();
162
void Searcher::slotSearchTextChanged(const QString & text)
164
if (text.isEmpty() || text == i18n("Click here and type to search..."))
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());
179
#include "search.moc"