on 551c708 help sign in
unity8/plugins/Unity/Indicators/Messaging/qml/MessageMenuItemFactory.qml
212 lines 8.8 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 Unity.Indicators 0.1 as Indicators
24import QMenuModel 0.1 as QMenuModel
26Indicators.BaseMenuItem {
27 id: messageFactoryItem
28 property var menuModel: null
29 property QtObject menuData: null
30 property int menuIndex: -1
32 property var extendedData: menuData && menuData.ext || undefined
33 property var actionsDescription: getExtendedProperty(extendedData, "xCanonicalMessageActions", undefined)
35 onMenuModelChanged: {
36 loadAttributes();
37 }
38 onMenuIndexChanged: {
39 loadAttributes();
40 }
42 function loadAttributes() {
43 if (!menuModel || menuIndex == undefined) return;
45 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-time': 'int64',
46 'x-canonical-text': 'string',
47 'x-canonical-message-actions': 'variant',
48 'icon': 'icon',
49 'x-canonical-app-icon': 'icon'});
50 }
52 function getExtendedProperty(object, propertyName, defaultValue) {
53 if (object && object.hasOwnProperty(propertyName)) {
54 return object[propertyName];
55 }
56 return defaultValue;
57 }
59 implicitHeight: contents.status == Loader.Ready ? contents.item.implicitHeight : 0
61 Loader {
62 id: contents
63 objectName: "loader"
64 anchors.fill: parent
66 sourceComponent: loadMessage(actionsDescription);
68 Component {
69 id: simpleTextMessage
70 SimpleTextMessage {
71 objectName: "simpleTextMessage"
72 // text
73 title: menuData && menuData.label || ""
74 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
75 message: getExtendedProperty(extendedData, "xCanonicalText", "")
76 // icons
77 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
78 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
79 // actions
80 enabled: menuData && menuData.sensitive || false
82 onActivateApp: {
83 menuModel.activate(menuIndex, true);
84 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
85 }
86 onDismiss: {
87 menuModel.activate(menuIndex, false);
88 }
90 menuSelected: messageFactoryItem.menuSelected
91 onSelectMenu: messageFactoryItem.selectMenu()
92 onDeselectMenu: messageFactoryItem.deselectMenu()
93 }
94 }
95 Component {
96 id: textMessage
97 TextMessage {
98 objectName: "textMessage"
99 property var replyActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
101 property var replyAction: QMenuModel.UnityMenuAction {
102 model: menuModel
103 index: menuIndex
104 name: getExtendedProperty(replyActionDescription, "name", "")
105 }
107 // text
108 title: menuData && menuData.label || ""
109 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
110 message: getExtendedProperty(extendedData, "xCanonicalText", "")
111 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
112 // icons
113 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
114 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
115 // actions
116 replyEnabled: replyAction.valid && replyAction.enabled
117 enabled: menuData && menuData.sensitive || false
119 onActivateApp: {
120 menuModel.activate(menuIndex, true);
121 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
122 }
123 onDismiss: {
124 menuModel.activate(menuIndex, false);
125 }
126 onReply: {
127 replyAction.activate(value);
128 }
130 menuSelected: messageFactoryItem.menuSelected
131 onSelectMenu: messageFactoryItem.selectMenu()
132 onDeselectMenu: messageFactoryItem.deselectMenu()
133 }
134 }
135 Component {
136 id: snapDecision
137 SnapDecision {
138 objectName: "snapDecision"
139 property var activateActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
140 property var replyActionDescription: actionsDescription && actionsDescription.length > 1 ? actionsDescription[1] : undefined
142 property var activateAction: QMenuModel.UnityMenuAction {
143 model: menuModel
144 index: menuIndex
145 name: getExtendedProperty(activateActionDescription, "name", "")
146 }
147 property var replyAction: QMenuModel.UnityMenuAction {
148 model: menuModel
149 index: menuIndex
150 name: getExtendedProperty(replyActionDescription, "name", "")
151 }
153 // text
154 title: menuData && menuData.label || ""
155 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
156 message: getExtendedProperty(extendedData, "xCanonicalText", "")
157 actionButtonText: getExtendedProperty(activateActionDescription, "label", "Call back")
158 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
159 replyMessages: getExtendedProperty(replyActionDescription, "parameter-hint", "")
160 // icons
161 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
162 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
163 // actions
164 activateEnabled: activateAction.valid && activateAction.enabled
165 replyEnabled: replyAction.valid && replyAction.enabled
166 enabled: menuData && menuData.sensitive || false
168 onActivateApp: {
169 menuModel.activate(menuIndex, true);
170 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
171 }
172 onDismiss: {
173 menuModel.activate(menuIndex, false);
174 }
175 onActivate: {
176 activateAction.activate();
177 }
178 onReply: {
179 replyAction.activate(value);
180 }
182 menuSelected: messageFactoryItem.menuSelected
183 onSelectMenu: messageFactoryItem.selectMenu()
184 onDeselectMenu: messageFactoryItem.deselectMenu()
185 }
186 }
187 }
189 function loadMessage(actions)
190 {
191 var parameterType = ""
192 for (var actIndex in actions) {
193 var desc = actions[actIndex];
194 if (desc["parameter-type"] !== undefined) {
195 parameterType += desc["parameter-type"];
196 } else {
197 parameterType += "_";
198 }
199 }
201 if (parameterType === "") {
202 return simpleTextMessage;
203 } else if (parameterType === "s") {
204 return textMessage;
205 } else if (parameterType === "_s") {
206 return snapDecision;
207 } else {
208 console.debug("Unknown paramater type: " + parameterType);
209 }
210 return undefined;
211 }