MimeData.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. }