summaryrefslogtreecommitdiff
path: root/src/main_window.cpp
blob: a5543582f57abf8d08c356012488c6e60394f602 (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
306
307
308
309
310
311
312
313
#include "main_window.hpp"

#include "config.hpp"

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

namespace getsuyomi {

using fud::FudStatus;
using GetEnvResult = fud::Result<GetsuyomiConfig, FudStatus>;

constexpr const char* HOME{"HOME"};

std::optional<std::string> getEnvVar(const char* envVar);
void getEnvVar(const std::string envVar, std::string& envValue, const char* backup);
GetEnvResult getEnvironment();

GetsuyomiApp::GetsuyomiApp() : m_getsuyomi{new Getsuyomi()}
{
    readSettings();
}

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

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

    auto envResult = getEnvironment();
    if (envResult.isError()) {
        return envResult.getError();
    }

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

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

    show();

    return FudStatus::Success;
}

void GetsuyomiApp::createActions()
{
    m_openFile = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentOpen), tr("&Open File"), this);
    m_openFile->setShortcuts(QKeySequence::Open);
    m_openFile->setStatusTip(tr("Open a file"));
    connect(m_openFile, &QAction::triggered, this, &GetsuyomiApp::openFile);

    m_openDirectory = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::FolderOpen), tr("Open Directory"), this);
    m_openDirectory->setShortcut(Qt::CTRL | Qt::ALT | Qt::Key_O);
    m_openDirectory->setStatusTip(tr("Open a directory"));
    connect(m_openDirectory, &QAction::triggered, this, &GetsuyomiApp::openDirectory);

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

    auto nextShortcuts = QList<QKeySequence>{};
    nextShortcuts.append(QKeySequence{Qt::Key_L});
    nextShortcuts.append(QKeySequence{Qt::Key_Right});
    nextShortcuts.append(QKeySequence{Qt::Key_Down});
    m_nextAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::GoNext), tr("Next"), this);
    m_nextAction->setShortcuts(nextShortcuts);
    m_nextAction->setStatusTip(tr("Next"));
    connect(m_nextAction, &QAction::triggered, this, &GetsuyomiApp::next);

    auto backShortcuts = QList<QKeySequence>{};
    backShortcuts.append(QKeySequence{Qt::Key_H});
    backShortcuts.append(QKeySequence{Qt::Key_Left});
    backShortcuts.append(QKeySequence{Qt::Key_Up});
    m_backAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::GoPrevious), tr("Back"), this);
    m_backAction->setShortcuts(backShortcuts);
    m_backAction->setStatusTip(tr("Back"));
    connect(m_backAction, &QAction::triggered, this, &GetsuyomiApp::back);

    m_setSinglePageLayout = new QAction(QIcon("../resources/pageSingle.png"), tr("Single Page Layout"), this);
    m_setSinglePageLayout->setShortcut(QKeySequence{Qt::Key_S});
    m_setSinglePageLayout->setStatusTip(tr("Set Single Page Layout"));
    connect(m_setSinglePageLayout, &QAction::triggered, this, &GetsuyomiApp::setSinglePageLayout);

    m_setDualPageLayout = new QAction(QIcon("../resources/pageDual.png"), tr("Dual Page Layout"), this);
    m_setDualPageLayout->setShortcut(QKeySequence{Qt::Key_D});
    m_setDualPageLayout->setStatusTip(tr("Set Dual Page Layout"));
    connect(m_setDualPageLayout, &QAction::triggered, this, &GetsuyomiApp::setDualPageLayout);

    m_setMangaLayout = new QAction(QIcon("../resources/pageManga.png"), tr("Manga Page Layout"), this);
    m_setMangaLayout->setShortcut(QKeySequence{Qt::Key_M});
    m_setMangaLayout->setStatusTip(tr("Set Manga Page Layout"));
    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);
}

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

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

    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::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();
}

std::optional<std::string> getEnvVar(const char* envVar)
{
    const QByteArray defaultArray{};
    if (envVar == nullptr) {
        return std::nullopt;
    }
    QByteArray varArray = qgetenv(envVar);
    if (varArray == defaultArray) {
        return std::nullopt;
    }
    return varArray.toStdString();
}

void getEnvVar(const std::string envVar, std::string& envValue, const char* backup)
{
    auto envValueOpt = getEnvVar(envVar.c_str());
    if (envValueOpt == std::nullopt || envValueOpt->length() == 0) {
        std::filesystem::path envValuePath{HOME};
        envValuePath.append(backup);
        envValue = envValuePath;
    } else {
        envValue = *envValueOpt;
    }
    // qDebug("%s is %s", envVar.c_str(), envValue.c_str());
}

GetEnvResult getEnvironment()
{
    GetsuyomiConfig config{};

    auto homeOpt = getEnvVar(HOME);
    if (homeOpt == std::nullopt || homeOpt->length() == 0) {
        qCritical("Error getting home");
        return GetEnvResult::error(FudStatus::Failure);
    }
    config.home = *homeOpt;
    // qDebug("Home is %s", config.home.c_str());

    /* If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used. */
    const std::string XdgDataHome{"XDG_DATA_HOME"};
    getEnvVar(XdgDataHome, config.dataHome, ".local/share");

    /* If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used. */
    const std::string XdgConfigHome{"XDG_CONFIG_HOME"};
    getEnvVar(XdgConfigHome, config.configHome, ".config");

    /* If $XDG_STATE_HOME is either not set or empty, a default equal to $HOME/.local/state should be used. */
    const std::string XdgStateHome{"XDG_STATE_HOME"};
    getEnvVar(XdgStateHome, config.stateHome, ".local/state");

    return GetEnvResult::okay(config);
}

} // namespace getsuyomi