summaryrefslogtreecommitdiff
path: root/src/main_window.cpp
blob: 52c14eff0518acdd9243372c29300686ee00732e (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "main_window.hpp"

#include "config.hpp"
#include "settings.hpp"

#include <QString>
#include <cerrno>
#include <fcntl.h>
#include <filesystem>
#include <fud_result.hpp>
#include <fud_status.hpp>
#include <string>
#include <vector>

namespace getsuyomi {

using fud::FudStatus;

GetsuyomiApp::GetsuyomiApp() : QMainWindow(nullptr), m_getsuyomi{new Getsuyomi(this)}
{
    readSettings();
}

FudStatus GetsuyomiApp::setup()
{
    setCentralWidget(m_getsuyomi);

    QCoreApplication::setApplicationName(AppName);
    QCoreApplication::setApplicationVersion(AppVersionString);
    setWindowTitle(AppName);

    auto envResult = getUserConfig();
    if (envResult.isError()) {
        qCritical("Failure %s", fud::FudStatusToString(envResult.getError()));
        return envResult.getError();
    }
    m_config = envResult.getOkay();

    createActions();
    createMenus();
    createToolBar();

    constexpr int minimumWidth = 640;
    constexpr int minimumHeight = 480;
    setMinimumSize(minimumWidth, minimumHeight);

    /* TODO: Wire up a proper status bar */
    statusBar()->addWidget(new QLabel("Status bar"));

    show();

    return FudStatus::Success;
}

void GetsuyomiApp::createActions()
{
    m_openFile = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentOpen), tr("&Open File"), this);
    connect(m_openFile, &QAction::triggered, this, &GetsuyomiApp::openFile);

    m_openDirectory = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::FolderOpen), tr("Open &Directory"), this);
    connect(m_openDirectory, &QAction::triggered, this, &GetsuyomiApp::openDirectory);

    m_quitAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::ApplicationExit), tr("&Quit"), this);
    connect(m_quitAction, &QAction::triggered, this, &GetsuyomiApp::quit);

    m_settingsAction = new QAction(QIcon("resources/gear.svg"), tr("&Settings"), this);
    connect(m_settingsAction, &QAction::triggered, this, &GetsuyomiApp::configure);

    m_aboutApp = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::HelpAbout), tr("&About"), this);
    connect(m_aboutApp, &QAction::triggered, this, &GetsuyomiApp::aboutApp);

    m_nextAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::GoNext), tr("Next"), this);
    connect(m_nextAction, &QAction::triggered, this, &GetsuyomiApp::next);

    m_backAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::GoPrevious), tr("Back"), this);
    connect(m_backAction, &QAction::triggered, this, &GetsuyomiApp::back);

    m_setSinglePageLayout = new QAction(QIcon("../resources/pageSingle.png"), tr("Single Page Layout"), this);
    connect(m_setSinglePageLayout, &QAction::triggered, this, &GetsuyomiApp::setSinglePageLayout);

    m_setDualPageLayout = new QAction(QIcon("../resources/pageDual.png"), tr("Dual Page Layout"), this);
    connect(m_setDualPageLayout, &QAction::triggered, this, &GetsuyomiApp::setDualPageLayout);

    m_setMangaLayout = new QAction(QIcon("../resources/pageManga.png"), tr("Manga Page Layout"), this);
    connect(m_setMangaLayout, &QAction::triggered, this, &GetsuyomiApp::setMangaLayout);

    m_setPageLayoutGroup = new QActionGroup(this);
    m_setPageLayoutGroup->addAction(m_setSinglePageLayout);
    m_setPageLayoutGroup->addAction(m_setDualPageLayout);
    m_setPageLayoutGroup->addAction(m_setMangaLayout);
    m_setPageLayoutGroup->setExclusionPolicy(QActionGroup::ExclusionPolicy::Exclusive);

    bindShortcuts();
}

void GetsuyomiApp::bindShortcuts()
{
    m_openFile->setShortcuts(m_config.openFileShortcuts);
    m_openFile->setStatusTip(tr("Open a file"));

    m_openDirectory->setShortcuts(m_config.openDirectoryShortcuts);
    m_openDirectory->setStatusTip(tr("Open a directory"));

    m_quitAction->setShortcuts(m_config.quitShortcuts);
    m_quitAction->setStatusTip(tr("Quit"));

    m_settingsAction->setShortcuts(m_config.settingsShortcuts);
    m_settingsAction->setStatusTip(tr("Configure getsuyomi"));

    m_nextAction->setShortcuts(m_config.nextShortcuts);
    m_nextAction->setStatusTip(tr("Next"));

    m_backAction->setShortcuts(m_config.backShortcuts);
    m_backAction->setStatusTip(tr("Back"));

    m_setSinglePageLayout->setShortcuts(m_config.singlePageShortcuts);
    m_setSinglePageLayout->setStatusTip(tr("Set Single Page Layout"));

    m_setDualPageLayout->setShortcuts(m_config.dualPageShortcuts);
    m_setDualPageLayout->setStatusTip(tr("Set Dual Page Layout"));

    m_setMangaLayout->setShortcuts(m_config.mangaPageShortcuts);
    m_setMangaLayout->setStatusTip(tr("Set Manga Page Layout"));
}

void GetsuyomiApp::createMenus()
{
    m_fileMenu = menuBar()->addMenu(tr("&File"));
    m_fileMenu->addAction(m_openFile);
    m_fileMenu->addAction(m_openDirectory);
    m_fileMenu->addAction(m_quitAction);

    m_settingsMenu = menuBar()->addMenu(tr("&Settings"));
    m_settingsMenu->addAction(m_settingsAction);

    m_helpMenu = menuBar()->addMenu(tr("&Help"));
    m_helpMenu->addAction(m_aboutApp);
}

void GetsuyomiApp::createToolBar()
{
    m_toolBar = addToolBar(tr("&Navigation"));
    m_toolBar->setObjectName("Navigation");

    // goto first
    m_toolBar->addAction(m_backAction);
    m_toolBar->addAction(m_nextAction);
    // goto last

    m_toolBar->addSeparator();

    m_toolBar->addAction(m_setSinglePageLayout);
    m_toolBar->addAction(m_setDualPageLayout);
    m_toolBar->addAction(m_setMangaLayout);
}

void GetsuyomiApp::openFile()
{
    auto dialog = QFileDialog(
        this,
        tr("Open Archive"),
        m_lastOpenedDirectory,
        tr("Archive types (*.zip *.cbz *.cbr *.gz)"));
    dialog.setFileMode(QFileDialog::ExistingFile);
    QString filename;
    if (dialog.exec()) {
        auto filenames = dialog.selectedFiles();
        if (filenames.length() == 0) {
            qWarning("No files selected.");
            return;
        } else if (filenames.length() > 1) {
            qWarning("Too many files selected %llu.", filenames.length());
            return;
        }
        filename = filenames[0];
        m_lastOpenedDirectory = dialog.directory().absolutePath();
        qDebug("Last opened directory is %s", qPrintable(m_lastOpenedDirectory));
    } else {
        qWarning("File dialog did not execute");
        return;
    }

    if (filename.endsWith(".zip")) {
        auto* archive = new ZipArchive(filename);
        if (!archive->valid()) {
            qCritical("Failed to change archive");
        } else {
            m_getsuyomi->setArchive(archive);
        }
    } else {
        qCritical("Unsupported file extension");
    }
}

void GetsuyomiApp::openDirectory()
{
    auto dialog = QFileDialog(this, tr("Open Directory"), m_lastOpenedDirectory);
    dialog.setFileMode(QFileDialog::Directory);
    QString directoryName;
    if (dialog.exec()) {
        auto filenames = dialog.selectedFiles();
        if (filenames.length() == 0) {
            qWarning("No files selected.");
            return;
        } else if (filenames.length() > 1) {
            qWarning("Too many files selected %llu.", filenames.length());
            return;
        }
        directoryName = filenames[0];
        m_lastOpenedDirectory = dialog.directory().absolutePath();
    } else {
        qWarning("File dialog did not execute");
        return;
    }
    qDebug("Opened directory %s", qPrintable(directoryName));
}

void GetsuyomiApp::quit()
{
    QCoreApplication::quit();
}

void GetsuyomiApp::configure()
{
    Settings settings(this, m_config.shortcuts());
    if (!settings.valid()) {
        qCritical("Invalid settings");
        return;
    }
    if (settings.exec()) {
        const auto& shortcuts = settings.shortcuts();
        setUserConfig(m_config, shortcuts.shortcutMap());
        bindShortcuts();
        auto saveStatus = shortcuts.save(m_config.configFilename);
        if (saveStatus != FudStatus::Success) {
            qWarning("Failed to save shortcuts.");
        }
    }
}

void GetsuyomiApp::aboutApp()
{
    /* TODO: proper implementation */
    QMessageBox::about(this, "About Getsuyomi", "A comic book reading app.");
}

void GetsuyomiApp::next()
{
    m_getsuyomi->next();
}

void GetsuyomiApp::back()
{
    m_getsuyomi->back();
}

void GetsuyomiApp::setSinglePageLayout()
{
    m_getsuyomi->setPageLayout(PageLayout::Single);
    m_setSinglePageLayout->setEnabled(false);
    m_setDualPageLayout->setEnabled(true);
    m_setMangaLayout->setEnabled(true);
}

void GetsuyomiApp::setDualPageLayout()
{
    m_getsuyomi->setPageLayout(PageLayout::Dual);
    m_setSinglePageLayout->setEnabled(true);
    m_setDualPageLayout->setEnabled(false);
    m_setMangaLayout->setEnabled(true);
}

void GetsuyomiApp::setMangaLayout()
{
    m_getsuyomi->setPageLayout(PageLayout::Manga);
    m_setSinglePageLayout->setEnabled(true);
    m_setDualPageLayout->setEnabled(true);
    m_setMangaLayout->setEnabled(false);
}

void GetsuyomiApp::closeEvent(QCloseEvent* event)
{
    writeSettings();
    QMainWindow::closeEvent(event);
}

void GetsuyomiApp::readSettings()
{

    QSettings settings{AppVendor, AppName};
    restoreGeometry(settings.value("geometry").toByteArray());
    restoreState(settings.value("windowState").toByteArray());
    m_lastOpenedDirectory = settings.value("lastOpenedDirectory", QDir::homePath()).toString();
}

void GetsuyomiApp::writeSettings()
{
    QSettings settings{AppVendor, AppName};
    settings.setValue("geometry", saveGeometry());
    settings.setValue("windowState", saveState());
    settings.setValue("lastOpenedDirectory", m_lastOpenedDirectory);
    settings.sync();
}

} // namespace getsuyomi