#include "mainwindow.h" #include "./ui_mainwindow.h" #include "src/archive_parser.h" #include "src/list_item.h" #include "src/command_line.h" #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::act_command_line(CommandLineParsedOptions &options, QCommandLineParser &parser) { switch (options.result) { case CommandLineError: QMessageBox::critical(this, tr("Command line"), tr("The passed command line is incorrect and will be ignored.\nMore information: %1").arg(options.error_message)); exit(EXIT_FAILURE); break; case CommandLineHelpRequested: QMessageBox::information(this, QGuiApplication::applicationDisplayName(), "
" + parser.helpText() + "
"); exit(EXIT_SUCCESS); break; case CommandLineVersionRequested: on_actionAbout_triggered(true); exit(EXIT_SUCCESS); break; case CommandLineOk: if (!options.outbox_filename.isEmpty()) open_file(options.outbox_filename); break; } } void MainWindow::on_actionOpen_triggered(bool checked) { QFileDialog fileDialog; fileDialog.setFileMode(QFileDialog::AnyFile); fileDialog.setNameFilter(tr("Mastodon data export directory (outbox.json)")); if (fileDialog.exec()) { QStringList files = fileDialog.selectedFiles(); open_file(files[0]); } } void MainWindow::on_actionQuit_triggered(bool checked) { QCoreApplication::quit(); } void MainWindow::on_actionAbout_triggered(bool checked) { QMessageBox::information(this, "title", "text"); } void MainWindow::on_listWidget_itemActivated(QListWidgetItem *item) { StatusListItem* status = dynamic_cast(item); if (status != nullptr) { QString status_info = status->get_info_html(ui->statusInfoText->width(), &locale_context); ui->statusInfoText->setHtml(status_info); } } void MainWindow::on_buttonRandom_clicked() { if (data_archive == nullptr) return; // No archive open, avoids crashing int index = QRandomGenerator::global()->bounded(ui->listWidget->count()); QListWidgetItem* item = ui->listWidget->item(index); on_listWidget_itemActivated(item); ui->listWidget->setCurrentItem(item); } void MainWindow::on_buttonCopy_clicked() { if (data_archive == nullptr) return; StatusListItem* item = dynamic_cast(ui->listWidget->selectedItems()[0]); if (item != nullptr) { QString status_text = data_archive->get_html_status_text(item->get_status_index()); QMimeData* clipboard_data = new QMimeData; clipboard_data->setHtml(status_text); QGuiApplication::clipboard()->setMimeData(clipboard_data); } } void MainWindow::on_buttonSearch_clicked() { static QString* last_search = new QString; static int selected_match = 0; QString current_search = ui->textInputSearch->text(); QList matches = ui->listWidget->findItems(current_search, Qt::MatchContains); if (not (*last_search == current_search)) { *last_search = current_search; selected_match = 0; } else if (++selected_match >= matches.size()) selected_match = 0; if (not matches.isEmpty()) { ui->listWidget->scrollToItem(matches[selected_match], QAbstractItemView::EnsureVisible); ui->listWidget->setCurrentItem(matches[selected_match]); on_listWidget_itemActivated(matches[selected_match]); } if (matches.size() > 0) ui->statusbar->showMessage(tr("Search result %1 out of %2") .arg(selected_match+1) .arg(matches.size())); else ui->statusbar->showMessage(tr("No search results.")); } void MainWindow::relist_statuses() { if (data_archive) { ui->listWidget->clear(); data_archive->update_status_list(view_filters, ui->listWidget); view_filters_changed = false; } } // Function used to reset filters when we have detected that the "All toots" toggle has been toggled on or shouldn't be toggled anymore void MainWindow::reset_view_filters() { if (ui->actionAll_toots->isChecked()) view_filters = {true, true, true, true, view_filters.includeReblogs, view_filters.onlyWithAttachment}; else view_filters = {false, false, false, false, view_filters.includeReblogs, view_filters.onlyWithAttachment}; } void MainWindow::on_actionAll_toots_triggered(bool checked) { ui->actionAll_toots->setChecked(true); ui->actionPublic_toots->setChecked(false); ui->actionUnlisted_toots->setChecked(false); ui->actionPrivate_toots->setChecked(false); ui->actionDirect_messages->setChecked(false); ui->actionOnly_with_attachment->setChecked(false); reset_view_filters(); relist_statuses(); } void MainWindow::on_actionPublic_toots_triggered(bool checked) { if (ui->actionAll_toots->isChecked()) { ui->actionAll_toots->setChecked(false); reset_view_filters(); } view_filters.includePublic = checked; view_filters_changed = true; } void MainWindow::on_actionUnlisted_toots_triggered(bool checked) { if (ui->actionAll_toots->isChecked()) { ui->actionAll_toots->setChecked(false); reset_view_filters(); } view_filters.includeUnlisted = checked; view_filters_changed = true; } void MainWindow::on_actionPrivate_toots_triggered(bool checked) { if (ui->actionAll_toots->isChecked()) { ui->actionAll_toots->setChecked(false); reset_view_filters(); } view_filters.includePrivate = checked; view_filters_changed = true; } void MainWindow::on_actionDirect_messages_triggered(bool checked) { if (ui->actionAll_toots->isChecked()) { ui->actionAll_toots->setChecked(false); reset_view_filters(); } view_filters.includeDirect = checked; view_filters_changed = true; } void MainWindow::on_actionOnly_with_attachment_triggered(bool checked) { view_filters.onlyWithAttachment = checked; view_filters_changed = true; } void MainWindow::on_actionReblogs_triggered(bool checked) { view_filters.includeReblogs = checked; view_filters_changed = true; } void MainWindow::on_menuView_aboutToHide() { if (view_filters_changed) relist_statuses(); } void MainWindow::open_file(const QString &filename) { if (data_archive) { delete data_archive; data_archive = nullptr; } // TODO: Do this in another thread QApplication::setOverrideCursor(Qt::WaitCursor); data_archive = new Archive(filename, ArchiveType::MASTODON); auto parse_error = data_archive->init(); switch (parse_error) { case Archive::FailedOpeningFile: QMessageBox::warning(this, tr("Archive Parser"), tr("Failed opening file %1").arg(filename)); break; case Archive::JsonParseError: QMessageBox::warning(this, tr("Archive Parser"), tr("Failed parsing outbox JSON.")); break; case Archive::JsonEmpty: case Archive::JsonNull: case Archive::JsonNotObject: case Archive::JsonNotActivityStream: QMessageBox::warning(this, tr("Archive Parser"), tr("The outbox JSON is empty or invalid in another way. Not an outbox.json?")); break; case Archive::NoError: break; } ui->listWidget->clear(); if (parse_error == Archive::NoError) { data_archive->update_status_list(view_filters, ui->listWidget); } QApplication::restoreOverrideCursor(); }