Serenity Operating System
at hosted 117 lines 3.6 kB view raw
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 27#include "BoardListModel.h" 28#include <AK/JsonArray.h> 29#include <AK/JsonObject.h> 30#include <AK/JsonValue.h> 31#include <LibCore/HttpRequest.h> 32#include <LibCore/NetworkJob.h> 33#include <LibCore/NetworkResponse.h> 34#include <stdio.h> 35 36BoardListModel::BoardListModel() 37{ 38 update(); 39} 40 41BoardListModel::~BoardListModel() 42{ 43} 44 45void BoardListModel::update() 46{ 47 Core::HttpRequest request; 48 request.set_url("http://a.4cdn.org/boards.json"); 49 50 if (m_pending_job) 51 m_pending_job->cancel(); 52 m_pending_job = request.schedule(); 53 54 m_pending_job->on_finish = [this](bool success) { 55 auto* response = m_pending_job->response(); 56 dbg() << "Board list download finished, success=" << success << ", response=" << response; 57 58 if (!success) 59 return; 60 61 dbg() << "Board list payload size: " << response->payload().size(); 62 63 auto json = JsonValue::from_string(response->payload()); 64 65 if (json.is_object()) { 66 auto new_boards = json.as_object().get("boards"); 67 if (new_boards.is_array()) 68 m_boards = move(new_boards.as_array()); 69 } 70 71 did_update(); 72 }; 73} 74 75int BoardListModel::row_count(const GUI::ModelIndex&) const 76{ 77 return m_boards.size(); 78} 79 80String BoardListModel::column_name(int column) const 81{ 82 switch (column) { 83 case Column::Board: 84 return "Board"; 85 default: 86 ASSERT_NOT_REACHED(); 87 } 88} 89 90GUI::Model::ColumnMetadata BoardListModel::column_metadata([[maybe_unused]] int column) const 91{ 92 return {}; 93} 94 95GUI::Variant BoardListModel::data(const GUI::ModelIndex& index, Role role) const 96{ 97 auto& board = m_boards.at(index.row()).as_object(); 98 if (role == Role::Display) { 99 switch (index.column()) { 100 case Column::Board: 101 return String::format("/%s/ - %s", 102 board.get("board").to_string().characters(), 103 board.get("title").to_string().characters()); 104 default: 105 ASSERT_NOT_REACHED(); 106 } 107 } 108 if (role == Role::Custom) { 109 switch (index.column()) { 110 case Column::Board: 111 return board.get("board").to_string(); 112 default: 113 ASSERT_NOT_REACHED(); 114 } 115 } 116 return {}; 117}