Unity 8
PageList.cpp
1 /*
2  * Copyright (C) 2014-2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE. See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
32 #include "PageList.h"
33 #include <paths.h>
34 #include <QDir>
35 #include <QSet>
36 #include <QStandardPaths>
37 #include <QSettings>
38 
39 PageList::PageList(QObject *parent)
40  : QObject(parent),
41  m_index(-1),
42  m_pages()
43 {
44  const QString qmlSuffix = QStringLiteral(".qml");
45  const QString disabledSuffix = QStringLiteral(".disabled");
46  QSet<QString> disabledPages;
47  QStringList dataDirs;
48 
49  if (!isRunningInstalled() && getenv("WIZARD_TESTING") == nullptr) {
50  dataDirs << qmlDirectory();
51  } else {
52  dataDirs = shellDataDirs();
53  }
54 
55  Q_FOREACH(const QString &dataDir, dataDirs) {
56  QDir dir(dataDir + "/Wizard/Pages");
57  const QStringList entries = dir.entryList(QStringList(QStringLiteral("[0-9]*")), QDir::Files | QDir::Readable);
58  Q_FOREACH(const QString &entry, entries) {
59  if (!m_pages.contains(entry) && entry.endsWith(qmlSuffix))
60  m_pages.insert(entry, dir.absoluteFilePath(entry));
61  else if (entry.endsWith(qmlSuffix + disabledSuffix))
62  disabledPages.insert(entry.left(entry.size() - disabledSuffix.size()));
63  }
64  }
65 
66  // Now remove any explicitly disabled entries
67  Q_FOREACH(const QString &page, disabledPages) {
68  m_pages.remove(page);
69  }
70 
71  // If there was a system update installed, skip until the last page to just greet the user
72  QSettings settings;
73  if (settings.value(QStringLiteral("Wizard/SkipUntilFinishedPage")).toBool()) {
74  const QString lastPage = m_pages.lastKey();
75  Q_FOREACH(const QString &page, m_pages.keys()) {
76  if (Q_UNLIKELY(page != lastPage)) {
77  m_pages.remove(page);
78  }
79  }
80 
81  // ... and reset it again for the next run
82  settings.remove(QStringLiteral("Wizard/SkipUntilFinishedPage"));
83  }
84 }
85 
86 QStringList PageList::entries() const
87 {
88  return m_pages.keys();
89 }
90 
91 QStringList PageList::paths() const
92 {
93  return m_pages.values();
94 }
95 
96 int PageList::index() const
97 {
98  return m_index;
99 }
100 
101 int PageList::numPages() const
102 {
103  return m_pages.size();
104 }
105 
106 QString PageList::prev()
107 {
108  if (m_index > 0)
109  return m_pages.values()[setIndex(m_index - 1)];
110  else
111  return QString();
112 }
113 
114 QString PageList::next()
115 {
116  if (m_index < m_pages.count() - 1)
117  return m_pages.values()[setIndex(m_index + 1)];
118  else
119  return QString();
120 }
121 
122 int PageList::setIndex(int index)
123 {
124  m_index = index;
125  Q_EMIT indexChanged();
126  return m_index;
127 }