~renatofilho/address-book-app/fix-1199109

« back to all changes in this revision

Viewing changes to src/imports/Common/ContactDetailGroupWithTypeBase.qml

  • Committer: Tarmac
  • Author(s): Renato Araujo Oliveira Filho
  • Date: 2013-07-11 21:58:15 UTC
  • mfrom: (4.2.25 contact-edit)
  • Revision ID: tarmac-20130711215815-vsumftfqcr3t8a53
Implemented contact edit support.

Approved by Bill Filler, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012-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 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 General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
import QtQuick 2.0
 
18
import Ubuntu.Components 0.1
 
19
import QtContacts 5.0 as QtContacts
 
20
 
 
21
ContactDetailGroupBase {
 
22
    id: root
 
23
 
 
24
    property string defaultIcon : "artwork:/protocol-other.png"
 
25
    property ListModel typeModel
 
26
 
 
27
    function getType(detail) {
 
28
        if (typeModel) {
 
29
            return typeModel.get(typeModel.getTypeIndex(detail))
 
30
        } else {
 
31
            return ""
 
32
        }
 
33
    }
 
34
 
 
35
    function updateDetail(detail, index) {
 
36
        if (typeModel) {
 
37
            return typeModel.updateDetail(detail, index)
 
38
        }
 
39
        return false
 
40
    }
 
41
 
 
42
    typeModel: ListModel {
 
43
 
 
44
        signal loaded()
 
45
 
 
46
        function getTypeIndex(detail) {
 
47
            var context = -1;
 
48
            for (var i = 0; i < detail.contexts.length; i++) {
 
49
                context = detail.contexts[i];
 
50
                break;
 
51
            }
 
52
            var subType = -1;
 
53
            // not all details have subTypes
 
54
            if (detail.subTypes) {
 
55
                for (var i = 0; i < detail.subTypes.length; i++) {
 
56
                    subType = detail.subTypes[i];
 
57
                    break;
 
58
                }
 
59
            }
 
60
            if (context === QtContacts.ContactDetail.ContextHome) {
 
61
                return 0
 
62
            } else if (context === QtContacts.ContactDetail.ContextWork) {
 
63
                return 1
 
64
            } else if (subType === QtContacts.ContactDetail.ContextOther) {
 
65
                return 2
 
66
            } else {
 
67
                return 2
 
68
            }
 
69
        }
 
70
 
 
71
        function isNotAModelValue(value) {
 
72
            for(var i=0; i < count; i++) {
 
73
                if (value === get(i).value) {
 
74
                    return false
 
75
                }
 
76
            }
 
77
            return true
 
78
        }
 
79
 
 
80
        function compareList(listA, listB) {
 
81
            if (!listA && !listB) {
 
82
                return true
 
83
            }
 
84
 
 
85
            if (!listA || !listB) {
 
86
                return false
 
87
            }
 
88
 
 
89
            if (listA.length != listB.length) {
 
90
                return false
 
91
            }
 
92
            for(var i=0; i < listA.length; i++) {
 
93
                if (listA[i] != listB[i]) {
 
94
                    return false
 
95
                }
 
96
            }
 
97
            return true
 
98
        }
 
99
 
 
100
        function updateDetail(detail, index) {
 
101
            var modelData = get(index)
 
102
            if (!modelData) {
 
103
                return false
 
104
            }
 
105
 
 
106
            // WORKAROUND: in EDS empty context is equal to QtContacts.ContactDetail.ContextOther
 
107
            // this will avoid call contact update if the context has not changed
 
108
            if ((detail.contexts.length === 0) && (modelData.value === QtContacts.ContactDetail.ContextOther)) {
 
109
                return false
 
110
            }
 
111
 
 
112
            var newContext = detail.contexts.filter(isNotAModelValue)
 
113
            newContext.push(modelData.value)
 
114
            if (!compareList(newContext, detail.contexts)) {
 
115
                detail.contexts = newContext
 
116
                return true
 
117
            }
 
118
            return false
 
119
        }
 
120
 
 
121
        Component.onCompleted: {
 
122
            append({"value": QtContacts.ContactDetail.ContextHome, "label": i18n.tr("Home"), icon: null})
 
123
            append({"value": QtContacts.ContactDetail.ContextWork, "label": i18n.tr("Work"), icon: null})
 
124
            append({"value": QtContacts.ContactDetail.ContextOther, "label": i18n.tr("Other"), icon: null})
 
125
            loaded()
 
126
        }
 
127
    }
 
128
}