1/* This file is part of the KDE project
2 Copyright (C) 1998-2009 David Faure <faure@kde.org>
3 2003 Sven Leiber <s.leiber@web.de>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 or at your option version 3.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#ifndef KNEWFILEMENU_H
22#define KNEWFILEMENU_H
23
24#include <kactionmenu.h>
25#include <kurl.h>
26#include <kfile_export.h>
27
28class KJob;
29
30class KActionCollection;
31class KNewFileMenuPrivate;
32
33/**
34 * The 'Create New' submenu, for creating files using templates
35 * (e.g. "new HTML file") and directories.
36 *
37 * The same instance can be used by both for the File menu and the RMB popup menu,
38 * in a file manager. This is also used in the file dialog's RMB menu.
39 *
40 * To use this class, you need to connect aboutToShow() of the File menu
41 * with slotCheckUpToDate() and to call slotCheckUpToDate() before showing
42 * the RMB popupmenu.
43 *
44 * KNewFileMenu automatically updates the list of templates shown if installed templates
45 * are added/updated/deleted.
46 *
47 * @author Björn Ruberg <bjoern@ruberg-wegener.de>
48 * Made dialogs working asynchronously
49 * @author David Faure <faure@kde.org>
50 * Ideas and code for the new template handling mechanism ('link' desktop files)
51 * from Christoph Pickart <pickart@iam.uni-bonn.de>
52 * @since 4.5
53 */
54class KFILE_EXPORT KNewFileMenu : public KActionMenu
55{
56 Q_OBJECT
57public:
58 /**
59 * Constructor.
60 * @param collection the KActionCollection this KAction should be added to.
61 * @param name action name, when adding the action to the collection
62 * @param parent the parent object, for ownership.
63 * If the parent object is a widget, it will also used as parent widget
64 * for any dialogs that this class might show. Otherwise, call setParentWidget.
65 */
66 KNewFileMenu(KActionCollection* collection, const QString& name, QObject* parent);
67
68 /**
69 * Destructor.
70 * KNewMenu uses internally a globally shared cache, so that multiple instances
71 * of it don't need to parse the installed templates multiple times. Therefore
72 * you can safely create and delete KNewMenu instances without a performance issue.
73 */
74 virtual ~KNewFileMenu();
75
76 /**
77 * Returns the modality of dialogs
78 */
79 bool isModal() const;
80
81 /**
82 * Returns the files that the popup is shown for
83 */
84 KUrl::List popupFiles() const;
85
86 /**
87 * Sets the modality of dialogs created by KNewFile. Set to false if you do not want to block
88 * your application window when entering a new directory name i.e.
89 */
90 void setModal(bool modality);
91
92 /**
93 * Sets a parent widget for the dialogs shown by KNewFileMenu.
94 * This is strongly recommended, for apps with a main window.
95 */
96 void setParentWidget(QWidget* parentWidget);
97
98 /**
99 * Set the files the popup is shown for
100 * Call this before showing up the menu
101 */
102 void setPopupFiles(const KUrl::List& files);
103
104 /**
105 * Only show the files in a given set of mimetypes.
106 * This is useful in specialized applications (while file managers, on
107 * the other hand, want to show all mimetypes).
108 */
109 void setSupportedMimeTypes(const QStringList& mime);
110
111 /**
112 * Set if the directory view currently shows dot files.
113 */
114 void setViewShowsHiddenFiles(bool b);
115
116 /**
117 * Returns the mimetypes set in supportedMimeTypes()
118 */
119 QStringList supportedMimeTypes() const;
120
121public Q_SLOTS:
122 /**
123 * Checks if updating the list is necessary
124 * IMPORTANT : Call this in the slot for aboutToShow.
125 * And while you're there, you probably want to call setViewShowsHiddenFiles ;)
126 */
127 void checkUpToDate();
128
129 /**
130 * Call this to create a new directory as if the user had done it using
131 * a popupmenu. This is useful to make sure that creating a directory with
132 * a key shortcut (e.g. F10) triggers the exact same code as when using
133 * the New menu.
134 * Requirements: call setPopupFiles first, and keep this KNewFileMenu instance
135 * alive (the mkdir is async).
136 */
137 void createDirectory();
138
139Q_SIGNALS:
140 /**
141 * Emitted once the file (or symlink) @p url has been successfully created
142 */
143 void fileCreated(const KUrl& url);
144
145 /**
146 * Emitted once the directory @p url has been successfully created
147 */
148 void directoryCreated(const KUrl& url);
149
150protected Q_SLOTS:
151
152 /**
153 * Called when the job that copied the template has finished.
154 * This method is virtual so that error handling can be reimplemented.
155 * Make sure to call the base class slotResult when !job->error() though.
156 */
157 virtual void slotResult(KJob* job);
158
159
160private:
161 Q_PRIVATE_SLOT(d, void _k_slotAbortDialog())
162 Q_PRIVATE_SLOT(d, void _k_slotActionTriggered(QAction*))
163 Q_PRIVATE_SLOT(d, void _k_slotCreateDirectory(bool writeHiddenDir = false))
164 Q_PRIVATE_SLOT(d, void _k_slotCreateHiddenDirectory())
165 Q_PRIVATE_SLOT(d, void _k_slotFillTemplates())
166 Q_PRIVATE_SLOT(d, void _k_slotOtherDesktopFile())
167 Q_PRIVATE_SLOT(d, void _k_slotRealFileOrDir())
168 Q_PRIVATE_SLOT(d, void _k_slotTextChanged(const QString))
169 Q_PRIVATE_SLOT(d, void _k_slotSymLink())
170 Q_PRIVATE_SLOT(d, void _k_slotUrlDesktopFile())
171
172 KNewFileMenuPrivate* const d;
173
174};
175
176#endif
177