~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to app/fileopscontextmanageritem.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2007 AurĆ©lien GĆ¢teau <agateau@kde.org>
55
55
#include "fileoperations.h"
56
56
#include "sidebar.h"
57
57
 
58
 
namespace Gwenview {
 
58
namespace Gwenview
 
59
{
59
60
 
60
61
struct FileOpsContextManagerItemPrivate {
61
 
        FileOpsContextManagerItem* mContextManagerItem;
62
 
        QListView* mThumbnailView;
63
 
        KXMLGUIClient* mXMLGUIClient;
64
 
        SideBarGroup* mGroup;
65
 
        KAction* mCutAction;
66
 
        KAction* mCopyAction;
67
 
        KAction* mPasteAction;
68
 
        KAction* mCopyToAction;
69
 
        KAction* mMoveToAction;
70
 
        KAction* mLinkToAction;
71
 
        KAction* mRenameAction;
72
 
        KAction* mTrashAction;
73
 
        KAction* mDelAction;
74
 
        KAction* mRestoreAction;
75
 
        KAction* mShowPropertiesAction;
76
 
        KAction* mCreateFolderAction;
77
 
        KAction* mOpenWithAction;
78
 
        QList<QAction*> mRegularFileActionList;
79
 
        QList<QAction*> mTrashFileActionList;
80
 
        QMap<QString, KService::Ptr> mServiceForName;
81
 
        bool mInTrash;
82
 
 
83
 
        KUrl::List urlList() const {
84
 
                KUrl::List urlList;
85
 
 
86
 
                KFileItemList list = mContextManagerItem->contextManager()->selectedFileItemList();
87
 
                if (list.count() > 0) {
88
 
                        urlList = list.urlList();
89
 
                } else {
90
 
                        KUrl url = mContextManagerItem->contextManager()->currentUrl();
91
 
                        Q_ASSERT(url.isValid());
92
 
                        urlList << url;
93
 
                }
94
 
                return urlList;
95
 
        }
96
 
 
97
 
 
98
 
        void updateServiceForName() {
99
 
                // This code is inspired from
100
 
                // kdebase/apps/lib/konq/konq_menuactions.cpp
101
 
 
102
 
                // Get list of all distinct mimetypes in selection
103
 
                QStringList mimeTypes;
104
 
                Q_FOREACH(const KFileItem& item, mContextManagerItem->contextManager()->selectedFileItemList()) {
105
 
                        const QString mimeType = item.mimetype();
106
 
                        if (!mimeTypes.contains(mimeType)) {
107
 
                                mimeTypes << mimeType;
108
 
                        }
109
 
                }
110
 
 
111
 
                // Query trader
112
 
                const QString firstMimeType = mimeTypes.takeFirst();
113
 
                const QString constraintTemplate = "'%1' in ServiceTypes";
114
 
                QStringList constraints;
115
 
                Q_FOREACH(const QString& mimeType, mimeTypes) {
116
 
                        constraints << constraintTemplate.arg(mimeType);
117
 
                }
118
 
 
119
 
                KService::List services = KMimeTypeTrader::self()->query(
120
 
                        firstMimeType, "Application",
121
 
                        constraints.join(" and "));
122
 
 
123
 
                // Update map
124
 
                mServiceForName.clear();
125
 
                Q_FOREACH(const KService::Ptr &service, services) {
126
 
                        mServiceForName[service->name()] = service;
127
 
                }
128
 
        }
129
 
 
130
 
 
131
 
        QMimeData* selectionMimeData() {
132
 
                QMimeData* mimeData = new QMimeData;
133
 
                KFileItemList list = mContextManagerItem->contextManager()->selectedFileItemList();
134
 
                list.urlList().populateMimeData(mimeData);
135
 
                return mimeData;
136
 
        }
137
 
 
138
 
 
139
 
        KUrl pasteTargetUrl() const {
140
 
                // If only one folder is selected, paste inside it, otherwise paste in
141
 
                // current
142
 
                KFileItemList list = mContextManagerItem->contextManager()->selectedFileItemList();
143
 
                if (list.count() == 1 && list.first().isDir()) {
144
 
                        return list.first().url();
145
 
                } else {
146
 
                        return mContextManagerItem->contextManager()->currentDirUrl();
147
 
                }
148
 
        }
 
62
    FileOpsContextManagerItem* q;
 
63
    QListView* mThumbnailView;
 
64
    KXMLGUIClient* mXMLGUIClient;
 
65
    SideBarGroup* mGroup;
 
66
    KAction* mCutAction;
 
67
    KAction* mCopyAction;
 
68
    KAction* mPasteAction;
 
69
    KAction* mCopyToAction;
 
70
    KAction* mMoveToAction;
 
71
    KAction* mLinkToAction;
 
72
    KAction* mRenameAction;
 
73
    KAction* mTrashAction;
 
74
    KAction* mDelAction;
 
75
    KAction* mRestoreAction;
 
76
    KAction* mShowPropertiesAction;
 
77
    KAction* mCreateFolderAction;
 
78
    KAction* mOpenWithAction;
 
79
    QList<QAction*> mRegularFileActionList;
 
80
    QList<QAction*> mTrashFileActionList;
 
81
    QMap<QString, KService::Ptr> mServiceForName;
 
82
    bool mInTrash;
 
83
 
 
84
    KUrl::List urlList() const {
 
85
        KUrl::List urlList;
 
86
 
 
87
        KFileItemList list = q->contextManager()->selectedFileItemList();
 
88
        if (list.count() > 0) {
 
89
            urlList = list.urlList();
 
90
        } else {
 
91
            KUrl url = q->contextManager()->currentUrl();
 
92
            Q_ASSERT(url.isValid());
 
93
            urlList << url;
 
94
        }
 
95
        return urlList;
 
96
    }
 
97
 
 
98
    void updateServiceForName()
 
99
    {
 
100
        // This code is inspired from
 
101
        // kdebase/apps/lib/konq/konq_menuactions.cpp
 
102
 
 
103
        // Get list of all distinct mimetypes in selection
 
104
        QStringList mimeTypes;
 
105
        Q_FOREACH(const KFileItem & item, q->contextManager()->selectedFileItemList()) {
 
106
            const QString mimeType = item.mimetype();
 
107
            if (!mimeTypes.contains(mimeType)) {
 
108
                mimeTypes << mimeType;
 
109
            }
 
110
        }
 
111
 
 
112
        // Query trader
 
113
        const QString firstMimeType = mimeTypes.takeFirst();
 
114
        const QString constraintTemplate = "'%1' in ServiceTypes";
 
115
        QStringList constraints;
 
116
        Q_FOREACH(const QString & mimeType, mimeTypes) {
 
117
            constraints << constraintTemplate.arg(mimeType);
 
118
        }
 
119
 
 
120
        KService::List services = KMimeTypeTrader::self()->query(
 
121
                                      firstMimeType, "Application",
 
122
                                      constraints.join(" and "));
 
123
 
 
124
        // Update map
 
125
        mServiceForName.clear();
 
126
        Q_FOREACH(const KService::Ptr & service, services) {
 
127
            mServiceForName[service->name()] = service;
 
128
        }
 
129
    }
 
130
 
 
131
    QMimeData* selectionMimeData()
 
132
    {
 
133
        QMimeData* mimeData = new QMimeData;
 
134
        KFileItemList list = q->contextManager()->selectedFileItemList();
 
135
        list.urlList().populateMimeData(mimeData);
 
136
        return mimeData;
 
137
    }
 
138
 
 
139
    KUrl pasteTargetUrl() const {
 
140
        // If only one folder is selected, paste inside it, otherwise paste in
 
141
        // current
 
142
        KFileItemList list = q->contextManager()->selectedFileItemList();
 
143
        if (list.count() == 1 && list.first().isDir()) {
 
144
            return list.first().url();
 
145
        } else {
 
146
            return q->contextManager()->currentDirUrl();
 
147
        }
 
148
    }
149
149
};
150
150
 
151
 
 
152
 
static QAction* createSeparator(QObject* parent) {
153
 
        QAction* action = new KAction(parent);
154
 
        action->setSeparator(true);
155
 
        return action;
 
151
static QAction* createSeparator(QObject* parent)
 
152
{
 
153
    QAction* action = new KAction(parent);
 
154
    action->setSeparator(true);
 
155
    return action;
156
156
}
157
157
 
158
 
 
159
158
FileOpsContextManagerItem::FileOpsContextManagerItem(ContextManager* manager, QListView* thumbnailView, KActionCollection* actionCollection, KXMLGUIClient* client)
160
159
: AbstractContextManagerItem(manager)
161
 
, d(new FileOpsContextManagerItemPrivate) {
162
 
        d->mContextManagerItem = this;
163
 
        d->mThumbnailView = thumbnailView;
164
 
        d->mXMLGUIClient = client;
165
 
        d->mGroup = new SideBarGroup(i18n("File Operations"));
166
 
        setWidget(d->mGroup);
167
 
        EventWatcher::install(d->mGroup, QEvent::Show, this, SLOT(updateSideBarContent()));
168
 
 
169
 
        d->mInTrash = false;
170
 
 
171
 
        connect(contextManager(), SIGNAL(selectionChanged()),
172
 
                SLOT(updateActions()) );
173
 
        connect(contextManager(), SIGNAL(currentDirUrlChanged()),
174
 
                SLOT(updateActions()) );
175
 
 
176
 
        KActionCategory* file = new KActionCategory(i18nc("@title actions category","File"), actionCollection);
177
 
        KActionCategory* edit = new KActionCategory(i18nc("@title actions category","Edit"), actionCollection);
178
 
 
179
 
        d->mCutAction = edit->addAction(KStandardAction::Cut, this, SLOT(cut()));
180
 
 
181
 
        // Copied from Dolphin:
 
160
, d(new FileOpsContextManagerItemPrivate)
 
161
{
 
162
    d->q = this;
 
163
    d->mThumbnailView = thumbnailView;
 
164
    d->mXMLGUIClient = client;
 
165
    d->mGroup = new SideBarGroup(i18n("File Operations"));
 
166
    setWidget(d->mGroup);
 
167
    EventWatcher::install(d->mGroup, QEvent::Show, this, SLOT(updateSideBarContent()));
 
168
 
 
169
    d->mInTrash = false;
 
170
 
 
171
    connect(contextManager(), SIGNAL(selectionChanged()),
 
172
            SLOT(updateActions()));
 
173
    connect(contextManager(), SIGNAL(currentDirUrlChanged()),
 
174
            SLOT(updateActions()));
 
175
 
 
176
    KActionCategory* file = new KActionCategory(i18nc("@title actions category", "File"), actionCollection);
 
177
    KActionCategory* edit = new KActionCategory(i18nc("@title actions category", "Edit"), actionCollection);
 
178
 
 
179
    d->mCutAction = edit->addAction(KStandardAction::Cut, this, SLOT(cut()));
 
180
 
 
181
    // Copied from Dolphin:
182
182
    // need to remove shift+del from cut action, else the shortcut for deletejob
183
183
    // doesn't work
184
 
        KShortcut cutShortcut = d->mCutAction->shortcut();
185
 
        cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
186
 
        d->mCutAction->setShortcut(cutShortcut);
187
 
 
188
 
        d->mCopyAction = edit->addAction(KStandardAction::Copy, this, SLOT(copy()));
189
 
        d->mPasteAction = edit->addAction(KStandardAction::Paste, this, SLOT(paste()));
190
 
 
191
 
        d->mCopyToAction = file->addAction("file_copy_to",this,SLOT(copyTo()));
192
 
        d->mCopyToAction->setText(i18nc("Verb", "Copy To..."));
193
 
        d->mCopyToAction->setShortcut(Qt::Key_F7);
194
 
 
195
 
        d->mMoveToAction = file->addAction("file_move_to",this,SLOT(moveTo()));
196
 
        d->mMoveToAction->setText(i18nc("Verb", "Move To..."));
197
 
        d->mMoveToAction->setShortcut(Qt::Key_F8);
198
 
 
199
 
        d->mLinkToAction = file->addAction("file_link_to",this,SLOT(linkTo()));
200
 
        d->mLinkToAction->setText(i18nc("Verb: create link to the file where user wants", "Link To..."));
201
 
        d->mLinkToAction->setShortcut(Qt::Key_F9);
202
 
 
203
 
        d->mRenameAction = file->addAction("file_rename",this,SLOT(rename()));
204
 
        d->mRenameAction->setText(i18nc("Verb", "Rename..."));
205
 
        d->mRenameAction->setShortcut(Qt::Key_F2);
206
 
 
207
 
        d->mTrashAction = file->addAction("file_trash",this,SLOT(trash()));
208
 
        d->mTrashAction->setText(i18nc("Verb", "Trash"));
209
 
        d->mTrashAction->setIcon(KIcon("user-trash"));
210
 
        d->mTrashAction->setShortcut(Qt::Key_Delete);
211
 
 
212
 
        d->mDelAction = file->addAction("file_delete",this,SLOT(del()));
213
 
        d->mDelAction->setText(i18n("Delete"));
214
 
        d->mDelAction->setIcon(KIcon("edit-delete"));
215
 
        d->mDelAction->setShortcut(QKeySequence(Qt::ShiftModifier | Qt::Key_Delete));
216
 
 
217
 
        d->mRestoreAction = file->addAction("file_restore", this, SLOT(restore()));
218
 
        d->mRestoreAction->setText(i18n("Restore"));
219
 
 
220
 
        d->mShowPropertiesAction = file->addAction("file_show_properties",this,SLOT(showProperties()));
221
 
        d->mShowPropertiesAction->setText(i18n("Properties"));
222
 
        d->mShowPropertiesAction->setIcon(KIcon("document-properties"));
223
 
 
224
 
        d->mCreateFolderAction = file->addAction("file_create_folder",this,SLOT(createFolder()));
225
 
        d->mCreateFolderAction->setText(i18n("Create Folder..."));
226
 
        d->mCreateFolderAction->setIcon(KIcon("folder-new"));
227
 
 
228
 
        d->mOpenWithAction = file->addAction("file_open_with");
229
 
        d->mOpenWithAction->setText(i18n("Open With"));
230
 
        QMenu* menu = new QMenu;
231
 
        d->mOpenWithAction->setMenu(menu);
232
 
        connect(menu, SIGNAL(aboutToShow()),
233
 
                SLOT(populateOpenMenu()) );
234
 
        connect(menu, SIGNAL(triggered(QAction*)),
235
 
                SLOT(openWith(QAction*)) );
236
 
 
237
 
        d->mRegularFileActionList
238
 
                << d->mRenameAction
239
 
                << d->mTrashAction
240
 
                << d->mDelAction
241
 
                << createSeparator(this)
242
 
                << d->mCopyToAction
243
 
                << d->mMoveToAction
244
 
                << d->mLinkToAction
245
 
                << createSeparator(this)
246
 
                << d->mOpenWithAction
247
 
                << d->mShowPropertiesAction
248
 
                << createSeparator(this)
249
 
                << d->mCreateFolderAction
250
 
                ;
251
 
 
252
 
        d->mTrashFileActionList
253
 
                << d->mRestoreAction
254
 
                << d->mDelAction
255
 
                << createSeparator(this)
256
 
                << d->mShowPropertiesAction
257
 
                ;
258
 
 
259
 
        connect(QApplication::clipboard(), SIGNAL(dataChanged()),
260
 
                SLOT(updatePasteAction()));
261
 
        updatePasteAction();
262
 
 
263
 
        // Delay action update because it must happen *after* main window has called
264
 
        // createGUI(), otherwise calling d->mXMLGUIClient->plugActionList() will
265
 
        // fail.
266
 
        QMetaObject::invokeMethod(this, "updateActions", Qt::QueuedConnection);
267
 
}
268
 
 
269
 
 
270
 
FileOpsContextManagerItem::~FileOpsContextManagerItem() {
271
 
        delete d->mOpenWithAction->menu();
272
 
        delete d;
273
 
}
274
 
 
275
 
 
276
 
void FileOpsContextManagerItem::updateActions() {
277
 
        const int count = contextManager()->selectedFileItemList().count();
278
 
        const bool selectionNotEmpty = count > 0;
279
 
        const bool urlIsValid = contextManager()->currentUrl().isValid();
280
 
        const bool dirUrlIsValid = contextManager()->currentDirUrl().isValid();
281
 
 
282
 
        d->mInTrash = contextManager()->currentDirUrl().protocol() == "trash";
283
 
 
284
 
        d->mCutAction->setEnabled(selectionNotEmpty);
285
 
        d->mCopyAction->setEnabled(selectionNotEmpty);
286
 
        d->mCopyToAction->setEnabled(selectionNotEmpty);
287
 
        d->mMoveToAction->setEnabled(selectionNotEmpty);
288
 
        d->mLinkToAction->setEnabled(selectionNotEmpty);
289
 
        d->mTrashAction->setEnabled(selectionNotEmpty);
290
 
        d->mRestoreAction->setEnabled(selectionNotEmpty);
291
 
        d->mDelAction->setEnabled(selectionNotEmpty);
292
 
        d->mOpenWithAction->setEnabled(selectionNotEmpty);
293
 
        d->mRenameAction->setEnabled(count == 1);
294
 
 
295
 
        d->mCreateFolderAction->setEnabled(dirUrlIsValid);
296
 
        d->mShowPropertiesAction->setEnabled(dirUrlIsValid || urlIsValid);
297
 
 
298
 
        d->mXMLGUIClient->unplugActionList("file_action_list");
299
 
        QList<QAction*>& list = d->mInTrash ? d->mTrashFileActionList : d->mRegularFileActionList;
300
 
        d->mXMLGUIClient->plugActionList("file_action_list", list);
301
 
 
302
 
        updateSideBarContent();
303
 
}
304
 
 
305
 
 
306
 
void FileOpsContextManagerItem::updatePasteAction() {
307
 
        QPair<bool, QString> info = KonqOperations::pasteInfo(d->pasteTargetUrl());
308
 
        d->mPasteAction->setEnabled(info.first);
309
 
        d->mPasteAction->setText(info.second);
310
 
}
311
 
 
312
 
 
313
 
void FileOpsContextManagerItem::updateSideBarContent() {
314
 
        if (!d->mGroup->isVisible()) {
315
 
                return;
316
 
        }
317
 
 
318
 
        d->mGroup->clear();
319
 
        QList<QAction*>& list = d->mInTrash ? d->mTrashFileActionList : d->mRegularFileActionList;
320
 
        Q_FOREACH(QAction* action, list) {
321
 
                if (action->isEnabled() && !action->isSeparator()) {
322
 
                        d->mGroup->addAction(action);
323
 
                }
324
 
        }
325
 
}
326
 
 
327
 
 
328
 
void FileOpsContextManagerItem::showProperties() {
329
 
        KFileItemList list = contextManager()->selectedFileItemList();
330
 
        if (list.count() > 0) {
331
 
                KPropertiesDialog::showDialog(list, d->mGroup);
332
 
        } else {
333
 
                KUrl url = contextManager()->currentDirUrl();
334
 
                KPropertiesDialog::showDialog(url, d->mGroup);
335
 
        }
336
 
}
337
 
 
338
 
 
339
 
void FileOpsContextManagerItem::cut() {
340
 
        QMimeData* mimeData = d->selectionMimeData();
341
 
        KonqMimeData::addIsCutSelection(mimeData, true);
342
 
        QApplication::clipboard()->setMimeData(mimeData);
343
 
}
344
 
 
345
 
 
346
 
void FileOpsContextManagerItem::copy() {
347
 
        QMimeData* mimeData = d->selectionMimeData();
348
 
        KonqMimeData::addIsCutSelection(mimeData, false);
349
 
        QApplication::clipboard()->setMimeData(mimeData);
350
 
}
351
 
 
352
 
 
353
 
void FileOpsContextManagerItem::paste() {
354
 
        const bool move = KonqMimeData::decodeIsCutSelection(QApplication::clipboard()->mimeData());
355
 
        KIO::pasteClipboard(d->pasteTargetUrl(), d->mGroup, move);
356
 
}
357
 
 
358
 
 
359
 
void FileOpsContextManagerItem::trash() {
360
 
        FileOperations::trash(d->urlList(), d->mGroup);
361
 
}
362
 
 
363
 
 
364
 
void FileOpsContextManagerItem::del() {
365
 
        FileOperations::del(d->urlList(), d->mGroup);
366
 
}
367
 
 
368
 
 
369
 
void FileOpsContextManagerItem::restore() {
370
 
        KonqOperations::restoreTrashedItems(d->urlList(), d->mGroup);
371
 
}
372
 
 
373
 
 
374
 
void FileOpsContextManagerItem::copyTo() {
375
 
        FileOperations::copyTo(d->urlList(), d->mGroup);
376
 
}
377
 
 
378
 
 
379
 
void FileOpsContextManagerItem::moveTo() {
380
 
        FileOperations::moveTo(d->urlList(), d->mGroup);
381
 
}
382
 
 
383
 
 
384
 
void FileOpsContextManagerItem::linkTo() {
385
 
        FileOperations::linkTo(d->urlList(), d->mGroup);
386
 
}
387
 
 
388
 
 
389
 
void FileOpsContextManagerItem::rename() {
390
 
        if (d->mThumbnailView->isVisible()) {
391
 
                QModelIndex index = d->mThumbnailView->currentIndex();
392
 
                d->mThumbnailView->edit(index);
393
 
        } else {
394
 
                FileOperations::rename(d->urlList().first(), d->mGroup);
395
 
        }
396
 
}
397
 
 
398
 
 
399
 
void FileOpsContextManagerItem::createFolder() {
400
 
        KUrl url = contextManager()->currentDirUrl();
401
 
        KonqOperations::newDir(d->mGroup, url);
402
 
}
403
 
 
404
 
 
405
 
void FileOpsContextManagerItem::populateOpenMenu() {
406
 
        QMenu* openMenu = d->mOpenWithAction->menu();
407
 
        qDeleteAll(openMenu->actions());
408
 
 
409
 
        d->updateServiceForName();
410
 
 
411
 
        Q_FOREACH(const KService::Ptr &service, d->mServiceForName) {
412
 
                QString text = service->name().replace( '&', "&&" );
413
 
                QAction* action = openMenu->addAction(text);
414
 
                action->setIcon(KIcon(service->icon()));
415
 
                action->setData(service->name());
416
 
        }
417
 
 
418
 
        openMenu->addSeparator();
419
 
        openMenu->addAction(i18n("Other Application..."));
420
 
}
421
 
 
422
 
 
423
 
void FileOpsContextManagerItem::openWith(QAction* action) {
424
 
        Q_ASSERT(action);
425
 
        KService::Ptr service;
426
 
        KUrl::List list = d->urlList();
427
 
 
428
 
        QString name = action->data().toString();
429
 
        if (name.isEmpty()) {
430
 
                // Other Application...
431
 
                KOpenWithDialog dlg(list, d->mGroup);
432
 
                if (!dlg.exec()) {
433
 
                        return;
434
 
                }
435
 
                service = dlg.service();
436
 
 
437
 
                if (!service) {
438
 
                        // User entered a custom command
439
 
                        Q_ASSERT(!dlg.text().isEmpty());
440
 
                        KRun::run(dlg.text(), list, d->mGroup);
441
 
                        return;
442
 
                }
443
 
        } else {
444
 
                service = d->mServiceForName[name];
445
 
        }
446
 
 
447
 
        Q_ASSERT(service);
448
 
        KRun::run(*service, list, d->mGroup);
449
 
}
450
 
 
 
184
    KShortcut cutShortcut = d->mCutAction->shortcut();
 
185
    cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
 
186
    d->mCutAction->setShortcut(cutShortcut);
 
187
 
 
188
    d->mCopyAction = edit->addAction(KStandardAction::Copy, this, SLOT(copy()));
 
189
    d->mPasteAction = edit->addAction(KStandardAction::Paste, this, SLOT(paste()));
 
190
 
 
191
    d->mCopyToAction = file->addAction("file_copy_to", this, SLOT(copyTo()));
 
192
    d->mCopyToAction->setText(i18nc("Verb", "Copy To..."));
 
193
    d->mCopyToAction->setShortcut(Qt::Key_F7);
 
194
 
 
195
    d->mMoveToAction = file->addAction("file_move_to", this, SLOT(moveTo()));
 
196
    d->mMoveToAction->setText(i18nc("Verb", "Move To..."));
 
197
    d->mMoveToAction->setShortcut(Qt::Key_F8);
 
198
 
 
199
    d->mLinkToAction = file->addAction("file_link_to", this, SLOT(linkTo()));
 
200
    d->mLinkToAction->setText(i18nc("Verb: create link to the file where user wants", "Link To..."));
 
201
    d->mLinkToAction->setShortcut(Qt::Key_F9);
 
202
 
 
203
    d->mRenameAction = file->addAction("file_rename", this, SLOT(rename()));
 
204
    d->mRenameAction->setText(i18nc("Verb", "Rename..."));
 
205
    d->mRenameAction->setShortcut(Qt::Key_F2);
 
206
 
 
207
    d->mTrashAction = file->addAction("file_trash", this, SLOT(trash()));
 
208
    d->mTrashAction->setText(i18nc("Verb", "Trash"));
 
209
    d->mTrashAction->setIcon(KIcon("user-trash"));
 
210
    d->mTrashAction->setShortcut(Qt::Key_Delete);
 
211
 
 
212
    d->mDelAction = file->addAction("file_delete", this, SLOT(del()));
 
213
    d->mDelAction->setText(i18n("Delete"));
 
214
    d->mDelAction->setIcon(KIcon("edit-delete"));
 
215
    d->mDelAction->setShortcut(QKeySequence(Qt::ShiftModifier | Qt::Key_Delete));
 
216
 
 
217
    d->mRestoreAction = file->addAction("file_restore", this, SLOT(restore()));
 
218
    d->mRestoreAction->setText(i18n("Restore"));
 
219
 
 
220
    d->mShowPropertiesAction = file->addAction("file_show_properties", this, SLOT(showProperties()));
 
221
    d->mShowPropertiesAction->setText(i18n("Properties"));
 
222
    d->mShowPropertiesAction->setIcon(KIcon("document-properties"));
 
223
 
 
224
    d->mCreateFolderAction = file->addAction("file_create_folder", this, SLOT(createFolder()));
 
225
    d->mCreateFolderAction->setText(i18n("Create Folder..."));
 
226
    d->mCreateFolderAction->setIcon(KIcon("folder-new"));
 
227
 
 
228
    d->mOpenWithAction = file->addAction("file_open_with");
 
229
    d->mOpenWithAction->setText(i18n("Open With"));
 
230
    QMenu* menu = new QMenu;
 
231
    d->mOpenWithAction->setMenu(menu);
 
232
    connect(menu, SIGNAL(aboutToShow()),
 
233
            SLOT(populateOpenMenu()));
 
234
    connect(menu, SIGNAL(triggered(QAction*)),
 
235
            SLOT(openWith(QAction*)));
 
236
 
 
237
    d->mRegularFileActionList
 
238
            << d->mRenameAction
 
239
            << d->mTrashAction
 
240
            << d->mDelAction
 
241
            << createSeparator(this)
 
242
            << d->mCopyToAction
 
243
            << d->mMoveToAction
 
244
            << d->mLinkToAction
 
245
            << createSeparator(this)
 
246
            << d->mOpenWithAction
 
247
            << d->mShowPropertiesAction
 
248
            << createSeparator(this)
 
249
            << d->mCreateFolderAction
 
250
            ;
 
251
 
 
252
    d->mTrashFileActionList
 
253
            << d->mRestoreAction
 
254
            << d->mDelAction
 
255
            << createSeparator(this)
 
256
            << d->mShowPropertiesAction
 
257
            ;
 
258
 
 
259
    connect(QApplication::clipboard(), SIGNAL(dataChanged()),
 
260
            SLOT(updatePasteAction()));
 
261
    updatePasteAction();
 
262
 
 
263
    // Delay action update because it must happen *after* main window has called
 
264
    // createGUI(), otherwise calling d->mXMLGUIClient->plugActionList() will
 
265
    // fail.
 
266
    QMetaObject::invokeMethod(this, "updateActions", Qt::QueuedConnection);
 
267
}
 
268
 
 
269
FileOpsContextManagerItem::~FileOpsContextManagerItem()
 
270
{
 
271
    delete d->mOpenWithAction->menu();
 
272
    delete d;
 
273
}
 
274
 
 
275
void FileOpsContextManagerItem::updateActions()
 
276
{
 
277
    const int count = contextManager()->selectedFileItemList().count();
 
278
    const bool selectionNotEmpty = count > 0;
 
279
    const bool urlIsValid = contextManager()->currentUrl().isValid();
 
280
    const bool dirUrlIsValid = contextManager()->currentDirUrl().isValid();
 
281
 
 
282
    d->mInTrash = contextManager()->currentDirUrl().protocol() == "trash";
 
283
 
 
284
    d->mCutAction->setEnabled(selectionNotEmpty);
 
285
    d->mCopyAction->setEnabled(selectionNotEmpty);
 
286
    d->mCopyToAction->setEnabled(selectionNotEmpty);
 
287
    d->mMoveToAction->setEnabled(selectionNotEmpty);
 
288
    d->mLinkToAction->setEnabled(selectionNotEmpty);
 
289
    d->mTrashAction->setEnabled(selectionNotEmpty);
 
290
    d->mRestoreAction->setEnabled(selectionNotEmpty);
 
291
    d->mDelAction->setEnabled(selectionNotEmpty);
 
292
    d->mOpenWithAction->setEnabled(selectionNotEmpty);
 
293
    d->mRenameAction->setEnabled(count == 1);
 
294
 
 
295
    d->mCreateFolderAction->setEnabled(dirUrlIsValid);
 
296
    d->mShowPropertiesAction->setEnabled(dirUrlIsValid || urlIsValid);
 
297
 
 
298
    d->mXMLGUIClient->unplugActionList("file_action_list");
 
299
    QList<QAction*>& list = d->mInTrash ? d->mTrashFileActionList : d->mRegularFileActionList;
 
300
    d->mXMLGUIClient->plugActionList("file_action_list", list);
 
301
 
 
302
    updateSideBarContent();
 
303
}
 
304
 
 
305
void FileOpsContextManagerItem::updatePasteAction()
 
306
{
 
307
    QPair<bool, QString> info = KonqOperations::pasteInfo(d->pasteTargetUrl());
 
308
    d->mPasteAction->setEnabled(info.first);
 
309
    d->mPasteAction->setText(info.second);
 
310
}
 
311
 
 
312
void FileOpsContextManagerItem::updateSideBarContent()
 
313
{
 
314
    if (!d->mGroup->isVisible()) {
 
315
        return;
 
316
    }
 
317
 
 
318
    d->mGroup->clear();
 
319
    QList<QAction*>& list = d->mInTrash ? d->mTrashFileActionList : d->mRegularFileActionList;
 
320
    Q_FOREACH(QAction * action, list) {
 
321
        if (action->isEnabled() && !action->isSeparator()) {
 
322
            d->mGroup->addAction(action);
 
323
        }
 
324
    }
 
325
}
 
326
 
 
327
void FileOpsContextManagerItem::showProperties()
 
328
{
 
329
    KFileItemList list = contextManager()->selectedFileItemList();
 
330
    if (list.count() > 0) {
 
331
        KPropertiesDialog::showDialog(list, d->mGroup);
 
332
    } else {
 
333
        KUrl url = contextManager()->currentDirUrl();
 
334
        KPropertiesDialog::showDialog(url, d->mGroup);
 
335
    }
 
336
}
 
337
 
 
338
void FileOpsContextManagerItem::cut()
 
339
{
 
340
    QMimeData* mimeData = d->selectionMimeData();
 
341
    KonqMimeData::addIsCutSelection(mimeData, true);
 
342
    QApplication::clipboard()->setMimeData(mimeData);
 
343
}
 
344
 
 
345
void FileOpsContextManagerItem::copy()
 
346
{
 
347
    QMimeData* mimeData = d->selectionMimeData();
 
348
    KonqMimeData::addIsCutSelection(mimeData, false);
 
349
    QApplication::clipboard()->setMimeData(mimeData);
 
350
}
 
351
 
 
352
void FileOpsContextManagerItem::paste()
 
353
{
 
354
    const bool move = KonqMimeData::decodeIsCutSelection(QApplication::clipboard()->mimeData());
 
355
    KIO::pasteClipboard(d->pasteTargetUrl(), d->mGroup, move);
 
356
}
 
357
 
 
358
void FileOpsContextManagerItem::trash()
 
359
{
 
360
    FileOperations::trash(d->urlList(), d->mGroup);
 
361
}
 
362
 
 
363
void FileOpsContextManagerItem::del()
 
364
{
 
365
    FileOperations::del(d->urlList(), d->mGroup);
 
366
}
 
367
 
 
368
void FileOpsContextManagerItem::restore()
 
369
{
 
370
    KonqOperations::restoreTrashedItems(d->urlList(), d->mGroup);
 
371
}
 
372
 
 
373
void FileOpsContextManagerItem::copyTo()
 
374
{
 
375
    FileOperations::copyTo(d->urlList(), d->mGroup);
 
376
}
 
377
 
 
378
void FileOpsContextManagerItem::moveTo()
 
379
{
 
380
    FileOperations::moveTo(d->urlList(), d->mGroup);
 
381
}
 
382
 
 
383
void FileOpsContextManagerItem::linkTo()
 
384
{
 
385
    FileOperations::linkTo(d->urlList(), d->mGroup);
 
386
}
 
387
 
 
388
void FileOpsContextManagerItem::rename()
 
389
{
 
390
    if (d->mThumbnailView->isVisible()) {
 
391
        QModelIndex index = d->mThumbnailView->currentIndex();
 
392
        d->mThumbnailView->edit(index);
 
393
    } else {
 
394
        FileOperations::rename(d->urlList().first(), d->mGroup);
 
395
    }
 
396
}
 
397
 
 
398
void FileOpsContextManagerItem::createFolder()
 
399
{
 
400
    KUrl url = contextManager()->currentDirUrl();
 
401
    KonqOperations::newDir(d->mGroup, url);
 
402
}
 
403
 
 
404
void FileOpsContextManagerItem::populateOpenMenu()
 
405
{
 
406
    QMenu* openMenu = d->mOpenWithAction->menu();
 
407
    qDeleteAll(openMenu->actions());
 
408
 
 
409
    d->updateServiceForName();
 
410
 
 
411
    Q_FOREACH(const KService::Ptr & service, d->mServiceForName) {
 
412
        QString text = service->name().replace('&', "&&");
 
413
        QAction* action = openMenu->addAction(text);
 
414
        action->setIcon(KIcon(service->icon()));
 
415
        action->setData(service->name());
 
416
    }
 
417
 
 
418
    openMenu->addSeparator();
 
419
    openMenu->addAction(i18n("Other Application..."));
 
420
}
 
421
 
 
422
void FileOpsContextManagerItem::openWith(QAction* action)
 
423
{
 
424
    Q_ASSERT(action);
 
425
    KService::Ptr service;
 
426
    KUrl::List list = d->urlList();
 
427
 
 
428
    QString name = action->data().toString();
 
429
    if (name.isEmpty()) {
 
430
        // Other Application...
 
431
        KOpenWithDialog dlg(list, d->mGroup);
 
432
        if (!dlg.exec()) {
 
433
            return;
 
434
        }
 
435
        service = dlg.service();
 
436
 
 
437
        if (!service) {
 
438
            // User entered a custom command
 
439
            Q_ASSERT(!dlg.text().isEmpty());
 
440
            KRun::run(dlg.text(), list, d->mGroup);
 
441
            return;
 
442
        }
 
443
    } else {
 
444
        service = d->mServiceForName[name];
 
445
    }
 
446
 
 
447
    Q_ASSERT(service);
 
448
    KRun::run(*service, list, d->mGroup);
 
449
}
451
450
 
452
451
} // namespace