BoardListModel.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "BoardListModel.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. BoardListModel::BoardListModel()
  35. {
  36. update();
  37. }
  38. BoardListModel::~BoardListModel()
  39. {
  40. }
  41. void BoardListModel::update()
  42. {
  43. Core::HttpRequest request;
  44. request.set_url("http://a.4cdn.org/boards.json");
  45. if (m_pending_job)
  46. m_pending_job->cancel();
  47. m_pending_job = request.schedule();
  48. m_pending_job->on_finish = [this](bool success) {
  49. auto* response = m_pending_job->response();
  50. dbg() << "Board list download finished, success=" << success << ", response=" << response;
  51. if (!success)
  52. return;
  53. dbg() << "Board list payload size: " << response->payload().size();
  54. auto json = JsonValue::from_string(response->payload());
  55. if (json.is_object()) {
  56. auto new_boards = json.as_object().get("boards");
  57. if (new_boards.is_array())
  58. m_boards = move(new_boards.as_array());
  59. }
  60. did_update();
  61. };
  62. }
  63. int BoardListModel::row_count(const GUI::ModelIndex&) const
  64. {
  65. return m_boards.size();
  66. }
  67. String BoardListModel::column_name(int column) const
  68. {
  69. switch (column) {
  70. case Column::Board:
  71. return "Board";
  72. default:
  73. ASSERT_NOT_REACHED();
  74. }
  75. }
  76. GUI::Model::ColumnMetadata BoardListModel::column_metadata([[maybe_unused]] int column) const
  77. {
  78. return {};
  79. }
  80. GUI::Variant BoardListModel::data(const GUI::ModelIndex& index, Role role) const
  81. {
  82. auto& board = m_boards.at(index.row()).as_object();
  83. if (role == Role::Display) {
  84. switch (index.column()) {
  85. case Column::Board:
  86. return String::format("/%s/ - %s",
  87. board.get("board").to_string().characters(),
  88. board.get("title").to_string().characters());
  89. default:
  90. ASSERT_NOT_REACHED();
  91. }
  92. }
  93. if (role == Role::Custom) {
  94. switch (index.column()) {
  95. case Column::Board:
  96. return board.get("board").to_string();
  97. default:
  98. ASSERT_NOT_REACHED();
  99. }
  100. }
  101. return {};
  102. }