File indexing completed on 2024-04-28 05:42:02

0001 /***************************************************************************
0002  *   Copyright (C) 2006-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "opencontextmenu.h"
0021 
0022 #include <KIO/ApplicationLauncherJob>
0023 #include <KIO/JobUiDelegateFactory>
0024 #include <KLocalizedString>
0025 
0026 #include <QAction>
0027 
0028 OpenContextmenu::OpenContextmenu(const QUrl &aPath, const KService::List &aList, QWidget *parent)
0029     : QMenu(parent)
0030     , m_Path(aPath)
0031     , m_List(aList)
0032 {
0033     setup();
0034 }
0035 
0036 OpenContextmenu::~OpenContextmenu()
0037 {
0038 }
0039 
0040 void OpenContextmenu::setup()
0041 {
0042     m_mapPopup.clear();
0043     QStringList _found;
0044     for (const KService::Ptr &ptr : qAsConst(m_List)) {
0045         if (_found.contains(ptr->name())) {
0046             continue;
0047         }
0048         _found.append(ptr->name());
0049         QString actionName(ptr->name().replace(QLatin1Char('&'), QLatin1String("&&")));
0050         QAction *act = addAction(QIcon::fromTheme(ptr->icon()), actionName);
0051         act->setData(m_mapPopup.size());
0052 
0053         m_mapPopup.push_back(ptr);
0054     }
0055     connect(this, &QMenu::triggered, this, &OpenContextmenu::slotRunService);
0056     if (!m_List.isEmpty()) {
0057         addSeparator();
0058     }
0059     QAction *act = new QAction(i18n("Other..."), this);
0060     act->setData(-1);
0061     addAction(act);
0062 }
0063 
0064 void OpenContextmenu::slotRunService(QAction *act)
0065 {
0066     const int idx = act->data().toInt();
0067     if (idx >= 0 && idx < m_mapPopup.size()) {
0068         auto *job = new KIO::ApplicationLauncherJob(m_mapPopup.at(idx));
0069         job->setUrls({m_Path});
0070         job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled, parentWidget()));
0071         job->start();
0072     } else {
0073         slotOpenWith();
0074     }
0075 }
0076 
0077 void OpenContextmenu::slotOpenWith()
0078 {
0079     auto *job = new KIO::ApplicationLauncherJob;
0080     job->setUrls({m_Path});
0081     job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled, parentWidget()));
0082     job->start();
0083 }
0084 
0085 #include "moc_opencontextmenu.cpp"