aboutsummaryrefslogtreecommitdiff
path: root/src/applicationmenu.cpp
blob: 112e790a85be2b2b32280bdc7de8bf1d356fb5ac (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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/cgit/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "applicationmenu.h"
#include "aboutdialog.h"
#include "browser.h"
#include "configuration.h"
#include "session/savesessiondialog.h"
#include "session/sessiondialog.h"
#include "smolbote/plugininterface.hpp"
#include <QPluginLoader>
#include <spdlog/spdlog.h>

ApplicationMenu::ApplicationMenu(Browser *app, QWidget *parent)
    : QMenu(parent)
{
    m_app = app;
    setTitle(qApp->applicationName());
    Configuration conf;

    const auto sessionPath = conf.value<QString>("session.path").value();
    auto *actionSaveSession = addAction(tr("Save Session"), this, [sessionPath]() {
        auto *sessionDialog = new SaveSessionDialog(nullptr);
        if(sessionDialog->exec() == QDialog::Accepted) {
            sessionDialog->save(sessionPath);
        }
    });
    conf.shortcut<QAction>(*actionSaveSession, "shortcuts.session.save");

    auto *actionOpenSession = addAction(tr("Open Session"), this, []() {
        auto *sessionDialog = new SessionDialog(nullptr);
        sessionDialog->exec();
    });
    conf.shortcut<QAction>(*actionOpenSession, "shortcuts.session.open");

    bottom_pluginSeparator = addSeparator();

    auto *actionAbout = addAction(tr("About"), app, []() {
        auto *dlg = new AboutDialog(qApp->activeWindow());
        dlg->show();
    });
    conf.shortcut<QAction>(*actionAbout, "shortcuts.window.about");

    auto *action_aboutPlugins = addAction(tr("About Plugins"), app, &Browser::aboutPlugins);
    conf.shortcut(*action_aboutPlugins, "app.shortcuts.about.plugins");

    auto *actionQuit = addAction(tr("Quit"), app, &Browser::quit);
    conf.shortcut<QAction>(*actionQuit, "shortcuts.window.quit");
}

void ApplicationMenu::addPlugin(QPluginLoader *plugin)
{
    if(top_pluginSeparator == nullptr) {
        top_pluginSeparator = insertSeparator(bottom_pluginSeparator);
    }

    const auto metadata = plugin->metaData().value("MetaData").toObject();
    auto *action = addAction(metadata.value("name").toString());
    action->setShortcut(QKeySequence::fromString(metadata.value("shortcut").toString()));

    connect(action, &QAction::triggered, [this, plugin]() {
        if(plugin->isLoaded()) {
            const auto *interface = qobject_cast<PluginInterface *>(plugin->instance());
            auto *dlg = interface->createWidget(this->parentWidget());

            if(auto *profileDlg = dynamic_cast<ProfileDialog*>(dlg)) {
                WebProfileManager mgr;
                mgr.walk([profileDlg](const QString &id, WebProfile *profile, QSettings *settings) {
                    profileDlg->addProfile(id, profile->name(), profile, settings);
                });
            }

            dlg->exec();
        } else {
            spdlog::warn("Trying to run unloaded plugin!");
        }
    });
    insertAction(bottom_pluginSeparator, action);
}