on 2f840ea help sign in
unity8/plugins/Unity/Indicators/Messaging/qml/MessageMenuItemFactory.qml
204 lines 8.4 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 anchors.fill: parent
65 sourceComponent: loadMessage(actionsDescription);
67 Component {
68 id: simpleTextMessage
69 SimpleTextMessage {
70 // text
71 title: menuData && menuData.label || ""
72 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
73 message: getExtendedProperty(extendedData, "xCanonicalText", "")
74 // icons
75 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
76 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
78 onActivateApp: {
79 menuModel.activate(menuIndex, true);
80 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
81 }
82 onDismiss: {
83 menuModel.activate(menuIndex, false);
84 }
86 menuSelected: messageFactoryItem.menuSelected
87 onSelectMenu: messageFactoryItem.selectMenu()
88 onDeselectMenu: messageFactoryItem.deselectMenu()
89 }
90 }
91 Component {
92 id: textMessage
93 TextMessage {
94 property var replyActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
96 property var replyAction: QMenuModel.UnityMenuAction {
97 model: menuModel
98 index: menuIndex
99 name: getExtendedProperty(replyActionDescription, "name", "")
100 }
102 // text
103 title: menuData && menuData.label || ""
104 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
105 message: getExtendedProperty(extendedData, "xCanonicalText", "")
106 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
107 // icons
108 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
109 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
110 // actions
111 replyEnabled: replyAction.valid && replyAction.enabled
113 onActivateApp: {
114 menuModel.activate(menuIndex, true);
115 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
116 }
117 onDismiss: {
118 menuModel.activate(menuIndex, false);
119 }
120 onReply: {
121 replyAction.activate(value);
122 }
124 menuSelected: messageFactoryItem.menuSelected
125 onSelectMenu: messageFactoryItem.selectMenu()
126 onDeselectMenu: messageFactoryItem.deselectMenu()
127 }
128 }
129 Component {
130 id: snapDecision
131 SnapDecision {
132 property var activateActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
133 property var replyActionDescription: actionsDescription && actionsDescription.length > 1 ? actionsDescription[1] : undefined
135 property var activateAction: QMenuModel.UnityMenuAction {
136 model: menuModel
137 index: menuIndex
138 name: getExtendedProperty(activateActionDescription, "name", "")
139 }
140 property var replyAction: QMenuModel.UnityMenuAction {
141 model: menuModel
142 index: menuIndex
143 name: getExtendedProperty(replyActionDescription, "name", "")
144 }
146 // text
147 title: menuData && menuData.label || ""
148 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
149 message: getExtendedProperty(extendedData, "xCanonicalText", "")
150 actionButtonText: getExtendedProperty(activateActionDescription, "label", "Call back")
151 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
152 replyMessages: getExtendedProperty(replyActionDescription, "parameter-hint", "")
153 // icons
154 avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
155 appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
156 // actions
157 activateEnabled: activateAction.valid && activateAction.enabled
158 replyEnabled: replyAction.valid && replyAction.enabled
160 onActivateApp: {
161 menuModel.activate(menuIndex, true);
162 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
163 }
164 onDismiss: {
165 menuModel.activate(menuIndex, false);
166 }
167 onActivate: {
168 activateAction.activate();
169 }
170 onReply: {
171 replyAction.activate(value);
172 }
174 menuSelected: messageFactoryItem.menuSelected
175 onSelectMenu: messageFactoryItem.selectMenu()
176 onDeselectMenu: messageFactoryItem.deselectMenu()
177 }
178 }
179 }
181 function loadMessage(actions)
182 {
183 var parameterType = ""
184 for (var actIndex in actions) {
185 var desc = actions[actIndex];
186 if (desc["parameter-type"] !== undefined) {
187 parameterType += desc["parameter-type"];
188 } else {
189 parameterType += "_";
190 }
191 }
193 if (parameterType === "") {
194 return simpleTextMessage;
195 } else if (parameterType === "s") {
196 return textMessage;
197 } else if (parameterType === "_s") {
198 return snapDecision;
199 } else {
200 console.debug("Unknown paramater type: " + parameterType);
201 }
202 return undefined;
203 }