File: PageList.cpp

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (127 lines) | stat: -rw-r--r-- 3,836 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2014-2016 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
 */

/**
 * This class lets the list of wizard pages be dynamic.
 * - To add new ones, drop them into
 *   $XDG_DATA_DIRS/Wizard/Pages with a numbered prefix,
 *   like "21-custom-page.qml".  The number determines the order in the page
 *   sequence that your page will appear.
 * - To disable an existing page, add a file like "21-custom-page.qml.disabled"
 * - To go to the next page, use pageStack.next()
 * - To go back to the previous page, use pageStack.prev()
 * - To load a page outside of the normal flow (so that it doesn't affect the
 *   back button), use pageStack.push(Qt.resolvedUrl("custom-page.qml")) in
 *   your page.
 * - See default pages for plenty of examples.
 */

#include "PageList.h"
#include <paths.h>
#include <QDir>
#include <QSet>
#include <QStandardPaths>
#include <QSettings>

PageList::PageList(QObject *parent)
    : QObject(parent),
      m_index(-1),
      m_pages()
{
    const QString qmlSuffix = QStringLiteral(".qml");
    const QString disabledSuffix = QStringLiteral(".disabled");
    QSet<QString> disabledPages;
    QStringList dataDirs;

    if (!isRunningInstalled() && getenv("WIZARD_TESTING") == nullptr) {
        dataDirs << qmlDirectory();
    } else {
        dataDirs = shellDataDirs();
    }

    Q_FOREACH(const QString &dataDir, dataDirs) {
        QDir dir(dataDir + "/Wizard/Pages");
        const QStringList entries = dir.entryList(QStringList(QStringLiteral("[0-9]*")), QDir::Files | QDir::Readable);
        Q_FOREACH(const QString &entry, entries) {
            if (!m_pages.contains(entry) && entry.endsWith(qmlSuffix))
                m_pages.insert(entry, dir.absoluteFilePath(entry));
            else if (entry.endsWith(qmlSuffix + disabledSuffix))
                disabledPages.insert(entry.left(entry.size() - disabledSuffix.size()));
        }
    }

    // Now remove any explicitly disabled entries
    Q_FOREACH(const QString &page, disabledPages) {
        m_pages.remove(page);
    }

    // If there was a system update installed, skip until the last page to just greet the user
    QSettings settings;
    if (settings.value(QStringLiteral("Wizard/SkipUntilFinishedPage")).toBool()) {
        const QString lastPage = m_pages.lastKey();
        Q_FOREACH(const QString &page, m_pages.keys()) {
            if (Q_UNLIKELY(page != lastPage)) {
                m_pages.remove(page);
            }
        }

        // ... and reset it again for the next run
        settings.remove(QStringLiteral("Wizard/SkipUntilFinishedPage"));
    }
}

QStringList PageList::entries() const
{
    return m_pages.keys();
}

QStringList PageList::paths() const
{
    return m_pages.values();
}

int PageList::index() const
{
    return m_index;
}

int PageList::numPages() const
{
    return m_pages.size();
}

QString PageList::prev()
{
    if (m_index > 0)
        return m_pages.values()[setIndex(m_index - 1)];
    else
        return QString();
}

QString PageList::next()
{
    if (m_index < m_pages.count() - 1)
        return m_pages.values()[setIndex(m_index + 1)];
    else
        return QString();
}

int PageList::setIndex(int index)
{
    m_index = index;
    Q_EMIT indexChanged();
    return m_index;
}