on 37d688d help sign in
unity8/qml/Panel/Indicators/MessageMenuItemFactory.qml
256 lines 9.9 kB history raw
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 * Renato Araujo Oliveira Filho <renato@canonical.com>
18 * Olivier Tilloy <olivier.tilloy@canonical.com>
19 */
21import QtQuick 2.0
22import Ubuntu.Components 0.1
23import Ubuntu.Settings.Menus 0.1 as Menus
24import QMenuModel 0.1 as QMenuModel
25import Utils 0.1 as Utils
27Item {
28 id: messageFactoryItem
29 property var menuModel: null
30 property QtObject menuData: null
31 property int menuIndex: -1
33 property bool selected: false
34 signal menuSelected
35 signal menuDeselected
37 property var extendedData: menuData && menuData.ext || undefined
38 property var actionsDescription: getExtendedProperty(extendedData, "xCanonicalMessageActions", undefined)
40 // TODO - bug #1260728
41 property var timeFormatter: Utils.TimeFormatter {
42 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
43 format: "hh:mm - MMM dd"
44 }
46 onMenuModelChanged: {
47 loadAttributes();
48 }
49 onMenuIndexChanged: {
50 loadAttributes();
51 }
53 function loadAttributes() {
54 if (!menuModel || menuIndex == -1) return;
55 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-time': 'int64',
56 'x-canonical-text': 'string',
57 'x-canonical-message-actions': 'variant',
58 'icon': 'icon',
59 'x-canonical-app-icon': 'icon'});
60 }
62 function getExtendedProperty(object, propertyName, defaultValue) {
63 if (object && object.hasOwnProperty(propertyName)) {
64 return object[propertyName];
65 }
66 return defaultValue;
67 }
69 implicitHeight: contents.status == Loader.Ready ? contents.item.implicitHeight : 0
71 Loader {
72 id: contents
73 objectName: "loader"
74 anchors.fill: parent
76 sourceComponent: loadMessage(actionsDescription);
78 Component {
79 id: simpleTextMessage
81 Menus.SimpleTextMessageMenu {
82 id: message
83 objectName: "simpleTextMessage"
84 // text
85 title: menuData && menuData.label || ""
86 time: timeFormatter.timeString
87 message: getExtendedProperty(extendedData, "xCanonicalText", "")
88 // icons
89 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
90 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
91 // actions
92 enabled: menuData && menuData.sensitive || false
93 removable: !selected
94 selected: messageFactoryItem.selected
96 onAppActivated: {
97 menuModel.activate(menuIndex, true);
98 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
99 }
100 onDismissed: {
101 menuModel.activate(menuIndex, false);
102 }
103 onClicked: {
104 if (selected) {
105 menuDeselected();
106 } else {
107 menuSelected();
108 }
109 }
111 backgroundIndicator: RemoveBackground {
112 state: message.swipingState
113 }
114 }
115 }
117 Component {
118 id: textMessage
120 Menus.TextMessageMenu {
121 id: message
122 objectName: "textMessage"
123 property var replyActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
125 property var replyAction: QMenuModel.UnityMenuAction {
126 model: menuModel
127 index: menuIndex
128 name: getExtendedProperty(replyActionDescription, "name", "")
129 }
131 // text
132 title: menuData && menuData.label || ""
133 time: timeFormatter.timeString
134 message: getExtendedProperty(extendedData, "xCanonicalText", "")
135 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
136 // icons
137 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
138 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
139 // actions
140 replyEnabled: replyAction.valid && replyAction.enabled
141 enabled: menuData && menuData.sensitive || false
142 removable: !selected
143 selected: messageFactoryItem.selected
145 onAppActivated: {
146 menuModel.activate(menuIndex, true);
147 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
148 }
149 onDismissed: {
150 menuModel.activate(menuIndex, false);
151 }
152 onReplied: {
153 replyAction.activate(value);
154 }
155 onClicked: {
156 if (selected) {
157 menuDeselected();
158 } else {
159 menuSelected();
160 }
161 }
163 backgroundIndicator: RemoveBackground {
164 state: message.swipingState
165 }
166 }
167 }
168 Component {
169 id: snapDecision
171 Menus.SnapDecisionMenu {
172 id: message
173 objectName: "snapDecision"
174 property var activateActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
175 property var replyActionDescription: actionsDescription && actionsDescription.length > 1 ? actionsDescription[1] : undefined
177 property var activateAction: QMenuModel.UnityMenuAction {
178 model: menuModel
179 index: menuIndex
180 name: getExtendedProperty(activateActionDescription, "name", "")
181 }
182 property var replyAction: QMenuModel.UnityMenuAction {
183 model: menuModel
184 index: menuIndex
185 name: getExtendedProperty(replyActionDescription, "name", "")
186 }
188 // text
189 title: menuData && menuData.label || ""
190 time: timeFormatter.timeString
191 message: getExtendedProperty(extendedData, "xCanonicalText", "")
192 actionButtonText: getExtendedProperty(activateActionDescription, "label", "Call back")
193 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
194 replyMessages: getExtendedProperty(replyActionDescription, "parameter-hint", "")
195 // icons
196 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
197 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
198 // actions
199 activateEnabled: activateAction.valid && activateAction.enabled
200 replyEnabled: replyAction.valid && replyAction.enabled
201 enabled: menuData && menuData.sensitive || false
202 removable: !selected
203 selected: messageFactoryItem.selected
205 onAppActivated: {
206 menuModel.activate(menuIndex, true);
207 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
208 }
209 onDismissed: {
210 menuModel.activate(menuIndex, false);
211 }
212 onActivated: {
213 activateAction.activate();
214 }
215 onReplied: {
216 replyAction.activate(value);
217 }
218 onClicked: {
219 if (selected) {
220 menuDeselected();
221 } else {
222 menuSelected();
223 }
224 }
226 backgroundIndicator: RemoveBackground {
227 state: message.swipingState
228 }
229 }
230 }
231 }
233 function loadMessage(actions)
234 {
235 var parameterType = ""
236 for (var actIndex in actions) {
237 var desc = actions[actIndex];
238 if (desc["parameter-type"] !== undefined) {
239 parameterType += desc["parameter-type"];
240 } else {
241 parameterType += "_";
242 }
243 }
245 if (parameterType === "") {
246 return simpleTextMessage;
247 } else if (parameterType === "s") {
248 return textMessage;
249 } else if (parameterType === "_s") {
250 return snapDecision;
251 } else {
252 console.debug("Unknown paramater type: " + parameterType);
253 }
254 return undefined;
255 }