|
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: Class for handling the attachment list |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "nmuiheaders.h" |
|
19 |
|
20 /*! |
|
21 \class NmAttachmentList |
|
22 \brief Class for handling the attachment list |
|
23 */ |
|
24 |
|
25 /*! |
|
26 Constructor |
|
27 */ |
|
28 NmAttachmentList::NmAttachmentList(NmAttachmentListWidget &listWidget) |
|
29 : mListWidget(listWidget) |
|
30 { |
|
31 NM_FUNCTION; |
|
32 } |
|
33 |
|
34 /*! |
|
35 Destructor |
|
36 */ |
|
37 NmAttachmentList::~NmAttachmentList() |
|
38 { |
|
39 NM_FUNCTION; |
|
40 } |
|
41 |
|
42 /*! |
|
43 Insert new list item. Returns the index of the attachment in list |
|
44 */ |
|
45 int NmAttachmentList::insertAttachment( |
|
46 const QString &fullFileName, |
|
47 const QString &fileSize, |
|
48 const NmId &attachmentPartId) |
|
49 { |
|
50 NM_FUNCTION; |
|
51 |
|
52 QString displayName = fullNameToDisplayName(fullFileName); |
|
53 mFullFileName.append(fullFileName); |
|
54 mDisplayFileName.append(displayName); |
|
55 mAttachmentPartId.append(attachmentPartId); |
|
56 mFileSize.append(fileSize); |
|
57 mListWidget.insertAttachment( |
|
58 count() - 1, |
|
59 displayName, |
|
60 NmUtilities::attachmentSizeString(fileSize.toDouble())); |
|
61 return count() - 1; |
|
62 } |
|
63 |
|
64 /*! |
|
65 Set attachmentPartId of the list item. Because there can be several attachments with |
|
66 same filename, function will search the one which nmid is not set. |
|
67 */ |
|
68 void NmAttachmentList::setAttachmentPartId(const QString fullFileName, |
|
69 const NmId &attachmentPartId) |
|
70 { |
|
71 NM_FUNCTION; |
|
72 |
|
73 for (int i=0; i<count(); ++i) { |
|
74 if (mFullFileName.at(i) == fullFileName && mAttachmentPartId.at(i).id() == 0) { |
|
75 mAttachmentPartId.replace(i, attachmentPartId); |
|
76 } |
|
77 } |
|
78 } |
|
79 |
|
80 /*! |
|
81 Set fileSize of the attachment. |
|
82 */ |
|
83 void NmAttachmentList::setAttachmentSize(const NmId &attachmentPartId, const QString &size) |
|
84 { |
|
85 NM_FUNCTION; |
|
86 |
|
87 for (int i=0; i<count(); ++i) { |
|
88 if (mAttachmentPartId.at(i) == attachmentPartId) { |
|
89 mFileSize.replace(i, size); |
|
90 mListWidget.setAttachmentSize(i, NmUtilities::attachmentSizeString(size.toDouble())); |
|
91 } |
|
92 } |
|
93 } |
|
94 |
|
95 /*! |
|
96 Removes attachment from list position |
|
97 */ |
|
98 void NmAttachmentList::removeAttachment(int arrayIndex) |
|
99 { |
|
100 NM_FUNCTION; |
|
101 |
|
102 if (arrayIndex < count() && |
|
103 arrayIndex >= 0 ) { |
|
104 // Remove UI |
|
105 mListWidget.removeAttachment(arrayIndex); |
|
106 // Remove from data structure |
|
107 mFullFileName.removeAt(arrayIndex); |
|
108 mDisplayFileName.removeAt(arrayIndex); |
|
109 mAttachmentPartId.removeAt(arrayIndex); |
|
110 } |
|
111 } |
|
112 |
|
113 /*! |
|
114 Return full filename of the list item |
|
115 */ |
|
116 QString NmAttachmentList::getFullFileNameByIndex(int arrayIndex) |
|
117 { |
|
118 NM_FUNCTION; |
|
119 |
|
120 QString result; |
|
121 |
|
122 if ( arrayIndex >= 0 && arrayIndex < mFullFileName.count() ) { |
|
123 result.append(mFullFileName.at(arrayIndex)); |
|
124 } |
|
125 return result; |
|
126 } |
|
127 |
|
128 /*! |
|
129 Remove attachment which have same fullFileName from list |
|
130 */ |
|
131 void NmAttachmentList::removeAttachment(const QString &fullFileName) |
|
132 { |
|
133 NM_FUNCTION; |
|
134 |
|
135 for (int i=0; i<count(); ++i) { |
|
136 if (mFullFileName.at(i) == fullFileName) { |
|
137 removeAttachment(i); |
|
138 } |
|
139 } |
|
140 } |
|
141 |
|
142 /*! |
|
143 Remove attachment which have same attachmentPartId from list |
|
144 */ |
|
145 void NmAttachmentList::removeAttachment(const NmId &attachmentPartId) |
|
146 { |
|
147 NM_FUNCTION; |
|
148 |
|
149 for (int i=0; i<count(); ++i) { |
|
150 if (mAttachmentPartId.at(i) == attachmentPartId) { |
|
151 removeAttachment(i); |
|
152 } |
|
153 } |
|
154 } |
|
155 |
|
156 /*! |
|
157 Clear attachment list from UI and from data structure |
|
158 */ |
|
159 void NmAttachmentList::clearList() |
|
160 { |
|
161 NM_FUNCTION; |
|
162 |
|
163 for (int i=count()-1; i>=0; --i) { |
|
164 // Remove from UI |
|
165 mListWidget.removeAttachment(i); |
|
166 // Remove from data structure |
|
167 mFullFileName.removeAt(i); |
|
168 mDisplayFileName.removeAt(i); |
|
169 mAttachmentPartId.removeAt(i); |
|
170 } |
|
171 } |
|
172 |
|
173 /*! |
|
174 Return attachment list widget |
|
175 */ |
|
176 NmAttachmentListWidget& NmAttachmentList::listWidget() |
|
177 { |
|
178 NM_FUNCTION; |
|
179 |
|
180 return mListWidget; |
|
181 } |
|
182 |
|
183 /*! |
|
184 Return attachment count |
|
185 */ |
|
186 int NmAttachmentList::count() |
|
187 { |
|
188 NM_FUNCTION; |
|
189 |
|
190 return mFullFileName.count(); |
|
191 } |
|
192 |
|
193 /*! |
|
194 Return attachment part id |
|
195 */ |
|
196 NmId NmAttachmentList::nmIdByIndex(int listIndex) |
|
197 { |
|
198 NM_FUNCTION; |
|
199 |
|
200 return mAttachmentPartId.at(listIndex); |
|
201 } |
|
202 |
|
203 /*! |
|
204 Return array index of attachment |
|
205 */ |
|
206 int NmAttachmentList::indexByNmId(const NmId &id) |
|
207 { |
|
208 NM_FUNCTION; |
|
209 |
|
210 for (int i=0; i<count(); ++i) { |
|
211 if (mAttachmentPartId.at(i) == id) { |
|
212 return i; |
|
213 } |
|
214 } |
|
215 return -1; |
|
216 } |
|
217 |
|
218 /*! |
|
219 Remove file path from full filename |
|
220 */ |
|
221 QString NmAttachmentList::fullNameToDisplayName(const QString &fullName) |
|
222 { |
|
223 NM_FUNCTION; |
|
224 |
|
225 return fullName.section('\\', -1); |
|
226 } |