Source

wazapp / UI / Chats.qml

Full commit
import QtQuick 1.1
import com.nokia.meego 1.0

import "chats.js" as ChatScript
import "contacts.js" as ContactsScript
Page {
    Component.onCompleted: ChatScript.importChats()
   // tools: commonTools
   // anchors.fill: parent

    id: chatsContainer
    property alias indicator_state:wa_notifier.state

    //width: parent.width
    //anchors.fill: parent
    //color: "#e6e6e6"

    state:"no_data"

    signal clicked(string number,string prev_state)

    function setContacts(contacts){
        ContactsScript.contacts = contacts;
    }

    function onMessageSent(msg_id){
        var chatItemIndex = findChatItemIndex(msg_id);
        if(chatItemIndex>=0){
            var chatItem = chatsModel.get(chatItemIndex);


              //chatsModel.remove(chatItemIndex);
             //chatsModel.insert(chatItemIndex,chatItem);
            //chatsModel.set(chatItemIndex,chatItem)

            chatItem.status = 1

        }


    }

    function onMessageDelivered(msg_id){
        var chatItemIndex = findChatItemIndex(msg_id);
        if(chatItemIndex>=0){
            var chatItem = chatsModel.get(chatItemIndex);

            chatItem.status = 2
        }
    }

    function findChatItemIndex(msg_id){

        for (var i=0; i<chatsModel.count;i++)
        {
            var chatItem = chatsModel.get(i);
            console.log(chatItem.id);
            if(chatItem.id == msg_id)
                   return i
        }
        return -1
    }

    //function updateConversation(msg_id,msgType,user_id, lastMsg,time,formattedDate)
    function updateConversation(msg)
    {

        var lastMsg = msg.content

        var tmp = lastMsg.split('\n');

        lastMsg = tmp[0];

        var maxLength = screen.currentOrientation == Screen.Portrait?30:100

        if(tmp.length>1){
            lastMsg += "..."
        }else if(lastMsg.length > maxLength){
            lastMsg = lastMsg.substring(0,maxLength).trim()+"...";
        }
        msg.content = lastMsg



       // ChatScript.updateChats(msg_id,msgType,user_id,lastMsg,time,formattedDate);
        ChatScript.updateChats(msg);
        chatsContainer.state=""
       // chatScript.sendMessage({'chatsModel':chatsModel});
    }

    states: [
        State {
            name: "no_data"
            PropertyChanges {
                target: no_data
                visible:true
            }
        }
    ]



    ListModel{
        id:chatsModel
    }
    Component{
        id:myDelegate;

        Chat{
            property variant contactInfo:ContactsScript.getContactData(model.jid)
            picture: contactInfo.picture;
            name: contactInfo.name
            number:model.jid;
            lastMsg:model.content
            time:model.timestamp
            formattedDate:model.formattedDate
            onClicked: chatsContainer.clicked(model.jid,"chats")
            width:chatsContainer.width
            msgId: model.id
            msgType:model.type
            state_status:model.status

	Rectangle {
		height: 1
		width: parent.width-30
		anchors.horizontalCenter: parent.horizontalCenter
		anchors.bottom: parent.bottom		
		color: theme.inverted?"#404040":"black"
	}

        }
    }

    Column{
        anchors.fill: parent;
        spacing:1
        width:parent.width
        height:parent.height
        WANotify{
            id:wa_notifier
        }
        Item{
            width:parent.width
            height:parent.height-wa_notifier.height
            visible:false;
            id:no_data

            Label{


                anchors.centerIn: parent;
                text:"No conversations yet"
                font.pointSize: 20
            }
        }

        ListView {
            id: list_view1
            //anchors.fill: parent
            width:parent.width
            height:parent.height-wa_notifier.height
            model: chatsModel
            delegate: myDelegate
            spacing: 1
            clip:true
        }
    }
}