aboutsummaryrefslogtreecommitdiffstats
path: root/src/mainwindow.cpp
blob: 0e63f421e50017b075c922caeeb5807881fe9a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "src/archive_parser.h"
#include "src/list_item.h"
#include "src/command_line.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QRandomGenerator>
#include <QClipboard>
#include <QMimeData>
#include <QCommandLineParser>
#include <QMessageBox>
#include <cstdlib>

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(), "<pre>" + parser.helpText() + "</pre>"); 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<StatusListItem*>(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<StatusListItem*>(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<QListWidgetItem*> 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();
}