summaryrefslogtreecommitdiffstats
path: root/katapult/plugins/catalogs/bookmarkcatalog/bookmarkcatalog.cpp
blob: 6a0267e45d14228c6e2bdf95c07f451cad1fdd1e (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
/***************************************************************************
 *   Copyright (C) 2005 by Joe Ferris                                      *
 *   jferris@optimistictech.com                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include <kservicegroup.h>
#include <ksycocaentry.h>
#include <ksycocatype.h>
#include <kapplication.h>
#include <kbookmarkmanager.h>
#include <kbookmark.h>
#include <knuminput.h>
#include <kurlrequester.h>

#include <tqstring.h>
#include <tqradiobutton.h>
#include <tqbuttongroup.h>
#include <tqdir.h>
#include <tqregexp.h>
#include <tqfile.h>

#include "bookmarkcatalog.h"
#include "bookmark.h"
#include "mozillabookmark.h"
#include "settings.h"
#include "actionregistry.h"
#include "actionopenbookmark.h"

K_EXPORT_COMPONENT_FACTORY( katapult_bookmarkcatalog,
                            KGenericFactory<BookmarkCatalog>( "katapult_bookmarkcatalog" ) )

BookmarkCatalog::BookmarkCatalog(TQObject *, const char *, const TQStringList&)
 : CachedCatalog()
{
	manager = KBookmarkManager::userBookmarksManager();
	_minQueryLen = 1;
	ActionRegistry::self()->registerAction(new ActionOpenBookmark());
}


BookmarkCatalog::~BookmarkCatalog()
{
}

void BookmarkCatalog::initialize()
{
	if(manager != 0)
		cacheBookmarkList(manager->root());
	if(_mozEnabled)
		cacheMozillaBookmarks();
}

void BookmarkCatalog::cacheBookmarkList(KBookmarkGroup group)
{
	KBookmark bookmark = group.first();
	while(!bookmark.isNull()) {
		if (bookmark.isGroup()) {
			cacheBookmarkList(bookmark.toGroup());
		} else {
			addItem(new Bookmark(bookmark));
		}
		bookmark = group.next(bookmark);
	}
}

void BookmarkCatalog::cacheMozillaBookmarks()
{
	if(_mozAuto)
		_mozFile = detectMozillaFile();
		
	if(_mozFile.isEmpty())
		return;
		
	TQFile bmFile(_mozFile);
	if(!bmFile.open(IO_ReadOnly))
		return;
		
	TQString contents = bmFile.readAll();
	TQRegExp rx("<A HREF=\"([^\"]+)\" [^>]+>([^<]+)</A>");
	int pos = 0;
	while(pos > -1) {
		pos = rx.search(contents, pos);
		if(pos > -1) {
			addItem(new MozillaBookmark(rx.cap(1), rx.cap(2), TQPixmap()));
			pos += rx.matchedLength();
		}
	}
}

TQString BookmarkCatalog::detectMozillaFile()
{
	TQStringList testDirs;
	testDirs << ".firefox" << ".mozilla" << ".phoenix" << ".netscape";
	TQDir home = TQDir::home();
	for(TQStringList::Iterator it = testDirs.begin(); it != testDirs.end(); ++it) {
		TQString testDir = *it;
		if(home.exists(testDir)) {
			TQDir mozDir = TQDir(home.path()+"/"+testDir).canonicalPath();
			if(mozDir.dirName() != testDir && testDirs.contains(mozDir.dirName()))
				continue;
			TQString path = searchMozDir(mozDir.path());
			if(!path.isEmpty())
				return path;
		}
	}
	
	return "";
}

TQString BookmarkCatalog::searchMozDir(TQString path)
{
	TQDir dir(path);
	if(dir.exists("bookmarks.html")) {
		return path+"/bookmarks.html";
	}
	TQStringList entries = dir.entryList(TQDir::Dirs | TQDir::NoSymLinks);
	for(TQStringList::Iterator it = entries.begin(); it != entries.end(); ++it) {
		if(*it != "." && *it != ".."){
			TQString result = searchMozDir(path + "/" + *it);
			if(!result.isEmpty())
				return result;
		}
	}
	return TQString();
}

unsigned int BookmarkCatalog::minQueryLen() const
{
	return _minQueryLen;
}

void BookmarkCatalog::readSettings(KConfigBase *config)
{
	_minQueryLen = config->readUnsignedNumEntry("MinQueryLen", 3);
	_mozEnabled = config->readBoolEntry("MozEnabled", TRUE);
	_mozAuto = config->readBoolEntry("MozAuto", TRUE);
	_mozFile = config->readEntry("MozFile", "");
}

void BookmarkCatalog::writeSettings(KConfigBase *config)
{
	config->writeEntry("MinQueryLen", _minQueryLen);
	config->writeEntry("MozEnabled", _mozEnabled);
	config->writeEntry("MozAuto", _mozAuto);
	config->writeEntry("MozFile", _mozFile);
}

TQWidget * BookmarkCatalog::configure()
{
	settings = new BookmarkCatalogSettings();
	
	settings->minQueryLen->setValue(_minQueryLen);
	connect(settings->minQueryLen, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(minQueryLenChanged(int)));
	
	settings->mozEnabled->setChecked(_mozEnabled);
	connect(settings->mozEnabled, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(toggleMozEnabled(bool)));
	
	settings->mozAuto->setChecked(_mozAuto);
	connect(settings->mozAuto, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(toggleMozAuto(bool)));
	
	settings->mozManual->setChecked(!_mozAuto);
	
	settings->mozFile->setURL(_mozFile);
	connect(settings->mozFile, TQT_SIGNAL(urlSelected(const TQString &)), this, TQT_SLOT(changeMozFile(const TQString &)));
	
	settings->mozAuto->setEnabled(_mozEnabled);
	settings->mozManual->setEnabled(_mozEnabled);
	settings->mozFile->setEnabled(_mozEnabled && !_mozAuto);
	
	return settings;
}

void BookmarkCatalog::minQueryLenChanged(int _minQueryLen)
{
	this->_minQueryLen = _minQueryLen;
}

void BookmarkCatalog::toggleMozEnabled(bool _mozEnabled)
{
	this->_mozEnabled = _mozEnabled;
	settings->mozAuto->setEnabled(_mozEnabled);
	settings->mozManual->setEnabled(_mozEnabled);
	settings->mozFile->setEnabled(_mozEnabled && !_mozAuto);
}

void BookmarkCatalog::toggleMozAuto(bool _mozAuto)
{
	this->_mozAuto = _mozAuto;
	settings->mozFile->setEnabled(!_mozAuto);
}

void BookmarkCatalog::changeMozFile(const TQString & _mozFile)
{
	this->_mozFile = _mozFile;
}

#include "bookmarkcatalog.moc"