|
1 /* |
|
2 * Copyright (c) 2010 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: Base class for cache data fetchers. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <cntabstractfetcher.h> |
|
19 #include <cntdebug.h> |
|
20 |
|
21 /*! |
|
22 \class CntAbstractCacheItem |
|
23 \brief Base class for cache data for one contact. |
|
24 */ |
|
25 |
|
26 /*! |
|
27 \class CntAbstractFetcher |
|
28 \brief CntAbstractFetcher is a base class for cache data fetchers. |
|
29 |
|
30 CntAbstractFetcher implements a priority queue for scheduling jobs. |
|
31 A job is added using scheduleJob() and executed using processNextJob(). |
|
32 If too many jobs are scheduled, the oldest jobs (not the ones with lowest |
|
33 priority) are cancelled. However, the cancelled jobs will be informed |
|
34 back to the client later so that it can reschedule them if needed. |
|
35 */ |
|
36 |
|
37 /*! |
|
38 Constructs a CntAbstractFetcher object. |
|
39 */ |
|
40 CntAbstractFetcher::CntAbstractFetcher(int maxAmountOfJobs) |
|
41 { |
|
42 CNT_ENTRY |
|
43 |
|
44 // set the max amount of jobs |
|
45 mMaxAmountOfJobs = maxAmountOfJobs; |
|
46 |
|
47 CNT_EXIT |
|
48 } |
|
49 |
|
50 /*! |
|
51 Cleans up and destructs the CntIconFetcher object. |
|
52 */ |
|
53 CntAbstractFetcher::~CntAbstractFetcher() |
|
54 { |
|
55 CNT_ENTRY |
|
56 |
|
57 disconnect(this); |
|
58 |
|
59 for (int i = 0; i < mJobs.count(); ++i) { |
|
60 delete mJobs.at(i).first; |
|
61 } |
|
62 mJobs.clear(); |
|
63 |
|
64 qDeleteAll(mCancelledJobs); |
|
65 mCancelledJobs.clear(); |
|
66 |
|
67 CNT_EXIT |
|
68 } |
|
69 |
|
70 /*! |
|
71 Schedules a job. A fetched() signal will be emitted when the job |
|
72 has been successfully completed; a failed() signal is sent if the |
|
73 job cannot be completed. |
|
74 |
|
75 The oldest job is cancelled if there are too many scheduled jobs; in this |
|
76 case a cancelled() signal is sent. |
|
77 |
|
78 \param job the job; ownership is taken |
|
79 \return true, if the job is - or already was - scheduled |
|
80 */ |
|
81 bool CntAbstractFetcher::scheduleJob(CntAbstractJob* job, int priority) |
|
82 { |
|
83 Q_ASSERT(job != NULL); |
|
84 |
|
85 CNT_ENTRY_ARGS("job =" << job->toString() << "prio =" << priority) |
|
86 |
|
87 if (job->isEmpty()) { |
|
88 CNT_EXIT_ARGS("bad job") |
|
89 return false; |
|
90 } |
|
91 |
|
92 int index = jobIndex(job); |
|
93 if (index != NoSuchJob) { |
|
94 // if the job already exists, update the priority... |
|
95 if (priority < mJobs.at(index).second) { |
|
96 mJobs[index].second = priority; |
|
97 } |
|
98 |
|
99 // ...and notify the client that an identical job already exists |
|
100 delete job; |
|
101 CNT_EXIT_ARGS("job already exists") |
|
102 return true; |
|
103 } |
|
104 |
|
105 if (mJobs.count() >= mMaxAmountOfJobs) { |
|
106 // the queue of jobs is full, so remove the oldest job |
|
107 mCancelledJobs.append(mJobs.takeFirst().first); |
|
108 } |
|
109 |
|
110 mJobs.append(qMakePair(job, priority)); |
|
111 |
|
112 // since this job has now been scheduled, remove it from the list of |
|
113 // cancelled jobs in case it is there |
|
114 index = cancelledJobIndex(job); |
|
115 if (index != NoSuchJob) { |
|
116 delete mCancelledJobs.takeAt(index); |
|
117 } |
|
118 |
|
119 CNT_EXIT |
|
120 |
|
121 return true; |
|
122 } |
|
123 |
|
124 /*! |
|
125 \return true if there are scheduled jobs left; otherwise returns false. |
|
126 */ |
|
127 bool CntAbstractFetcher::hasScheduledJobs() |
|
128 { |
|
129 return (mJobs.count() > 0); |
|
130 } |
|
131 |
|
132 /*! |
|
133 \return true if there are cancelled jobs left; otherwise returns false. |
|
134 */ |
|
135 bool CntAbstractFetcher::hasCancelledJobs() |
|
136 { |
|
137 return (mCancelledJobs.count() > 0); |
|
138 } |
|
139 |
|
140 /*! |
|
141 Picks the next job from the list of scheduled jobs, i.e. the job |
|
142 with the highest priority. |
|
143 |
|
144 \return the next job in the job list |
|
145 */ |
|
146 CntAbstractJob * CntAbstractFetcher::takeNextJob() |
|
147 { |
|
148 CNT_ENTRY |
|
149 |
|
150 int selectionIndex = -1; |
|
151 int selectionPriority = -1; |
|
152 |
|
153 int jobCount = mJobs.count(); |
|
154 if (jobCount == 0) { |
|
155 CNT_EXIT_ARGS("no more jobs") |
|
156 return NULL; |
|
157 } |
|
158 |
|
159 for (int i = 0; i < jobCount; ++i) { |
|
160 int jobPriority = mJobs.at(i).second; |
|
161 if (jobPriority < selectionPriority || selectionPriority == -1) { |
|
162 selectionIndex = i; |
|
163 selectionPriority = jobPriority; |
|
164 } |
|
165 } |
|
166 |
|
167 CNT_EXIT_ARGS(mJobs.at(selectionIndex).first->toString()) |
|
168 |
|
169 return mJobs.takeAt(selectionIndex).first; |
|
170 } |
|
171 |
|
172 /*! |
|
173 \return the next job in the list of cancelled jobs |
|
174 */ |
|
175 CntAbstractJob * CntAbstractFetcher::takeNextCancelledJob() |
|
176 { |
|
177 CNT_ENTRY |
|
178 |
|
179 return mCancelledJobs.takeFirst(); |
|
180 } |
|
181 |
|
182 |
|
183 /*! |
|
184 Finds out the index of a scheduled job. |
|
185 |
|
186 \param job the kind of job to look for |
|
187 \return index of the job, or NoSuchJob if such a job is not scheduled. |
|
188 */ |
|
189 int CntAbstractFetcher::jobIndex(CntAbstractJob *job) |
|
190 { |
|
191 CNT_ENTRY |
|
192 |
|
193 for (int i = 0; i < mJobs.count(); ++i) { |
|
194 CntAbstractJob *scheduledJob = mJobs.at(i).first; |
|
195 if (job->equals(*scheduledJob)) { |
|
196 CNT_EXIT |
|
197 return i; |
|
198 } |
|
199 } |
|
200 |
|
201 CNT_EXIT_ARGS("no such job") |
|
202 return NoSuchJob; |
|
203 } |
|
204 |
|
205 /*! |
|
206 Finds out the index of a cancelled job. |
|
207 |
|
208 \param job the kind of job to look for |
|
209 \return index of the job, or NoSuchJob if such a job is not cancelled. |
|
210 */ |
|
211 int CntAbstractFetcher::cancelledJobIndex(CntAbstractJob *job) |
|
212 { |
|
213 CNT_ENTRY |
|
214 |
|
215 for (int i = 0; i < mCancelledJobs.count(); ++i) { |
|
216 CntAbstractJob *cancelledJob = mCancelledJobs.at(i); |
|
217 if (job->equals(*cancelledJob)) { |
|
218 CNT_EXIT |
|
219 return i; |
|
220 } |
|
221 } |
|
222 |
|
223 CNT_EXIT_ARGS("no such cancelled job") |
|
224 return NoSuchJob; |
|
225 } |