File: tabsmodel.cpp

package info (click to toggle)
angelfish 25.08.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,720 kB
  • sloc: cpp: 2,751; xml: 607; javascript: 273; makefile: 16; sh: 11; sql: 7
file content (482 lines) | stat: -rw-r--r-- 12,267 bytes parent folder | download
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 *  SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
 *
 *  SPDX-License-Identifier: LGPL-2.0-only
 */

#include "tabsmodel.h"

#include <QDebug>
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardPaths>
#include <QUrl>

#include <ranges>

#include "angelfishsettings.h"
#include "browsermanager.h"

namespace ranges = std::ranges;

TabsModel::TabsModel(QObject *parent)
    : QAbstractListModel(parent)
{
    connect(this, &TabsModel::currentTabChanged, [this] {
        qDebug() << "Current tab changed to" << m_currentTab;
    });

    // The fallback tab must not be saved, it would overwrite our actual data.
    m_tabsReadOnly = true;
    // Make sure model always contains at least one tab
    createEmptyTab();

    // Only load tabs after private mode is known
    connect(this, &TabsModel::privateModeChanged, [this] {
        loadInitialTabs();
    });
}

QHash<int, QByteArray> TabsModel::roleNames() const
{
    return {
        {RoleNames::UrlRole, QByteArrayLiteral("pageurl")},
        {RoleNames::IsMobileRole, QByteArrayLiteral("isMobile")},
        {RoleNames::IsDeveloperToolsOpen, QByteArrayLiteral("isDeveloperToolsOpen")},
    };
}

QVariant TabsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() < 0 || size_t(index.row()) >= m_tabs.size()) {
        return {};
    }

    switch (role) {
    case RoleNames::UrlRole:
        return m_tabs.at(index.row()).url();
    case RoleNames::IsMobileRole:
        return m_tabs.at(index.row()).isMobile();
    case RoleNames::IsDeveloperToolsOpen:
        return m_tabs.at(index.row()).isDeveloperToolsOpen();
    }

    return {};
}

int TabsModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : int(m_tabs.size());
}

/**
 * @brief TabsModel::tab returns the tab at the given index
 * @param index
 * @return tab at the index
 */
TabState TabsModel::tab(int index)
{
    if (index < 0 || size_t(index) >= m_tabs.size())
        return {}; // index out of bounds

    return m_tabs.at(index);
}

/**
 * @brief TabsModel::loadInitialTabs sets up the tabs that should already be open when starting the browser
 * This includes the configured homepage, an url passed on the command line (usually by another app) and tabs
 * which were still open when the browser was last closed.
 *
 * @warning It is impossible to save any new tabs until this function was called.
 */
void TabsModel::loadInitialTabs()
{
    if (m_initialTabsLoaded) {
        return;
    }

    if (!m_privateMode) {
        loadTabs();
    }

    m_tabsReadOnly = false;

    if (!m_privateMode) {
        if (BrowserManager::instance()->initialUrl().isEmpty()) {
            if (m_tabs.front().url() == QUrl(QStringLiteral("about:blank")))
                setUrl(0, AngelfishSettings::self()->homepage());
        } else {
            if (m_tabs.front().url() == QUrl(QStringLiteral("about:blank")))
                setUrl(0, BrowserManager::instance()->initialUrl());
            else
                newTab(BrowserManager::instance()->initialUrl());
        }
    }

    m_initialTabsLoaded = true;
}

/**
 * @brief TabsModel::currentTab returns the index of the tab that is currently visible to the user
 * @return index
 */
int TabsModel::currentTab() const
{
    return m_currentTab;
}

/**
 * @brief TabsModel::setCurrentTab sets the tab that is currently visible to the user
 * @param index
 */
void TabsModel::setCurrentTab(int index)
{
    if (index < 0 || size_t(index) >= m_tabs.size())
        return;

    m_currentTab = index;
    Q_EMIT currentTabChanged();
    saveTabs();
}

void TabsModel::setLatestTab()
{
    m_currentTab = m_tabs.size() - 1;
    Q_EMIT currentTabChanged();
    saveTabs();
}

const std::vector<TabState> &TabsModel::tabs() const
{
    return m_tabs;
}

/**
 * @brief TabsModel::loadTabs restores tabs saved in tabs.json
 * @return whether any tabs were restored
 */
bool TabsModel::loadTabs()
{
    if (!m_privateMode) {
        beginResetModel();
        const QString input = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/angelfish/tabs.json");

        QFile inputFile(input);
        if (!inputFile.exists()) {
            return false;
        }

        if (!inputFile.open(QIODevice::ReadOnly)) {
            qDebug() << "Failed to load tabs from disk";
        }

        const auto tabsStorage = QJsonDocument::fromJson(inputFile.readAll()).object();
        m_tabs.clear();

        const auto tabs = tabsStorage.value(QLatin1String("tabs")).toArray();

        ranges::transform(tabs, std::back_inserter(m_tabs), [](const QJsonValue &tab) {
            return TabState::fromJson(tab.toObject());
        });

        qDebug() << "loaded from file:" << m_tabs.size() << input;

        m_currentTab = tabsStorage.value(QLatin1String("currentTab")).toInt();

        // Make sure model always contains at least one tab
        if (m_tabs.size() == 0) {
            createEmptyTab();
        }

        endResetModel();
        Q_EMIT currentTabChanged();

        return true;
    }
    return false;
}

/**
 * @brief TabsModel::saveTabs saves the current state of the model to disk
 * @return whether the tabs could be saved
 */
bool TabsModel::saveTabs() const
{
    // only save if not in private mode
    if (!m_privateMode && !m_tabsReadOnly) {
        const QString outputDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/angelfish/");

        QFile outputFile(outputDir + QStringLiteral("tabs.json"));
        if (!QDir(outputDir).mkpath(QStringLiteral("."))) {
            qDebug() << "Destdir doesn't exist and I can't create it: " << outputDir;
            return false;
        }
        if (!outputFile.open(QIODevice::WriteOnly)) {
            qDebug() << "Failed to write tabs to disk";
        }

        QJsonArray tabsArray;
        ranges::transform(m_tabs, std::back_inserter(tabsArray), [](const TabState &tab) {
            return tab.toJson();
        });

        qDebug() << "Wrote to file" << outputFile.fileName() << "(" << tabsArray.count() << "urls"
                 << ")";

        const QJsonDocument document({
            {QLatin1String("tabs"), tabsArray},
            {QLatin1String("currentTab"), m_currentTab},
        });

        outputFile.write(document.toJson());
        return true;
    }
    return false;
}

bool TabsModel::isMobileDefault() const
{
    return m_isMobileDefault;
}

void TabsModel::setIsMobileDefault(bool def)
{
    if (m_isMobileDefault != def) {
        m_isMobileDefault = def;
        Q_EMIT isMobileDefaultChanged();

        // used in initialization of the tab
        if (m_tabs.size() == 1) {
            setIsMobile(0, def);
        }
    }
}

bool TabsModel::privateMode() const
{
    return m_privateMode;
}

void TabsModel::setPrivateMode(bool privateMode)
{
    m_privateMode = privateMode;
    Q_EMIT privateModeChanged();
}

/**
 * @brief TabsModel::createEmptyTab convinience function for opening a tab containing "about:blank"
 */
void TabsModel::createEmptyTab()
{
    newTab(QUrl(QStringLiteral("about:blank")));
};

/**
 * @brief TabsModel::newTab
 * @param url
 * @param isMobile
 */
void TabsModel::newTab(const QUrl &url)
{
    beginInsertRows({}, m_tabs.size(), m_tabs.size());

    m_tabs.push_back(TabState(url, m_isMobileDefault));

    endInsertRows();

    // Switch to last tab
    if (AngelfishSettings::self()->switchToNewTab()) {
        m_currentTab = m_tabs.size() - 1;
        Q_EMIT currentTabChanged();
    }
    saveTabs();
}

/**
 * @brief TabsModel::closeTab removes the tab at the index, handles moving the tabs after it and sets a new currentTab
 * @param index
 */
void TabsModel::closeTab(int index)
{
    if (index < 0 || size_t(index) >= m_tabs.size())
        return; // index out of bounds

    if (m_tabs.size() <= 1) {
        // if not in mobile, close application
        if (!SettingsHelper::isMobile()) {
            QCoreApplication::quit();
        }

        // create new tab before removing the last one
        // to avoid linking all signals to null object
        createEmptyTab();

        // now we have (tab_to_remove, "about:blank)

        // 0 will be the correct current tab index after tab_to_remove is gone
        m_currentTab = 0;

        // index to remove
        index = 0;
    }

    if (m_currentTab > index) {
        // decrease index if it's after the removed tab
        m_currentTab--;
    }

    if (m_currentTab == index) {
        // handle the removal of current tab
        // Just reset to first tab
        if (index != 0) {
            m_currentTab = index - 1;
        } else {
            m_currentTab = 0;
        }
    }

    m_closedTabs.push_back(m_tabs[index]);
    if (m_closedTabs.size() > 20)
        m_closedTabs.pop_front();

    beginRemoveRows({}, index, index);
    m_tabs.erase(m_tabs.begin() + index);
    endRemoveRows();

    Q_EMIT currentTabChanged();
    saveTabs();
}

void TabsModel::reopenTab()
{
    if (!m_closedTabs.empty()) {
        beginInsertRows({}, m_tabs.size(), m_tabs.size());

        m_tabs.push_back(m_closedTabs.back());
        m_closedTabs.pop_back();

        endInsertRows();

        // Switch to last tab
        if (AngelfishSettings::self()->switchToNewTab()) {
            m_currentTab = m_tabs.size() - 1;
            Q_EMIT currentTabChanged();
        }
        saveTabs();
    }
}

void TabsModel::setIsMobile(int index, bool isMobile)
{
    qDebug() << "Setting isMobile:" << index << isMobile << "tabs open" << m_tabs.size();
    if (index < 0 || size_t(index) >= m_tabs.size())
        return; // index out of bounds

    m_tabs[index].setIsMobile(isMobile);

    const QModelIndex mindex = createIndex(index, index);
    Q_EMIT dataChanged(mindex, mindex, {RoleNames::IsMobileRole});
    saveTabs();
}

void TabsModel::toggleDeveloperTools(int index)
{
    if (index < 0 || size_t(index) >= m_tabs.size())
        return; // index out of bounds

    auto &tab = m_tabs[index];
    tab.setIsDeveloperToolsOpen(!tab.isDeveloperToolsOpen());

    const QModelIndex mindex = createIndex(index, index);
    Q_EMIT dataChanged(mindex, mindex, {RoleNames::IsDeveloperToolsOpen});
    saveTabs();
}

bool TabsModel::isDeveloperToolsOpen(int index)
{
    if (index < 0 || size_t(index) >= m_tabs.size())
        return false;

    return m_tabs.at(index).isDeveloperToolsOpen();
}

void TabsModel::setUrl(int index, const QUrl &url)
{
    qDebug() << "Setting URL:" << index << url << "tabs open" << m_tabs.size();
    if (index < 0 || size_t(index) >= m_tabs.size())
        return; // index out of bounds

    m_tabs[index].setUrl(url);

    const QModelIndex mindex = createIndex(index, index);
    Q_EMIT dataChanged(mindex, mindex, {RoleNames::UrlRole});
    saveTabs();
}

QUrl TabState::url() const
{
    return m_url;
}

void TabState::setUrl(const QUrl &url)
{
    m_url = url;
}

bool TabState::isMobile() const
{
    return m_isMobile;
}

void TabState::setIsMobile(bool isMobile)
{
    m_isMobile = isMobile;
}

bool TabState::isDeveloperToolsOpen() const
{
    return m_isDeveloperToolsOpen;
}

void TabState::setIsDeveloperToolsOpen(bool isDeveloperToolsOpen)
{
    m_isDeveloperToolsOpen = isDeveloperToolsOpen;
}

TabState TabState::fromJson(const QJsonObject &obj)
{
    TabState tab;
    tab.setUrl(QUrl(obj.value(QStringLiteral("url")).toString()));
    tab.setIsMobile(obj.value(QStringLiteral("isMobile")).toBool());
    tab.setIsDeveloperToolsOpen(obj.value(QStringLiteral("isDeveloperToolsOpen")).toBool());
    return tab;
}

TabState::TabState(const QUrl &url, const bool isMobile)
{
    setIsMobile(isMobile);
    setUrl(url);
}

bool TabState::operator==(const TabState &other) const
{
    return (
        m_url == other.url() &&
        m_isMobile == other.isMobile() &&
        m_isDeveloperToolsOpen == other.isDeveloperToolsOpen()
    );
}

QJsonObject TabState::toJson() const
{
    return {
        {QStringLiteral("url"), m_url.toString()},
        {QStringLiteral("isMobile"), m_isMobile},
        {QStringLiteral("isDeveloperToolsOpen"), m_isDeveloperToolsOpen},
    };
}

#include "moc_tabsmodel.cpp"