File indexing completed on 2024-04-14 03:53:13

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1998-2009 David Faure <faure@kde.org>
0004     SPDX-FileCopyrightText: 2003 Sven Leiber <s.leiber@web.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only
0007 */
0008 
0009 #ifndef KNEWFILEMENU_H
0010 #define KNEWFILEMENU_H
0011 
0012 #include "kiofilewidgets_export.h"
0013 
0014 #include <KActionMenu>
0015 #include <QUrl>
0016 
0017 #include <memory>
0018 
0019 class KJob;
0020 
0021 class KActionCollection;
0022 class KNewFileMenuPrivate;
0023 
0024 /**
0025  * @class KNewFileMenu knewfilemenu.h <KNewFileMenu>
0026  *
0027  * The 'Create New' submenu, for creating files using templates
0028  * (e.g.\ "new HTML file") and directories.
0029  *
0030  * The same instance can be used by both for the File menu and the RMB popup menu,
0031  * in a file manager. This is also used in the file dialog's RMB menu.
0032  *
0033  * To use this class, you need to connect aboutToShow() of the File menu
0034  * with slotCheckUpToDate() and to call slotCheckUpToDate() before showing
0035  * the RMB popupmenu.
0036  *
0037  * KNewFileMenu automatically updates the list of templates shown if installed templates
0038  * are added/updated/deleted.
0039  *
0040  * @author Björn Ruberg <bjoern@ruberg-wegener.de>
0041  * Made dialogs working asynchronously
0042  * @author David Faure <faure@kde.org>
0043  * Ideas and code for the new template handling mechanism ('link' desktop files)
0044  * from Christoph Pickart <pickart@iam.uni-bonn.de>
0045  */
0046 class KIOFILEWIDGETS_EXPORT KNewFileMenu : public KActionMenu
0047 {
0048     Q_OBJECT
0049 public:
0050     /**
0051      * Constructor.
0052      *
0053      * @param parent the parent object, for ownership.
0054      * If the parent object is a widget, it will also be used as the parent widget
0055      * for any dialogs that this class might show. Otherwise, call setParentWidget.
0056      *
0057      * @since 5.100
0058      */
0059     KNewFileMenu(QObject *parent);
0060 
0061     /**
0062      * Destructor.
0063      * KNewMenu uses internally a globally shared cache, so that multiple instances
0064      * of it don't need to parse the installed templates multiple times. Therefore
0065      * you can safely create and delete KNewMenu instances without a performance issue.
0066      */
0067     ~KNewFileMenu() override;
0068 
0069     /**
0070      * Returns the modality of dialogs
0071      */
0072     bool isModal() const;
0073 
0074     /**
0075      * Sets the modality of dialogs created by KNewFile. Set to false if you do not want to block
0076      * your application window when entering a new directory name i.e.
0077      */
0078     void setModal(bool modality);
0079 
0080     /**
0081      * Sets a parent widget for the dialogs shown by KNewFileMenu.
0082      * This is strongly recommended, for apps with a main window.
0083      */
0084     void setParentWidget(QWidget *parentWidget);
0085 
0086     /**
0087      * Set the working directory.
0088      * Files will be created relative to this directory.
0089      * @since 5.97.
0090      */
0091     void setWorkingDirectory(const QUrl &directory);
0092 
0093     /**
0094      * Returns the working directory.
0095      * Files will be created relative to this directory.
0096      * @since 5.97.
0097      */
0098     QUrl workingDirectory() const;
0099 
0100     /**
0101      * Only show the files in a given set of MIME types.
0102      * This is useful in specialized applications (while file managers, on
0103      * the other hand, want to show all MIME types).
0104      */
0105     void setSupportedMimeTypes(const QStringList &mime);
0106 
0107     /**
0108      * Returns the MIME types set in supportedMimeTypes()
0109      */
0110     QStringList supportedMimeTypes() const;
0111 
0112     /**
0113      * Whether on not the dialog should emit `selectExistingDir` when trying to create an exist directory
0114      *
0115      * default: false
0116      *
0117      * @since 5.76
0118      */
0119     void setSelectDirWhenAlreadyExist(bool b);
0120 
0121     /**
0122      * Use this to set a shortcut for the "New Folder" action.
0123      *
0124      * The shortcut is copied from @param action.
0125      *
0126      * @since 5.100
0127      */
0128     void setNewFolderShortcutAction(QAction *action);
0129 
0130     /**
0131      * Use this to set a shortcut for the new file action.
0132      *
0133      * The shortcut is copied from @param action.
0134      *
0135      * @since 5.100
0136      */
0137     void setNewFileShortcutAction(QAction *action);
0138 
0139 public Q_SLOTS:
0140     /**
0141      * Checks if updating the list is necessary
0142      * IMPORTANT : Call this in the slot for aboutToShow.
0143      */
0144     void checkUpToDate();
0145 
0146     /**
0147      * Call this to create a new directory as if the user had done it using
0148      * a popupmenu. This is useful to make sure that creating a directory with
0149      * a key shortcut (e.g. F10) triggers the exact same code as when using
0150      * the New menu.
0151      * Requirements: since 5.97 call setWorkingDirectory first (for older releases call setPopupFiles first), and keep this KNewFileMenu instance
0152      * alive (the mkdir is async).
0153      */
0154     void createDirectory();
0155 
0156     /**
0157      * Call this to create a new file as if the user had done it using
0158      * a popupmenu. This is useful to make sure that creating a directory with
0159      * a key shortcut (e.g. Shift-F10) triggers the exact same code as when using
0160      * the New menu.
0161      * Requirements: since 5.97 call setWorkingDirectory first (for older releases call setPopupFiles first), and keep this KNewFileMenu instance
0162      * alive (the copy is async).
0163      * @since 5.53
0164      */
0165     void createFile();
0166 
0167 Q_SIGNALS:
0168     /**
0169      * Emitted once the file (or symlink) @p url has been successfully created
0170      */
0171     void fileCreated(const QUrl &url);
0172 
0173     /**
0174      * Emitted once the directory @p url has been successfully created
0175      */
0176     void directoryCreated(const QUrl &url);
0177 
0178     /**
0179      * Emitted when trying to create a new directory that has the same name as
0180      * an existing one, so that KDirOperator can select the existing item in
0181      * the view (in case the user wants to use that directory instead of creating
0182      * a new one).
0183      *
0184      * @since 5.76
0185      */
0186     void selectExistingDir(const QUrl &url);
0187 
0188 protected Q_SLOTS:
0189 
0190     /**
0191      * Called when the job that copied the template has finished.
0192      * This method is virtual so that error handling can be reimplemented.
0193      * Make sure to call the base class slotResult when !job->error() though.
0194      */
0195     virtual void slotResult(KJob *job);
0196 
0197 private:
0198     friend class KNewFileMenuPrivate;
0199     std::unique_ptr<KNewFileMenuPrivate> const d;
0200 };
0201 
0202 #endif