| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-26 09:09:45 -07:00
										 |  |  |  * Copyright (c) 2022, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							|  |  |  | #include <AK/URL.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:09:39 +02:00
										 |  |  | #include <LibCore/EventReceiver.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:09:39 +02:00
										 |  |  | class MimeData : public EventReceiver { | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |     C_OBJECT(MimeData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-02-26 09:09:45 -07:00
										 |  |  |     virtual ~MimeData() = default; | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 16:45:46 +02:00
										 |  |  |     ByteBuffer data(StringView mime_type) const { return m_data.get(mime_type).value_or({}); } | 
					
						
							| 
									
										
										
										
											2023-08-21 17:06:49 +02:00
										 |  |  |     void set_data(String const& mime_type, ByteBuffer&& data) { m_data.set(mime_type, move(data)); } | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 16:45:46 +02:00
										 |  |  |     bool has_format(StringView mime_type) const { return m_data.contains(mime_type); } | 
					
						
							| 
									
										
										
										
											2023-08-23 14:41:26 +02:00
										 |  |  |     Vector<String> formats() const { return m_data.keys(); } | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Convenience helpers for "text/plain"
 | 
					
						
							| 
									
										
										
										
											2023-08-21 16:45:46 +02:00
										 |  |  |     bool has_text() const { return has_format("text/plain"sv); } | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString text() const; | 
					
						
							|  |  |  |     void set_text(DeprecatedString const&); | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Convenience helpers for "text/uri-list"
 | 
					
						
							| 
									
										
										
										
											2023-08-21 16:45:46 +02:00
										 |  |  |     bool has_urls() const { return has_format("text/uri-list"sv); } | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |     Vector<URL> urls() const; | 
					
						
							| 
									
										
										
										
											2023-03-07 14:31:34 +01:00
										 |  |  |     ErrorOr<void> set_urls(Vector<URL> const&); | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 17:06:49 +02:00
										 |  |  |     HashMap<String, ByteBuffer> const& all_data() const { return m_data; } | 
					
						
							| 
									
										
										
										
											2020-11-07 23:14:21 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-02-26 09:09:45 -07:00
										 |  |  |     MimeData() = default; | 
					
						
							| 
									
										
										
										
											2023-08-21 17:06:49 +02:00
										 |  |  |     explicit MimeData(HashMap<String, ByteBuffer> const& data) | 
					
						
							| 
									
										
										
										
											2023-05-13 20:44:07 +02:00
										 |  |  |         : m_data(data.clone().release_value_but_fixme_should_propagate_errors()) | 
					
						
							| 
									
										
										
										
											2020-11-07 23:14:21 +03:30
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 17:06:49 +02:00
										 |  |  |     HashMap<String, ByteBuffer> m_data; | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-20 01:57:33 +02:00
										 |  |  | StringView guess_mime_type_based_on_filename(StringView); | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 21:39:31 +02:00
										 |  |  | Optional<StringView> guess_mime_type_based_on_sniffed_bytes(ReadonlyBytes); | 
					
						
							|  |  |  | Optional<StringView> guess_mime_type_based_on_sniffed_bytes(Core::File&); | 
					
						
							| 
									
										
										
										
											2021-05-03 21:48:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 19:00:23 -04:00
										 |  |  | struct MimeType { | 
					
						
							|  |  |  |     StringView name {}; | 
					
						
							|  |  |  |     Vector<StringView> common_extensions {}; | 
					
						
							|  |  |  |     StringView description {}; | 
					
						
							|  |  |  |     Optional<Vector<u8>> magic_bytes {}; | 
					
						
							|  |  |  |     u64 offset { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 19:03:23 -04:00
										 |  |  | Optional<StringView> get_description_from_mime_type(StringView); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  | } |