File indexing completed on 2022-11-29 20:24:10

0001 // vim: set ts=4 sts=4 sw=4 et:
0002 /* This file is part of the KDE project
0003    Copyright (C) 2000 David Faure <faure@kde.org>
0004    Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License, or (at your option) version 3.
0010 
0011    This program is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014    GNU General Public License for more details.
0015 
0016    You should have received a copy of the GNU General Public License
0017    along with this program.  If not, see <http://www.gnu.org/licenses/>
0018 */
0019 
0020 #ifndef __importers_h
0021 #define __importers_h
0022 
0023 #include "kbookmarkmodel/commands.h"
0024 #include <KLocalizedString>
0025 
0026 #include <QObject>
0027 
0028 class KBookmark;
0029 
0030 // TODO: remove QObject base class? doesn't seem to be used.
0031 class ImportCommand : public QObject, public QUndoCommand, public IKEBCommand
0032 {
0033     Q_OBJECT
0034 public:
0035     explicit ImportCommand(KBookmarkModel *model);
0036 
0037     virtual void import(const QString &fileName, bool folder) = 0;
0038 
0039     void setVisibleName(const QString &visibleName);
0040     QString visibleName() const
0041     {
0042         return m_visibleName;
0043     }
0044     virtual QString requestFilename() const = 0;
0045 
0046     static ImportCommand *performImport(KBookmarkModel *model, const QString &, QWidget *);
0047     static ImportCommand *importerFactory(KBookmarkModel *model, const QString &);
0048 
0049     ~ImportCommand() override
0050     {
0051     }
0052 
0053     void redo() override;
0054     void undo() override;
0055     QString affectedBookmarks() const override;
0056 
0057     QString groupAddress() const
0058     {
0059         return m_group;
0060     }
0061     QString folder() const;
0062 
0063 protected:
0064     /**
0065      * @param fileName HTML file to import
0066      * @param folder name of the folder to create. Empty for no creation (root()).
0067      * @param icon icon for the new folder, if @p folder isn't empty
0068      * @param utf8 true if the HTML is in utf-8 encoding
0069      */
0070     void init(const QString &fileName, bool folder, const QString &icon, bool utf8)
0071     {
0072         m_fileName = fileName;
0073         m_folder = folder;
0074         m_icon = icon;
0075         m_utf8 = utf8;
0076     }
0077 
0078     virtual void doCreateHoldingFolder(KBookmarkGroup &bkGroup);
0079     virtual void doExecute(const KBookmarkGroup &) = 0;
0080 
0081 protected:
0082     KBookmarkModel *m_model;
0083     QString m_visibleName;
0084     QString m_fileName;
0085     QString m_icon;
0086     QString m_group;
0087     bool m_utf8;
0088 
0089 private:
0090     bool m_folder;
0091     QUndoCommand *m_cleanUpCmd;
0092 };
0093 
0094 // part pure
0095 class XBELImportCommand : public ImportCommand
0096 {
0097 public:
0098     explicit XBELImportCommand(KBookmarkModel *model)
0099         : ImportCommand(model)
0100     {
0101     }
0102     void import(const QString &fileName, bool folder) override = 0;
0103     QString requestFilename() const override = 0;
0104 
0105 private:
0106     void doCreateHoldingFolder(KBookmarkGroup &bkGroup) override;
0107     void doExecute(const KBookmarkGroup &) override;
0108 };
0109 
0110 class GaleonImportCommand : public XBELImportCommand
0111 {
0112 public:
0113     explicit GaleonImportCommand(KBookmarkModel *model)
0114         : XBELImportCommand(model)
0115     {
0116         setVisibleName(i18n("Galeon"));
0117     }
0118     void import(const QString &fileName, bool folder) override
0119     {
0120         init(fileName, folder, QLatin1String(""), false);
0121     }
0122     QString requestFilename() const override;
0123 };
0124 
0125 class KDE2ImportCommand : public XBELImportCommand
0126 {
0127 public:
0128     explicit KDE2ImportCommand(KBookmarkModel *model)
0129         : XBELImportCommand(model)
0130     {
0131         setVisibleName(i18n("KDE"));
0132     }
0133     void import(const QString &fileName, bool folder) override
0134     {
0135         init(fileName, folder, QLatin1String(""), false);
0136     }
0137     QString requestFilename() const override;
0138 };
0139 
0140 // part pure
0141 class HTMLImportCommand : public ImportCommand
0142 {
0143 public:
0144     explicit HTMLImportCommand(KBookmarkModel *model)
0145         : ImportCommand(model)
0146     {
0147     }
0148     void import(const QString &fileName, bool folder) override = 0;
0149     QString requestFilename() const override = 0;
0150 
0151 private:
0152     void doExecute(const KBookmarkGroup &) override;
0153 };
0154 
0155 class NSImportCommand : public HTMLImportCommand
0156 {
0157 public:
0158     explicit NSImportCommand(KBookmarkModel *model)
0159         : HTMLImportCommand(model)
0160     {
0161         setVisibleName(i18n("Netscape"));
0162     }
0163     void import(const QString &fileName, bool folder) override
0164     {
0165         init(fileName, folder, QStringLiteral("netscape"), false);
0166     }
0167     QString requestFilename() const override;
0168 };
0169 
0170 class MozImportCommand : public HTMLImportCommand
0171 {
0172 public:
0173     explicit MozImportCommand(KBookmarkModel *model)
0174         : HTMLImportCommand(model)
0175     {
0176         setVisibleName(i18n("Mozilla"));
0177     }
0178     void import(const QString &fileName, bool folder) override
0179     {
0180         init(fileName, folder, QStringLiteral("mozilla"), true);
0181     }
0182     QString requestFilename() const override;
0183 };
0184 
0185 class IEImportCommand : public ImportCommand
0186 {
0187 public:
0188     explicit IEImportCommand(KBookmarkModel *model)
0189         : ImportCommand(model)
0190     {
0191         setVisibleName(i18n("IE"));
0192     }
0193     void import(const QString &fileName, bool folder) override
0194     {
0195         init(fileName, folder, QLatin1String(""), false);
0196     }
0197     QString requestFilename() const override;
0198 
0199 private:
0200     void doExecute(const KBookmarkGroup &) override;
0201 };
0202 
0203 class OperaImportCommand : public ImportCommand
0204 {
0205 public:
0206     explicit OperaImportCommand(KBookmarkModel *model)
0207         : ImportCommand(model)
0208     {
0209         setVisibleName(i18n("Opera"));
0210     }
0211     void import(const QString &fileName, bool folder) override
0212     {
0213         init(fileName, folder, QStringLiteral("opera"), false);
0214     }
0215     QString requestFilename() const override;
0216 
0217 private:
0218     void doExecute(const KBookmarkGroup &) override;
0219 };
0220 
0221 #endif