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 (getenv("WIZARD_TESTING") != nullptr) {
50  dataDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
51  } else if (!isRunningInstalled()) {
52  dataDirs << qmlDirectory();
53  } else {
54  dataDirs = shellDataDirs();
55  }
56 
57  Q_FOREACH(const QString &dataDir, dataDirs) {
58  QDir dir(dataDir + "/Wizard/Pages");
59  const QStringList entries = dir.entryList(QStringList(QStringLiteral("[0-9]*")), QDir::Files | QDir::Readable);
60  Q_FOREACH(const QString &entry, entries) {
61  if (!m_pages.contains(entry) && entry.endsWith(qmlSuffix))
62  m_pages.insert(entry, dir.absoluteFilePath(entry));
63  else if (entry.endsWith(qmlSuffix + disabledSuffix))
64  disabledPages.insert(entry.left(entry.size() - disabledSuffix.size()));
65  }
66  }
67 
68  // Now remove any explicitly disabled entries
69  Q_FOREACH(const QString &page, disabledPages) {
70  m_pages.remove(page);
71  }
72 
73  // If there was a system update installed, skip until the last page to just greet the user
74  QSettings settings;
75  if (settings.value(QStringLiteral("Wizard/SkipUntilFinishedPage")).toBool()) {
76  while (m_pages.count() > 1) {
77  m_pages.erase(m_pages.begin());
78  }
79 
80  // ... and reset it again for the next run
81  settings.remove(QStringLiteral("Wizard/SkipUntilFinishedPage"));
82  }
83 }
84 
85 QStringList PageList::entries() const
86 {
87  return m_pages.keys();
88 }
89 
90 QStringList PageList::paths() const
91 {
92  return m_pages.values();
93 }
94 
95 int PageList::index() const
96 {
97  return m_index;
98 }
99 
100 int PageList::numPages() const
101 {
102  return m_pages.size();
103 }
104 
105 QString PageList::prev()
106 {
107  if (m_index > 0)
108  return m_pages.values()[setIndex(m_index - 1)];
109  else
110  return QString();
111 }
112 
113 QString PageList::next()
114 {
115  if (m_index < m_pages.count() - 1)
116  return m_pages.values()[setIndex(m_index + 1)];
117  else
118  return QString();
119 }
120 
121 int PageList::setIndex(int index)
122 {
123  m_index = index;
124  Q_EMIT indexChanged();
125  return m_index;
126 }