some UI changes for usability
[makneto-zunavac1.git] / src / ui-mobile / declarative / GlobalStatus.qml
blobb72da35276cc948393b35acecdbd83da0f8d607b
1 /*
2  *   Copyright (C) 2011 Lukáš Karas <lukas.karas@centrum.cz>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program; if not, write to the
16  *   Free Software Foundation, Inc.,
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
20 import Qt 4.7
21 import QtQuick 1.0
22 import org.makneto 0.1 as Makneto
24 Item {
25     id: globalStatusComponent
27     property variant _model: ListModel {}
28     property variant _presenceManager : undefined
29     property variant popup: undefined;
30     property string defaultStatusMsg: "Click here for change your status..."
31     property string globalStatus: "Offline";
32     property variant statusPopupComponent : Qt.createComponent("GlobalStatusPopup.qml");
34     function log(msg){
35         console.log("II [GlobalStatus.qml]: "+msg);
36     }
37     function error(msg){
38         console.log("EE [GlobalStatus.qml]: "+msg);
39     }
40     function warn(msg){
41         console.log("WW [GlobalStatus.qml]: "+msg);
42     }
44     function setGlobalStatus(statusIndex, status, statusMessage){
45         /*
46         try{
47             var presence = statusIndexToPressence(statusIndex);
48             log("set global presence to "+presence );
49             for (var i=0; i< _model.accountCount; i++){
50                 var id = _model.data(i, Makneto.AccountsModel.IdRole);
51                 var account = _model.accountItemForId(id);
52                 account.setRequestedPresence(presence, status, statusMessage);
53             }
54         }catch(e){
55             error(e);
56         }
57         */
58         var presence = statusIndexToPressence(statusIndex);
59         _presenceManager.requestGlobalPresence(presence, status, statusMessage);
60         //log("request global presence...");
61     }
63     function onGlobalPresenceChanged(type, status, statusMessage){
64         log("onGlobalPresenceChanged (1) "+type+" "+status+", "+statusMessage);
65         var i = presenceToStatusIndex(type);
66         globalStatusIcon.source =  statusesModel.get(i).icon;
67         globalStatusMsgText.text = statusMessage === ""? defaultStatusMsg: statusMessage;
68         presenceText.text = presenceToStr( type );
69         log("onGlobalPresenceChanged "+type+" "+status+", "+statusMessage);
70     }
72     function onAccountsModelChanged(model){
73         _model = model;
74         // this is emited every change
75         /*
76         _model.accountCountChanged.connect(
77                     function(){
78                         updateGlobalStatus();
79                     });
80                     */
81     }       
82     function onPresenceManagerChaged(manager){
83         _presenceManager = manager;
84         _presenceManager.globalPresenceChanged.connect( onGlobalPresenceChanged);
85     }
87     function presenceToStr(i){
88         switch(i){
89         case Makneto.TelepathyTypes.ConnectionPresenceTypeAvailable: return "Available";
90         case Makneto.TelepathyTypes.ConnectionPresenceTypeOffline: return "Offline";
91         case Makneto.TelepathyTypes.ConnectionPresenceTypeAway: return "Away";
92         case Makneto.TelepathyTypes.ConnectionPresenceTypeExtendedAway: return "ExtendedAway";
93         case Makneto.TelepathyTypes.ConnectionPresenceTypeBusy: return "Busy";
94         case Makneto.TelepathyTypes.ConnectionPresenceTypeHidden: return "Hidden";
95         }
96         return "Offline";
97     }
99     function statusIndexToPressence(i){
100         switch(i){
101         case 0: return Makneto.TelepathyTypes.ConnectionPresenceTypeAvailable;
102         case 1: return Makneto.TelepathyTypes.ConnectionPresenceTypeOffline;
103         case 2: return Makneto.TelepathyTypes.ConnectionPresenceTypeAway;
104         case 3: return Makneto.TelepathyTypes.ConnectionPresenceTypeExtendedAway;
105         case 4: return Makneto.TelepathyTypes.ConnectionPresenceTypeBusy;
106         case 5: return Makneto.TelepathyTypes.ConnectionPresenceTypeHidden;
107         }
108         return Makneto.TelepathyTypes.ConnectionPresenceTypeOffline;
109     }
110     function presenceToStatusIndex(i){
111         switch(i){
112         case Makneto.TelepathyTypes.ConnectionPresenceTypeAvailable: return 0;
113         case Makneto.TelepathyTypes.ConnectionPresenceTypeOffline: return 1;
114         case Makneto.TelepathyTypes.ConnectionPresenceTypeAway: return 2;
115         case Makneto.TelepathyTypes.ConnectionPresenceTypeExtendedAway: return 3;
116         case Makneto.TelepathyTypes.ConnectionPresenceTypeBusy: return 4;
117         case Makneto.TelepathyTypes.ConnectionPresenceTypeHidden: return 5;
118         }
119         return 1;
120     }
122     Component.onCompleted: {
123         //main.addAccountsModelListener( globalStatusComponent );
124         main.accountsModelChanged.connect(onAccountsModelChanged);
125         main.presenceManagerChanged.connect(onPresenceManagerChaged);
126     }
128     ListModel {
129         id: statusesModel
130         ListElement { text: "Online"; icon: "img/status-online.png"     ; index: 0}
131         ListElement { text: "Offline"; icon: "img/status-offline.png"   ; index: 1}
132         ListElement { text: "Away"; icon: "img/status-away.png"         ; index: 2}
133         ListElement { text: "eXtended Away"; icon: "img/status-xa.png" ; index: 3}
134         ListElement { text: "DND"; icon: "img/status-dnd.png"         ; index: 4}
135         ListElement { text: "Invisible"; icon: "img/status-invisible.png" ; index: 5}
136     }
138     SystemPalette{ id: syspal }
140     Rectangle{
141         id:statusIndicator
143         anchors.fill: parent
144         color:"transparent"
146         Image {
147             id: globalStatusIcon
148             anchors.verticalCenter: parent.verticalCenter
149             source: statusesModel.get(Makneto.TelepathyTypes.ConnectionPresenceTypeOffline).icon
150             height: parent.height - 6
151             width: height
152         }
153         Rectangle{
154             id: textWrapper
155             height: presenceText.height + globalStatusMsgText.height
156             anchors.verticalCenter: globalStatusIcon.verticalCenter
157             anchors.left: globalStatusIcon.right
158             anchors.right: parent.right
159             anchors.leftMargin: 5
160             color: "transparent"
162             Text {
163                 id: presenceText
164                 text: globalStatus
166                 anchors.top: parent.top
167                 anchors.right: parent.right
168                 anchors.left: parent.left
169                 wrapMode: Text.NoWrap
170                 color: main.useSyspal? syspal.windowText : "white"
171                 font.bold: true
172             }
173             Text {
174                 id: globalStatusMsgText
175                 text: defaultStatusMsg
176                 color: main.useSyspal? syspal.windowText : "white"
178                 anchors.right: parent.right
179                 anchors.left: parent.left
180                 anchors.top: presenceText.bottom
181                 //anchors.verticalCenter: globalStatusIcon.verticalCenter
183                 //height: parent.height
184                 wrapMode: Text.NoWrap
186                 elide: Text.ElideRight; textFormat: Text.PlainText // this adds \x2026 (…) when text is too long
187             }
188         }
189         MouseArea{
190             anchors.fill: parent
191             onClicked: {
192                 //log("clicked on statusIndicator ");
194                 if(statusPopupComponent.status != Component.Ready){
195                     error("Component "+statusPopupComponent.url+" is not ready!");
196                     error(statusPopupComponent.errorString());
197                     return false;
198                 }
200                 if (popup !== undefined && popup !== null && popup.state != "hide"){
201                     popup.hidePopup();
202                 }
204                 popup = statusPopupComponent.createObject(main);
205                 if(popup === null){
206                     error("error creating popup");
207                     error(statusPopupComponent.errorString());
208                     return false;
209                 }
210                 var pos = statusIndicator.mapToItem(main,statusIndicator.width - main.toolbarHeight, main.toolbarHeight);
211                 popup.popupFromX = pos.x;
212                 popup.popupFromY = pos.y;
213                 popup.finalWidth = main.width * .7;
214                 popup.finalHeight = main.height * .5;
215                 popup.statusesModel = statusesModel;
217                 if (globalStatusComponent._presenceManager === undefined)
218                     error("we don't have pressenceManager!");
219                 popup.setGlobalStatusComponent( globalStatusComponent );
221                 popup.state = "visible"
222                 //log("visible popup "+JSON.stringify(parent));
223             }
224         }
225     }