Skip to content

Commit

Permalink
FancyMenu: experimental support for Favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgit committed Jan 6, 2024
1 parent d3ee863 commit fee6b1b
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 24 deletions.
55 changes: 55 additions & 0 deletions plugin-fancymenu/lxqtfancymenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include <QMimeData>
#include <XdgAction>

#include <QDir>

#define DEFAULT_SHORTCUT "Alt+F1"

LXQtFancyMenu::LXQtFancyMenu(const ILXQtPanelPluginStartupInfo &startupInfo):
Expand Down Expand Up @@ -232,6 +234,59 @@ void LXQtFancyMenu::buildMenu()
setMenuFontSize();
}

void LXQtFancyMenu::loadFavorites()
{
bool listChanged = false;

const QList<QMap<QString, QVariant> > list = settings()->readArray(QStringLiteral("favorites"));
QStringList fileList;
for(const QMap<QString, QVariant>& item : list)
{
QString file = item.value(QStringLiteral("desktopFile")).toString();
if(file.isEmpty())
{
listChanged = true;
continue;
}

QString canonicalPath = QDir(file).canonicalPath();
if(canonicalPath != file)
listChanged = true;

if(canonicalPath.isEmpty())
continue;

if(fileList.contains(canonicalPath))
{
// Don't add duplicates
listChanged = true;
continue;
}

fileList.append(canonicalPath);
}

mWindow->setFavorites(fileList);

if(listChanged)
saveFavorites();
}

void LXQtFancyMenu::saveFavorites()
{
const QStringList fileList = mWindow->favorites();

QList<QMap<QString, QVariant> > list;
for(const QString& file : fileList)
{
QMap<QString, QVariant> item;
item.insert(QStringLiteral("desktopFile"), file);
list.append(item);
}

settings()->setArray(QStringLiteral("favorites"), list);
}

/************************************************
************************************************/
Expand Down
3 changes: 3 additions & 0 deletions plugin-fancymenu/lxqtfancymenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ protected slots:
virtual void settingsChanged();
void buildMenu();

void loadFavorites();
void saveFavorites();

private slots:
void showMenu();
void showHideMenu();
Expand Down
122 changes: 99 additions & 23 deletions plugin-fancymenu/lxqtfancymenuappmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ LXQtFancyMenuAppMap::LXQtFancyMenuAppMap()
{
mCachedIndex = -1;
mCachedIterator = mAppSortedByName.constEnd();

//Add Favorites category
Category favorites;
favorites.menuTitle = QLatin1String("Favorites"); //TODO: translate
favorites.icon = QIcon::fromTheme(QLatin1String("bookmarks"));
mCategories.append(favorites);
}

LXQtFancyMenuAppMap::~LXQtFancyMenuAppMap()
{
clear();
clearFavorites();
}

void LXQtFancyMenuAppMap::clear()
{
// Keep favorites
Category favoritesCat = mCategories.takeFirst();
mCategories.clear();
mCategories.append(favoritesCat);

mAppSortedByName.clear();
qDeleteAll(mAppSortedByDesktopFile);
mAppSortedByDesktopFile.clear();
Expand All @@ -24,16 +35,17 @@ void LXQtFancyMenuAppMap::clear()
mCachedIterator = mAppSortedByName.constEnd();
}

void LXQtFancyMenuAppMap::clearFavorites()
{
Category favoritesCat = mCategories[0];
qDeleteAll(favoritesCat.apps);
favoritesCat.apps.clear();
}

bool LXQtFancyMenuAppMap::rebuildModel(const XdgMenu &menu)
{
clear();

//Add Favorites category
Category favorites;
favorites.menuTitle = QLatin1String("Favorites"); //TODO: translate
favorites.icon = QIcon::fromTheme(QLatin1String("bookmarks"));
mCategories.append(favorites);

//Add All Apps category
Category allAppsCategory;
allAppsCategory.menuTitle = QLatin1String("All Applications");
Expand All @@ -47,6 +59,63 @@ bool LXQtFancyMenuAppMap::rebuildModel(const XdgMenu &menu)
return true;
}

void LXQtFancyMenuAppMap::setFavorites(const QStringList &favorites)
{
clearFavorites();

Category favoritesCat = mCategories[0];

for(const QString& desktopFile : favorites)
{
AppItem *item = loadAppItem(desktopFile);
if(!item)
continue;
favoritesCat.apps.append(item);
}
}

bool LXQtFancyMenuAppMap::isFavorite(const QString &desktopFile) const
{
const Category favoritesCat = mCategories.at(0);
for(const AppItem *item : favoritesCat.apps)
{
if(item->desktopFile == desktopFile)
return true;
}

return false;
}

void LXQtFancyMenuAppMap::addToFavorites(const QString &desktopFile)
{
if(isFavorite(desktopFile))
return;

Category favoritesCat = mCategories[0];
AppItem *item = loadAppItem(desktopFile);
if(!item)
return;
favoritesCat.apps.append(item);
}

void LXQtFancyMenuAppMap::removeFromFavorites(const QString &desktopFile)
{
if(!isFavorite(desktopFile))
return;

Category favoritesCat = mCategories[0];
for(auto it = favoritesCat.apps.begin(); it != favoritesCat.apps.end(); it++)
{
if((*it)->desktopFile == desktopFile)
{
AppItem *item = *it;
favoritesCat.apps.erase(it);
delete item;
return;
}
}
}

LXQtFancyMenuAppMap::AppItem *LXQtFancyMenuAppMap::getAppAt(int index)
{
if(index < 0 || index >= getTotalAppCount())
Expand Down Expand Up @@ -162,23 +231,9 @@ void LXQtFancyMenuAppMap::parseAppLink(const QDomElement &app, const QString& to
{
// Add new app

XdgDesktopFile f;
if(!f.load(desktopFile))
return; // Invalid App

item = new AppItem;
item->desktopFile = desktopFile;
item->title = app.attribute(QLatin1Literal("title"));
item->comment = app.attribute(QLatin1String("comment"));
if(item->comment.isEmpty())
item->comment = f.value(QLatin1String("GenericName")).toString();
QString iconName = app.attribute(QLatin1String("icon"));
item->icon = QIcon::fromTheme(iconName);
item->desktopFileCache = f;

item->keywords << f.value(QLatin1String("Keywords")).toString().toLower().split(QLatin1Char(';'));
item->keywords.append(item->title.toLower().split(QLatin1Char(' ')));
item->keywords.append(item->comment.toLower().split(QLatin1Char(' ')));
item = loadAppItem(desktopFile);
if(!item)
return;

mAppSortedByDesktopFile.insert(item->desktopFile, item);
mAppSortedByName.insert(item->title, item);
Expand All @@ -194,3 +249,24 @@ void LXQtFancyMenuAppMap::parseAppLink(const QDomElement &app, const QString& to
}
}
}

LXQtFancyMenuAppMap::AppItem *LXQtFancyMenuAppMap::loadAppItem(const QString &desktopFile)
{
XdgDesktopFile f;
if(!f.load(desktopFile))
return nullptr; // Invalid App

AppItem *item = new AppItem;
item->desktopFile = desktopFile;
item->title = f.name();
item->comment = f.comment();
if(item->comment.isEmpty())
item->comment = f.localizedValue(QLatin1String("GenericName")).toString();
item->icon = f.icon();
item->desktopFileCache = f;

item->keywords << f.localizedValue(QLatin1String("Keywords")).toString().toLower().split(QLatin1Char(';'));
item->keywords.append(item->title.toLower().split(QLatin1Char(' ')));
item->keywords.append(item->comment.toLower().split(QLatin1Char(' ')));
return item;
}
8 changes: 8 additions & 0 deletions plugin-fancymenu/lxqtfancymenuappmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ class LXQtFancyMenuAppMap
~LXQtFancyMenuAppMap();

void clear();
void clearFavorites();
bool rebuildModel(const XdgMenu &menu);

void setFavorites(const QStringList& favorites);
bool isFavorite(const QString& desktopFile) const;
void addToFavorites(const QString& desktopFile);
void removeFromFavorites(const QString& desktopFile);

inline int getCategoriesCount() const { return mCategories.size(); }
inline const Category& getCategoryAt(int index) { return mCategories.at(index); }

Expand All @@ -53,6 +59,8 @@ class LXQtFancyMenuAppMap
void parseMenu(const QDomElement& menu, const QString &topLevelCategory);
void parseAppLink(const QDomElement& app, const QString &topLevelCategory);

AppItem *loadAppItem(const QString& desktopFile);

private:
typedef QMap<QString, AppItem *> AppMap;
AppMap mAppSortedByDesktopFile;
Expand Down
54 changes: 54 additions & 0 deletions plugin-fancymenu/lxqtfancymenuwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <QMenu>
#include <QStandardPaths>
#include <QDir>
#include <QMimeData>
#include <XdgIcon>
#include <QFile>
Expand All @@ -25,6 +26,7 @@
#include <QKeyEvent>
#include <QCoreApplication>


LXQtFancyMenuWindow::LXQtFancyMenuWindow(QWidget *parent)
: QWidget{parent, Qt::Popup}
{
Expand Down Expand Up @@ -210,6 +212,7 @@ void LXQtFancyMenuWindow::onAppViewCustomMenu(const QPoint& p)
}
QFile::copy(file, desktopFile);
});

a = menu.addAction(XdgIcon::fromTheme(QLatin1String("edit-copy")), tr("Copy"));
connect(a, &QAction::triggered, this, [file] {
QClipboard* clipboard = QApplication::clipboard();
Expand All @@ -218,6 +221,22 @@ void LXQtFancyMenuWindow::onAppViewCustomMenu(const QPoint& p)
clipboard->setMimeData(data);
});

QString canonicalFile = QDir(file).canonicalPath();
if(mAppMap->isFavorite(canonicalFile))
{
a = menu.addAction(XdgIcon::fromTheme(QLatin1String("bookmark-remove")), tr("Remove from Favorites"));
connect(a, &QAction::triggered, this, [this, canonicalFile] {
removeFromFavorites(canonicalFile);
});
}
else
{
a = menu.addAction(XdgIcon::fromTheme(QLatin1String("bookmark-new")), tr("Add to Favorites"));
connect(a, &QAction::triggered, this, [this, canonicalFile] {
addToFavorites(canonicalFile);
});
}

QPoint globalPos = mAppView->mapToGlobal(p);
menu.exec(globalPos);
}
Expand Down Expand Up @@ -306,3 +325,38 @@ void LXQtFancyMenuWindow::runCommandHelper(const QString &cmd)
tr("Cannot find <b>%1</b> executable.").arg(cmd));
}
}

void LXQtFancyMenuWindow::addToFavorites(const QString &desktopFile)
{
mFavorites.append(desktopFile);
mAppMap->addToFavorites(desktopFile);
reloadFavoritesModel();
emit favoritesChanged();
}

void LXQtFancyMenuWindow::removeFromFavorites(const QString &desktopFile)
{
mFavorites.removeOne(desktopFile);
mAppMap->removeFromFavorites(desktopFile);
reloadFavoritesModel();
emit favoritesChanged();
}

void LXQtFancyMenuWindow::reloadFavoritesModel()
{
// TODO: reload only if showing favorites
mAppModel->reloadAppMap(false);
mAppModel->reloadAppMap(true);
}

QStringList LXQtFancyMenuWindow::favorites() const
{
return mFavorites;
}

void LXQtFancyMenuWindow::setFavorites(const QStringList &newFavorites)
{
mFavorites = newFavorites;
mAppMap->setFavorites(mFavorites);
reloadFavoritesModel();
}
12 changes: 11 additions & 1 deletion plugin-fancymenu/lxqtfancymenuwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ class LXQtFancyMenuWindow : public QWidget

bool eventFilter(QObject *watched, QEvent *e) override;

Q_SIGNALS:
QStringList favorites() const;
void setFavorites(const QStringList &newFavorites);

signals:
void aboutToShow();
void aboutToHide();
void favoritesChanged();

public slots:
void doSearch();
Expand All @@ -56,7 +60,13 @@ private slots:
private:
void runCommandHelper(const QString& cmd);

void addToFavorites(const QString& desktopFile);
void removeFromFavorites(const QString& desktopFile);
void reloadFavoritesModel();

private:
QStringList mFavorites;

QToolButton *mSettingsButton;
QToolButton *mPowerButton;
QLineEdit *mSearchEdit;
Expand Down

0 comments on commit fee6b1b

Please sign in to comment.