2016-02-27 21:00:58 +01:00
|
|
|
#include "./mainwindow.h"
|
|
|
|
#include "./webpage.h"
|
|
|
|
|
2016-04-25 22:30:11 +02:00
|
|
|
#include "../lib/alpm/manager.h"
|
2016-02-28 02:33:25 +01:00
|
|
|
|
2016-02-27 21:00:58 +01:00
|
|
|
#include "resources/config.h"
|
|
|
|
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QAction>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
namespace RepoIndex {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new main window.
|
|
|
|
*/
|
2016-02-28 02:33:25 +01:00
|
|
|
MainWindow::MainWindow(const QString &webdir, Manager *manager) :
|
|
|
|
m_manager(manager),
|
|
|
|
m_contextMenu(nullptr)
|
2016-02-27 21:00:58 +01:00
|
|
|
{
|
|
|
|
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
|
|
|
|
setWindowTitle(QStringLiteral(APP_NAME));
|
|
|
|
setWindowIcon(QIcon::fromTheme(QStringLiteral(PROJECT_NAME)));
|
|
|
|
m_webView.setPage(new WebPage(&m_webView));
|
|
|
|
QUrl url(QStringLiteral("file://") % webdir % QStringLiteral("/index.html"));
|
|
|
|
url.setFragment(settings.value(QStringLiteral("fragment"), QStringLiteral("packages")).toString());
|
|
|
|
m_webView.setUrl(url);
|
|
|
|
m_webView.setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(&m_webView, &QWidget::customContextMenuRequested, this, &MainWindow::showInfoWebViewContextMenu);
|
|
|
|
setCentralWidget(&m_webView);
|
|
|
|
restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainWindow::event(QEvent *event)
|
|
|
|
{
|
|
|
|
switch(event->type()) {
|
|
|
|
case QEvent::KeyRelease: {
|
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
|
|
|
|
switch(keyEvent->key()) {
|
|
|
|
case Qt::Key_Left:
|
|
|
|
case Qt::Key_Back:
|
|
|
|
m_webView.back();
|
|
|
|
return true;
|
|
|
|
case Qt::Key_Right:
|
|
|
|
case Qt::Key_Forward:
|
|
|
|
m_webView.forward();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} case QEvent::MouseButtonPress: {
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
switch(mouseEvent->button()) {
|
|
|
|
case Qt::BackButton:
|
|
|
|
m_webView.back();
|
|
|
|
return true;
|
|
|
|
case Qt::ForwardButton:
|
|
|
|
m_webView.forward();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} case QEvent::Close: {
|
|
|
|
// save settings
|
|
|
|
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
|
|
|
|
settings.setValue(QStringLiteral("geometry"), saveGeometry());
|
|
|
|
settings.setValue(QStringLiteral("fragment"), m_webView.url().fragment());
|
|
|
|
break;
|
|
|
|
} default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return QMainWindow::event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Shows the context menu for the info web view.
|
|
|
|
*/
|
|
|
|
void MainWindow::showInfoWebViewContextMenu(const QPoint &)
|
|
|
|
{
|
2016-02-28 02:33:25 +01:00
|
|
|
if(!m_contextMenu) {
|
|
|
|
m_contextMenu = new QMenu(this);
|
|
|
|
auto *reloadAction = new QAction(QIcon::fromTheme(QStringLiteral("view-refresh")), tr("Reload UI"), m_contextMenu);
|
|
|
|
connect(reloadAction, &QAction::triggered, &m_webView, &WEB_VIEW_PROVIDER::reload);
|
|
|
|
m_copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), tr("Copy"), m_contextMenu);
|
|
|
|
connect(m_copyAction, &QAction::triggered, this, &MainWindow::copyInfoWebViewSelection);
|
|
|
|
auto *updateAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-download")), tr("Update sync-databases"), m_contextMenu);
|
|
|
|
connect(updateAction, &QAction::triggered, m_manager, &Manager::forceUpdateAlpmDatabases);
|
|
|
|
auto *wipeCacheAction = new QAction(QIcon::fromTheme(QStringLiteral("document-revert")), tr("Wipe cached packages"), m_contextMenu);
|
|
|
|
connect(wipeCacheAction, &QAction::triggered, m_manager, &Manager::wipeCache);
|
|
|
|
m_contextMenu->addAction(m_copyAction);
|
|
|
|
m_contextMenu->addAction(reloadAction);
|
|
|
|
m_contextMenu->addSeparator();
|
|
|
|
m_contextMenu->addAction(updateAction);
|
|
|
|
m_contextMenu->addAction(wipeCacheAction);
|
|
|
|
}
|
|
|
|
m_copyAction->setDisabled(m_webView.selectedText().isEmpty());
|
|
|
|
m_contextMenu->exec(QCursor::pos());
|
2016-02-27 21:00:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Copies the current selection of the info web view.
|
|
|
|
*/
|
|
|
|
void MainWindow::copyInfoWebViewSelection()
|
|
|
|
{
|
|
|
|
QGuiApplication::clipboard()->setText(m_webView.selectedText());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|