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  m_quickList->appendAction(nameAction);
45 
46  QuickListEntry pinningAction;
47  pinningAction.setActionId(QStringLiteral("pin_item"));
48  pinningAction.setText(gettext("Pin shortcut"));
49  m_quickList->appendAction(pinningAction);
50 
51  m_quitAction.setActionId(QStringLiteral("stop_item"));
52  m_quitAction.setIcon(QStringLiteral("application-exit"));
53  m_quitAction.setText(gettext("Quit"));
54 }
55 
56 QString LauncherItem::appId() const
57 {
58  return m_appId;
59 }
60 
61 QString LauncherItem::name() const
62 {
63  return m_name;
64 }
65 
66 void LauncherItem::setName(const QString &name)
67 {
68  if (m_name != name) {
69  m_name = name;
70  QuickListEntry entry;
71  entry.setActionId(QStringLiteral("launch_item"));
72  entry.setText(m_name);
73  m_quickList->updateAction(entry);
74  Q_EMIT nameChanged(name);
75  }
76 }
77 
78 QString LauncherItem::icon() const
79 {
80  return m_icon;
81 }
82 
83 void LauncherItem::setIcon(const QString &icon)
84 {
85  if (m_icon != icon) {
86  m_icon = icon;
87  Q_EMIT iconChanged(icon);
88  }
89 }
90 
91 bool LauncherItem::pinned() const
92 {
93  return m_pinned;
94 }
95 
96 void LauncherItem::setPinned(bool pinned)
97 {
98  if (m_pinned != pinned) {
99  m_pinned = pinned;
100  Q_EMIT pinnedChanged(pinned);
101  }
102 
103  // Even if pinned status didn't change, we want to update text in case
104  // the locale has changed since we last set pinned status.
105  QuickListEntry entry;
106  entry.setActionId(QStringLiteral("pin_item"));
107  entry.setText(pinned ? gettext("Unpin shortcut") : gettext("Pin shortcut"));
108  m_quickList->updateAction(entry);
109 }
110 
111 bool LauncherItem::running() const
112 {
113  return m_running;
114 }
115 
116 void LauncherItem::setRunning(bool running)
117 {
118  if (m_running != running) {
119  m_running = running;
120  if (m_running) { // add the quit action
121  m_quickList->appendAction(m_quitAction);
122  } else { // remove the quit action
123  m_quickList->removeAction(m_quitAction);
124  }
125  Q_EMIT runningChanged(running);
126  }
127 }
128 
129 bool LauncherItem::recent() const
130 {
131  return m_recent;
132 }
133 
134 void LauncherItem::setRecent(bool recent)
135 {
136  if (m_recent != recent) {
137  m_recent = recent;
138  Q_EMIT recentChanged(recent);
139  }
140 }
141 
142 int LauncherItem::progress() const
143 {
144  return m_progress;
145 }
146 
147 void LauncherItem::setProgress(int progress)
148 {
149  if (m_progress != progress) {
150  m_progress = progress;
151  Q_EMIT progressChanged(progress);
152  }
153 }
154 
155 int LauncherItem::count() const
156 {
157  return m_count;
158 }
159 
160 void LauncherItem::setCount(int count)
161 {
162  if (m_count != count) {
163  m_count = count;
164  Q_EMIT countChanged(count);
165  if (m_countVisible) {
166  setAlerting(true);
167  }
168  }
169 }
170 
171 bool LauncherItem::countVisible() const
172 {
173  return m_countVisible;
174 }
175 
176 void LauncherItem::setCountVisible(bool countVisible)
177 {
178  if (m_countVisible != countVisible) {
179  m_countVisible = countVisible;
180  Q_EMIT countVisibleChanged(countVisible);
181  if (countVisible) {
182  setAlerting(true);
183  }
184  }
185 }
186 
187 bool LauncherItem::focused() const
188 {
189  return m_focused;
190 }
191 
192 void LauncherItem::setFocused(bool focused)
193 {
194  if (m_focused != focused) {
195  m_focused = focused;
196  if (focused) {
197  setAlerting(false);
198  }
199  Q_EMIT focusedChanged(focused);
200  }
201 }
202 
203 bool LauncherItem::alerting() const
204 {
205  return m_alerting;
206 }
207 
208 void LauncherItem::setAlerting(bool alerting)
209 {
210  if (m_alerting != alerting) {
211  m_alerting = alerting;
212  Q_EMIT alertingChanged(alerting);
213  }
214 }
215 
216 unity::shell::launcher::QuickListModelInterface *LauncherItem::quickList() const
217 {
218  return m_quickList;
219 }