dn: remove bullshit from assoc code
This commit is contained in:
+27
-49
@@ -469,32 +469,22 @@ to_app(const Desktop &d)
|
||||
return a;
|
||||
}
|
||||
|
||||
vector<QString>
|
||||
filename_types(const QString &path)
|
||||
QMimeDatabase &
|
||||
db()
|
||||
{
|
||||
// Content sniff (mime/magic / QMimeDatabase::mimeTypeForFile) is a later
|
||||
// follow-up. Directories are inode/directory from stat (GIO
|
||||
// get_content_type). Regular files use filename globs only.
|
||||
if (QFileInfo(path).isDir())
|
||||
return {QStringLiteral("inode/directory")};
|
||||
return types_for_filename(path);
|
||||
static QMimeDatabase mime;
|
||||
return mime;
|
||||
}
|
||||
|
||||
vector<QString>
|
||||
ancestor_types(const vector<QString> &types)
|
||||
ancestor_types(const QString &type)
|
||||
{
|
||||
vector<QString> ancestors;
|
||||
QMimeDatabase mime;
|
||||
for (const QString &type : types) {
|
||||
const QMimeType mt = mime.mimeTypeForName(type);
|
||||
if (!mt.isValid())
|
||||
continue;
|
||||
for (const QString &a : mt.allAncestors()) {
|
||||
if (find(types.begin(), types.end(), a) != types.end())
|
||||
continue;
|
||||
append_unique(ancestors, a);
|
||||
}
|
||||
}
|
||||
const QMimeType mt = db().mimeTypeForName(type);
|
||||
if (!mt.isValid())
|
||||
return ancestors;
|
||||
for (const QString &a : mt.allAncestors())
|
||||
append_unique(ancestors, a);
|
||||
return ancestors;
|
||||
}
|
||||
|
||||
@@ -655,10 +645,8 @@ prepend_id(const QString &value, const QString &id)
|
||||
Handler
|
||||
default_for(const QString &path)
|
||||
{
|
||||
const vector<QString> types = filename_types(path);
|
||||
AssocSets acc;
|
||||
for (const QString &type : types)
|
||||
merge_assoc(acc, associations_for_type(type));
|
||||
const QString type = db().mimeTypeForFile(path).name();
|
||||
const AssocSets acc = associations_for_type(type);
|
||||
for (const QString &id : acc.defaults) {
|
||||
if (!usable_id(id, acc.removed))
|
||||
continue;
|
||||
@@ -670,14 +658,9 @@ default_for(const QString &path)
|
||||
vector<Handler>
|
||||
recommended_for(const QString &path)
|
||||
{
|
||||
const vector<QString> types = filename_types(path);
|
||||
AssocSets acc;
|
||||
vector<QString> cache_ids;
|
||||
for (const QString &type : types) {
|
||||
merge_assoc(acc, associations_for_type(type));
|
||||
for (const QString &id : cache_ids_for_type(type))
|
||||
append_unique(cache_ids, id);
|
||||
}
|
||||
const QString type = db().mimeTypeForFile(path).name();
|
||||
const AssocSets acc = associations_for_type(type);
|
||||
const vector<QString> cache_ids = cache_ids_for_type(type);
|
||||
|
||||
const Handler def = default_for(path);
|
||||
vector<Handler> out;
|
||||
@@ -700,11 +683,9 @@ recommended_for(const QString &path)
|
||||
vector<Handler>
|
||||
fallback_for(const QString &path)
|
||||
{
|
||||
const vector<QString> types = filename_types(path);
|
||||
const vector<QString> ancestors = ancestor_types(types);
|
||||
AssocSets acc;
|
||||
for (const QString &type : types)
|
||||
merge_assoc(acc, associations_for_type(type));
|
||||
const QString type = db().mimeTypeForFile(path).name();
|
||||
const vector<QString> ancestors = ancestor_types(type);
|
||||
AssocSets acc = associations_for_type(type);
|
||||
for (const QString &type : ancestors)
|
||||
merge_assoc(acc, associations_for_type(type));
|
||||
|
||||
@@ -751,9 +732,8 @@ set_last_used(const Handler &app, const QString &path)
|
||||
const QString id = normalize_desktop_id(app.id);
|
||||
if (id.isEmpty() || id == kSelfDesktop)
|
||||
return;
|
||||
const vector<QString> types = filename_types(path);
|
||||
if (types.empty())
|
||||
return;
|
||||
|
||||
const QString type = db().mimeTypeForFile(path).name();
|
||||
const QString dest = user_mimeapps_path();
|
||||
if (dest.isEmpty())
|
||||
return;
|
||||
@@ -798,15 +778,13 @@ set_last_used(const Handler &app, const QString &path)
|
||||
group, type_utf8, value.toUtf8().toStdString());
|
||||
}
|
||||
};
|
||||
for (const QString &type : types) {
|
||||
const string type_utf8 = type.toUtf8().toStdString();
|
||||
const QString previous = QString::fromStdString(
|
||||
dawn::detail::ini_get(*added, type_utf8));
|
||||
dawn::detail::ini_set(*added, type_utf8,
|
||||
prepend_id(previous, id).toUtf8().toStdString());
|
||||
if (removed)
|
||||
drop_id(*removed, type);
|
||||
}
|
||||
const string type_utf8 = type.toUtf8().toStdString();
|
||||
const QString previous = QString::fromStdString(
|
||||
dawn::detail::ini_get(*added, type_utf8));
|
||||
dawn::detail::ini_set(*added, type_utf8,
|
||||
prepend_id(previous, id).toUtf8().toStdString());
|
||||
if (removed)
|
||||
drop_id(*removed, type);
|
||||
write_text_file(dest, dawn::detail::ini_serialize(ini));
|
||||
}
|
||||
|
||||
|
||||
-35
@@ -267,39 +267,4 @@ extract_mime_globs(const vector<string> &media_types)
|
||||
return out;
|
||||
}
|
||||
|
||||
vector<QString>
|
||||
types_for_filename(const QString &path)
|
||||
{
|
||||
if (path.isEmpty())
|
||||
return {};
|
||||
const QString name = QFileInfo(path).fileName().toLower();
|
||||
if (name.isEmpty())
|
||||
return {};
|
||||
|
||||
const MimeDb &db = mime_db();
|
||||
unordered_map<QString, int> best_weight;
|
||||
for (const MimeGlob &g : db.globs) {
|
||||
if (!g.glob_re.match(name).hasMatch())
|
||||
continue;
|
||||
auto it = best_weight.find(g.type);
|
||||
if (it == best_weight.end())
|
||||
best_weight.insert({g.type, g.weight});
|
||||
else if (g.weight > it->second)
|
||||
it->second = g.weight;
|
||||
}
|
||||
|
||||
vector<QString> out;
|
||||
out.reserve(best_weight.size());
|
||||
for (const auto &kv : best_weight)
|
||||
out.push_back(kv.first);
|
||||
sort(out.begin(), out.end(), [&](const QString &a, const QString &b) {
|
||||
const int wa = best_weight.at(a);
|
||||
const int wb = best_weight.at(b);
|
||||
if (wa != wb)
|
||||
return wa > wb;
|
||||
return a < b;
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace dn
|
||||
|
||||
@@ -18,9 +18,6 @@ namespace dn
|
||||
std::vector<QString> extract_mime_globs(
|
||||
const std::vector<std::string> &media_types);
|
||||
|
||||
// MIME types whose shared-mime-info globs match the filename (suffix/glob).
|
||||
std::vector<QString> types_for_filename(const QString &path);
|
||||
|
||||
std::vector<QString> xdg_data_dirs();
|
||||
std::vector<QString> xdg_config_dirs();
|
||||
|
||||
|
||||
@@ -25,15 +25,6 @@ test_mime_globs()
|
||||
globs.end());
|
||||
}
|
||||
|
||||
void
|
||||
test_types_for_filename()
|
||||
{
|
||||
const vector<QString> types =
|
||||
dn::types_for_filename(QStringLiteral("photo.jpg"));
|
||||
CHECK(find(types.begin(), types.end(), QLatin1String("image/jpeg")) !=
|
||||
types.end());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int
|
||||
@@ -41,6 +32,5 @@ main()
|
||||
{
|
||||
return test::run({
|
||||
{"MIME globs", test_mime_globs},
|
||||
{"filename types", test_types_for_filename},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user