Unity 8
appdrawermodel.cpp
1 /*
2  * Copyright (C) 2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "appdrawermodel.h"
18 #include "ualwrapper.h"
19 
20 #include <QDebug>
21 #include <QDateTime>
22 
23 AppDrawerModel::AppDrawerModel(QObject *parent):
24  AppDrawerModelInterface(parent)
25 {
26  Q_FOREACH (const QString &appId, UalWrapper::installedApps()) {
27  UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
28  if (!info.valid) {
29  qWarning() << "Failed to get app info for app" << appId;
30  continue;
31  }
32  m_list.append(new LauncherItem(appId, info.name, info.icon, this));
33  m_list.last()->setKeywords(info.keywords);
34  }
35  qsrand(QDateTime::currentMSecsSinceEpoch() / 100);
36 }
37 
38 int AppDrawerModel::rowCount(const QModelIndex &parent) const
39 {
40  Q_UNUSED(parent)
41  return m_list.count();
42 }
43 
44 QVariant AppDrawerModel::data(const QModelIndex &index, int role) const
45 {
46  switch (role) {
47  case RoleAppId:
48  return m_list.at(index.row())->appId();
49  case RoleName:
50  return m_list.at(index.row())->name();
51  case RoleIcon:
52  return m_list.at(index.row())->icon();
53  case RoleKeywords:
54  return m_list.at(index.row())->keywords();
55  case RoleUsage:
56  // FIXME: u-a-l needs to provide API for usage stats.
57  // don't forget to drop the qsrand() call in the ctor when dropping this.
58  return qrand();
59  }
60 
61  return QVariant();
62 }