Unity 8
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 Loader {
28  id: messageFactoryItem
29  objectName: "messageItem"
30  property var menuModel: null
31  property QtObject menuData: null
32  property int menuIndex: -1
33 
34  property bool selected: false
35  signal menuSelected
36  signal menuDeselected
37 
38  property var extendedData: menuData && menuData.ext || undefined
39  property var actionsDescription: getExtendedProperty(extendedData, "xCanonicalMessageActions", undefined)
40 
41  // TODO - bug #1260728
42  property var timeFormatter: Utils.RelativeTimeFormatter {
43  time: getExtendedProperty(extendedData, "xCanonicalTime", 0) / 1000000
44  }
45 
46  onMenuModelChanged: {
47  loadAttributes();
48  }
49  onMenuIndexChanged: {
50  loadAttributes();
51  }
52 
53  sourceComponent: loadMessage(actionsDescription)
54 
55  function loadMessage(actions)
56  {
57  var parameterType = ""
58  for (var actIndex in actions) {
59  var desc = actions[actIndex];
60  if (desc["parameter-type"] !== undefined) {
61  parameterType += desc["parameter-type"];
62  } else {
63  parameterType += "_";
64  }
65  }
66 
67  if (parameterType === "") {
68  return simpleMessage;
69  } else if (parameterType === "s") {
70  return textMessage;
71  } else if (parameterType === "_s") {
72  return snapDecision;
73  } else {
74  console.debug("Unknown paramater type: " + parameterType);
75  }
76  return undefined;
77  }
78 
79  function loadAttributes() {
80  if (!menuModel || menuIndex == -1) return;
81  menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-time': 'int64',
82  'x-canonical-text': 'string',
83  'x-canonical-message-actions': 'variant',
84  'icon': 'icon',
85  'x-canonical-app-icon': 'icon'});
86  }
87 
88  function getExtendedProperty(object, propertyName, defaultValue) {
89  if (object && object.hasOwnProperty(propertyName)) {
90  return object[propertyName];
91  }
92  return defaultValue;
93  }
94 
95  Component {
96  id: simpleMessage
97 
98  Menus.SimpleMessageMenu {
99  id: message
100  objectName: "simpleTextMessage"
101  // text
102  title: menuData && menuData.label || ""
103  time: timeFormatter.timeString
104  body: getExtendedProperty(extendedData, "xCanonicalText", "")
105  // icons
106  avatar: getExtendedProperty(extendedData, "icon", "image://theme/contact")
107  icon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "image://theme/message")
108  // actions
109  enabled: menuData && menuData.sensitive || false
110  removable: !selected
111  confirmRemoval: true
112  selected: messageFactoryItem.selected
113 
114  onIconActivated: {
115  menuModel.activate(menuIndex, true);
116  }
117  onDismissed: {
118  menuModel.activate(menuIndex, false);
119  }
120  onTriggered: {
121  if (selected) {
122  menuDeselected();
123  } else {
124  menuSelected();
125  }
126  }
127  }
128  }
129 
130  Component {
131  id: textMessage
132 
133  Menus.TextMessageMenu {
134  id: message
135  objectName: "textMessage"
136  property var replyActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
137 
138  property var replyAction: QMenuModel.UnityMenuAction {
139  model: menuModel
140  index: menuIndex
141  name: getExtendedProperty(replyActionDescription, "name", "")
142  }
143 
144  // text
145  title: menuData && menuData.label || ""
146  time: timeFormatter.timeString
147  body: getExtendedProperty(extendedData, "xCanonicalText", "")
148  replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
149  replyHintText: i18n.tr("Reply")
150  // icons
151  avatar: getExtendedProperty(extendedData, "icon", "image://theme/contact")
152  icon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "image://theme/message")
153  // actions
154  replyEnabled: replyAction.valid && replyAction.enabled
155  enabled: menuData && menuData.sensitive || false
156  removable: !selected
157  confirmRemoval: true
158  selected: messageFactoryItem.selected
159  highlightWhenPressed: false
160 
161  onIconActivated: {
162  menuModel.activate(menuIndex, true);
163  }
164  onDismissed: {
165  menuModel.activate(menuIndex, false);
166  }
167  onReplied: {
168  replyAction.activate(value);
169  }
170  onTriggered: {
171  if (selected) {
172  menuDeselected();
173  } else {
174  menuSelected();
175  }
176  }
177  }
178  }
179 
180  Component {
181  id: snapDecision
182 
183  Menus.SnapDecisionMenu {
184  id: message
185  objectName: "snapDecision"
186  property var activateActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
187  property var replyActionDescription: actionsDescription && actionsDescription.length > 1 ? actionsDescription[1] : undefined
188 
189  property var activateAction: QMenuModel.UnityMenuAction {
190  model: menuModel
191  index: menuIndex
192  name: getExtendedProperty(activateActionDescription, "name", "")
193  }
194  property var replyAction: QMenuModel.UnityMenuAction {
195  model: menuModel
196  index: menuIndex
197  name: getExtendedProperty(replyActionDescription, "name", "")
198  }
199 
200  // text
201  title: menuData && menuData.label || ""
202  time: timeFormatter.timeString
203  body: getExtendedProperty(extendedData, "xCanonicalText", "")
204  actionButtonText: getExtendedProperty(activateActionDescription, "label", "Call back")
205  replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
206  // icons
207  avatar: getExtendedProperty(extendedData, "icon", "image://theme/contact")
208  icon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "image://theme/missed-call")
209  // actions
210  actionEnabled: activateAction.valid && activateAction.enabled
211  replyEnabled: replyAction.valid && replyAction.enabled
212  enabled: menuData && menuData.sensitive || false
213  removable: !selected
214  confirmRemoval: true
215  selected: messageFactoryItem.selected
216  highlightWhenPressed: false
217 
218  onIconActivated: {
219  menuModel.activate(menuIndex, true);
220  }
221  onDismissed: {
222  menuModel.activate(menuIndex, false);
223  }
224  onActionActivated: {
225  activateAction.activate();
226  }
227  onReplied: {
228  replyAction.activate(value);
229  }
230  onTriggered: {
231  if (selected) {
232  menuDeselected();
233  } else {
234  menuSelected();
235  }
236  }
237  }
238  }
239 }