File indexing completed on 2024-03-24 15:25:59

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
0004     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0005     SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
0006     SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
0007     SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
0008     SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
0009     SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
0010     SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
0011     SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
0012 
0013     SPDX-License-Identifier: LGPL-2.0-only
0014 */
0015 
0016 #ifndef KRECENTFILESACTION_H
0017 #define KRECENTFILESACTION_H
0018 
0019 #include <KSelectAction>
0020 #include <QUrl>
0021 #include <kconfigwidgets_export.h>
0022 
0023 class KConfigGroup;
0024 class KRecentFilesActionPrivate;
0025 
0026 /**
0027  *  @class KRecentFilesAction krecentfilesaction.h KRecentFilesAction
0028  *
0029  *  @short Recent files action
0030  *
0031  *  This class is an action to handle a recent files submenu.
0032  *  The best way to create the action is to use KStandardAction::openRecent.
0033  *  Then you simply need to call loadEntries on startup, saveEntries
0034  *  on shutdown, addURL when your application loads/saves a file.
0035  *
0036  *  @author Michael Koch
0037  */
0038 class KCONFIGWIDGETS_EXPORT KRecentFilesAction : public KSelectAction
0039 {
0040     Q_OBJECT
0041     Q_PROPERTY(int maxItems READ maxItems WRITE setMaxItems)
0042     Q_DECLARE_PRIVATE(KRecentFilesAction)
0043 
0044 public:
0045     /**
0046      * Constructs an action with the specified parent.
0047      *
0048      * @param parent The parent of this action.
0049      */
0050     explicit KRecentFilesAction(QObject *parent);
0051 
0052     /**
0053      * Constructs an action with text; a shortcut may be specified by
0054      * the ampersand character (e.g. \"&amp;Option\" creates a shortcut with key \e O )
0055      *
0056      * This is the most common KAction used when you do not have a
0057      * corresponding icon (note that it won't appear in the current version
0058      * of the "Edit ToolBar" dialog, because an action needs an icon to be
0059      * plugged in a toolbar...).
0060      *
0061      * @param text The text that will be displayed.
0062      * @param parent The parent of this action.
0063      */
0064     KRecentFilesAction(const QString &text, QObject *parent);
0065 
0066     /**
0067      * Constructs an action with text and an icon; a shortcut may be specified by
0068      * the ampersand character (e.g. \"&amp;Option\" creates a shortcut with key \e O )
0069      *
0070      * This is the other common KAction used.  Use it when you
0071      * \e do have a corresponding icon.
0072      *
0073      * @param icon The icon to display.
0074      * @param text The text that will be displayed.
0075      * @param parent The parent of this action.
0076      */
0077     KRecentFilesAction(const QIcon &icon, const QString &text, QObject *parent);
0078 
0079     /**
0080      *  Destructor.
0081      */
0082     ~KRecentFilesAction() override;
0083 
0084     /**
0085      * Adds \a action to the list of URLs, with \a url and title \a name.
0086      *
0087      * Do not use addAction(QAction*), as no url will be associated, and
0088      * consequently urlSelected() will not be emitted when \a action is selected.
0089      */
0090     void addAction(QAction *action, const QUrl &url, const QString &name);
0091 
0092     /**
0093      * Reimplemented for internal reasons.
0094      */
0095     QAction *removeAction(QAction *action) override;
0096 
0097 public Q_SLOTS:
0098     /**
0099      * Clears the recent files list.
0100      * Note that there is also an action shown to the user for clearing the list.
0101      */
0102     virtual void clear();
0103 
0104 public:
0105     /**
0106      *  Returns the maximum of items in the recent files list.
0107      */
0108     int maxItems() const;
0109 
0110     /**
0111      *  Sets the maximum of items in the recent files list.
0112      *  The default for this value is 10 set in the constructor.
0113      *
0114      *  If this value is lesser than the number of items currently
0115      *  in the recent files list the last items are deleted until
0116      *  the number of items are equal to the new maximum.
0117      *
0118      *  Negative values will be normalized to 0.
0119      */
0120     void setMaxItems(int maxItems);
0121 
0122     /**
0123      *  Loads the recent files entries from a given KConfigGroup object.
0124      *  You can provide the name of the group used to load the entries.
0125      *  If the groupname is empty, entries are loaded from a group called 'RecentFiles'.
0126      *  Local file entries that do not exist anymore are not restored.
0127      *
0128      */
0129     void loadEntries(const KConfigGroup &config);
0130 
0131     /**
0132      *  Saves the current recent files entries to a given KConfigGroup object.
0133      *  You can provide the name of the group used to load the entries.
0134      *  If the groupname is empty, entries are saved to a group called 'RecentFiles'.
0135      *
0136      */
0137     void saveEntries(const KConfigGroup &config);
0138 
0139     /**
0140      * Add URL to the recent files list. This will enable this action.
0141      *
0142      *  @param url The URL of the file
0143      *  @param name The user visible pretty name that appears before the URL
0144      *
0145      * @note URLs corresponding to local files in the temporary directory
0146      * (see @ref QDir::tempPath()) are automatically ignored by this method.
0147      */
0148     void addUrl(const QUrl &url, const QString &name = QString());
0149 
0150     /**
0151      *  Remove an URL from the recent files list.
0152      *
0153      *  @param url The URL of the file
0154      */
0155     void removeUrl(const QUrl &url);
0156 
0157     /**
0158      *  Retrieve a list of all URLs in the recent files list.
0159      */
0160     QList<QUrl> urls() const;
0161 
0162 Q_SIGNALS:
0163     /**
0164      *  This signal gets emitted when the user selects an URL.
0165      *
0166      *  @param url The URL that the user selected.
0167      */
0168     void urlSelected(const QUrl &url);
0169 
0170     /**
0171      *  This signal gets emitted when the user clear list.
0172      *  So when user store url in specific config file it can saveEntry.
0173      *  @since 4.3
0174      */
0175     void recentListCleared();
0176 
0177 private:
0178     // Internal
0179     KCONFIGWIDGETS_NO_EXPORT void clearEntries();
0180     // Don't warn about the virtual overload. As the comment of the other
0181     // addAction() says, addAction( QAction* ) should not be used.
0182     using KSelectAction::addAction;
0183 
0184 private:
0185     std::unique_ptr<KRecentFilesActionPrivate> const d_ptr;
0186 };
0187 
0188 #endif