Skip to content

Commit

Permalink
Merge branch 'jb39303' into 'master'
Browse files Browse the repository at this point in the history
[lipstick] Pick correct size variant for the launcher icons. Contributes to JB#39303

See merge request !76
  • Loading branch information
jpetrell committed Nov 23, 2017
2 parents 771f9b6 + 1a3277e commit fa4d9da
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 62 deletions.
95 changes: 34 additions & 61 deletions src/components/launchermodel.cpp
Expand Up @@ -54,19 +54,6 @@ static inline bool isIconFile(const QString &filename)
return filename.startsWith(QLatin1Char('/')) && filename.endsWith(".png");
}

static inline QString iconIdFromFilename(const QString &filename)
{
int start = filename.lastIndexOf('/') + 1;
int end = filename.lastIndexOf('.');

if (start == -1 || end == -1) {
// something's fishy..
return QString();
}

return filename.mid(start, end - start);
}

static inline QString filenameFromIconId(const QString &filename, const QString &path)
{
return QString("%1%2%3").arg(path).arg(filename).arg(".png");
Expand Down Expand Up @@ -171,7 +158,7 @@ void LauncherModel::onFilesUpdated(const QStringList &added,
QStringList modifiedAndNeedUpdating = modified;

// First, remove all removed launcher items before adding new ones
foreach (const QString &filename, removed) {
for (const QString &filename : removed) {
if (isDesktopFile(m_directories, filename)) {
// Desktop file has been removed - remove launcher
LauncherItem *item = itemInModel(filename);
Expand All @@ -181,13 +168,10 @@ void LauncherModel::onFilesUpdated(const QStringList &added,
removeItem(item);
}
delete item;
} else if (isIconFile(filename)) {
// Icons has been removed - find item and clear its icon path
updateItemsWithIcon(filename, false);
}
}

foreach (const QString &filename, added) {
for (const QString &filename : added) {
if (isDesktopFile(m_directories, filename)) {
// New desktop file appeared - add launcher
LauncherItem *item = itemInModel(filename);
Expand Down Expand Up @@ -215,15 +199,7 @@ void LauncherModel::onFilesUpdated(const QStringList &added,
item = addItemIfValid(filename);

if (item != NULL) {
// Try to look up an already-installed icon in the icons directory
foreach (const QString &iconPath, m_launcherMonitor.iconDirectories()) {
QString iconname = filenameFromIconId(item->getOriginalIconId(), iconPath);
if (QFile(iconname).exists()) {
LAUNCHER_DEBUG("Loading existing icon:" << iconname);
updateItemsWithIcon(iconname, true);
break;
}
}
updateItemsWithIcon(item->getOriginalIconId(), QString());
}
} else {
// This case happens if a .desktop file is found as new, but we
Expand All @@ -237,12 +213,11 @@ void LauncherModel::onFilesUpdated(const QStringList &added,
modifiedAndNeedUpdating << filename;
}
} else if (isIconFile(filename)) {
// Icons has been added - find item and update its icon path
updateItemsWithIcon(filename, true);
updateItemsWithIcon(QString(), filename);
}
}

foreach (const QString &filename, modifiedAndNeedUpdating) {
for (const QString &filename : modifiedAndNeedUpdating) {
if (isDesktopFile(m_directories, filename)) {
// Desktop file has been updated - update launcher
LauncherItem *item = itemInModel(filename);
Expand All @@ -255,55 +230,53 @@ void LauncherModel::onFilesUpdated(const QStringList &added,
unsetTemporary(item);
removeItem(item);
} else {
// File has been updated and is still valid; check if we
// might need to auto-update the icon file
if (item->iconFilename().isEmpty()) {
foreach (const QString &iconPath, m_launcherMonitor.iconDirectories()) {
QString filename = filenameFromIconId(item->getOriginalIconId(), iconPath);
LAUNCHER_DEBUG("Desktop file changed, checking for:" << filename);
if (QFile(filename).exists()) {
updateItemsWithIcon(filename, true);
break;
}
}
}
updateItemsWithIcon(item->getOriginalIconId(), QString());
}
} else {
// No item yet (maybe it had Hidden=true before), try to see if
// we should show the item now
addItemIfValid(filename);
}
} else if (isIconFile(filename)) {
// Icons has been updated - find item and update its icon path
updateItemsWithIcon(filename, true);
}
}

reorderItems();
savePositions();
}

void LauncherModel::updateItemsWithIcon(const QString &filename, bool existing)
void LauncherModel::updateItemsWithIcon(const QString &iconId, const QString &filename)
{
QString iconId = iconIdFromFilename(filename);
LAUNCHER_DEBUG("updateItemsWithIcon: iconId=" << iconId << "filename=" << filename);

LAUNCHER_DEBUG("updateItemsWithIcon: filename=" << filename << ", existing=" << existing << ", id=" << iconId);
QStringList iconDirectories = m_launcherMonitor.iconDirectories();

foreach (LauncherItem *item, *getList<LauncherItem>()) {
const QString &currentId = item->getOriginalIconId();
if (currentId.isEmpty()) {
continue;
// Try to determine icon id from filename
QString id = iconId;
if (id.isEmpty()) {
QFileInfo fileInfo(filename);
if (iconDirectories.contains(fileInfo.path() + "/")) {
id = fileInfo.baseName();
}
}

// Prefer to use scalable icons in icon directories, sorted by the closest icon size
QString scalableIconFilename;
if (!id.isEmpty()) {
for (const QString &iconPath : iconDirectories) {
QString filename = filenameFromIconId(id, iconPath);
if (QFile(filename).exists()) {
scalableIconFilename = filename;
break;
}
}
}

if (!existing && filename == item->iconFilename()) {
// File is currently used as icon, but has been removed
LAUNCHER_DEBUG("Icon vanished, removing:" << filename);
item->setIconFilename("");
} else if (existing) {
if ((filename == currentId) /* absolute file path in .desktop file */ ||
(iconId == currentId) /* icon id matches */) {
LAUNCHER_DEBUG("Icon was added or updated:" << filename);
item->setIconFilename(filename);
if (!scalableIconFilename.isEmpty()) {
// Use the most suitable scalable icon in the launcher items that share the icon id
for (LauncherItem *item : *getList<LauncherItem>()) {
if (id == item->getOriginalIconId()) {
item->setIconFilename(scalableIconFilename);
LAUNCHER_DEBUG("Scalable icon" << scalableIconFilename << "was updated for id" << id);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/launchermodel.h
Expand Up @@ -112,7 +112,7 @@ public slots:
LauncherItem *packageInModel(const QString &packageName);
QVariant launcherPos(const QString &path);
LauncherItem *addItemIfValid(const QString &path);
void updateItemsWithIcon(const QString &filename, bool existing);
void updateItemsWithIcon(const QString &iconId, const QString &filename);
void updateWatchedDBusServices();
void setTemporary(LauncherItem *item);
void unsetTemporary(LauncherItem *item);
Expand Down

0 comments on commit fa4d9da

Please sign in to comment.