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)"));
195 #include "kstandarddirs.h"
197 QString
KDE2ImportCommand::requestFilename() const {
198 return KFileDialog::getOpenFileName(
199 KStandardDirs::locateLocal("data", "konqueror"),
200 i18n("*.xml|KDE Bookmark Files (*.xml)"));
203 /* -------------------------------------- */
205 static void parseInto(const KBookmarkGroup
&bkGroup
, KBookmarkImporterBase
*importer
) {
206 KBookmarkDomBuilder
builder(bkGroup
, CurrentMgr::self()->mgr());
207 builder
.connectImporter(importer
);
211 void OperaImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
212 KOperaBookmarkImporterImpl importer
;
213 importer
.setFilename(m_fileName
);
214 parseInto(bkGroup
, &importer
);
217 void CrashesImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
218 KCrashBookmarkImporterImpl importer
;
219 importer
.setShouldDelete(true);
220 importer
.setFilename(m_fileName
);
221 parseInto(bkGroup
, &importer
);
224 void IEImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
225 KIEBookmarkImporterImpl importer
;
226 importer
.setFilename(m_fileName
);
227 parseInto(bkGroup
, &importer
);
230 void HTMLImportCommand::doExecute(const KBookmarkGroup
&bkGroup
) {
231 KNSBookmarkImporterImpl importer
;
232 importer
.setFilename(m_fileName
);
233 importer
.setUtf8(m_utf8
);
234 parseInto(bkGroup
, &importer
);
237 /* -------------------------------------- */
239 void XBELImportCommand::doCreateHoldingFolder(KBookmarkGroup
&) {
240 // rather than reuse the old group node we transform the
241 // root xbel node into the group when doing an xbel import
244 void XBELImportCommand::doExecute(const KBookmarkGroup
&/*bkGroup*/) {
245 // check if already open first???
246 KBookmarkManager
*pManager
= KBookmarkManager::managerForFile(m_fileName
, QString());
248 QDomDocument doc
= CurrentMgr::self()->mgr()->internalDocument();
251 QDomNode subDoc
= pManager
->internalDocument().namedItem("xbel").cloneNode();
252 if (subDoc
.isProcessingInstruction())
253 subDoc
= subDoc
.nextSibling();
254 if (subDoc
.isDocumentType())
255 subDoc
= subDoc
.nextSibling();
256 if (subDoc
.nodeName() != "xbel")
259 if (!folder().isEmpty()) {
260 // transform into folder
261 subDoc
.toElement().setTagName("folder");
265 for (int i
= 0; i
< subDoc
.attributes().count(); i
++)
266 tags
<< subDoc
.attributes().item(i
).toAttr().name();
267 for (QStringList::Iterator it
= tags
.begin(); it
!= tags
.end(); ++it
)
268 subDoc
.attributes().removeNamedItem((*it
));
270 subDoc
.toElement().setAttribute("icon", m_icon
);
272 // give the folder a name
273 QDomElement textElem
= doc
.createElement("title");
274 subDoc
.insertBefore(textElem
, subDoc
.firstChild());
275 textElem
.appendChild(doc
.createTextNode(folder()));
279 QDomNode node
= doc
.importNode(subDoc
, true);
281 if (!folder().isEmpty()) {
282 CurrentMgr::self()->root().internalElement().appendChild(node
);
283 m_group
= KBookmarkGroup(node
.toElement()).address();
286 QDomElement root
= CurrentMgr::self()->root().internalElement();
288 QList
<QDomElement
> childList
;
290 QDomNode n
= subDoc
.firstChild().toElement();
291 while (!n
.isNull()) {
292 QDomElement e
= n
.toElement();
298 QList
<QDomElement
>::Iterator it
= childList
.begin();
299 QList
<QDomElement
>::Iterator end
= childList
.end();
300 for (; it
!= end
; ++it
)
301 root
.appendChild((*it
));
305 #include "importers.moc"