|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 // INCLUDES |
|
19 #include "debugtraces.h" |
|
20 #include <HbTextItem> |
|
21 #include <HbPushButton> |
|
22 #include <HbAction> |
|
23 #include <hbinputeditorinterface.h> |
|
24 #include <cntservicescontact.h> |
|
25 |
|
26 // USER INCLUDES |
|
27 #include "msgunieditoraddress.h" |
|
28 #include "msgunifiededitorlineedit.h" |
|
29 #include "msguiutilsmanager.h" |
|
30 #include "matchphnumberutil.h" |
|
31 |
|
32 const QString PBK_ICON(":/qtg_mono_contacts.svg"); |
|
33 const QString SEND_ICON(":/qtg_mono_send.svg"); |
|
34 |
|
35 // Constants |
|
36 |
|
37 MsgUnifiedEditorAddress::MsgUnifiedEditorAddress( const QString& label, |
|
38 const QString& pluginPath, |
|
39 QGraphicsItem *parent ) : |
|
40 HbWidget(parent), |
|
41 mPluginPath(pluginPath), |
|
42 mUtilsManager(0) |
|
43 { |
|
44 #ifdef _DEBUG_TRACES_ |
|
45 qDebug() << "MsgUnifiedEditorAddress calling HbStyle::registerPlugin"; |
|
46 #endif |
|
47 |
|
48 setPluginBaseId(style()->registerPlugin(mPluginPath)); |
|
49 mUtilsManager = new MsgUiUtilsManager(this); |
|
50 |
|
51 mLaunchBtn = new HbPushButton(this); |
|
52 HbStyle::setItemName(mLaunchBtn,"launchBtn"); |
|
53 connect(mLaunchBtn,SIGNAL(clicked()),mUtilsManager,SLOT(fetchContacts())); |
|
54 |
|
55 mLaunchBtn->setIcon(HbIcon(PBK_ICON)); |
|
56 |
|
57 mAddressEdit = new MsgUnifiedEditorLineEdit(label,this); |
|
58 HbStyle::setItemName(mAddressEdit,"addressField"); |
|
59 |
|
60 mAddressEdit->setMaxRows(40); |
|
61 connect(mAddressEdit, SIGNAL(addressTextChanged(const QString&)), |
|
62 this, SLOT(onTextChanged(const QString&))); |
|
63 |
|
64 // add "Send" action in VKB |
|
65 HbEditorInterface editorInterface(mAddressEdit); |
|
66 HbAction *sendAction = new HbAction(HbIcon(SEND_ICON), QString(),this); |
|
67 connect(sendAction, SIGNAL(triggered()),this, SIGNAL(sendMessage())); |
|
68 editorInterface.addAction(sendAction); |
|
69 |
|
70 connect(mUtilsManager, SIGNAL(contactsFetched(const QVariant&)), |
|
71 this, SLOT(contactsFetched(const QVariant&))); |
|
72 } |
|
73 |
|
74 MsgUnifiedEditorAddress::~MsgUnifiedEditorAddress() |
|
75 { |
|
76 style()->unregisterPlugin(mPluginPath); |
|
77 } |
|
78 |
|
79 void MsgUnifiedEditorAddress::contactsFetched(const QVariant& value) |
|
80 { |
|
81 CntServicesContactList contactList; |
|
82 contactList = qVariantValue<CntServicesContactList>(value); |
|
83 |
|
84 for(int i = 0; i < contactList.count(); i++ ) |
|
85 { |
|
86 mAddressMap.insert(contactList[i].mPhoneNumber, contactList[i].mDisplayName); |
|
87 if(!contactList[i].mDisplayName.isEmpty()) |
|
88 { |
|
89 mAddressEdit->setText(contactList[i].mDisplayName); |
|
90 } |
|
91 else |
|
92 { |
|
93 mAddressEdit->setText(contactList[i].mPhoneNumber); |
|
94 } |
|
95 } |
|
96 } |
|
97 |
|
98 ConvergedMessageAddressList MsgUnifiedEditorAddress::addresses() |
|
99 { |
|
100 #ifdef _DEBUG_TRACES_ |
|
101 qCritical() << "MsgUnifiedEditorAddress::address start"; |
|
102 #endif |
|
103 // sync-up map to account for user-actions on edit-field |
|
104 syncDeletionsToMap(); |
|
105 syncAdditionsToMap(); |
|
106 |
|
107 QStringList uniqueAddr; |
|
108 uniqueAddr = uniqueAddressList(); |
|
109 |
|
110 ConvergedMessageAddressList addresses; |
|
111 foreach(QString addr, uniqueAddr) |
|
112 { |
|
113 ConvergedMessageAddress* address = new ConvergedMessageAddress; |
|
114 address->setAddress(addr); |
|
115 if(!mAddressMap.value(addr).isEmpty()) |
|
116 { |
|
117 address->setAlias(mAddressMap.value(addr)); |
|
118 } |
|
119 addresses.append(address); |
|
120 } |
|
121 #ifdef _DEBUG_TRACES_ |
|
122 qCritical() << "MsgUnifiedEditorAddress::address end"; |
|
123 #endif |
|
124 return addresses; |
|
125 } |
|
126 |
|
127 int MsgUnifiedEditorAddress::addressCount() |
|
128 { |
|
129 return mAddressEdit->addresses().count(); |
|
130 } |
|
131 |
|
132 void MsgUnifiedEditorAddress::setAddresses(ConvergedMessageAddressList addrlist) |
|
133 { |
|
134 int count = addrlist.count(); |
|
135 for(int i = 0; i < count; i++ ) |
|
136 { |
|
137 mAddressMap.insert(addrlist[i]->address(), addrlist[i]->alias()); |
|
138 if(!addrlist[i]->alias().isEmpty()) |
|
139 { |
|
140 mAddressEdit->setText(addrlist[i]->alias()); |
|
141 } |
|
142 else |
|
143 { |
|
144 mAddressEdit->setText(addrlist[i]->address()); |
|
145 } |
|
146 } |
|
147 } |
|
148 |
|
149 void MsgUnifiedEditorAddress::onTextChanged(const QString& text) |
|
150 { |
|
151 if(!text.isEmpty()) |
|
152 { |
|
153 disconnect(mAddressEdit, SIGNAL(addressTextChanged(const QString&)), |
|
154 this, SLOT(onTextChanged(const QString&))); |
|
155 emit mmContentAdded(true); |
|
156 connect(mAddressEdit, SIGNAL(addressTextChanged(const QString&)), |
|
157 this, SLOT(onTextRemoved(const QString&))); |
|
158 } |
|
159 } |
|
160 |
|
161 void MsgUnifiedEditorAddress::onTextRemoved(const QString& text) |
|
162 { |
|
163 if(text.isEmpty()) |
|
164 { |
|
165 disconnect(mAddressEdit, SIGNAL(addressTextChanged(const QString&)), |
|
166 this, SLOT(onTextRemoved(const QString&))); |
|
167 emit mmContentAdded(false); |
|
168 connect(mAddressEdit, SIGNAL(addressTextChanged(const QString&)), |
|
169 this, SLOT(onTextChanged(const QString&))); |
|
170 } |
|
171 } |
|
172 |
|
173 void MsgUnifiedEditorAddress::syncDeletionsToMap() |
|
174 { |
|
175 // get address list from edit-field |
|
176 QStringList addrList = mAddressEdit->addresses(); |
|
177 |
|
178 QMap<QString, QString>::iterator i = mAddressMap.begin(); |
|
179 while (i != mAddressMap.end()) |
|
180 { |
|
181 // check if either key or value is present in the list |
|
182 if( !(addrList.contains(i.value(), Qt::CaseSensitive) || |
|
183 addrList.contains(i.key(), Qt::CaseSensitive)) ) |
|
184 {// if none are present, then delete entry from map |
|
185 i = mAddressMap.erase(i); |
|
186 } |
|
187 else |
|
188 { |
|
189 ++i; |
|
190 } |
|
191 } |
|
192 } |
|
193 |
|
194 void MsgUnifiedEditorAddress::syncAdditionsToMap() |
|
195 { |
|
196 QStringList mapAddrList = mAddressMap.values(); |
|
197 |
|
198 // remove already mapped addresses from edit-field |
|
199 QStringList userInputAddrList(mAddressEdit->addresses()); |
|
200 foreach(QString addr, mapAddrList) |
|
201 { |
|
202 userInputAddrList.removeAll(addr); |
|
203 } |
|
204 |
|
205 // add the unmapped addresses to address-map |
|
206 foreach(QString addr, userInputAddrList) |
|
207 { |
|
208 mAddressMap.insertMulti(addr, addr); |
|
209 } |
|
210 } |
|
211 |
|
212 QStringList MsgUnifiedEditorAddress::uniqueAddressList() |
|
213 { |
|
214 int matchDigitCount = MatchPhNumberUtil::matchDigits(); |
|
215 QStringList mapAddrList = mAddressMap.keys(); |
|
216 for(int j = 0;j<mapAddrList.count()-1;j++) |
|
217 { |
|
218 for(int i =j+1;i<mapAddrList.count();i++) |
|
219 { |
|
220 if(0 == mapAddrList[j].right(matchDigitCount).compare(mapAddrList[i].right(matchDigitCount))) |
|
221 { |
|
222 mapAddrList.removeAt(i); |
|
223 i--; |
|
224 } |
|
225 } |
|
226 } |
|
227 return mapAddrList; |
|
228 } |
|
229 Q_IMPLEMENT_USER_METATYPE(CntServicesContact) |
|
230 Q_IMPLEMENT_USER_METATYPE_NO_OPERATORS(CntServicesContactList) |
|
231 |
|
232 //EOF |