summaryrefslogtreecommitdiffstats
path: root/konqueror/keditbookmarks/importers.cpp
blob: 188398481a74cb7356066845eb4244137ae4f54e (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// -*- indent-tabs-mode:nil -*-
// vim: set ts=4 sts=4 sw=4 et:
/* This file is part of the KDE project
   Copyright (C) 2000 David Faure <[email protected]>
   Copyright (C) 2002-2003 Alexander Kellett <[email protected]>

   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., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "importers.h"

#include "commands.h"
#include "toplevel.h"
#include "listview.h"

#include <tqregexp.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>
#include <kbookmarkimporter_crash.h>
#include <kbookmarkdombuilder.h>

TQString ImportCommand::name() const {
    return i18n("Import %1 Bookmarks").arg(visibleName());
}

TQString ImportCommand::folder() const {
    return m_folder ? i18n("%1 Bookmarks").arg(visibleName()) : TQString::null;
}

ImportCommand* ImportCommand::importerFactory(const TQCString &type) {
    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;
    }
}

ImportCommand* ImportCommand::performImport(const TQCString &type, TQWidget *top) {
    ImportCommand *importer = ImportCommand::importerFactory(type);

    TQString mydirname = importer->requestFilename();
    if (mydirname.isEmpty()) {
        delete importer;
        return 0;
    }

    int answer =
        KMessageBox::questionYesNoCancel(
                top, i18n("Import as a new subfolder or tqreplace 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;
}

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();
}

void ImportCommand::execute() {
    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 */));
        m_cleanUpCmd->execute();

        // import at the root
        m_group = "";
    }

    doExecute(bkGroup);
}

void ImportCommand::unexecute() {
    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);

        cmd->execute();
        delete cmd;

        // and recreate what was there before
        m_cleanUpCmd->unexecute();
    }
}

TQString ImportCommand::affectedBookmarks() const
{
    TQString rootAdr = CurrentMgr::self()->mgr()->root().address();
    if(m_group == rootAdr)
        return m_group;
    else
        return KBookmark::parentAddress(m_group);
}

/* -------------------------------------- */

TQString MozImportCommand::requestFilename() const {
    static KMozillaBookmarkImporterImpl importer;
    return importer.tqfindDefaultLocation();
}

TQString NSImportCommand::requestFilename() const {
    static KNSBookmarkImporterImpl importer;
    return importer.tqfindDefaultLocation();
}

TQString OperaImportCommand::requestFilename() const {
    static KOperaBookmarkImporterImpl importer;
    return importer.tqfindDefaultLocation();
}

TQString CrashesImportCommand::requestFilename() const {
    static KCrashBookmarkImporterImpl importer;
    return importer.tqfindDefaultLocation();
}

TQString IEImportCommand::requestFilename() const {
    static KIEBookmarkImporterImpl importer;
    return importer.tqfindDefaultLocation();
}

// following two are really just xbel

TQString GaleonImportCommand::requestFilename() const {
    return KFileDialog::getOpenFileName(
            TQDir::homeDirPath() + "/.galeon",
            i18n("*.xbel|Galeon Bookmark Files (*.xbel)"));
}

#include "kstandarddirs.h"

TQString KDE2ImportCommand::requestFilename() const {
    return KFileDialog::getOpenFileName(
            locateLocal("data", "konqueror"),
            i18n("*.xml|KDE Bookmark Files (*.xml)"));
}

/* -------------------------------------- */

static void parseInto(const KBookmarkGroup &bkGroup, KBookmarkImporterBase *importer) {
    KBookmarkDomBuilder builder(bkGroup, CurrentMgr::self()->mgr());
    builder.connectImporter(importer);
    importer->parse();
}

void OperaImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
    KOperaBookmarkImporterImpl importer;
    importer.setFilename(m_fileName);
    parseInto(bkGroup, &importer);
}

void CrashesImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
    KCrashBookmarkImporterImpl importer;
    importer.setShouldDelete(true);
    importer.setFilename(m_fileName);
    parseInto(bkGroup, &importer);
}

void IEImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
    KIEBookmarkImporterImpl importer;
    importer.setFilename(m_fileName);
    parseInto(bkGroup, &importer);
}

void HTMLImportCommand::doExecute(const KBookmarkGroup &bkGroup) {
    KNSBookmarkImporterImpl importer;
    importer.setFilename(m_fileName);
    importer.setUtf8(m_utf8);
    parseInto(bkGroup, &importer);
}

/* -------------------------------------- */

void XBELImportCommand::doCreateHoldingFolder(KBookmarkGroup &) {
    // rather than reuse the old group node we transform the
    // root xbel node into the group when doing an xbel import
}

void XBELImportCommand::doExecute(const KBookmarkGroup &/*bkGroup*/) {
    // check if already open first???
    KBookmarkManager *pManager = KBookmarkManager::managerForFile(m_fileName, false);

    TQDomDocument doc = CurrentMgr::self()->mgr()->internalDocument();

    // get the xbel
    TQDomNode subDoc = pManager->internalDocument().namedItem("xbel").cloneNode();
    if (subDoc.isProcessingInstruction())
        subDoc = subDoc.nextSibling();
    if (subDoc.isDocumentType())
        subDoc = subDoc.nextSibling();
    if (subDoc.nodeName() != "xbel")
        return;

    if (!folder().isEmpty()) {
        // transform into folder
        subDoc.toElement().setTagName("folder");

        // clear attributes
        TQStringList tags;
        for (uint i = 0; i < subDoc.attributes().count(); i++)
            tags << subDoc.attributes().item(i).toAttr().name();
        for (TQStringList::Iterator it = tags.begin(); it != tags.end(); ++it)
            subDoc.attributes().removeNamedItem((*it));

        subDoc.toElement().setAttribute("icon", m_icon);

        // give the folder a name
        TQDomElement textElem = doc.createElement("title");
        subDoc.insertBefore(textElem, subDoc.firstChild());
        textElem.appendChild(doc.createTextNode(folder()));
    }

    // import and add it
    TQDomNode node = doc.importNode(subDoc, true);

    if (!folder().isEmpty()) {
        CurrentMgr::self()->mgr()->root().internalElement().appendChild(node);
        m_group = KBookmarkGroup(node.toElement()).address();

    } else {
        TQDomElement root = CurrentMgr::self()->mgr()->root().internalElement();

        TQValueList<TQDomElement> childList;

        TQDomNode n = subDoc.firstChild().toElement();
        while (!n.isNull()) {
            TQDomElement e = n.toElement();
            if (!e.isNull())
                childList.append(e);
            n = n.nextSibling();
        }

        TQValueList<TQDomElement>::Iterator it = childList.begin();
        TQValueList<TQDomElement>::Iterator end = childList.end();
        for (; it!= end ; ++it)
            root.appendChild((*it));
    }
}

#include "importers.moc"