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 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  // icons
150  avatar: getExtendedProperty(extendedData, "icon", "image://theme/contact")
151  icon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "image://theme/message")
152  // actions
153  replyEnabled: replyAction.valid && replyAction.enabled
154  enabled: menuData && menuData.sensitive || false
155  removable: !selected
156  confirmRemoval: true
157  selected: messageFactoryItem.selected
158  highlightWhenPressed: false
159 
160  onIconActivated: {
161  menuModel.activate(menuIndex, true);
162  }
163  onDismissed: {
164  menuModel.activate(menuIndex, false);
165  }
166  onReplied: {
167  replyAction.activate(value);
168  }
169  onTriggered: {
170  if (selected) {
171  menuDeselected();
172  } else {
173  menuSelected();
174  }
175  }
176  }
177  }
178 
179  Component {
180  id: snapDecision
181 
182  Menus.SnapDecisionMenu {
183  id: message
184  objectName: "snapDecision"
185  property var activateActionDescription: actionsDescription && actionsDescription.length > 0 ? actionsDescription[0] : undefined
186  property var replyActionDescription: actionsDescription && actionsDescription.length > 1 ? actionsDescription[1] : undefined
187 
188  property var activateAction: QMenuModel.UnityMenuAction {
189  model: menuModel
190  index: menuIndex
191  name: getExtendedProperty(activateActionDescription, "name", "")
192  }
193  property var replyAction: QMenuModel.UnityMenuAction {
194  model: menuModel
195  index: menuIndex
196  name: getExtendedProperty(replyActionDescription, "name", "")
197  }
198 
199  // text
200  title: menuData && menuData.label || ""
201  time: timeFormatter.timeString
202  body: getExtendedProperty(extendedData, "xCanonicalText", "")
203  actionButtonText: getExtendedProperty(activateActionDescription, "label", "Call back")
204  replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
205  // icons
206  avatar: getExtendedProperty(extendedData, "icon", "image://theme/contact")
207  icon: getExtendedProperty(extendedData, "xCanonicalAppIcon", "image://theme/missed-call")
208  // actions
209  actionEnabled: activateAction.valid && activateAction.enabled
210  replyEnabled: replyAction.valid && replyAction.enabled
211  enabled: menuData && menuData.sensitive || false
212  removable: !selected
213  confirmRemoval: true
214  selected: messageFactoryItem.selected
215  highlightWhenPressed: false
216 
217  onIconActivated: {
218  menuModel.activate(menuIndex, true);
219  }
220  onDismissed: {
221  menuModel.activate(menuIndex, false);
222  }
223  onActionActivated: {
224  activateAction.activate();
225  }
226  onReplied: {
227  replyAction.activate(value);
228  }
229  onTriggered: {
230  if (selected) {
231  menuDeselected();
232  } else {
233  menuSelected();
234  }
235  }
236  }
237  }
238 }