on 7ab33aa help sign in
unity8/plugins/Unity/Indicators/qml/MessageMenuItemFactory.qml
253 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
80 Menus.SimpleTextMessageMenu {
81 id: message
82 objectName: "simpleTextMessage"
83 // text
84 title: menuData && menuData.label || ""
85 time: timeFormatter.timeString
86 message: getExtendedProperty(extendedData, "xCanonicalText", "")
87 // icons
88 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
89 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
90 // actions
91 enabled: menuData && menuData.sensitive || false
92 removable: !selected
93 selected: messageFactoryItem.selected
95 onAppActivated: {
96 menuModel.activate(menuIndex, true);
97 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
98 }
99 onDismissed: {
100 menuModel.activate(menuIndex, false);
101 }
102 onClicked: {
103 if (selected) {
104 menuDeselected();
105 } else {
106 menuSelected();
107 }
108 }
110 backgroundIndicator: RemoveBackground {
111 state: message.swipingState
112 }
113 }
114 }
116 Component {
117 id: textMessage
118 Menus.TextMessageMenu {
119 id: message
120 objectName: "textMessage"
121 property var replyActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
123 property var replyAction: QMenuModel.UnityMenuAction {
124 model: menuModel
125 index: menuIndex
126 name: getExtendedProperty(replyActionDescription, "name", "")
127 }
129 // text
130 title: menuData && menuData.label || ""
131 time: timeFormatter.timeString
132 message: getExtendedProperty(extendedData, "xCanonicalText", "")
133 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
134 // icons
135 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
136 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
137 // actions
138 replyEnabled: replyAction.valid && replyAction.enabled
139 enabled: menuData && menuData.sensitive || false
140 removable: !selected
141 selected: messageFactoryItem.selected
143 onAppActivated: {
144 menuModel.activate(menuIndex, true);
145 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
146 }
147 onDismissed: {
148 menuModel.activate(menuIndex, false);
149 }
150 onReplied: {
151 replyAction.activate(value);
152 }
153 onClicked: {
154 if (selected) {
155 menuDeselected();
156 } else {
157 menuSelected();
158 }
159 }
161 backgroundIndicator: RemoveBackground {
162 state: message.swipingState
163 }
164 }
165 }
166 Component {
167 id: snapDecision
168 Menus.SnapDecisionMenu {
169 id: message
170 objectName: "snapDecision"
171 property var activateActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
172 property var replyActionDescription: actionsDescription && actionsDescription.length > 1 ? actionsDescription[1] : undefined
174 property var activateAction: QMenuModel.UnityMenuAction {
175 model: menuModel
176 index: menuIndex
177 name: getExtendedProperty(activateActionDescription, "name", "")
178 }
179 property var replyAction: QMenuModel.UnityMenuAction {
180 model: menuModel
181 index: menuIndex
182 name: getExtendedProperty(replyActionDescription, "name", "")
183 }
185 // text
186 title: menuData && menuData.label || ""
187 time: timeFormatter.timeString
188 message: getExtendedProperty(extendedData, "xCanonicalText", "")
189 actionButtonText: getExtendedProperty(activateActionDescription, "label", "Call back")
190 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
191 replyMessages: getExtendedProperty(replyActionDescription, "parameter-hint", "")
192 // icons
193 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
194 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
195 // actions
196 activateEnabled: activateAction.valid && activateAction.enabled
197 replyEnabled: replyAction.valid && replyAction.enabled
198 enabled: menuData && menuData.sensitive || false
199 removable: !selected
200 selected: messageFactoryItem.selected
202 onAppActivated: {
203 menuModel.activate(menuIndex, true);
204 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
205 }
206 onDismissed: {
207 menuModel.activate(menuIndex, false);
208 }
209 onActivated: {
210 activateAction.activate();
211 }
212 onReplied: {
213 replyAction.activate(value);
214 }
215 onClicked: {
216 if (selected) {
217 menuDeselected();
218 } else {
219 menuSelected();
220 }
221 }
223 backgroundIndicator: RemoveBackground {
224 state: message.swipingState
225 }
226 }
227 }
228 }
230 function loadMessage(actions)
231 {
232 var parameterType = ""
233 for (var actIndex in actions) {
234 var desc = actions[actIndex];
235 if (desc["parameter-type"] !== undefined) {
236 parameterType += desc["parameter-type"];
237 } else {
238 parameterType += "_";
239 }
240 }
242 if (parameterType === "") {
243 return simpleTextMessage;
244 } else if (parameterType === "s") {
245 return textMessage;
246 } else if (parameterType === "_s") {
247 return snapDecision;
248 } else {
249 console.debug("Unknown paramater type: " + parameterType);
250 }
251 return undefined;
252 }