File indexing completed on 2024-03-24 17:23:08

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <Launcher.hxx>
0022 
0023 #include <QVBoxLayout>
0024 #include <QFileDialog>
0025 #include <QAction>
0026 #include <QCursor>
0027 #include <QMenu>
0028 #include <QIcon>
0029 #include <QDebug>
0030 
0031 #include <KRun>
0032 #include <KConfig>
0033 #include <KConfigGroup>
0034 #include <KLocalizedString>
0035 
0036 //--------------------------------------------------------------------------------
0037 
0038 Launcher::Launcher(QWidget *parent, const QString &theId)
0039   : QWidget(parent), id(theId)
0040 {
0041   new QVBoxLayout(this);
0042   layout()->setContentsMargins(QMargins());
0043 }
0044 
0045 //--------------------------------------------------------------------------------
0046 
0047 void Launcher::loadConfig(const QString &defaultDir)
0048 {
0049   KConfig config;
0050   KConfigGroup group = config.group(id);
0051   setDir(group.readEntry(QString("dirPath"), defaultDir));
0052 }
0053 
0054 //--------------------------------------------------------------------------------
0055 
0056 void Launcher::setDir(const QString &theDirPath)
0057 {
0058   if ( !dirWatcher.directories().isEmpty() )
0059     dirWatcher.removePaths(dirWatcher.directories());
0060 
0061   dirPath = theDirPath;
0062   fill();
0063 
0064   if ( !dirPath.isEmpty() )
0065   {
0066     dirWatcher.addPath(dirPath);
0067     connect(&dirWatcher, &QFileSystemWatcher::directoryChanged, this, &Launcher::fill);
0068   }
0069 }
0070 
0071 //--------------------------------------------------------------------------------
0072 
0073 void Launcher::contextMenuEvent(QContextMenuEvent *event)
0074 {
0075   Q_UNUSED(event)
0076 
0077   QMenu menu;
0078 
0079   QAction *action = menu.addAction(QIcon::fromTheme("configure"), i18n("Configure..."));
0080   connect(action, &QAction::triggered,
0081           [this]()
0082           {
0083             QString path = QFileDialog::getExistingDirectory(this, QString(), dirPath);
0084             if ( !path.isEmpty() )
0085             {
0086               KConfig config;
0087               KConfigGroup group = config.group(id);
0088               group.writeEntry(QString("dirPath"), path);
0089               setDir(path);
0090             }
0091           }
0092          );
0093 
0094   if ( !dirPath.isEmpty() )
0095   {
0096     action = menu.addAction(QIcon::fromTheme("folder"), i18n("Open Folder"));
0097     connect(action, &QAction::triggered, [this]() { new KRun(QUrl::fromLocalFile(dirPath), nullptr); });
0098   }
0099 
0100   menu.exec(QCursor::pos());
0101 }
0102 
0103 //--------------------------------------------------------------------------------