File indexing completed on 2024-05-12 17:16:14
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 <KIconLoader> 0023 #include <KLocalizedString> 0024 #include <KRun> 0025 #include <QAction> 0026 #include <QApplication> 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(SmallIcon(ptr->icon()), actionName); 0051 QVariant _data = m_mapPopup.size(); 0052 act->setData(_data); 0053 0054 m_mapPopup.push_back(ptr); 0055 } 0056 connect(this, &QMenu::triggered, this, &OpenContextmenu::slotRunService); 0057 if (!m_List.isEmpty()) { 0058 addSeparator(); 0059 } 0060 QAction *act = new QAction(i18n("Other..."), this); 0061 QVariant _data = int(0); 0062 act->setData(_data); 0063 addAction(act); 0064 } 0065 0066 void OpenContextmenu::slotRunService(QAction *act) 0067 { 0068 const int idx = act->data().toInt(); 0069 if (idx >= 0 && idx < m_mapPopup.size()) { 0070 KRun::runService(*m_mapPopup.at(idx), QList<QUrl>() << m_Path, QApplication::activeWindow()); 0071 } else { 0072 slotOpenWith(); 0073 } 0074 0075 } 0076 0077 void OpenContextmenu::slotOpenWith() 0078 { 0079 QList<QUrl> lst; 0080 lst.append(m_Path); 0081 KRun::displayOpenWithDialog(lst, QApplication::activeWindow()); 0082 }