2020-02-14 13:17:26 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-14 13:17:26 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
#include <LibCore/MimeData.h>
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
Vector<String> MimeData::formats() const
|
|
|
|
|
{
|
|
|
|
|
Vector<String> mime_types;
|
|
|
|
|
mime_types.ensure_capacity(m_data.size());
|
|
|
|
|
for (auto it : m_data)
|
|
|
|
|
mime_types.unchecked_append(it.key);
|
|
|
|
|
return mime_types;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<URL> MimeData::urls() const
|
|
|
|
|
{
|
|
|
|
|
auto it = m_data.find("text/uri-list");
|
|
|
|
|
if (it == m_data.end())
|
|
|
|
|
return {};
|
|
|
|
|
Vector<URL> urls;
|
|
|
|
|
for (auto& line : StringView(it->value).split_view('\n')) {
|
|
|
|
|
urls.append(URL(line));
|
|
|
|
|
}
|
|
|
|
|
return urls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MimeData::set_urls(const Vector<URL>& urls)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
for (auto& url : urls) {
|
|
|
|
|
builder.append(url.to_string());
|
|
|
|
|
builder.append('\n');
|
|
|
|
|
}
|
|
|
|
|
set_data("text/uri-list", builder.to_byte_buffer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String MimeData::text() const
|
|
|
|
|
{
|
|
|
|
|
return String::copy(m_data.get("text/plain").value_or({}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MimeData::set_text(const String& text)
|
|
|
|
|
{
|
|
|
|
|
set_data("text/plain", text.to_byte_buffer());
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 21:14:16 +02:00
|
|
|
|
String guess_mime_type_based_on_filename(const StringView& path)
|
2020-07-27 19:49:43 +02:00
|
|
|
|
{
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".pbm", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "image/x‑portable‑bitmap";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".pgm", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "image/x‑portable‑graymap";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".png", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "image/png";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".ppm", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "image/x‑portable‑pixmap";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".gif", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "image/gif";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".bmp", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "image/bmp";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".jpg", CaseSensitivity::CaseInsensitive) || path.ends_with(".jpeg", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "image/jpeg";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".svg", CaseSensitivity::CaseInsensitive))
|
2020-09-07 19:45:14 +02:00
|
|
|
|
return "image/svg+xml";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".md", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "text/markdown";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with(".html", CaseSensitivity::CaseInsensitive) || path.ends_with(".htm", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "text/html";
|
2020-10-21 21:14:16 +02:00
|
|
|
|
if (path.ends_with("/", CaseSensitivity::CaseInsensitive))
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "text/html";
|
2020-11-23 16:16:27 +03:30
|
|
|
|
if (path.ends_with(".csv", CaseSensitivity::CaseInsensitive))
|
|
|
|
|
return "text/csv";
|
2020-07-27 19:49:43 +02:00
|
|
|
|
return "text/plain";
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 13:17:26 +01:00
|
|
|
|
}
|