]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
provide a 'Create New' entry for a context menu above a directory
[dolphin.git] / src / dolphincontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program 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 *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphincontextmenu.h"
22
23 #include "dolphinmainwindow.h"
24 #include "dolphinnewmenu.h"
25 #include "dolphinsettings.h"
26 #include "dolphinview.h"
27 #include "dolphinviewcontainer.h"
28 #include "dolphin_generalsettings.h"
29
30 #include <kactioncollection.h>
31 #include <kfileplacesmodel.h>
32 #include <kdesktopfile.h>
33 #include <kglobal.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
36 #include <kmenu.h>
37 #include <kmenubar.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
40 #include <knewmenu.h>
41 #include <konqmimedata.h>
42 #include <konq_fileitemcapabilities.h>
43 #include <konq_operations.h>
44 #include <konq_menuactions.h>
45 #include <konq_popupmenuinformation.h>
46 #include <klocale.h>
47 #include <kpropertiesdialog.h>
48 #include <krun.h>
49 #include <kstandardaction.h>
50 #include <kstandarddirs.h>
51
52 #include <QtGui/QApplication>
53 #include <QtGui/QClipboard>
54 #include <QtCore/QDir>
55
56 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
57 const KFileItem& fileInfo,
58 const KUrl& baseUrl) :
59 m_mainWindow(parent),
60 m_capabilities(0),
61 m_fileInfo(fileInfo),
62 m_baseUrl(baseUrl),
63 m_context(NoContext)
64 {
65 // The context menu either accesses the URLs of the selected items
66 // or the items itself. To increase the performance both lists are cached.
67 DolphinView* view = m_mainWindow->activeViewContainer()->view();
68 m_selectedUrls = view->selectedUrls();
69 m_selectedItems = view->selectedItems();
70 }
71
72 DolphinContextMenu::~DolphinContextMenu()
73 {
74 delete m_capabilities;
75 m_capabilities = 0;
76 }
77
78 void DolphinContextMenu::open()
79 {
80 // get the context information
81 if (m_baseUrl.protocol() == "trash") {
82 m_context |= TrashContext;
83 }
84
85 if (!m_fileInfo.isNull() && (m_selectedItems.count() > 0)) {
86 m_context |= ItemContext;
87 // TODO: handle other use cases like devices + desktop files
88 }
89
90 // open the corresponding popup for the context
91 if (m_context & TrashContext) {
92 if (m_context & ItemContext) {
93 openTrashItemContextMenu();
94 } else {
95 openTrashContextMenu();
96 }
97 } else if (m_context & ItemContext) {
98 openItemContextMenu();
99 } else {
100 Q_ASSERT(m_context == NoContext);
101 openViewportContextMenu();
102 }
103 }
104
105 void DolphinContextMenu::openTrashContextMenu()
106 {
107 Q_ASSERT(m_context & TrashContext);
108
109 KMenu* popup = new KMenu(m_mainWindow);
110
111 addShowMenubarAction(popup);
112
113 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup);
114 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
115 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
116 popup->addAction(emptyTrashAction);
117
118 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
119 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
120
121 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
122 popup->addAction(propertiesAction);
123
124 QAction *action = popup->exec(QCursor::pos());
125 if (action == emptyTrashAction) {
126 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
127 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
128 text,
129 QString(),
130 KGuiItem(i18nc("@action:button", "Empty Trash"),
131 KIcon("user-trash"))
132 ) == KMessageBox::Continue;
133 if (del) {
134 KonqOperations::emptyTrash(m_mainWindow);
135 }
136 } else if (action == addToPlacesAction) {
137 const KUrl& url = m_mainWindow->activeViewContainer()->url();
138 if (url.isValid()) {
139 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
140 }
141 }
142
143 popup->deleteLater();
144 }
145
146 void DolphinContextMenu::openTrashItemContextMenu()
147 {
148 Q_ASSERT(m_context & TrashContext);
149 Q_ASSERT(m_context & ItemContext);
150
151 KMenu* popup = new KMenu(m_mainWindow);
152
153 addShowMenubarAction(popup);
154
155 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
156 popup->addAction(restoreAction);
157
158 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
159 popup->addAction(deleteAction);
160
161 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
162 popup->addAction(propertiesAction);
163
164 if (popup->exec(QCursor::pos()) == restoreAction) {
165 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
166 }
167
168 popup->deleteLater();
169 }
170
171 void DolphinContextMenu::openItemContextMenu()
172 {
173 Q_ASSERT(!m_fileInfo.isNull());
174
175 KMenu* popup = new KMenu(m_mainWindow);
176 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
177 // setup 'Create New' menu
178 DolphinNewMenu* newMenu = new DolphinNewMenu(popup, m_mainWindow);
179 newMenu->slotCheckUpToDate();
180 newMenu->setPopupFiles(m_fileInfo.url());
181
182 KMenu* menu = newMenu->menu();
183 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
184 menu->setIcon(KIcon("document-new"));
185 popup->addMenu(newMenu->menu());
186 popup->addSeparator();
187
188 // insert 'Open in new window' and 'Open in new tab' entries
189 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
190 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
191 popup->addSeparator();
192 }
193 addShowMenubarAction(popup);
194 insertDefaultItemActions(popup);
195
196 popup->addSeparator();
197
198 // insert 'Bookmark This Folder' entry if exactly one item is selected
199 QAction* addToPlacesAction = 0;
200 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
201 addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
202 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
203 }
204
205 KonqPopupMenuInformation popupInfo;
206 popupInfo.setItems(m_selectedItems);
207 popupInfo.setParentWidget(m_mainWindow);
208 KonqMenuActions menuActions;
209 menuActions.setPopupMenuInfo(popupInfo);
210
211 // Insert 'Open With...' action or sub menu
212 menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'");
213
214 // Insert 'Actions' sub menu
215 if (menuActions.addActionsTo(popup)) {
216 popup->addSeparator();
217 }
218
219 // Insert 'Copy To' and 'Move To' sub menus
220 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
221 m_copyToMenu.setItems(m_selectedItems);
222 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
223 m_copyToMenu.addActionsTo(popup);
224 popup->addSeparator();
225 }
226
227 // insert 'Properties...' entry
228 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
229 popup->addAction(propertiesAction);
230
231 QAction* activatedAction = popup->exec(QCursor::pos());
232
233 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
234 const KUrl selectedUrl(m_fileInfo.url());
235 if (selectedUrl.isValid()) {
236 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
237 selectedUrl);
238 }
239 }
240
241 popup->deleteLater();
242 }
243
244 void DolphinContextMenu::openViewportContextMenu()
245 {
246 KMenu* popup = new KMenu(m_mainWindow);
247
248 addShowMenubarAction(popup);
249
250 // setup 'Create New' menu
251 KNewMenu* newMenu = m_mainWindow->newMenu();
252 newMenu->slotCheckUpToDate();
253 newMenu->setPopupFiles(m_baseUrl);
254 popup->addMenu(newMenu->menu());
255 popup->addSeparator();
256
257 QAction* pasteAction = createPasteAction();
258 popup->addAction(pasteAction);
259
260 // setup 'View Mode' menu
261 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
262
263 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
264 viewModeMenu->addAction(iconsMode);
265
266 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
267 viewModeMenu->addAction(detailsMode);
268
269 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
270 viewModeMenu->addAction(columnsMode);
271
272 popup->addMenu(viewModeMenu);
273
274 popup->addSeparator();
275
276 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
277 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
278 popup->addSeparator();
279
280 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
281
282 QAction* action = popup->exec(QCursor::pos());
283 if (action == propertiesAction) {
284 const KUrl& url = m_mainWindow->activeViewContainer()->url();
285
286 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
287 dialog->setAttribute(Qt::WA_DeleteOnClose);
288 dialog->show();
289 dialog->raise();
290 dialog->activateWindow();
291 } else if (action == addToPlacesAction) {
292 const KUrl& url = m_mainWindow->activeViewContainer()->url();
293 if (url.isValid()) {
294 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
295 }
296 }
297
298 popup->deleteLater();
299 }
300
301 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
302 {
303 Q_ASSERT(popup != 0);
304 const KActionCollection* collection = m_mainWindow->actionCollection();
305
306 // insert 'Cut', 'Copy' and 'Paste'
307 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
308 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
309 QAction* pasteAction = createPasteAction();
310
311 popup->addAction(cutAction);
312 popup->addAction(copyAction);
313 popup->addAction(pasteAction);
314 popup->addSeparator();
315
316 // insert 'Rename'
317 QAction* renameAction = collection->action("rename");
318 popup->addAction(renameAction);
319
320 // insert 'Move to Trash' and (optionally) 'Delete'
321 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
322 KConfigGroup configGroup(globalConfig, "KDE");
323 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
324
325 const KUrl& url = m_mainWindow->activeViewContainer()->url();
326 if (url.isLocalFile()) {
327 QAction* moveToTrashAction = collection->action("move_to_trash");
328 popup->addAction(moveToTrashAction);
329 } else {
330 showDeleteCommand = true;
331 }
332
333 if (showDeleteCommand) {
334 QAction* deleteAction = collection->action("delete");
335 popup->addAction(deleteAction);
336 }
337 }
338
339 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
340 {
341 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
342 if (!m_mainWindow->menuBar()->isVisible()) {
343 menu->addAction(showMenuBar);
344 menu->addSeparator();
345 }
346 }
347
348 QString DolphinContextMenu::placesName(const KUrl& url) const
349 {
350 QString name = url.fileName();
351 if (name.isEmpty()) {
352 name = url.host();
353 }
354 return name;
355 }
356
357 QAction* DolphinContextMenu::createPasteAction()
358 {
359 QAction* action = 0;
360 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
361 if (isDir && (m_selectedItems.count() == 1)) {
362 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
363 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
364 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
365 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
366 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
367 } else {
368 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
369 }
370
371 return action;
372 }
373
374 KonqFileItemCapabilities& DolphinContextMenu::capabilities()
375 {
376 if (m_capabilities == 0) {
377 m_capabilities = new KonqFileItemCapabilities(m_selectedItems);
378 }
379 return *m_capabilities;
380 }
381
382 #include "dolphincontextmenu.moc"