on xenial help sign in
adjust model to follow fixes in unity-api
commit18dce5952433b32060f9647f9abd20e62ceeeb26 authorMichael Zanetti <michael.zanetti@canonical.com> dateMon 8 Jul 2013, 12:47 (13 years ago) parent513acf7
8 files changed+92−52
plugins/Unity/Launcher/launcheritem.cpp+1−1 plugins/Unity/Launcher/launcheritem.h+1−3 plugins/Unity/Launcher/launchermodel.cpp+33−17 plugins/Unity/Launcher/launchermodel.h+4−5 tests/mocks/Unity/Launcher/MockLauncherItem.cpp+7−1 tests/mocks/Unity/Launcher/MockLauncherItem.h+3−2 tests/mocks/Unity/Launcher/MockLauncherModel.cpp+37−20 tests/mocks/Unity/Launcher/MockLauncherModel.h+6−3
plugins/Unity/Launcher/launcheritem.cpp+1−1
@@ -36,7 +36,7 @@ LauncherItem::LauncherItem(const QString &appId, const QString &desktopFile, con
3636
3737}
3838
39−QString LauncherItem::appId()
39+QString LauncherItem::appId() const
4040{
4141 return m_appId;
4242}
plugins/Unity/Launcher/launcheritem.h+1−3
@@ -34,10 +34,8 @@ class LauncherItem: public LauncherItemInterface
3434public:
3535 LauncherItem(const QString &appId, const QString &desktopFile, const QString &name, const QString &icon, QObject *parent = 0);
3636
37− QString appId();
38−
37+ QString appId() const;
3938 QString desktopFile() const;
40−
4139 QString name() const;
4240 QString icon() const;
4341
plugins/Unity/Launcher/launchermodel.cpp+33−17
@@ -57,6 +57,8 @@ QVariant LauncherModel::data(const QModelIndex &index, int role) const
5757{
5858 LauncherItem *item = m_list.at(index.row());
5959 switch(role) {
60+ case RoleAppId:
61+ return item->desktopFile();
6062 case RoleDesktopFile:
6163 return item->desktopFile();
6264 case RoleName:
@@ -87,11 +89,16 @@ void LauncherModel::move(int oldIndex, int newIndex)
8789
8890 storeAppList();
8991
90− pin(newIndex);
92+ pin(m_list.at(newIndex)->appId());
9193}
9294
93−void LauncherModel::pin(int index)
95+void LauncherModel::pin(const QString &appId)
9496{
97+ int index = findApplication(appId);
98+ if (index < 0) {
99+ return;
100+ }
101+
95102 LauncherItem *item = m_list.at(index);
96103 if (!item->pinned()) {
97104
@@ -107,8 +114,13 @@ void LauncherModel::pin(int index)
107114 }
108115}
109116
110−void LauncherModel::remove(int index)
117+void LauncherModel::requestRemove(const QString &appId)
111118{
119+ int index = findApplication(appId);
120+ if (index < 0) {
121+ return;
122+ }
123+
112124 beginRemoveRows(QModelIndex(), index, index);
113125 m_list.takeAt(index)->deleteLater();
114126 endRemoveRows();
@@ -116,27 +128,20 @@ void LauncherModel::remove(int index)
116128 storeAppList();
117129}
118130
119−void LauncherModel::triggerQuickListAction(int itemIndex, int quickListIndex)
131+void LauncherModel::triggerQuickListAction(const QString &appId, int quickListIndex)
120132{
121− QString appId = m_list.at(itemIndex)->appId();
122− QuickListModel *model = qobject_cast<QuickListModel*>(m_list.at(itemIndex)->quickList());
133+ int index = findApplication(appId);
134+ if (index < 0) {
135+ return;
136+ }
137+
138+ QuickListModel *model = qobject_cast<QuickListModel*>(m_list.at(index)->quickList());
123139 if (model) {
124140 QString actionId = model->get(quickListIndex).actionId();
125141 m_backend->triggerQuickListAction(appId, actionId);
126142 }
127143}
128144
129−QHash<int, QByteArray> LauncherModel::roleNames() const
130−{
131− QHash<int, QByteArray> roles;
132− roles.insert(RoleDesktopFile, "desktopFile");
133− roles.insert(RoleName, "name");
134− roles.insert(RoleIcon, "icon");
135− roles.insert(RolePinned, "pinned");
136− roles.insert(RoleRunning, "runnng");
137− return roles;
138−}
139−
140145void LauncherModel::storeAppList()
141146{
142147 QStringList appIds;
@@ -147,3 +152,14 @@ void LauncherModel::storeAppList()
147152 }
148153 m_backend->setStoredApplications(appIds);
149154}
155+
156+int LauncherModel::findApplication(const QString &appId)
157+{
158+ for (int i = 0; i < m_list.count(); ++i) {
159+ LauncherItem *item = m_list.at(i);
160+ if (item->appId() == appId) {
161+ return i;
162+ }
163+ }
164+ return -1;
165+}
plugins/Unity/Launcher/launchermodel.h+4−5
@@ -45,14 +45,13 @@ public:
4545
4646 Q_INVOKABLE unity::shell::launcher::LauncherItemInterface* get(int index) const;
4747 Q_INVOKABLE void move(int oldIndex, int newIndex);
48− Q_INVOKABLE void pin(int index);
49− Q_INVOKABLE void remove(int index);
50− Q_INVOKABLE void triggerQuickListAction(int itemIndex, int quickListIndex);
51−
52− QHash<int, QByteArray> roleNames() const;
48+ Q_INVOKABLE void pin(const QString &appId);
49+ Q_INVOKABLE void requestRemove(const QString &appId);
50+ Q_INVOKABLE void triggerQuickListAction(const QString &appId, int quickListIndex);
5351
5452private:
5553 void storeAppList();
54+ int findApplication(const QString &appId);
5655
5756private:
5857 QList<LauncherItem*> m_list;
tests/mocks/Unity/Launcher/MockLauncherItem.cpp+7−1
@@ -22,8 +22,9 @@
2222
2323using namespace unity::shell::launcher;
2424
25−MockLauncherItem::MockLauncherItem(const QString& desktopFile, const QString& name, const QString& icon, QObject* parent):
25+MockLauncherItem::MockLauncherItem(const QString &appId, const QString& desktopFile, const QString& name, const QString& icon, QObject* parent):
2626 LauncherItemInterface(parent),
27+ m_appId(appId),
2728 m_desktopFile(desktopFile),
2829 m_name(name),
2930 m_icon(icon),
@@ -37,6 +38,11 @@ MockLauncherItem::MockLauncherItem(const QString& desktopFile, const QString& na
3738
3839}
3940
41+QString MockLauncherItem::appId() const
42+{
43+ return m_appId;
44+}
45+
4046QString MockLauncherItem::desktopFile() const
4147{
4248 return m_desktopFile;
tests/mocks/Unity/Launcher/MockLauncherItem.h+3−2
@@ -30,10 +30,10 @@ class MockLauncherItem: public LauncherItemInterface
3030{
3131 Q_OBJECT
3232public:
33− MockLauncherItem(const QString& desktopFile, const QString& name, const QString& icon, QObject* parent = 0);
33+ MockLauncherItem(const QString &appId, const QString& desktopFile, const QString& name, const QString& icon, QObject* parent = 0);
3434
35+ QString appId() const;
3536 QString desktopFile() const;
36−
3737 QString name() const;
3838 QString icon() const;
3939
@@ -55,6 +55,7 @@ public:
5555 unity::shell::launcher::QuickListModelInterface *quickList() const;
5656
5757private:
58+ QString m_appId;
5859 QString m_desktopFile;
5960 QString m_name;
6061 QString m_icon;
tests/mocks/Unity/Launcher/MockLauncherModel.cpp+37−20
@@ -22,25 +22,25 @@
2222
2323MockLauncherModel::MockLauncherModel(QObject* parent): LauncherModelInterface(parent)
2424{
25− MockLauncherItem *item = new MockLauncherItem("/usr/share/applications/phone-app.desktop", "Phone", "phone-app");
25+ MockLauncherItem *item = new MockLauncherItem("phone-app", "/usr/share/applications/phone-app.desktop", "Phone", "phone-app");
2626 m_list.append(item);
27− item = new MockLauncherItem("/usr/share/applications/camera-app.desktop", "Camera", "camera");
27+ item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera");
2828 m_list.append(item);
29− item = new MockLauncherItem("/usr/share/applications/gallery-app.desktop", "Gallery", "gallery");
29+ item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery");
3030 m_list.append(item);
31− item = new MockLauncherItem("/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook");
31+ item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook");
3232 m_list.append(item);
33− item = new MockLauncherItem("/usr/share/applications/webbrowser-app.desktop", "Browser", "browser");
33+ item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser");
3434 m_list.append(item);
35− item = new MockLauncherItem("/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter");
35+ item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter");
3636 m_list.append(item);
37− item = new MockLauncherItem("/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail");
37+ item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail");
3838 m_list.append(item);
39− item = new MockLauncherItem("/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather");
39+ item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather");
4040 m_list.append(item);
41− item = new MockLauncherItem("/usr/share/applications/notes-app.desktop", "Notepad", "notepad");
41+ item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad");
4242 m_list.append(item);
43− item = new MockLauncherItem("/usr/share/applications/ubuntu-calendar-app.desktop","Calendar", "calendar");
43+ item = new MockLauncherItem("ubuntu-calendar-app", "/usr/share/applications/ubuntu-calendar-app.desktop","Calendar", "calendar");
4444 m_list.append(item);
4545}
4646
@@ -100,23 +100,40 @@ void MockLauncherModel::move(int oldIndex, int newIndex)
100100 endMoveRows();
101101}
102102
103−void MockLauncherModel::pin(int index)
103+void MockLauncherModel::pin(const QString &appId)
104104{
105− m_list.at(index)->setPinned(true);
106− QModelIndex modelIndex = this->index(index);
107− Q_EMIT dataChanged(modelIndex, modelIndex);
105+ int index = findApp(appId);
106+ if (index >= 0) {
107+ m_list.at(index)->setPinned(true);
108+ QModelIndex modelIndex = this->index(index);
109+ Q_EMIT dataChanged(modelIndex, modelIndex);
110+ }
108111}
109112
110−void MockLauncherModel::remove(int index)
113+void MockLauncherModel::requestRemove(const QString &appId)
111114{
112− beginRemoveRows(QModelIndex(), index, 0);
113− m_list.takeAt(index)->deleteLater();
114− endRemoveRows();
115+ int index = findApp(appId);
116+ if (index >= 0) {
117+ beginRemoveRows(QModelIndex(), index, 0);
118+ m_list.takeAt(index)->deleteLater();
119+ endRemoveRows();
120+ }
115121}
116122
117−void MockLauncherModel::triggerQuickListAction(int itemIndex, int quickListIndex)
123+void MockLauncherModel::triggerQuickListAction(const QString &appId, int quickListIndex)
118124{
119− Q_UNUSED(itemIndex)
125+ Q_UNUSED(appId)
120126 Q_UNUSED(quickListIndex)
121127 // Nothing to mock yet...
122128}
129+
130+int MockLauncherModel::findApp(const QString &appId)
131+{
132+ for (int i = 0; i < m_list.count(); ++i) {
133+ MockLauncherItem *item = m_list.at(i);
134+ if (item->appId() == appId) {
135+ return i;
136+ }
137+ }
138+ return -1;
139+}
tests/mocks/Unity/Launcher/MockLauncherModel.h+6−3
@@ -40,9 +40,12 @@ public:
4040
4141 Q_INVOKABLE unity::shell::launcher::LauncherItemInterface *get(int index) const;
4242 Q_INVOKABLE void move(int oldIndex, int newIndex);
43− Q_INVOKABLE void pin(int index);
44− Q_INVOKABLE void remove(int index);
45− Q_INVOKABLE void triggerQuickListAction(int itemIndex, int quickListIndex);
43+ Q_INVOKABLE void pin(const QString &appId);
44+ Q_INVOKABLE void requestRemove(const QString &appId);
45+ Q_INVOKABLE void triggerQuickListAction(const QString &appId, int quickListIndex);
46+
47+private:
48+ int findApp(const QString &appId);
4649
4750private:
4851 QList<MockLauncherItem*> m_list;