Unity 8
launcheritem.cpp
1 /*
2  * Copyright 2013 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authors:
17  * Michael Zanetti <michael.zanetti@canonical.com>
18  */
19 
20 #include "launcheritem.h"
21 #include "quicklistmodel.h"
22 
23 #include <libintl.h>
24 
25 LauncherItem::LauncherItem(const QString &appId, const QString &name, const QString &icon, QObject *parent) :
26  LauncherItemInterface(parent),
27  m_appId(appId),
28  m_name(name),
29  m_icon(icon),
30  m_pinned(false),
31  m_running(false),
32  m_recent(false),
33  m_progress(-1),
34  m_count(0),
35  m_countVisible(false),
36  m_focused(false),
37  m_alerting(false),
38  m_quickList(new QuickListModel(this))
39 {
40  Q_ASSERT(parent != nullptr);
41  QuickListEntry nameAction;
42  nameAction.setActionId(QStringLiteral("launch_item"));
43  nameAction.setText(m_name);
44  nameAction.setHasSeparator(true);
45  m_quickList->appendAction(nameAction);
46 
47  QuickListEntry pinningAction;
48  pinningAction.setActionId(QStringLiteral("pin_item"));
49  pinningAction.setText(gettext("Pin shortcut"));
50  pinningAction.setIsPrivate(true);
51  m_quickList->appendAction(pinningAction);
52 
53  m_quitAction.setActionId(QStringLiteral("stop_item"));
54  m_quitAction.setIcon(QStringLiteral("application-exit"));
55  m_quitAction.setText(gettext("Quit"));
56  m_quitAction.setIsPrivate(true);
57 }
58 
59 QString LauncherItem::appId() const
60 {
61  return m_appId;
62 }
63 
64 QString LauncherItem::name() const
65 {
66  return m_name;
67 }
68 
69 void LauncherItem::setName(const QString &name)
70 {
71  if (m_name != name) {
72  m_name = name;
73  QuickListEntry entry;
74  entry.setActionId(QStringLiteral("launch_item"));
75  entry.setText(m_name);
76  m_quickList->updateAction(entry);
77  Q_EMIT nameChanged(name);
78  }
79 }
80 
81 QString LauncherItem::icon() const
82 {
83  return m_icon;
84 }
85 
86 void LauncherItem::setIcon(const QString &icon)
87 {
88  if (m_icon != icon) {
89  m_icon = icon;
90  Q_EMIT iconChanged(icon);
91  }
92 }
93 
94 QStringList LauncherItem::keywords() const
95 {
96  return m_keywords;
97 }
98 
99 void LauncherItem::setKeywords(const QStringList &keywords)
100 {
101  if (m_keywords != keywords) {
102  m_keywords = keywords;
103  Q_EMIT keywordsChanged(keywords);
104  }
105 }
106 
107 bool LauncherItem::pinned() const
108 {
109  return m_pinned;
110 }
111 
112 void LauncherItem::setPinned(bool pinned)
113 {
114  if (m_pinned != pinned) {
115  m_pinned = pinned;
116  Q_EMIT pinnedChanged(pinned);
117  }
118 
119  // Even if pinned status didn't change, we want to update text in case
120  // the locale has changed since we last set pinned status.
121  QuickListEntry entry;
122  entry.setActionId(QStringLiteral("pin_item"));
123  entry.setText(pinned ? gettext("Unpin shortcut") : gettext("Pin shortcut"));
124  entry.setIsPrivate(true);
125  m_quickList->updateAction(entry);
126 }
127 
128 bool LauncherItem::running() const
129 {
130  return m_running;
131 }
132 
133 void LauncherItem::setRunning(bool running)
134 {
135  if (m_running != running) {
136  m_running = running;
137  if (m_running) { // add the quit action
138  m_quickList->appendAction(m_quitAction);
139  } else { // remove the quit action
140  m_quickList->removeAction(m_quitAction);
141  }
142  Q_EMIT runningChanged(running);
143  }
144 }
145 
146 bool LauncherItem::recent() const
147 {
148  return m_recent;
149 }
150 
151 void LauncherItem::setRecent(bool recent)
152 {
153  if (m_recent != recent) {
154  m_recent = recent;
155  Q_EMIT recentChanged(recent);
156  }
157 }
158 
159 int LauncherItem::progress() const
160 {
161  return m_progress;
162 }
163 
164 void LauncherItem::setProgress(int progress)
165 {
166  if (m_progress != progress) {
167  m_progress = progress;
168  Q_EMIT progressChanged(progress);
169  }
170 }
171 
172 int LauncherItem::count() const
173 {
174  return m_count;
175 }
176 
177 void LauncherItem::setCount(int count)
178 {
179  if (m_count != count) {
180  m_count = count;
181  Q_EMIT countChanged(count);
182  }
183 }
184 
185 bool LauncherItem::countVisible() const
186 {
187  return m_countVisible;
188 }
189 
190 void LauncherItem::setCountVisible(bool countVisible)
191 {
192  if (m_countVisible != countVisible) {
193  m_countVisible = countVisible;
194  Q_EMIT countVisibleChanged(countVisible);
195  }
196 }
197 
198 bool LauncherItem::focused() const
199 {
200  return m_focused;
201 }
202 
203 void LauncherItem::setFocused(bool focused)
204 {
205  if (m_focused != focused) {
206  m_focused = focused;
207  Q_EMIT focusedChanged(focused);
208  }
209 }
210 
211 bool LauncherItem::alerting() const
212 {
213  return m_alerting;
214 }
215 
216 void LauncherItem::setAlerting(bool alerting)
217 {
218  if (m_alerting != alerting) {
219  m_alerting = alerting;
220  Q_EMIT alertingChanged(alerting);
221  }
222 }
223 
224 int LauncherItem::surfaceCount() const
225 {
226  return m_surfaces.count();
227 }
228 
229 void LauncherItem::setSurfaces(const QList<QPair<QString, QString> > &surfaces)
230 {
231  if (m_surfaces != surfaces) {
232  m_surfaces = surfaces;
233 
234  QList<QuickListEntry> removedEntries;
235  for (int i = 0; i < m_quickList->rowCount(); ++i) {
236  QuickListEntry entry = m_quickList->get(i);
237  if (entry.actionId().startsWith(QStringLiteral("surface_"))) {
238  removedEntries.append(entry);
239  }
240  }
241  Q_FOREACH (const QuickListEntry &entry, removedEntries) {
242  m_quickList->removeAction(entry);
243  }
244  for (int i = 0; i < surfaces.count(); ++i) {
245  QuickListEntry entry;
246  entry.setActionId(QStringLiteral("surface_") + surfaces.at(i).first);
247  entry.setText(surfaces.at(i).second);
248  entry.setIsPrivate(true);
249  if (i == surfaces.count() - 1) {
250  entry.setHasSeparator(true);
251  }
252  m_quickList->insertAction(entry, i + 1);
253  }
254 
255  Q_EMIT surfaceCountChanged(m_surfaces.count());
256  }
257 }
258 
259 unity::shell::launcher::QuickListModelInterface *LauncherItem::quickList() const
260 {
261  return m_quickList;
262 }