File indexing completed on 2024-02-25 05:07:09

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2006 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de>
0005    Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 */
0012 
0013 #ifndef TRANSFERTREEMODEL_H
0014 #define TRANSFERTREEMODEL_H
0015 
0016 #include <QList>
0017 #include <QMimeData>
0018 #include <QPointer>
0019 #include <QStandardItemModel>
0020 #include <QUrl>
0021 
0022 #include "core/handler.h"
0023 #include "core/transfer.h"
0024 #include "core/transfergroup.h"
0025 #include "kget_export.h"
0026 
0027 class TransferGroupHandler;
0028 class TransferGroup;
0029 class TransferHandler;
0030 class Transfer;
0031 class Scheduler;
0032 class TransferModelItem;
0033 class GroupModelItem;
0034 
0035 class ItemMimeData : public QMimeData
0036 {
0037     Q_OBJECT
0038 public:
0039     ItemMimeData();
0040     ~ItemMimeData() override;
0041 
0042     /**
0043      * Appends a transfer to the list of transfers.
0044      * The pointer is there to check later on, that the transfer still exists
0045      */
0046     void appendTransfer(const QPointer<TransferHandler> &transfer);
0047 
0048     /**
0049      * Returns all appended transfers
0050      * The pointer is there to check later on, that the transfer still exists
0051      */
0052     QList<QPointer<TransferHandler>> transfers() const;
0053 
0054 private:
0055     QList<QPointer<TransferHandler>> m_transfers;
0056 };
0057 
0058 class KGET_EXPORT ModelItem : public QStandardItem
0059 {
0060 public:
0061     ModelItem(Handler *handler);
0062     ~ModelItem() override;
0063 
0064     QVariant data(int role = Qt::UserRole + 1) const override = 0;
0065     void emitDataChanged();
0066     Handler *handler();
0067     virtual bool isGroup();
0068 
0069     GroupModelItem *asGroup();
0070     TransferModelItem *asTransfer();
0071 
0072 private:
0073     Handler *m_handler;
0074 };
0075 
0076 class KGET_EXPORT TransferModelItem : public ModelItem
0077 {
0078 public:
0079     TransferModelItem(TransferHandler *handler);
0080     ~TransferModelItem() override;
0081 
0082     QVariant data(int role = Qt::UserRole + 1) const override;
0083 
0084     TransferHandler *transferHandler();
0085 
0086 private:
0087     TransferHandler *m_transferHandler;
0088     mutable QIcon m_mimeType;
0089 };
0090 
0091 class KGET_EXPORT GroupModelItem : public ModelItem
0092 {
0093 public:
0094     GroupModelItem(TransferGroupHandler *handler);
0095     ~GroupModelItem() override;
0096 
0097     QVariant data(int role = Qt::UserRole + 1) const override;
0098 
0099     TransferGroupHandler *groupHandler();
0100 
0101     bool isGroup() override;
0102 
0103 private:
0104     TransferGroupHandler *m_groupHandler;
0105 };
0106 
0107 class KGET_EXPORT TransferTreeModel : public QStandardItemModel
0108 {
0109     Q_OBJECT
0110 
0111     friend class TransferGroupHandler;
0112     friend class TransferGroup;
0113     friend class TransferHandler;
0114     friend class Transfer;
0115 
0116 public:
0117     enum Columns { Name, Status, Size, Progress, Speed, RemainingTime };
0118     TransferTreeModel(Scheduler *scheduler);
0119     ~TransferTreeModel() override;
0120 
0121     void addGroup(TransferGroup *group);
0122     void delGroup(TransferGroup *group);
0123 
0124     void addTransfers(const QList<Transfer *> &transfers, TransferGroup *group);
0125     void delTransfers(const QList<Transfer *> &transfers);
0126 
0127     TransferModelItem *itemFromTransferHandler(TransferHandler *handler);
0128     GroupModelItem *itemFromTransferGroupHandler(TransferGroupHandler *handler);
0129     ModelItem *itemFromHandler(Handler *handler);
0130 
0131     ModelItem *itemFromIndex(const QModelIndex &index) const;
0132 
0133     void moveTransfer(Transfer *transfer, TransferGroup *destGroup, Transfer *after = nullptr);
0134     void moveTransfer(TransferHandler *transfer, TransferGroupHandler *destGroup, TransferHandler *after = nullptr);
0135 
0136     QList<TransferGroup *> transferGroups();
0137 
0138     TransferGroup *findGroup(const QString &groupName);
0139     Transfer *findTransfer(const QUrl &src);
0140     Transfer *findTransferByDestination(const QUrl &dest);
0141     Transfer *findTransferByDBusObjectPath(const QString &dbusObjectPath);
0142 
0143     void postDataChangedEvent(TransferHandler *transfer);
0144     void postDataChangedEvent(TransferGroupHandler *group);
0145 
0146     Qt::ItemFlags flags(const QModelIndex &index) const override;
0147     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0148 
0149     // Drag & drop functions
0150     Qt::DropActions supportedDropActions() const override;
0151     QStringList mimeTypes() const override;
0152     QMimeData *mimeData(const QModelIndexList &indexes) const override;
0153     bool dropMimeData(const QMimeData *mdata, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
0154 
0155     static QString columnName(int column);
0156     static int column(Transfer::TransferChange flag);
0157     static int column(TransferGroup::GroupChange flag);
0158 
0159 Q_SIGNALS:
0160     void groupAddedEvent(TransferGroupHandler *);
0161     void groupRemovedEvent(TransferGroupHandler *);
0162     void groupsChangedEvent(QMap<TransferGroupHandler *, TransferGroup::ChangesFlags>);
0163     void transfersAddedEvent(QList<TransferHandler *> transfers);
0164     void transfersAboutToBeRemovedEvent(const QList<TransferHandler *> &transfers);
0165     void transfersRemovedEvent(const QList<TransferHandler *> &transfers);
0166     void transferMovedEvent(TransferHandler *, TransferGroupHandler *);
0167     void transfersChangedEvent(QMap<TransferHandler *, Transfer::ChangesFlags>);
0168 
0169 private:
0170     void timerEvent(QTimerEvent *event) override;
0171 
0172     Scheduler *m_scheduler;
0173 
0174     // Timer related variables
0175     QList<TransferHandler *> m_changedTransfers;
0176     QList<TransferGroupHandler *> m_changedGroups;
0177 
0178     QList<GroupModelItem *> m_transferGroups;
0179     QList<TransferModelItem *> m_transfers;
0180 
0181     int m_timerId;
0182 };
0183 
0184 #endif