Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/StringBuilder.h>
28#include <LibCore/MimeData.h>
29
30namespace Core {
31
32Vector<String> MimeData::formats() const
33{
34 Vector<String> mime_types;
35 mime_types.ensure_capacity(m_data.size());
36 for (auto it : m_data)
37 mime_types.unchecked_append(it.key);
38 return mime_types;
39}
40
41Vector<URL> MimeData::urls() const
42{
43 auto it = m_data.find("text/uri-list");
44 if (it == m_data.end())
45 return {};
46 Vector<URL> urls;
47 for (auto& line : StringView(it->value).split_view('\n')) {
48 urls.append(URL(line));
49 }
50 return urls;
51}
52
53void MimeData::set_urls(const Vector<URL>& urls)
54{
55 StringBuilder builder;
56 for (auto& url : urls) {
57 builder.append(url.to_string());
58 builder.append('\n');
59 }
60 set_data("text/uri-list", builder.to_byte_buffer());
61}
62
63String MimeData::text() const
64{
65 return String::copy(m_data.get("text/plain").value_or({}));
66}
67
68void MimeData::set_text(const String& text)
69{
70 set_data("text/plain", text.to_byte_buffer());
71}
72
73}