aboutsummaryrefslogtreecommitdiff
path: root/src/browser.cpp
blob: a1c097af08d289de9b94aa7a71d6c45d96260b80 (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
/** LICENSE ********************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include "browser.h"
#include "mainwindow.h"
#include "settings.h"
#include <QtWebEngine>
#include <QMessageBox>

Browser::Browser(int &argc, char *argv[]) :
    QApplication(argc, argv)
{
    setApplicationName("smolbote");

    // This lets the web view automatically scale on high-dpi displays.
    setAttribute(Qt::AA_EnableHighDpiScaling);
}

Browser::~Browser()
{
    qDeleteAll(m_windows);
    m_windows.clear();

    delete m_bookmarksManager;
    delete m_downloadManager;
}

/*!
 * Check if the settings are empty
 */
void Browser::firstRun()
{
    Settings settings;
    if(settings.allKeys().isEmpty()) {
        // There are no keys in the settings
        QMessageBox::information(0,
                                 tr("Configuration is empty"),
                                 tr("The configuration file <i>%1</i> is empty. Using default values").arg(settings.staticFilePath()));
    }
}

/*!
 * Anything that needs to run after the QCommandLineParser but before showing a main window
 */
bool Browser::preLaunch(QStringList urls)
{
    Settings settings;

    if(settings.value("browser/singleInstance", true).toBool()) {
        QString serverName = settings.value("browser/localSocket", "smolbote-singlelock").toString();

        // Check for other running instance
        QLocalSocket socket;
        socket.connectToServer(serverName);
        if(socket.waitForConnected(500)) {
            QTextStream stream(&socket);
            stream << urls.join('|');
            stream.flush();
            socket.waitForBytesWritten();
            return false;
        }

        // There is no other instance
        m_localServer = new QLocalServer(this);
        connect(m_localServer, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
        if (!m_localServer->listen(serverName) && m_localServer->serverError() == QAbstractSocket::AddressInUseError) {
            // Could not create local server because the socket is already in use
            QLocalServer::removeServer(serverName);
            if (!m_localServer->listen(serverName)) {
                // Couldn't free the socket
                qWarning("Could not create local socket %s.", qPrintable(serverName));
            }
        } else {
            qDebug("Created local socket.");
        }
    }

    m_bookmarksManager = new BookmarksDialog;
    m_downloadManager = new DownloadDialog;

    QtWebEngine::initialize();

    return true;
}

Browser *Browser::instance()
{
    return static_cast<Browser *>(QCoreApplication::instance());
}

BookmarksDialog *Browser::bookmarks()
{
    return m_bookmarksManager;
}

DownloadDialog *Browser::downloads()
{
    return m_downloadManager;
}

void Browser::setConfigPath(const QString &path)
{
    if(path.isEmpty()) {
        // set default config path
        Settings::setFilePath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/config.ini");
    } else {
        // set custom config path
        Settings::setFilePath(path);
    }
}

void Browser::addWindow(MainWindow *window)
{
    if(m_windows.contains(window)) {
        return;
    }

    m_windows.append(window);
    connect(window, &QObject::destroyed, [this, window]() {
        this->removeWindow(window);
    });

    window->show();
}

MainWindow *Browser::mainWindow()
{
    if(m_windows.isEmpty()) {
        addWindow(new MainWindow());
    }
    return m_windows.first();
}

void Browser::removeWindow(MainWindow *window)
{
    m_windows.removeOne(window);
}

void Browser::handleNewConnection()
{
    QLocalSocket *socket = m_localServer->nextPendingConnection();
    if(!socket) {
        // null socket -> return
        return;
    }

    socket->waitForReadyRead();
    QStringList urls = QString(socket->readAll()).split('|');
    delete socket;

    for(QString s : urls) {
        mainWindow()->addNewTab(QUrl::fromUserInput(s));
    }
}