ThreadCatalogModel.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2018-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 "ThreadCatalogModel.h"
  27. #include <AK/JsonArray.h>
  28. #include <AK/JsonObject.h>
  29. #include <AK/JsonValue.h>
  30. #include <LibCore/HttpRequest.h>
  31. #include <LibCore/NetworkJob.h>
  32. #include <LibCore/NetworkResponse.h>
  33. #include <stdio.h>
  34. ThreadCatalogModel::ThreadCatalogModel()
  35. {
  36. update();
  37. }
  38. ThreadCatalogModel::~ThreadCatalogModel()
  39. {
  40. }
  41. void ThreadCatalogModel::set_board(const String& board)
  42. {
  43. if (m_board == board)
  44. return;
  45. m_board = board;
  46. update();
  47. }
  48. void ThreadCatalogModel::update()
  49. {
  50. Core::HttpRequest request;
  51. request.set_url(String::format("http://a.4cdn.org/%s/catalog.json", m_board.characters()));
  52. if (m_pending_job)
  53. m_pending_job->cancel();
  54. m_pending_job = request.schedule();
  55. if (on_load_started)
  56. on_load_started();
  57. m_pending_job->on_finish = [this](bool success) {
  58. auto* response = m_pending_job->response();
  59. dbg() << "Catalog download finished, success=" << success << ", response=" << response;
  60. if (!success) {
  61. if (on_load_finished)
  62. on_load_finished(false);
  63. return;
  64. }
  65. dbg() << "Catalog payload size: " << response->payload().size();
  66. auto json = JsonValue::from_string(response->payload());
  67. if (json.is_array()) {
  68. JsonArray new_catalog;
  69. for (auto& page : json.as_array().values()) {
  70. if (!page.is_object())
  71. continue;
  72. auto threads_value = page.as_object().get("threads");
  73. if (!threads_value.is_array())
  74. continue;
  75. for (auto& thread : threads_value.as_array().values()) {
  76. new_catalog.append(thread);
  77. }
  78. }
  79. m_catalog = move(new_catalog);
  80. }
  81. did_update();
  82. if (on_load_finished)
  83. on_load_finished(true);
  84. };
  85. }
  86. int ThreadCatalogModel::row_count(const GUI::ModelIndex&) const
  87. {
  88. return m_catalog.size();
  89. }
  90. String ThreadCatalogModel::column_name(int column) const
  91. {
  92. switch (column) {
  93. case Column::ThreadNumber:
  94. return "#";
  95. case Column::Subject:
  96. return "Subject";
  97. case Column::Text:
  98. return "Text";
  99. case Column::ReplyCount:
  100. return "Replies";
  101. case Column::ImageCount:
  102. return "Images";
  103. case Column::PostTime:
  104. return "Time";
  105. default:
  106. ASSERT_NOT_REACHED();
  107. }
  108. }
  109. GUI::Model::ColumnMetadata ThreadCatalogModel::column_metadata(int column) const
  110. {
  111. switch (column) {
  112. case Column::ThreadNumber:
  113. return { 70, Gfx::TextAlignment::CenterRight };
  114. case Column::Subject:
  115. return { 170, Gfx::TextAlignment::CenterLeft };
  116. case Column::Text:
  117. return { 270, Gfx::TextAlignment::CenterLeft };
  118. case Column::ReplyCount:
  119. return { 45, Gfx::TextAlignment::CenterRight };
  120. case Column::ImageCount:
  121. return { 40, Gfx::TextAlignment::CenterRight };
  122. case Column::PostTime:
  123. return { 120, Gfx::TextAlignment::CenterLeft };
  124. default:
  125. ASSERT_NOT_REACHED();
  126. }
  127. }
  128. GUI::Variant ThreadCatalogModel::data(const GUI::ModelIndex& index, Role role) const
  129. {
  130. auto& thread = m_catalog.at(index.row()).as_object();
  131. if (role == Role::Display) {
  132. switch (index.column()) {
  133. case Column::ThreadNumber:
  134. return thread.get("no").to_u32();
  135. case Column::Subject:
  136. return thread.get("sub").as_string_or({});
  137. case Column::Text:
  138. return thread.get("com").as_string_or({});
  139. case Column::ReplyCount:
  140. return thread.get("replies").to_u32();
  141. case Column::ImageCount:
  142. return thread.get("images").to_u32();
  143. case Column::PostTime:
  144. return thread.get("now").to_string();
  145. default:
  146. ASSERT_NOT_REACHED();
  147. }
  148. }
  149. return {};
  150. }