Unity 8
 All Classes Functions
MessageMenuItemFactory.qml
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  */
20 
21 import QtQuick 2.0
22 import Ubuntu.Components 0.1
23 import Ubuntu.Settings.Menus 0.1 as Menus
24 import QMenuModel 0.1 as QMenuModel
25 import Utils 0.1 as Utils
26 
27 Item {
28  id: messageFactoryItem
29  property var menuModel: null
30  property QtObject menuData: null
31  property int menuIndex: -1
32 
33  property bool selected: false
34  signal menuSelected
35  signal menuDeselected
36 
37  property var extendedData: menuData && menuData.ext || undefined
38  property var actionsDescription: getExtendedProperty(extendedData, "xCanonicalMessageActions", undefined)
39 
40  // TODO - bug #1260728
41  property var timeFormatter: Utils.TimeFormatter {
42  time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
43  format: "hh:mm - MMM dd"
44  }
45 
46  onMenuModelChanged: {
47  loadAttributes();
48  }
49  onMenuIndexChanged: {
50  loadAttributes();
51  }
52 
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  }
61 
62  function getExtendedProperty(object, propertyName, defaultValue) {
63  if (object && object.hasOwnProperty(propertyName)) {
64  return object[propertyName];
65  }
66  return defaultValue;
67  }
68 
69  implicitHeight: contents.status == Loader.Ready ? contents.item.implicitHeight : 0
70 
71  Loader {
72  id: contents
73  objectName: "loader"
74  anchors.fill: parent
75 
76  sourceComponent: loadMessage(actionsDescription);
77 
78  Component {
79  id: simpleTextMessage
80 
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
95 
96  onAppActivated: {
97  menuModel.activate(menuIndex, true);
98  }
99  onDismissed: {
100  menuModel.activate(menuIndex, false);
101  }
102  onClicked: {
103  if (selected) {
104  menuDeselected();
105  } else {
106  menuSelected();
107  }
108  }
109 
110  backgroundIndicator: RemoveBackground {
111  state: message.swipingState
112  }
113  }
114  }
115 
116  Component {
117  id: textMessage
118 
119  Menus.TextMessageMenu {
120  id: message
121  objectName: "textMessage"
122  property var replyActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
123 
124  property var replyAction: QMenuModel.UnityMenuAction {
125  model: menuModel
126  index: menuIndex
127  name: getExtendedProperty(replyActionDescription, "name", "")
128  }
129 
130  // text
131  title: menuData && menuData.label || ""
132  time: timeFormatter.timeString
133  message: getExtendedProperty(extendedData, "xCanonicalText", "")
134  replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
135  // icons
136  avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
137  appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
138  // actions
139  replyEnabled: replyAction.valid && replyAction.enabled
140  enabled: menuData && menuData.sensitive || false
141  removable: !selected
142  selected: messageFactoryItem.selected
143 
144  onAppActivated: {
145  menuModel.activate(menuIndex, true);
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  }
160 
161  backgroundIndicator: RemoveBackground {
162  state: message.swipingState
163  }
164  }
165  }
166  Component {
167  id: snapDecision
168 
169  Menus.SnapDecisionMenu {
170  id: message
171  objectName: "snapDecision"
172  property var activateActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
173  property var replyActionDescription: actionsDescription && actionsDescription.length > 1 ? actionsDescription[1] : undefined
174 
175  property var activateAction: QMenuModel.UnityMenuAction {
176  model: menuModel
177  index: menuIndex
178  name: getExtendedProperty(activateActionDescription, "name", "")
179  }
180  property var replyAction: QMenuModel.UnityMenuAction {
181  model: menuModel
182  index: menuIndex
183  name: getExtendedProperty(replyActionDescription, "name", "")
184  }
185 
186  // text
187  title: menuData && menuData.label || ""
188  time: timeFormatter.timeString
189  message: getExtendedProperty(extendedData, "xCanonicalText", "")
190  actionButtonText: getExtendedProperty(activateActionDescription, "label", "Call back")
191  replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
192  replyMessages: getExtendedProperty(replyActionDescription, "parameter-hint", "")
193  // icons
194  avatar: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_contact.png")
195  appIcon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "qrc:/indicators/artwork/messaging/default_app.svg")
196  // actions
197  activateEnabled: activateAction.valid && activateAction.enabled
198  replyEnabled: replyAction.valid && replyAction.enabled
199  enabled: menuData && menuData.sensitive || false
200  removable: !selected
201  selected: messageFactoryItem.selected
202 
203  onAppActivated: {
204  menuModel.activate(menuIndex, true);
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  }
222 
223  backgroundIndicator: RemoveBackground {
224  state: message.swipingState
225  }
226  }
227  }
228  }
229 
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  }
241 
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  }
253 }