/***************************************************************************
 *   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 <tdesycocaentry.h>
#include <tdesycocatype.h>
#include <tdeapplication.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(TDEConfigBase *config)
{
	_minQueryLen = config->readUnsignedNumEntry("MinQueryLen", 3);
	_mozEnabled = config->readBoolEntry("MozEnabled", true);
	_mozAuto = config->readBoolEntry("MozAuto", true);
	_mozFile = config->readEntry("MozFile", "");
}

void BookmarkCatalog::writeSettings(TDEConfigBase *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, TQ_SIGNAL(valueChanged(int)), this, TQ_SLOT(minQueryLenChanged(int)));
	
	settings->mozEnabled->setChecked(_mozEnabled);
	connect(settings->mozEnabled, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(toggleMozEnabled(bool)));
	
	settings->mozAuto->setChecked(_mozAuto);
	connect(settings->mozAuto, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(toggleMozAuto(bool)));
	
	settings->mozManual->setChecked(!_mozAuto);
	
	settings->mozFile->setURL(_mozFile);
	connect(settings->mozFile, TQ_SIGNAL(urlSelected(const TQString &)), this, TQ_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"
