File indexing completed on 2024-04-21 16:30:36

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 - 2023 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 <QuickLaunch.hxx>
0022 
0023 #include <QFrame>
0024 #include <QIcon>
0025 #include <QToolButton>
0026 #include <QMimeDatabase>
0027 #include <QFileInfo>
0028 #include <QDir>
0029 #include <QDebug>
0030 
0031 #include <KDesktopFile>
0032 #include <KRun>
0033 #include <KFileItem>
0034 #include <KConfigGroup>
0035 #include <KLocalizedString>
0036 #include <KIconLoader>
0037 
0038 //--------------------------------------------------------------------------------
0039 
0040 QuickLaunch::QuickLaunch(DesktopPanel *parent)
0041   : Launcher(parent, "QuickLaunch")
0042 {
0043   QFrame *frame = new QFrame;
0044   frame->setFrameShape(QFrame::StyledPanel);
0045 
0046   grid = new QGridLayout(frame);
0047   grid->setContentsMargins(QMargins());
0048   grid->setSpacing(2);
0049 
0050   layout()->addWidget(frame);
0051 
0052   // create default folder
0053   QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/QuickLaunch");
0054   if ( !dir.exists() )
0055     dir.mkpath(dir.path());
0056 
0057   loadConfig(dir.path());
0058 
0059   connect(parent, &DesktopPanel::rowsChanged, this, &QuickLaunch::fill);
0060   connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this, &QuickLaunch::fill);
0061 }
0062 
0063 //--------------------------------------------------------------------------------
0064 
0065 void QuickLaunch::fill()
0066 {
0067   const int MAX_ROWS = qobject_cast<DesktopPanel *>(parentWidget())->getRows();
0068 
0069   QLayoutItem *child;
0070   while ( (child = grid->takeAt(0)) )
0071   {
0072     delete child->widget();
0073     delete child;
0074   }
0075 
0076   if ( !dirPath.isEmpty() )
0077   {
0078     QDir dir(dirPath);
0079     QFileInfoList entries = dir.entryInfoList(QDir::Files);
0080     int row = 0, col = 0;
0081 
0082     for (const QFileInfo &info : entries)
0083     {
0084       QUrl url(QUrl::fromLocalFile(info.absoluteFilePath()));
0085       QIcon icon;
0086       QString name = info.fileName();
0087 
0088       KFileItem item(url);
0089 
0090       if ( item.isDesktopFile() )
0091       {
0092         KDesktopFile desktopFile(info.absoluteFilePath());
0093         if ( desktopFile.noDisplay() )
0094           continue;
0095 
0096         name = desktopFile.readName();
0097         if ( name.isEmpty() )
0098           name = desktopFile.readGenericName();
0099 
0100         QString iconName = desktopFile.readIcon();
0101         icon = QIcon::fromTheme(iconName.isEmpty() ? name : iconName);
0102       }
0103       else
0104       {
0105         QMimeDatabase db;
0106         icon = QIcon::fromTheme(db.mimeTypeForFile(info.absoluteFilePath()).iconName());
0107       }
0108 
0109       QToolButton *button = new QToolButton(this);
0110       button->setAutoRaise(true);
0111       button->setIcon(icon);
0112       button->setToolTip(name);
0113 
0114       if ( MAX_ROWS > 1 )
0115         button->setIconSize(QSize(22, 22));
0116       else
0117       {
0118         int size = KIconLoader::global()->currentSize(KIconLoader::Panel);
0119         button->setIconSize(QSize(size, size));
0120       }
0121 
0122       button->setFixedHeight(button->sizeHint().height() - 2);
0123 
0124       connect(button, &QToolButton::clicked, [url]() { new KRun(url, nullptr); });
0125 
0126       grid->addWidget(button, row, col, Qt::AlignCenter);
0127 
0128       row = (row + 1) % MAX_ROWS;
0129       if ( row == 0 ) col++;
0130 
0131       // limit width in case one selects a directory with too many items
0132       const int MAX_COLS = 15;
0133       if ( col > MAX_COLS )
0134         break;
0135     }
0136   }
0137 
0138   if ( grid->count() == 0 )
0139   {
0140     // add default entry
0141     QToolButton *button = new QToolButton(this);
0142     button->setAutoRaise(true);
0143     button->setIcon(QIcon::fromTheme("user-home"));
0144     button->setIconSize(QSize(22, 22));
0145     button->setFixedHeight(button->sizeHint().height() - 2);
0146     button->setToolTip(QStandardPaths::displayName(QStandardPaths::HomeLocation));
0147     QUrl url = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
0148     connect(button, &QToolButton::clicked, [url]() { new KRun(url, nullptr); });
0149 
0150     grid->addWidget(button, 0, 0, Qt::AlignCenter);
0151 
0152     button = new QToolButton(this);
0153     button->setAutoRaise(true);
0154     button->setIcon(QIcon::fromTheme("internet-web-browser"));
0155     button->setIconSize(QSize(22, 22));
0156     button->setFixedHeight(button->sizeHint().height() - 2);
0157     button->setToolTip(i18n("Web Browser"));
0158     connect(button, &QToolButton::clicked, []() { new KRun(QUrl("http://www.kde.org"), nullptr); });
0159 
0160     if ( MAX_ROWS == 1 )
0161       grid->addWidget(button, 0, 1, Qt::AlignCenter);
0162     else
0163       grid->addWidget(button, 1, 0, Qt::AlignCenter);
0164   }
0165 }
0166 
0167 //--------------------------------------------------------------------------------