File indexing completed on 2024-12-01 07:38:56

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2007 by Javier Goday <jgoday@gmail.com>
0005    Copyright (C) 2008 - 2009 by Lukas Appelhans <l.appelhans@gmx.de>
0006    Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
0007 
0008    This program is free software; you can redistribute it and/or
0009    modify it under the terms of the GNU General Public
0010    License as published by the Free Software Foundation; either
0011    version 2 of the License, or (at your option) any later version.
0012 */
0013 
0014 #ifndef NEW_TRANSFER_DIALOG_H
0015 #define NEW_TRANSFER_DIALOG_H
0016 
0017 #include <QDialog>
0018 #include <QUrl>
0019 
0020 #include "ui_newtransferwidget.h"
0021 
0022 class KJob;
0023 class TransferHandler;
0024 
0025 /**
0026  * Dialog to allow add one or more transfers to kget.
0027  * If only one transfer is added then the dialog shows a QUrlRequester.
0028  * If a list of transfers are added then the dialog shows a KListWidget (multiple = true)
0029  * with the transfers as checkable items.
0030  * Also display a QUrlComboRequester for the destination file (or folder if multiple = true)
0031  * And a QComboBox with the groups of transfer in case there are more than one
0032  *
0033  * @note this class is private and should be used via NewTransferDialogHandler
0034  */
0035 class NewTransferDialog : public QDialog
0036 {
0037     Q_OBJECT
0038 
0039     friend class NewTransferDialogHandler;
0040 
0041 public:
0042     ~NewTransferDialog() override;
0043 
0044 public Q_SLOTS:
0045     /**
0046      * Called when the transfer group or the urlREquester changed, the dialog sets the default destination
0047      * for transfers in the new group
0048      */
0049     void setDefaultDestination();
0050 
0051 private Q_SLOTS:
0052     void inputTimer();
0053     void checkInput();
0054     void slotFinished(int resultCode);
0055 
0056 private:
0057     explicit NewTransferDialog(QWidget *parent = nullptr);
0058 
0059     /**
0060      * Shows the dialog adding one url list transfers
0061      */
0062     void showDialog(QList<QUrl> list, const QString &suggestedFileName = QString());
0063     void prepareDialog();
0064     bool isEmpty();
0065 
0066     /**
0067      * Determines where is a multiple (listwidget) or single (kurlrequester) transfer
0068      */
0069     void setMultiple(bool useMultiple);
0070 
0071     /**
0072      * Set sources to the dialog
0073      */
0074     void setSource(const QList<QUrl> &sources);
0075 
0076     void setDestinationFileName(const QString &filename);
0077     void setDestination();
0078 
0079     void setWarning(const QString &warning);
0080     void setInformation(const QString &information);
0081 
0082     void dialogAccepted();
0083 
0084     void clear();
0085 
0086 private:
0087     Ui::NewTransferWidget ui;
0088     QWidget *m_window;
0089     QTimer *m_timer;
0090     QList<QUrl> m_sources;
0091 
0092     // points to a folder if m_multiple otherwise to the destination
0093     QUrl m_destination;
0094 
0095     TransferHandler *m_existingTransfer;
0096 
0097     QBrush m_existingFileBackground;
0098     QBrush m_normalBackground;
0099 
0100     bool m_multiple;
0101     bool m_overWriteSingle;
0102 };
0103 
0104 class NewTransferDialogHandler : public QObject
0105 {
0106     Q_OBJECT
0107 
0108 public:
0109     explicit NewTransferDialogHandler(QObject *parent = nullptr);
0110     ~NewTransferDialogHandler() override;
0111 
0112     /**
0113      * @see showNewTransferDialog(QList<QUrl>)
0114      */
0115     static void showNewTransferDialog(const QUrl &url = QUrl());
0116 
0117     /**
0118      * This will show a dialog to the user to input needed information.
0119      * If the last url of the list is a local file or directory, then all files will
0120      * be downloaded to that destination.
0121      * If there are matching groups with default folders and the user set the option to
0122      * use those, then the affected urls will be downloaded without showing them in the dialog
0123      *
0124      * @note MainWindow will always be the parent widget
0125      */
0126     static void showNewTransferDialog(QList<QUrl> list);
0127 
0128 private Q_SLOTS:
0129     void slotMostLocalUrlResult(KJob *job);
0130 
0131 private:
0132     void handleUrls(const int jobId);
0133     void createDialog(const QList<QUrl> &urls, const QString &suggestedFileName);
0134 
0135 private:
0136     struct UrlData {
0137         QList<QUrl> urls;
0138         QString folder;
0139         QString suggestedFileName;
0140         QWidget *parent;
0141     };
0142 
0143     /**
0144      * Always points to the next unused jobId
0145      */
0146     int m_nextJobId;
0147 
0148     /**
0149      * QHash<jobId, numJobsForId>
0150      * Calling addUrls will create jobs for each url with the same id
0151      */
0152     QHash<int, int> m_numJobs;
0153 
0154     /**
0155      * QHash<jobId, UrlData>
0156      * Urls for which mosLocalUrl has finished already,
0157      * folder and suggestedFileName can be an empty string if there are none
0158      */
0159     QHash<int, UrlData> m_urls;
0160 
0161     QPointer<NewTransferDialog> m_dialog;
0162 };
0163 
0164 #endif