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: Utility class for cpcategorymodel. |
|
15 * |
|
16 */ |
|
17 #include "cpcategorymodelutility.h" |
|
18 #include <QString> |
|
19 #include <QDir> |
|
20 #include <QFileInfo> |
|
21 #include <hbinstance.h> |
|
22 #include <cppluginloader.h> |
|
23 #include <cpplugininterface.h> |
|
24 #include <cpbasepath.h> |
|
25 #include <cplogger.h> |
|
26 #include <cpevent.h> |
|
27 #include <cptaskexecutor.h> |
|
28 #include <cpsettingformentryitemdata.h> |
|
29 #include "cpcategorysettingformitemdata.h" |
|
30 #include "cppluginconfigreader.h" |
|
31 #include "cpplaceholderitemdata.h" |
|
32 |
|
33 static HbMainWindow *mainWindow() |
|
34 { |
|
35 QList< HbMainWindow* > mainWindows = hbInstance->allMainWindows(); |
|
36 if (!mainWindows.isEmpty()) { |
|
37 return mainWindows.front(); |
|
38 } |
|
39 return 0; |
|
40 } |
|
41 |
|
42 |
|
43 //#define ASYNC_LOAD_CPPLUGIN |
|
44 |
|
45 class CpLoadConfigPluginsTask : public CpTask |
|
46 { |
|
47 public: |
|
48 CpLoadConfigPluginsTask(HbDataFormModelItem *parentItem, |
|
49 const QString &configFile, |
|
50 CpItemDataHelper *itemDataHelper, |
|
51 int startPosition) : |
|
52 mParentItem(parentItem), |
|
53 mConfigFile(configFile), |
|
54 mItemDataHelper(itemDataHelper), |
|
55 mStartPosition(startPosition) |
|
56 { |
|
57 |
|
58 } |
|
59 |
|
60 virtual ~CpLoadConfigPluginsTask() |
|
61 { |
|
62 |
|
63 } |
|
64 |
|
65 virtual void execute(volatile bool *stopped) |
|
66 { |
|
67 if (!mParentItem) { |
|
68 return; |
|
69 } |
|
70 |
|
71 QString configPath(mConfigFile); |
|
72 QFileInfo fi(mConfigFile); |
|
73 //if it is a relative path, search the config file from device drives. |
|
74 if (!fi.isAbsolute()) { |
|
75 QStringList dirs = CpCategoryModelUtility::configFileDirectories(); |
|
76 foreach(const QString &dir,dirs) { |
|
77 configPath = dir + fi.fileName(); |
|
78 if (QFileInfo(configPath).exists()) { |
|
79 CPFW_LOG(configPath + " has been found."); |
|
80 break; |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 QList<CpPluginConfig> pluginConfigs = CpPluginConfigReader(configPath).readCpPluginConfigs(); |
|
86 |
|
87 int position = mStartPosition; |
|
88 foreach(const CpPluginConfig &pluginConfig, pluginConfigs) { |
|
89 if (stopped && *stopped) { |
|
90 break; |
|
91 } |
|
92 |
|
93 CPFW_LOG(QLatin1String("Load plugin: ") + pluginConfig.mPluginFile + " from " + configPath); |
|
94 |
|
95 CpCreatePluginItemDataEvent *event = new CpCreatePluginItemDataEvent; |
|
96 |
|
97 event->mParentItem = mParentItem; |
|
98 |
|
99 if (mStartPosition < 0) { |
|
100 event->mItemPosition = mStartPosition; //append |
|
101 } |
|
102 else { |
|
103 event->mItemPosition = position++; |
|
104 } |
|
105 |
|
106 event->mItemDataHelper = mItemDataHelper; |
|
107 |
|
108 event->mDisplayName = pluginConfig.mDisplayName; |
|
109 event->mDescription = pluginConfig.mDescription; |
|
110 event->mPluginFile = pluginConfig.mPluginFile; |
|
111 |
|
112 //firstly, handle CpPluginInterface |
|
113 if (CpPluginInterface *plugin = CpPluginLoader::loadCpPluginInterface(pluginConfig.mPluginFile)) { |
|
114 CPFW_LOG("Load root component CpPluginInterface succeed."); |
|
115 event->mPluginInterface = plugin; |
|
116 } |
|
117 |
|
118 //post event to main window in main thread |
|
119 QCoreApplication::postEvent(mainWindow(),event); |
|
120 |
|
121 } //end foreach |
|
122 |
|
123 } |
|
124 private: |
|
125 HbDataFormModelItem *mParentItem; |
|
126 QString mConfigFile; |
|
127 CpItemDataHelper *mItemDataHelper; |
|
128 int mStartPosition; |
|
129 }; |
|
130 |
|
131 void CpCategoryModelUtility::buildConfigPluginItems(HbDataFormModelItem *parent, |
|
132 const QString &configFile, |
|
133 CpItemDataHelper &itemDataHelper, |
|
134 int startPosition) |
|
135 { |
|
136 #ifdef ASYNC_LOAD_CPPLUGIN |
|
137 CpTaskExecutor::globalInstance()->runTask |
|
138 ( new CpLoadConfigPluginsTask(parent,configFile,&itemDataHelper,startPosition) ); |
|
139 |
|
140 #else |
|
141 if (!parent) { |
|
142 return; |
|
143 } |
|
144 |
|
145 QString configPath(configFile); |
|
146 QFileInfo fi(configFile); |
|
147 //if it is a relative path, search the config file from device drives. |
|
148 if (!fi.isAbsolute()) { |
|
149 QStringList dirs = CpCategoryModelUtility::configFileDirectories(); |
|
150 foreach(const QString &dir,dirs) { |
|
151 configPath = dir + fi.fileName(); |
|
152 if (QFileInfo(configPath).exists()) { |
|
153 CPFW_LOG(configPath + " has been found."); |
|
154 break; |
|
155 } |
|
156 } |
|
157 } |
|
158 |
|
159 QList<CpPluginConfig> pluginConfigs = CpPluginConfigReader(configPath).readCpPluginConfigs(); |
|
160 |
|
161 foreach(const CpPluginConfig &pluginConfig, pluginConfigs) { |
|
162 CPFW_LOG(QLatin1String("Load plugin: ") + pluginConfig.mPluginFile + " from " + configPath); |
|
163 |
|
164 QList<CpSettingFormItemData*> itemDataList; |
|
165 |
|
166 //firstly, handle CpPluginInterface |
|
167 if (CpPluginInterface *plugin = CpPluginLoader::loadCpPluginInterface(pluginConfig.mPluginFile)) { |
|
168 CPFW_LOG("Load root component CpPluginInterface succeed."); |
|
169 itemDataList = plugin->createSettingFormItemData(itemDataHelper); |
|
170 } |
|
171 |
|
172 else { |
|
173 CPFW_LOG(QLatin1String("Load plugin:") + pluginConfig.mPluginFile + QLatin1String(" failed.")); |
|
174 #ifdef _DEBUG |
|
175 CPFW_LOG(QLatin1String("***Add a placeholder.")); |
|
176 itemDataList.append(new CpPlaceHolderItemData(itemDataHelper,pluginConfig)); |
|
177 #endif |
|
178 } |
|
179 |
|
180 foreach(CpSettingFormItemData *itemData,itemDataList) { |
|
181 if (itemData) { |
|
182 //append the new created setting form item to its parent item. |
|
183 parent->insertChild(startPosition++,itemData); |
|
184 //commented this for only loading group plugin when startup |
|
185 /*if (CpCategorySettingFormItemData *categoryItemData |
|
186 = qobject_cast<CpCategorySettingFormItemData*>(itemData)) { |
|
187 categoryItemData->initialize(itemDataHelper); |
|
188 }*/ |
|
189 |
|
190 //set the text and description from config if it is empty. |
|
191 setEntryItemContentIfEmpty(itemData,pluginConfig.mDisplayName,pluginConfig.mDescription); |
|
192 } |
|
193 } //end foreach |
|
194 |
|
195 } //end foreach |
|
196 #endif |
|
197 |
|
198 } |
|
199 |
|
200 bool CpCategoryModelUtility::setEntryItemContentIfEmpty(CpSettingFormItemData *itemData, |
|
201 const QString &displayName, |
|
202 const QString &description) |
|
203 { |
|
204 CpSettingFormEntryItemData *entryItemData = qobject_cast< CpSettingFormEntryItemData* > (itemData); |
|
205 if (!entryItemData) { |
|
206 return false; |
|
207 } |
|
208 |
|
209 if (entryItemData->text().isEmpty()) { |
|
210 entryItemData->setText(displayName); |
|
211 } |
|
212 if (entryItemData->description().isEmpty()) { |
|
213 entryItemData->setDescription(description); |
|
214 } |
|
215 if (entryItemData->iconName().isEmpty()) { |
|
216 entryItemData->setIconName( |
|
217 QLatin1String(":/icon/qgn_prop_set_default_sub.svg") ); |
|
218 } |
|
219 |
|
220 return true; |
|
221 } |
|
222 |
|
223 QStringList CpCategoryModelUtility::drives() |
|
224 { |
|
225 static QStringList drives; |
|
226 |
|
227 if (drives.empty()) { |
|
228 CPFW_LOG("device drives:"); |
|
229 #ifdef WIN32 |
|
230 drives.append("C:"); |
|
231 CPFW_LOG("C:"); |
|
232 #else |
|
233 QFileInfoList fileInfoList = QDir::drives(); |
|
234 foreach(const QFileInfo &fileInfo,fileInfoList) { |
|
235 QString str = fileInfo.filePath(); |
|
236 if (str.length() > 2) { |
|
237 str = str.left(2); |
|
238 } |
|
239 drives.append(str); |
|
240 CPFW_LOG(str); |
|
241 } |
|
242 #endif |
|
243 } |
|
244 |
|
245 return drives; |
|
246 } |
|
247 |
|
248 static QStringList directoriesFromAllDrives(const QString &baseDir) |
|
249 { |
|
250 QStringList dirs; |
|
251 |
|
252 QStringList drives = CpCategoryModelUtility::drives(); |
|
253 foreach(const QString &drive,drives) { |
|
254 QString dir = drive + baseDir + QDir::separator(); |
|
255 if (QFileInfo(dir).exists()) { |
|
256 dirs.append(dir); |
|
257 CPFW_LOG(dir); |
|
258 } |
|
259 } |
|
260 |
|
261 return dirs; |
|
262 } |
|
263 |
|
264 QStringList CpCategoryModelUtility::pluginDirectories() |
|
265 { |
|
266 static QStringList dirs; |
|
267 if (dirs.empty()) { |
|
268 CPFW_LOG("ControlPanel plugin derectories:") |
|
269 dirs = directoriesFromAllDrives(CP_PLUGIN_PATH); |
|
270 } |
|
271 return dirs; |
|
272 } |
|
273 |
|
274 QStringList CpCategoryModelUtility::configFileDirectories() |
|
275 { |
|
276 static QStringList dirs; |
|
277 if (dirs.empty()) { |
|
278 CPFW_LOG("ControlPanel configuration file derectories:"); |
|
279 dirs = directoriesFromAllDrives(CP_PLUGIN_CONFIG_PATH); |
|
280 } |
|
281 return dirs; |
|
282 } |
|
283 |
|
284 CP_CATEGORY_EXPORT int createCpPluginItemData(CpCreatePluginItemDataEvent *event) |
|
285 { |
|
286 QList<CpSettingFormItemData*> itemDataList; |
|
287 |
|
288 if (event->mPluginInterface) { |
|
289 itemDataList = event->mPluginInterface->createSettingFormItemData(*(event->mItemDataHelper)); |
|
290 } |
|
291 |
|
292 else { |
|
293 #ifdef _DEBUG |
|
294 itemDataList.append(new CpPlaceHolderItemData(*(event->mItemDataHelper),event->mDisplayName,event->mPluginFile)); |
|
295 #endif |
|
296 } |
|
297 |
|
298 foreach(CpSettingFormItemData *itemData,itemDataList) { |
|
299 if (itemData) { |
|
300 //append the new created setting form item to its parent item. |
|
301 if (event->mItemPosition < 0) { |
|
302 event->mParentItem->appendChild(itemData); |
|
303 } |
|
304 else { |
|
305 event->mParentItem->insertChild(event->mItemPosition,itemData); |
|
306 } |
|
307 |
|
308 if (CpCategorySettingFormItemData *categoryItemData |
|
309 = qobject_cast<CpCategorySettingFormItemData*>(itemData)) { |
|
310 categoryItemData->initialize(*(event->mItemDataHelper)); |
|
311 } |
|
312 |
|
313 //set the text and description from config if it is empty. |
|
314 CpCategoryModelUtility::setEntryItemContentIfEmpty(itemData,event->mDisplayName,event->mDescription); |
|
315 } |
|
316 } //end foreach |
|
317 |
|
318 return itemDataList.count(); |
|
319 } |
|
320 |
|
321 //End of File |
|
322 |
|