File indexing completed on 2024-04-21 04:57:21

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 
0012 #include "contextmenu.h"
0013 
0014 #include "core/kget.h"
0015 #include "core/plugin/transferfactory.h"
0016 #include "core/transfergrouphandler.h"
0017 #include "core/transferhandler.h"
0018 #include <QMenu>
0019 #include <QWidgetAction>
0020 
0021 #include <KFileItem>
0022 #include <KFileItemActions>
0023 #include <KFileItemListProperties>
0024 
0025 QMenu *ContextMenu::createTransferContextMenu(QList<TransferHandler *> transfers, QWidget *parent)
0026 {
0027     if (transfers.empty())
0028         return nullptr;
0029 
0030     // First check whether all the transfers in the list belong to the same
0031     // transferfactory
0032     // bool sameFactory = true;
0033 
0034     /*QList<TransferHandler *>::iterator it = transfers.begin();
0035     QList<TransferHandler *>::iterator itEnd = transfers.end();
0036 
0037     for(; (it!=itEnd) && (sameFactory) ; ++it)
0038     {
0039         //sameFactory = ( (*it)->m_transfer->factory() == //Port factory() to transferhandler
0040          //               transfers.first()->m_transfer->factory() );
0041     }*/
0042 
0043     auto *popup = new QMenu(parent);
0044     popup->addSection(transfers.first()->dest().fileName());
0045     // Get the transfer factory actions
0046     QList<QAction *> actionList = transfers.first()->factoryActions();
0047     //     popup->addSection( i18np("%1 Download selected", "%1 Downloads selected", transfers.count()) );
0048 
0049     // Plug all the actions in the popup menu
0050     popup->addActions(transfers.first()->contextActions());
0051     popup->addSeparator();
0052     popup->addAction(KGet::actionCollection()->action("transfer_settings"));
0053     popup->addSeparator();
0054 
0055     foreach (QAction *it, actionList) {
0056         // Plug each action in the popup menu
0057         popup->addAction(it);
0058     }
0059 
0060     if (!actionList.isEmpty())
0061         popup->addSeparator();
0062 
0063     popup->addAction(KGet::actionCollection()->action("transfer_open_dest"));
0064     // popup->addAction( KGet::actionCollection()->action("transfer_open_file") );
0065     popup->addAction(KGet::actionCollection()->action("transfer_show_details"));
0066     popup->addAction(KGet::actionCollection()->action("transfer_copy_source_url"));
0067 
0068     return popup;
0069 }
0070 
0071 QMenu *ContextMenu::createTransferContextMenu(TransferHandler *handler, QWidget *parent)
0072 {
0073     QMenu *popup = ContextMenu::createTransferContextMenu(QList<TransferHandler *>() << handler, parent);
0074 
0075     // only shows the open with actions if the transfer is finished
0076     if (handler->status() == Job::Finished || handler->status() == Job::FinishedKeepAlive) {
0077         KFileItemList items;
0078         items << KFileItem(handler->dest());
0079 
0080         KFileItemActions menuActions;
0081 
0082         menuActions.setItemListProperties(KFileItemListProperties(items));
0083         menuActions.setParentWidget(parent);
0084 
0085         menuActions.addActionsTo(popup, KFileItemActions::MenuActionSource::Services);
0086         menuActions.insertOpenWithActionsTo(nullptr, popup, QStringList("org.kde.kget"));
0087 
0088         // TODO : seems like the popup menu has to be showed while the KonqMenuActions instance exists ?
0089         popup->exec(QCursor::pos());
0090         popup->deleteLater();
0091 
0092         return nullptr;
0093     }
0094 
0095     return popup;
0096 }
0097 
0098 QMenu *ContextMenu::createTransferGroupContextMenu(TransferGroupHandler *handler, QWidget *parent)
0099 {
0100     if (!handler)
0101         return nullptr;
0102 
0103     auto *popup = new QMenu(parent);
0104     popup->addSection(handler->name());
0105 
0106     popup->addActions(handler->actions());
0107     popup->addSeparator();
0108     popup->addAction(KGet::actionCollection()->action("transfer_group_settings"));
0109     popup->addSeparator();
0110 
0111     QList<TransferGroupHandler *> transferGroups = KGet::selectedTransferGroups();
0112     if (transferGroups.count() != KGet::allTransferGroups().count()) {
0113         const int numGroups = transferGroups.count();
0114         QAction *action = KGet::actionCollection()->action("delete_groups");
0115         action->setText(i18np("Delete Group", "Delete Groups", numGroups));
0116         popup->addAction(action);
0117 
0118         action = KGet::actionCollection()->action("rename_groups");
0119         action->setText(i18np("Rename Group...", "Rename Groups...", numGroups));
0120         popup->addAction(action);
0121     }
0122     popup->addAction(KGet::actionCollection()->action("seticon_groups"));
0123     return popup;
0124 }