Unity 8
PageList.cpp
1 /*
2  * Copyright (C) 2014 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 
38 PageList::PageList(QObject *parent)
39  : QObject(parent),
40  m_index(-1),
41  m_pages()
42 {
43  QString qmlSuffix = QStringLiteral(".qml");
44  QString disabledSuffix = QStringLiteral(".disabled");
45  QSet<QString> disabledPages;
46  QStringList dataDirs;
47 
48  if (!isRunningInstalled() && getenv("WIZARD_TESTING") == nullptr) {
49  dataDirs = QStringList() << qmlDirectory();
50  } else {
51  dataDirs = shellDataDirs();
52  }
53 
54  Q_FOREACH(const QString &dataDir, dataDirs) {
55  QDir dir(dataDir + "/Wizard/Pages");
56  QStringList entries = dir.entryList(QStringList(QStringLiteral("[0-9]*")), QDir::Files | QDir::Readable);
57  Q_FOREACH(const QString &entry, entries) {
58  if (!m_pages.contains(entry) && entry.endsWith(qmlSuffix))
59  m_pages.insert(entry, dir.absoluteFilePath(entry));
60  else if (entry.endsWith(qmlSuffix + disabledSuffix))
61  disabledPages.insert(entry.left(entry.size() - disabledSuffix.size()));
62  }
63  }
64 
65  // Now remove any explicitly disabled entries
66  Q_FOREACH(const QString &page, disabledPages) {
67  m_pages.remove(page);
68  }
69 }
70 
71 QStringList PageList::entries() const
72 {
73  return m_pages.keys();
74 }
75 
76 QStringList PageList::paths() const
77 {
78  return m_pages.values();
79 }
80 
81 int PageList::index() const
82 {
83  return m_index;
84 }
85 
86 int PageList::numPages() const
87 {
88  return m_pages.size();
89 }
90 
91 QString PageList::prev()
92 {
93  if (m_index > 0)
94  return m_pages.values()[setIndex(m_index - 1)];
95  else
96  return QString();
97 }
98 
99 QString PageList::next()
100 {
101  if (m_index < m_pages.count() - 1)
102  return m_pages.values()[setIndex(m_index + 1)];
103  else
104  return QString();
105 }
106 
107 int PageList::setIndex(int index)
108 {
109  m_index = index;
110  Q_EMIT indexChanged();
111  return m_index;
112 }