MimeData.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include <AK/StringBuilder.h>
  27. #include <LibCore/MimeData.h>
  28. namespace Core {
  29. Vector<String> MimeData::formats() const
  30. {
  31. Vector<String> mime_types;
  32. mime_types.ensure_capacity(m_data.size());
  33. for (auto it : m_data)
  34. mime_types.unchecked_append(it.key);
  35. return mime_types;
  36. }
  37. Vector<URL> MimeData::urls() const
  38. {
  39. auto it = m_data.find("text/uri-list");
  40. if (it == m_data.end())
  41. return {};
  42. Vector<URL> urls;
  43. for (auto& line : StringView(it->value).split_view('\n')) {
  44. urls.append(URL(line));
  45. }
  46. return urls;
  47. }
  48. void MimeData::set_urls(const Vector<URL>& urls)
  49. {
  50. StringBuilder builder;
  51. for (auto& url : urls) {
  52. builder.append(url.to_string());
  53. builder.append('\n');
  54. }
  55. set_data("text/uri-list", builder.to_byte_buffer());
  56. }
  57. String MimeData::text() const
  58. {
  59. return String::copy(m_data.get("text/plain").value_or({}));
  60. }
  61. void MimeData::set_text(const String& text)
  62. {
  63. set_data("text/plain", text.to_byte_buffer());
  64. }
  65. String guess_mime_type_based_on_filename(const URL& url)
  66. {
  67. String lowercase_url = url.path().to_lowercase();
  68. if (lowercase_url.ends_with(".pbm"))
  69. return "image/x‑portable‑bitmap";
  70. if (url.path().ends_with(".pgm"))
  71. return "image/x‑portable‑graymap";
  72. if (url.path().ends_with(".png"))
  73. return "image/png";
  74. if (lowercase_url.ends_with(".ppm"))
  75. return "image/x‑portable‑pixmap";
  76. if (lowercase_url.ends_with(".gif"))
  77. return "image/gif";
  78. if (lowercase_url.ends_with(".bmp"))
  79. return "image/bmp";
  80. if (lowercase_url.ends_with(".jpg") || lowercase_url.ends_with(".jpeg"))
  81. return "image/jpeg";
  82. if (lowercase_url.ends_with(".svg"))
  83. return "image/svg+xml";
  84. if (lowercase_url.ends_with(".md"))
  85. return "text/markdown";
  86. if (lowercase_url.ends_with(".html") || lowercase_url.ends_with(".htm"))
  87. return "text/html";
  88. if (lowercase_url.ends_with("/"))
  89. return "text/html";
  90. return "text/plain";
  91. }
  92. }