Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libmimeapps: avoid unnecessary work #1745

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 29 additions & 36 deletions 3rdparty/libmimeapps/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "ConfigReader.h"

#include <algorithm>

namespace LibMimeApps
{

Expand Down Expand Up @@ -203,21 +205,27 @@ void Index::processDesktopFile(const std::string &baseDirectory, const std::stri

void Index::addApplication(DesktopEntry *entry)
{
removeApplication(entry->identifier());
std::string const& entryId = entry->identifier();

knownApplications_[entry->identifier()] = entry;
removeApplication(entryId);

for (std::vector<std::string>::size_type i = 0; i < entry->types().size(); ++i)
{
addToType(entry->types().at(i), entry);
knownApplications_[entryId] = entry;

// There is no need to call addToType here, since we just removed the entry ID
// from the cache, so the only missing information is the mapping from the types
// in the applicationsCache_
for (auto& type : entry->types()) {
applicationsCache_[type].push_front(entry);
}
}

void Index::addToType(const std::string &type, DesktopEntry *entry)
{
std::string const& entryId = entry->identifier();

if (applicationsCache_.find(type) != applicationsCache_.end())
{
removeFromType(type, entry->identifier());
removeFromType(type, entryId);
}

applicationsCache_[type].push_front(entry);
Expand All @@ -226,14 +234,19 @@ void Index::addToType(const std::string &type, DesktopEntry *entry)

void Index::removeApplication(const std::string &entryId)
{
for (std::map<std::string, std::list<DesktopEntry*> >::iterator type = applicationsCache_.begin(); type != applicationsCache_.end(); ++type)
auto found = knownApplications_.find(entryId);
if (found == knownApplications_.end())
return;
for (auto& type : found->second->types_)
{
removeFromType(type->first, entryId);
removeFromTypeCache(type, entryId);
}
knownApplications_.erase(found);
}

void Index::removeFromType(const std::string &type, const std::string &entryId)
void Index::removeFromTypeCache(const std::string &type, const std::string &entryId)
{

if (applicationsCache_.find(type) != applicationsCache_.end())
{
for (std::list<DesktopEntry*>::iterator it = applicationsCache_.at(type).begin(); it != applicationsCache_.at(type).end();)
Expand All @@ -249,36 +262,16 @@ void Index::removeFromType(const std::string &type, const std::string &entryId)
}
}

}

void Index::removeFromType(const std::string &type, const std::string &entryId)
{
removeFromTypeCache(type, entryId);

if (knownApplications_.find(entryId) != knownApplications_.end())
{
std::vector<std::string> &types = knownApplications_.at(entryId)->types_;

for (std::vector<std::string>::iterator it = types.begin(); it != types.end();)
{
if (*it == type)
{
if (it == types.begin())
{
types.erase(it);

it = types.begin();
}
else
{
std::vector<std::string>::iterator temp = it;
--temp;

types.erase(it);

it = ++temp;
}

}
else
{
++it;
}
}
(void)std::remove(types.begin(), types.end(), type);
}
}

Expand Down
1 change: 1 addition & 0 deletions 3rdparty/libmimeapps/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Index
void addToType(const std::string &type, DesktopEntry *entry);
void removeApplication(const std::string &entryId);
void removeFromType(const std::string &type, const std::string &entryId);
void removeFromTypeCache(const std::string &type, const std::string &entryId);
void removeUnused();
static std::list<std::string> resolveVariable(const std::string &name);
static std::vector<lookupDirectory> initDirectoryPatterns();
Expand Down