Skip to content

Commit

Permalink
[Search] Fixup view
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Jul 17, 2024
1 parent 78f771e commit f2d9181
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 181 deletions.
2 changes: 2 additions & 0 deletions lib/search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ target_link_libraries(${PROJECT}
Qt6::Core)

set_target_properties(${PROJECT} PROPERTIES VERSION 0.1 SOVERSION 0)
add_definitions( -DINSTALL_LIBDIR="${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
add_subdirectory(plugins)

install(TARGETS ${PROJECT}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/)
Expand Down
11 changes: 10 additions & 1 deletion lib/search/glaciersearchplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@
class GLACIER_EXPORT GlacierSearchPlugin : public QObject {
Q_OBJECT
public:
struct SearchResult
{
QString iconTitle;
QString iconSource;
QString category;
QString extraCaption;
QVariant action;
};

virtual void search(QString searchString) = 0;
signals:
void searchResultReady(QMap<QString, QVariant> results);
void searchResultReady(QList<SearchResult> results);
};
Q_DECLARE_INTERFACE(GlacierSearchPlugin, "GlacierHome.SearchPlugin")

Expand Down
1 change: 1 addition & 0 deletions lib/search/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(application)
19 changes: 19 additions & 0 deletions lib/search/plugins/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SET(PLUGINNAME application)

set(SRC ${PLUGINNAME}searchplugin.cpp)
SET(HEADERS ${PLUGINNAME}searchplugin.h)

include_directories(${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_AUTOMOC ON)
add_definitions(-DQT_PLUGIN)

add_library(${PLUGINNAME} MODULE ${SRC} ${HEADERS})

target_link_libraries(${PLUGINNAME} PUBLIC
Qt6::Core
Qt6::DBus
GlacierHome::Search
PkgConfig::LIPSTICK)

install(TARGETS ${PLUGINNAME}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/glacier-home/plugins/search)
60 changes: 60 additions & 0 deletions lib/search/plugins/application/applicationsearchplugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "applicationsearchplugin.h"

ApplicationSearchPlugin::ApplicationSearchPlugin(QObject *parent)
:m_launchModel(new LauncherModel)
{
}

void ApplicationSearchPlugin::search(QString searchString)
{
qDebug() << Q_FUNC_INFO << searchString;
m_searchResults.clear();

for(int i = 0; i < m_launchModel.itemCount(); i++) {
QObject* item = m_launchModel.get(i);
if(item->property("title").toString().toLower().indexOf(searchString) != -1){
SearchResult result;
result.iconTitle = item->property("title").toString();

QString iconSource = item->property("iconId").toString();
if(iconSource.isEmpty()) {
iconSource = "/usr/share/glacier-home/qml/theme/default-icon.png";
} else {
if(iconSource.startsWith("/")) {
iconSource = "file://" + iconSource;
} else if(!iconSource.startsWith("file:///")) {
iconSource = "image://theme/" + iconSource;
}
}
result.iconSource = iconSource;

result.category = tr("Application");
result.extraCaption = tr("installed on your device");

m_searchResults.push_back(result);
}
}

if(!m_searchResults.isEmpty()) {
emit searchResultReady(m_searchResults);
}
}
40 changes: 40 additions & 0 deletions lib/search/plugins/application/applicationsearchplugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2024 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef APPLICATIONSEARCHPLUGIN_H
#define APPLICATIONSEARCHPLUGIN_H

#include <search/glaciersearchplugin.h>
#include <lipstick-qt6/launchermodel.h>

class ApplicationSearchPlugin : public GlacierSearchPlugin
{
Q_OBJECT
Q_INTERFACES(GlacierSearchPlugin)
Q_PLUGIN_METADATA(IID "GlacierHome.SearchPlugin")
public:
explicit ApplicationSearchPlugin(QObject *parent = nullptr);
void search(QString searchString);

private:
LauncherModel m_launchModel;
QList<SearchResult> m_searchResults;
};

#endif // APPLICATIONSEARCHPLUGIN_H
37 changes: 30 additions & 7 deletions lib/search/searchpluginmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <QPluginLoader>
#include <QTimer>

#ifndef INSTALL_LIBDIR
#error INTALLINSTALL_LIBDIR is not set!
#endif

SearchPluginManager::SearchPluginManager(QObject* parent)
: QObject { parent }
{
Expand All @@ -31,26 +35,45 @@ SearchPluginManager::SearchPluginManager(QObject* parent)
SearchPluginManager::~SearchPluginManager()
{
foreach (const GlacierSearchPlugin* plugin, m_pluginList) {
disconnect(plugin, &GlacierSearchPlugin::searchResultReady, this, &SearchPluginManager::searchResultReady);
disconnect(plugin, &GlacierSearchPlugin::searchResultReady, this, &SearchPluginManager::searchResultPluginHandler);
delete plugin;
}
}

void SearchPluginManager::search(QString searchString)
{
m_searchResults.clear();

foreach (GlacierSearchPlugin* plugin, m_pluginList) {
plugin->search(searchString);
}
}

void SearchPluginManager::loadSearchPlugins()
{
QDir pluginsDir("/usr/lib/glacier-home/plugins/search");
QDir pluginsDir(QString::fromUtf8(INSTALL_LIBDIR) + "/glacier-home/plugins/search");
QList<QString> pluginsLibList = pluginsDir.entryList(QDir::Files);

for (const QString& file : qAsConst(pluginsLibList)) {
QPluginLoader pluginLoader(pluginsDir.path() + file);
QPluginLoader pluginLoader(pluginsDir.path() + "/" + file);

QObject* plugin = pluginLoader.instance();
if (plugin) {
GlacierSearchPlugin* sourcePlugin = qobject_cast<GlacierSearchPlugin*>(plugin);
if (sourcePlugin != nullptr) {
m_pluginList.push_back(sourcePlugin);
connect(sourcePlugin, &GlacierSearchPlugin::searchResultReady, this, &SearchPluginManager::searchResultReady);
GlacierSearchPlugin* searchPlugin = qobject_cast<GlacierSearchPlugin*>(plugin);
if (searchPlugin != nullptr) {
m_pluginList.push_back(searchPlugin);
connect(searchPlugin, &GlacierSearchPlugin::searchResultReady, this, &SearchPluginManager::searchResultReady);
} else {
qWarning() << "CANT CAST PLIUGIN FROM" << pluginsDir.path() + "/" + file;
}
} else {
delete plugin;
}
}
}

void SearchPluginManager::searchResultPluginHandler(QList<GlacierSearchPlugin::SearchResult> results)
{
m_searchResults.append(results);
emit searchResultReady(m_searchResults);
}
6 changes: 5 additions & 1 deletion lib/search/searchpluginmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ class SearchPluginManager : public QObject {
explicit SearchPluginManager(QObject* parent = nullptr);
virtual ~SearchPluginManager();

void search(QString searchString);

signals:
void searchResultReady(QMap<QString, QVariant> results);
void searchResultReady(QList<GlacierSearchPlugin::SearchResult> results);

private slots:
void loadSearchPlugins();
void searchResultPluginHandler(QList<GlacierSearchPlugin::SearchResult> results);

private:
QList<GlacierSearchPlugin*> m_pluginList;
QList<GlacierSearchPlugin::SearchResult> m_searchResults;
};

#endif // SEARCHPLUGINMANAGER_H
45 changes: 43 additions & 2 deletions src/models/searchmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,60 @@ SearchModel::SearchModel(QObject* parent)
m_hash.insert(Qt::UserRole + 2, QByteArray("category"));
m_hash.insert(Qt::UserRole + 3, QByteArray("extraCaption"));
m_hash.insert(Qt::UserRole + 4, QByteArray("action"));

connect(m_manager, &SearchPluginManager::searchResultReady, this, &SearchModel::searchResultHandler);
}

int SearchModel::rowCount(const QModelIndex& parent) const
{
return -1;
return m_searchResults.count();
}

QVariant SearchModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}

if (index.row() >= m_searchResults.size()) {
return QVariant();
}

GlacierSearchPlugin::SearchResult result = m_searchResults.at(index.row());
if(role == Qt::UserRole) {
return result.iconTitle;
}
if(role == Qt::UserRole+1) {
return result.iconSource;
}
if(role == Qt::UserRole+2) {
return result.category;
}
if(role == Qt::UserRole+3) {
return result.extraCaption;
}
if(role == Qt::UserRole+4) {
return result.action;
}

return QVariant();
}

void SearchModel::search(QString searchString)
{
qDebug() << Q_FUNC_INFO << searchString;
m_manager->search(searchString);
}

void SearchModel::searchResultHandler(QList<GlacierSearchPlugin::SearchResult> results)
{
beginResetModel();
m_searchResults.clear();
m_searchResults = results;
endResetModel();
emit countChanged();
}

int SearchModel::count() const
{
return m_searchResults.count();
}
10 changes: 10 additions & 0 deletions src/models/searchmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

class SearchModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged FINAL)

public:
explicit SearchModel(QObject* parent = nullptr);

Expand All @@ -35,10 +37,18 @@ class SearchModel : public QAbstractListModel {
QHash<int, QByteArray> roleNames() const { return m_hash; }

Q_INVOKABLE void search(QString searchString);
int count() const;

signals:
void countChanged();

private slots:
void searchResultHandler(QList<GlacierSearchPlugin::SearchResult> results);

private:
QHash<int, QByteArray> m_hash;
SearchPluginManager* m_manager;
QList<GlacierSearchPlugin::SearchResult> m_searchResults;
};

#endif // SEARCHMODEL_H
2 changes: 1 addition & 1 deletion src/qml/AppLauncher.qml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Flickable{
width: parent.width
height: desktop.height
property var switcher: null
property string searchString
property alias searchString: searchListView.searchString

ConfigurationValue {
id: alwaysShowSearch
Expand Down
Loading

0 comments on commit f2d9181

Please sign in to comment.