File indexing completed on 2024-05-19 05:55:46

0001 /*
0002     SPDX-FileCopyrightText: 2003-2005 George Staikos <staikos@kde.org>
0003     SPDX-FileCopyrightText: 2005 Isaac Clerencia <isaac@warp.es>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef ALLYOURBASE_H
0009 #define ALLYOURBASE_H
0010 
0011 #include <KWallet>
0012 #include <KService>
0013 #include <QListWidget>
0014 
0015 #include <QPixmap>
0016 #include <QDropEvent>
0017 #include <QDragEnterEvent>
0018 #include <QMouseEvent>
0019 #include <QTreeWidget>
0020 
0021 #define KWALLETENTRYMAGIC ((quint32) 0x6B776C65)
0022 #define KWALLETFOLDERMAGIC ((quint32) 0x6B776C66)
0023 
0024 enum KWalletListItemClasses {
0025     KWalletFolderItemClass = QTreeWidgetItem::UserType,
0026     KWalletContainerItemClass,
0027     KWalletEntryItemClass,
0028     KWalletUnknownClass
0029 };
0030 
0031 class KWalletEntryItem : public QTreeWidgetItem
0032 {
0033 public:
0034     KWalletEntryItem(KWallet::Wallet *w, QTreeWidgetItem *parent, const QString &ename);
0035     ~KWalletEntryItem() override;
0036 
0037     const QString &name() const
0038     {
0039         return m_name;
0040     }
0041     void setName(const QString &n);
0042     // Cancel renaming
0043     void restoreName();
0044 
0045 public:
0046     KWallet::Wallet *_wallet = nullptr;
0047 
0048 private:
0049     void setText(int, const QString &) {} // forbidden
0050     QString m_name;
0051 };
0052 
0053 class KWalletContainerItem : public QTreeWidgetItem
0054 {
0055 public:
0056     KWalletContainerItem(QTreeWidgetItem *parent, const QString &name, KWallet::Wallet::EntryType entryType);
0057     ~KWalletContainerItem() override;
0058 
0059 public:
0060     KWallet::Wallet::EntryType entryType();
0061     bool contains(const QString &itemKey) const;
0062     QTreeWidgetItem *getItem(const QString &itemKey) const;
0063 
0064 private:
0065     KWallet::Wallet::EntryType _type;
0066 };
0067 
0068 class KWalletFolderItem : public QTreeWidgetItem
0069 {
0070 public:
0071     KWalletFolderItem(KWallet::Wallet *w, QTreeWidget *parent, const QString &name, int entries);
0072     ~KWalletFolderItem() override;
0073 
0074     virtual bool acceptDrop(const QMimeData *mime) const;
0075 
0076     QString name() const;
0077     void refresh();
0078     KWalletContainerItem *getContainer(KWallet::Wallet::EntryType type);
0079     QIcon getFolderIcon() const;
0080     bool contains(const QString &itemKey) const;
0081     QTreeWidgetItem *getItem(const QString &itemKey) const;
0082     void refreshItemsCount();
0083 
0084 public:
0085     KWallet::Wallet *_wallet = nullptr;
0086 
0087 private:
0088     QString _name;
0089     int _entries;
0090     KService::List m_services;
0091 };
0092 
0093 class KWalletEntryList : public QTreeWidget
0094 {
0095     Q_OBJECT
0096 
0097 public:
0098     explicit KWalletEntryList(QWidget *parent, const QString &name = QString());
0099     ~KWalletEntryList() override;
0100 
0101     bool existsFolder(const QString &name);
0102     KWalletFolderItem *getFolder(const QString &name) const;
0103     void setWallet(KWallet::Wallet *w);
0104 
0105 protected:
0106     void dragEnterEvent(QDragEnterEvent *e) override;
0107     void dragMoveEvent(QDragMoveEvent *e) override;
0108     void dropEvent(QDropEvent *e) override;
0109     void mousePressEvent(QMouseEvent *e) override;
0110     void mouseMoveEvent(QMouseEvent *e) override;
0111 
0112     void itemDropped(QDropEvent *e, QTreeWidgetItem *item);
0113 
0114 private:
0115     static KWalletFolderItem *getItemFolder(QTreeWidgetItem *item);
0116     QMimeData *itemMimeData(const QTreeWidgetItem *i) const;
0117 
0118 public:
0119     KWallet::Wallet *_wallet = nullptr;
0120     QPoint _mousePos;
0121 
0122 public Q_SLOTS:
0123     void selectFirstVisible();
0124     void refreshItemsCount();
0125 };
0126 
0127 class KWalletItem : public QListWidgetItem
0128 {
0129 public:
0130     KWalletItem(QListWidget *parent, const QString &walletName);
0131     ~KWalletItem() override;
0132 
0133     void setOpen(bool state);
0134 
0135     void processDropEvent(QDropEvent *e);
0136 
0137 private:
0138     bool _open;
0139 };
0140 
0141 inline QDataStream &operator<<(QDataStream &str, const KWalletEntryItem &w)
0142 {
0143     QString name = w.text(0);
0144     str << name;
0145     KWallet::Wallet::EntryType et = w._wallet->entryType(name);
0146     str << qint64(et);
0147     QByteArray a;
0148     w._wallet->readEntry(name, a);
0149     str << a;
0150     return str;
0151 }
0152 
0153 inline QDataStream &operator<<(QDataStream &str, const KWalletFolderItem &w)
0154 {
0155     QString oldFolder = w._wallet->currentFolder();
0156     str << w.name();
0157     w._wallet->setFolder(w.name());
0158     const QStringList entries = w._wallet->entryList();
0159     for (const QString &entry : entries) {
0160         str << entry;
0161         KWallet::Wallet::EntryType et = w._wallet->entryType(entry);
0162         str << (qint32)et;
0163         QByteArray a;
0164         w._wallet->readEntry(entry, a);
0165         str << a;
0166     }
0167     w._wallet->setFolder(oldFolder);
0168     return str;
0169 }
0170 
0171 #endif