2003-10-06 18:31:25 +00:00
|
|
|
// -*- mode:cperl; cperl-indent-level:4; cperl-continued-statement-offset:4; indent-tabs-mode:nil -*-
|
|
|
|
// vim: set ts=4 sts=4 sw=4 et:
|
2003-01-21 16:16:53 +00:00
|
|
|
/* This file is part of the KDE project
|
|
|
|
Copyright (C) 2000 David Faure <faure@kde.org>
|
|
|
|
Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public
|
|
|
|
License version 2 as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
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; see the file COPYING. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2003-09-14 18:07:31 +00:00
|
|
|
#include "importers.h"
|
|
|
|
|
|
|
|
#include "commands.h"
|
|
|
|
#include "toplevel.h"
|
|
|
|
#include "listview.h"
|
|
|
|
|
2003-01-21 16:16:53 +00:00
|
|
|
#include <qregexp.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <klocale.h>
|
|
|
|
|
|
|
|
#include <kmessagebox.h>
|
|
|
|
#include <kfiledialog.h>
|
|
|
|
|
|
|
|
#include <kbookmarkmanager.h>
|
|
|
|
|
|
|
|
#include <kbookmarkimporter.h>
|
|
|
|
#include <kbookmarkimporter_ie.h>
|
|
|
|
#include <kbookmarkimporter_opera.h>
|
2003-07-05 10:01:16 +00:00
|
|
|
#include <kbookmarkimporter_crash.h>
|
2003-03-15 20:15:17 +00:00
|
|
|
#include <kbookmarkdombuilder.h>
|
2003-01-21 16:16:53 +00:00
|
|
|
|
|
|
|
QString ImportCommand::name() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
return i18n("Import %1 Bookmarks").arg(visibleName());
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
2003-02-05 22:31:55 +00:00
|
|
|
QString ImportCommand::folder() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
return m_folder ? i18n("%1 Bookmarks").arg(visibleName()) : QString::null;
|
2003-02-05 22:31:55 +00:00
|
|
|
}
|
|
|
|
|
2003-02-16 00:05:39 +00:00
|
|
|
ImportCommand* ImportCommand::importerFactory(const QCString &type) {
|
2003-10-11 13:39:50 +00:00
|
|
|
if (type == "Galeon") return new GaleonImportCommand();
|
|
|
|
else if (type == "IE") return new IEImportCommand();
|
|
|
|
else if (type == "KDE2") return new KDE2ImportCommand();
|
|
|
|
else if (type == "Opera") return new OperaImportCommand();
|
|
|
|
else if (type == "Crashes") return new CrashesImportCommand();
|
|
|
|
else if (type == "Moz") return new MozImportCommand();
|
|
|
|
else if (type == "NS") return new NSImportCommand();
|
|
|
|
else {
|
|
|
|
kdError() << "ImportCommand::importerFactory() - invalid type (" << type << ")!" << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-16 00:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ImportCommand* ImportCommand::performImport(const QCString &type, QWidget *top) {
|
2003-10-11 13:39:50 +00:00
|
|
|
ImportCommand *importer = ImportCommand::importerFactory(type);
|
|
|
|
|
|
|
|
QString mydirname = importer->requestFilename();
|
|
|
|
if (mydirname.isEmpty()) {
|
|
|
|
delete importer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int answer =
|
|
|
|
KMessageBox::questionYesNoCancel(
|
|
|
|
top, i18n("Import as a new subfolder or replace all the current bookmarks?"),
|
|
|
|
i18n("%1 Import").arg(importer->visibleName()),
|
|
|
|
i18n("As New Folder"), i18n("Replace"));
|
|
|
|
|
|
|
|
if (answer == KMessageBox::Cancel) {
|
|
|
|
delete importer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
importer->import(mydirname, answer == KMessageBox::Yes);
|
|
|
|
return importer;
|
2003-02-16 00:05:39 +00:00
|
|
|
}
|
|
|
|
|
2003-10-16 20:08:08 +00:00
|
|
|
void ImportCommand::doCreateHoldingFolder(KBookmarkGroup &bkGroup) {
|
|
|
|
bkGroup = CurrentMgr::self()->mgr()
|
|
|
|
->root().createNewFolder(CurrentMgr::self()->mgr(), folder(), false);
|
|
|
|
bkGroup.internalElement().setAttribute("icon", m_icon);
|
|
|
|
m_group = bkGroup.address();
|
|
|
|
}
|
2003-03-22 20:50:12 +00:00
|
|
|
|
2003-01-21 16:16:53 +00:00
|
|
|
void ImportCommand::execute() {
|
2003-10-11 13:39:50 +00:00
|
|
|
KBookmarkGroup bkGroup;
|
|
|
|
|
|
|
|
if (!folder().isNull()) {
|
|
|
|
doCreateHoldingFolder(bkGroup);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// import into the root, after cleaning it up
|
|
|
|
bkGroup = CurrentMgr::self()->mgr()->root();
|
|
|
|
delete m_cleanUpCmd;
|
|
|
|
m_cleanUpCmd = DeleteCommand::deleteAll(bkGroup);
|
|
|
|
|
|
|
|
KMacroCommand *mcmd = (KMacroCommand*) m_cleanUpCmd;
|
|
|
|
mcmd->addCommand(new DeleteCommand(bkGroup.address(),
|
|
|
|
true /* contentOnly */));
|
|
|
|
|
|
|
|
// unselect current item, it doesn't exist anymore
|
2003-10-16 20:08:08 +00:00
|
|
|
if (ListView::self())
|
|
|
|
ListView::self()->clearSelection();
|
2003-10-11 13:39:50 +00:00
|
|
|
m_cleanUpCmd->execute();
|
|
|
|
|
|
|
|
// import at the root
|
|
|
|
m_group = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
doExecute(bkGroup);
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImportCommand::unexecute() {
|
2003-10-11 13:39:50 +00:00
|
|
|
if ( !folder().isEmpty() ) {
|
|
|
|
// we created a group -> just delete it
|
|
|
|
DeleteCommand cmd(m_group);
|
|
|
|
cmd.execute();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// we imported at the root -> delete everything
|
|
|
|
KBookmarkGroup root = CurrentMgr::self()->mgr()->root();
|
|
|
|
KCommand *cmd = DeleteCommand::deleteAll(root);
|
|
|
|
|
|
|
|
// unselect current item, it doesn't exist anymore
|
2003-10-16 20:08:08 +00:00
|
|
|
if (ListView::self())
|
|
|
|
ListView::self()->clearSelection();
|
2003-10-11 13:39:50 +00:00
|
|
|
cmd->execute();
|
|
|
|
delete cmd;
|
|
|
|
|
|
|
|
// and recreate what was there before
|
|
|
|
m_cleanUpCmd->unexecute();
|
|
|
|
}
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
2003-03-22 20:50:12 +00:00
|
|
|
/* -------------------------------------- */
|
2003-03-06 22:02:36 +00:00
|
|
|
|
2003-03-22 20:50:12 +00:00
|
|
|
QString MozImportCommand::requestFilename() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
static KMozillaBookmarkImporterImpl importer;
|
|
|
|
return importer.findDefaultLocation();
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
2003-03-22 20:50:12 +00:00
|
|
|
QString NSImportCommand::requestFilename() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
static KNSBookmarkImporterImpl importer;
|
|
|
|
return importer.findDefaultLocation();
|
2003-03-22 20:50:12 +00:00
|
|
|
}
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-02-05 21:07:30 +00:00
|
|
|
QString OperaImportCommand::requestFilename() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
static KOperaBookmarkImporterImpl importer;
|
|
|
|
return importer.findDefaultLocation();
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
2003-07-05 10:01:16 +00:00
|
|
|
QString CrashesImportCommand::requestFilename() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
static KCrashBookmarkImporterImpl importer;
|
|
|
|
return importer.findDefaultLocation();
|
2003-07-05 10:01:16 +00:00
|
|
|
}
|
|
|
|
|
2003-03-22 20:50:12 +00:00
|
|
|
QString IEImportCommand::requestFilename() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
static KIEBookmarkImporterImpl importer;
|
|
|
|
return importer.findDefaultLocation();
|
2003-03-22 20:50:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-22 23:10:30 +00:00
|
|
|
// following two are really just xbel
|
|
|
|
|
2003-03-22 20:50:12 +00:00
|
|
|
QString GaleonImportCommand::requestFilename() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
return KFileDialog::getOpenFileName(
|
|
|
|
QDir::homeDirPath() + "/.galeon",
|
|
|
|
i18n("*.xbel|Galeon bookmark files (*.xbel)"));
|
2003-03-22 20:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString KDE2ImportCommand::requestFilename() const {
|
2003-10-11 13:39:50 +00:00
|
|
|
// locateLocal on the bookmarks file and get dir?
|
|
|
|
return KFileDialog::getOpenFileName(
|
2003-11-16 13:11:51 +00:00
|
|
|
QDir::homeDirPath() + "/.kde/share/apps/konqueror",
|
2003-10-11 13:39:50 +00:00
|
|
|
i18n("*.xml|KDE bookmark files (*.xml)"));
|
2003-03-22 20:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------- */
|
|
|
|
|
2003-03-22 23:10:30 +00:00
|
|
|
static void parseInto(const KBookmarkGroup &bkGroup, KBookmarkImporterBase *importer) {
|
2003-10-11 13:39:50 +00:00
|
|
|
KBookmarkDomBuilder builder(bkGroup, CurrentMgr::self()->mgr());
|
|
|
|
builder.connectImporter(importer);
|
|
|
|
importer->parse();
|
2003-03-22 23:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OperaImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
|
2003-10-11 13:39:50 +00:00
|
|
|
KOperaBookmarkImporterImpl importer;
|
|
|
|
importer.setFilename(m_fileName);
|
|
|
|
parseInto(bkGroup, &importer);
|
2003-01-22 20:01:44 +00:00
|
|
|
}
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-07-05 10:01:16 +00:00
|
|
|
void CrashesImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
|
2003-10-11 13:39:50 +00:00
|
|
|
KCrashBookmarkImporterImpl importer;
|
|
|
|
importer.setShouldDelete(true);
|
|
|
|
importer.setFilename(m_fileName);
|
|
|
|
parseInto(bkGroup, &importer);
|
2003-07-05 10:01:16 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 14:52:20 +00:00
|
|
|
void IEImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
|
2003-10-11 13:39:50 +00:00
|
|
|
KIEBookmarkImporterImpl importer;
|
|
|
|
importer.setFilename(m_fileName);
|
|
|
|
parseInto(bkGroup, &importer);
|
2003-01-22 20:01:44 +00:00
|
|
|
}
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-03-06 14:52:20 +00:00
|
|
|
void HTMLImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
|
2003-10-11 13:39:50 +00:00
|
|
|
KNSBookmarkImporterImpl importer;
|
|
|
|
importer.setFilename(m_fileName);
|
|
|
|
importer.setUtf8(m_utf8);
|
|
|
|
parseInto(bkGroup, &importer);
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
2003-02-16 00:05:39 +00:00
|
|
|
/* -------------------------------------- */
|
|
|
|
|
2003-01-21 16:16:53 +00:00
|
|
|
void XBELImportCommand::doCreateHoldingFolder(KBookmarkGroup &) {
|
2003-10-11 13:39:50 +00:00
|
|
|
// rather than reuse the old group node we transform the
|
|
|
|
// root xbel node into the group when doing an xbel import
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
2003-03-08 13:44:44 +00:00
|
|
|
void XBELImportCommand::doExecute(const KBookmarkGroup &/*bkGroup*/) {
|
2003-10-11 13:39:50 +00:00
|
|
|
// check if already open first???
|
2003-11-16 13:11:51 +00:00
|
|
|
KBookmarkManager *pManager = KBookmarkManager::managerForFile(m_fileName, false);
|
2003-02-26 08:13:02 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
QDomDocument doc = CurrentMgr::self()->mgr()->internalDocument();
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
// get the xbel
|
|
|
|
QDomNode subDoc = pManager->internalDocument().namedItem("xbel").cloneNode();
|
2003-11-16 13:37:00 +00:00
|
|
|
if (subDoc.isProcessingInstruction())
|
|
|
|
subDoc = subDoc.nextSibling();
|
|
|
|
if (subDoc.isDocumentType())
|
|
|
|
subDoc = subDoc.nextSibling();
|
|
|
|
if (subDoc.nodeName() != "xbel")
|
|
|
|
return;
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
if (!folder().isEmpty()) {
|
|
|
|
// transform into folder
|
|
|
|
subDoc.toElement().setTagName("folder");
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
// clear attributes
|
|
|
|
QStringList tags;
|
|
|
|
for (uint i = 0; i < subDoc.attributes().count(); i++)
|
|
|
|
tags << subDoc.attributes().item(i).toAttr().name();
|
|
|
|
for (QStringList::Iterator it = tags.begin(); it != tags.end(); ++it)
|
|
|
|
subDoc.attributes().removeNamedItem((*it));
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
subDoc.toElement().setAttribute("icon", m_icon);
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
// give the folder a name
|
|
|
|
QDomElement textElem = doc.createElement("title");
|
|
|
|
subDoc.insertBefore(textElem, subDoc.firstChild());
|
|
|
|
textElem.appendChild(doc.createTextNode(folder()));
|
|
|
|
}
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
// import and add it
|
|
|
|
QDomNode node = doc.importNode(subDoc, true);
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
if (!folder().isEmpty()) {
|
|
|
|
CurrentMgr::self()->mgr()->root().internalElement().appendChild(node);
|
|
|
|
m_group = KBookmarkGroup(node.toElement()).address();
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
} else {
|
|
|
|
QDomElement root = CurrentMgr::self()->mgr()->root().internalElement();
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
QValueList<QDomElement> childList;
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
QDomNode n = subDoc.firstChild().toElement();
|
|
|
|
while (!n.isNull()) {
|
|
|
|
QDomElement e = n.toElement();
|
|
|
|
if (!e.isNull())
|
|
|
|
childList.append(e);
|
|
|
|
n = n.nextSibling();
|
|
|
|
}
|
2003-01-21 16:16:53 +00:00
|
|
|
|
2003-10-11 13:39:50 +00:00
|
|
|
QValueList<QDomElement>::Iterator it = childList.begin();
|
|
|
|
QValueList<QDomElement>::Iterator end = childList.end();
|
|
|
|
for (; it!= end ; ++it)
|
|
|
|
root.appendChild((*it));
|
|
|
|
}
|
2003-01-21 16:16:53 +00:00
|
|
|
}
|
2003-11-16 13:37:00 +00:00
|
|
|
|
2003-01-21 16:16:53 +00:00
|
|
|
#include "importers.moc"
|