1 // -*- 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) 2000 David Faure <faure@kde.org>
5 Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License version 2 or at your option version 3 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #include "importers.h"
27 #include "bookmarkmodel.h"
29 #include <QtCore/QRegExp>
33 #include <kmessagebox.h>
34 #include <kfiledialog.h>
36 #include <kbookmark.h>
37 #include <kbookmarkmanager.h>
39 #include <kbookmarkimporter.h>
40 #include <kbookmarkimporter_ie.h>
41 #include <kbookmarkimporter_opera.h>
42 #include <kbookmarkimporter_crash.h>
43 #include <kbookmarkdombuilder.h>
44 #include <kbookmarkimporter_ns.h>
46 QString
ImportCommand::name() const {
47 return i18n("Import %1 Bookmarks", visibleName());
50 QString
ImportCommand::folder() const {
51 return m_folder
? i18n("%1 Bookmarks", visibleName()) : QString();
54 ImportCommand
* ImportCommand::importerFactory(const QString
&type
) {
55 if (type
== "Galeon") return new GaleonImportCommand();
56 else if (type
== "IE") return new IEImportCommand();
57 else if (type
== "KDE2") return new KDE2ImportCommand();
58 else if (type
== "Opera") return new OperaImportCommand();
59 else if (type
== "Crashes") return new CrashesImportCommand();
60 else if (type
== "Moz") return new MozImportCommand();
61 else if (type
== "NS") return new NSImportCommand();
63 kError() << "ImportCommand::importerFactory() - invalid type (" << type
<< ")!" << endl
;
68 ImportCommand
* ImportCommand::performImport(const QString
&type
, QWidget
*top
) {
69 ImportCommand
*importer
= ImportCommand::importerFactory(type
);
73 QString mydirname
= importer
->requestFilename();
74 if (mydirname
.isEmpty()) {
80 KMessageBox::questionYesNoCancel(
81 top
, i18n("Import as a new subfolder or replace all the current bookmarks?"),
82 i18n("%1 Import", importer
->visibleName()),
83 KGuiItem(i18n("As New Folder")), KGuiItem(i18n("Replace")));
85 if (answer
== KMessageBox::Cancel
) {
90 importer
->import(mydirname
, answer
== KMessageBox::Yes
);
94 void ImportCommand::doCreateHoldingFolder(KBookmarkGroup
&bkGroup
) {
95 bkGroup
= CurrentMgr::self()->mgr()
96 ->root().createNewFolder(folder());
97 bkGroup
.internalElement().setAttribute("icon", m_icon
);
98 m_group
= bkGroup
.address();
101 void ImportCommand::execute() {
102 KBookmarkGroup bkGroup
;
104 if (!folder().isNull()) {
105 doCreateHoldingFolder(bkGroup
);
108 // import into the root, after cleaning it up
109 bkGroup
= CurrentMgr::self()->root();
111 m_cleanUpCmd
= DeleteCommand::deleteAll(bkGroup
);
113 K3MacroCommand
*mcmd
= (K3MacroCommand
*) m_cleanUpCmd
;
114 mcmd
->addCommand(new DeleteCommand(bkGroup
.address(),
115 true /* contentOnly */));
116 m_cleanUpCmd
->execute();
118 // import at the root
124 // notify the model that the data has changed
126 // FIXME Resetting the model completely has the unwanted
127 // side-effect of collapsing all items in tree views
128 // (and possibly other side effects)
129 CurrentMgr::self()->model()->resetModel();
132 void ImportCommand::unexecute() {
133 if ( !folder().isEmpty() ) {
134 // we created a group -> just delete it
135 DeleteCommand
cmd(m_group
);
139 // we imported at the root -> delete everything
140 KBookmarkGroup root
= CurrentMgr::self()->root();
141 K3Command
*cmd
= DeleteCommand::deleteAll(root
);
146 // and recreate what was there before
147 m_cleanUpCmd
->unexecute();
151 QString
ImportCommand::affectedBookmarks() const
153 QString rootAdr
= CurrentMgr::self()->root().address();
154 if(m_group
== rootAdr
)
157 return KBookmark::parentAddress(m_group
);
160 /* -------------------------------------- */
162 QString
MozImportCommand::requestFilename() const {
163 static KMozillaBookmarkImporterImpl importer
;
164 return importer
.findDefaultLocation();
167 QString
NSImportCommand::requestFilename() const {
168 static KNSBookmarkImporterImpl importer
;
169 return importer
.findDefaultLocation();
172 QString
OperaImportCommand::requestFilename() const {
173 static KOperaBookmarkImporterImpl importer
;
174 return importer
.findDefaultLocation();
177 QString
CrashesImportCommand::requestFilename() const {
178 static KCrashBookmarkImporterImpl importer
;
179 return importer
.findDefaultLocation();
182 QString
IEImportCommand::requestFilename() const {
183 static KIEBookmarkImporterImpl importer
;
184 return importer
.findDefaultLocation();
187 // following two are really just xbel
189 QString
GaleonImportCommand::requestFilename() const {
190 return KFileDialog::getOpenFileName(
191 QDir::homePath() + "/.galeon",
192 i18n("*.xbel|Galeon Bookmark Files (*.xbel)"),
196 #include "kstandarddirs.h"
198 QString
KDE2ImportCommand::requestFilename() const {
199 return KFileDialog::getOpenFileName(
200 KStandardDirs::locateLocal("data", "konqueror"),
201 i18n("*.xml|KDE Bookmark Files (*.xml)"),
205 /* -------------------------------------- */
207 static void parseInto(const KBookmarkGroup
&bkGroup
, KBookmarkImporterBase
*importer
) {
208 KBookmarkDomBuilder
builder(bkGroup
, CurrentMgr::self()->mgr());
209 builder
.connectImporter(importer
);
213 void OperaImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
214 KOperaBookmarkImporterImpl importer
;
215 importer
.setFilename(m_fileName
);
216 parseInto(bkGroup
, &importer
);
219 void CrashesImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
220 KCrashBookmarkImporterImpl importer
;
221 importer
.setShouldDelete(true);
222 importer
.setFilename(m_fileName
);
223 parseInto(bkGroup
, &importer
);
226 void IEImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
227 KIEBookmarkImporterImpl importer
;
228 importer
.setFilename(m_fileName
);
229 parseInto(bkGroup
, &importer
);
232 void HTMLImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
233 KNSBookmarkImporterImpl importer
;
234 importer
.setFilename(m_fileName
);
235 importer
.setUtf8(m_utf8
);
236 parseInto(bkGroup
, &importer
);
239 /* -------------------------------------- */
241 void XBELImportCommand::doCreateHoldingFolder(KBookmarkGroup
&) {
242 // rather than reuse the old group node we transform the
243 // root xbel node into the group when doing an xbel import
246 void XBELImportCommand::doExecute(const KBookmarkGroup
&/*bkGroup*/) {
247 // check if already open first???
248 KBookmarkManager
*pManager
= KBookmarkManager::managerForFile(m_fileName
, QString());
250 QDomDocument doc
= CurrentMgr::self()->mgr()->internalDocument();
253 QDomNode subDoc
= pManager
->internalDocument().namedItem("xbel").cloneNode();
254 if (subDoc
.isProcessingInstruction())
255 subDoc
= subDoc
.nextSibling();
256 if (subDoc
.isDocumentType())
257 subDoc
= subDoc
.nextSibling();
258 if (subDoc
.nodeName() != "xbel")
261 if (!folder().isEmpty()) {
262 // transform into folder
263 subDoc
.toElement().setTagName("folder");
267 for (int i
= 0; i
< subDoc
.attributes().count(); i
++)
268 tags
<< subDoc
.attributes().item(i
).toAttr().name();
269 for (QStringList::const_iterator it
= tags
.constBegin(); it
!= tags
.constEnd(); ++it
)
270 subDoc
.attributes().removeNamedItem((*it
));
272 subDoc
.toElement().setAttribute("icon", m_icon
);
274 // give the folder a name
275 QDomElement textElem
= doc
.createElement("title");
276 subDoc
.insertBefore(textElem
, subDoc
.firstChild());
277 textElem
.appendChild(doc
.createTextNode(folder()));
281 QDomNode node
= doc
.importNode(subDoc
, true);
283 if (!folder().isEmpty()) {
284 CurrentMgr::self()->root().internalElement().appendChild(node
);
285 m_group
= KBookmarkGroup(node
.toElement()).address();
288 QDomElement root
= CurrentMgr::self()->root().internalElement();
290 QList
<QDomElement
> childList
;
292 QDomNode n
= subDoc
.firstChild().toElement();
293 while (!n
.isNull()) {
294 QDomElement e
= n
.toElement();
300 QList
<QDomElement
>::Iterator it
= childList
.begin();
301 QList
<QDomElement
>::Iterator end
= childList
.end();
302 for (; it
!= end
; ++it
)
303 root
.appendChild((*it
));
307 #include "importers.moc"