settings.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include <QDebug>
  2. #include <QDir>
  3. #include <QFile>
  4. #include <QFileInfoList>
  5. #include <QMessageBox>
  6. #include <QProcessEnvironment>
  7. #include <QRegExp>
  8. #include <QScriptValue>
  9. #include <QScriptEngine>
  10. #include <QScriptValueIterator>
  11. #include "settings.h"
  12. #define DEFAULT_PROXY_PORT 0
  13. Settings::Settings()
  14. {
  15. m_settings = new QSettings("Nokia", "Hybrid Application Generator");
  16. m_overridingSettings = new QMap<Settings::Parameter, QVariant>;
  17. }
  18. Settings::~Settings()
  19. {
  20. delete m_settings;
  21. delete m_overridingSettings;
  22. }
  23. /**
  24. * Returns a QVariant configuration parameter associated with the given argument.
  25. * If no value is saved yet, a default value will be returned instead.
  26. */
  27. QVariant Settings::get(Parameter key,
  28. QString app)
  29. {
  30. // Check if the key is found in the temporary settings
  31. if (instance().m_overridingSettings->contains(key))
  32. {
  33. return instance().m_overridingSettings->value(key);
  34. }
  35. QString settingKey = getKey(key,
  36. app);
  37. QVariant value;
  38. if (key == ShowFullScreen)
  39. {
  40. // Special cases not saved in QSettings
  41. LayoutMode mode;
  42. mode = (LayoutMode) get(WidgetLayout).toInt();
  43. if (mode == FullScreenWithSoftKeys || mode == FullScreenWithoutSoftKeys)
  44. return true;
  45. else
  46. return false;
  47. }
  48. else if (key == ShowSoftKeys)
  49. {
  50. // Special cases not saved in QSettings
  51. LayoutMode mode;
  52. mode = (LayoutMode) get(WidgetLayout).toInt();
  53. if (mode == FullScreenWithoutSoftKeys)
  54. return false;
  55. else
  56. return true;
  57. }
  58. else if (key == QtVersionString)
  59. {
  60. // Check version. Recognizes style "Using Qt version 4.6.2"
  61. QString version = "<not detected>";
  62. QProcess process;
  63. process.start("cmd /C \"" + get(Settings::QtEnvBatPath).toString() + " & qmake --version\"");
  64. process.waitForFinished();
  65. QString output = process.readAllStandardOutput();
  66. QRegExp versionRx;
  67. versionRx.setPattern("^.*([0-9].[0-9].[0-9])");
  68. if (versionRx.indexIn(output) != -1)
  69. {
  70. version = versionRx.cap(1);
  71. }
  72. return version;
  73. }
  74. else if (key == QtVersion)
  75. {
  76. // Check version. Recognizes style "Using Qt version 4.6.2"
  77. int version = -1;
  78. QRegExp versionRx;
  79. versionRx.setPattern("^.*([0-9]).([0-9]).([0-9])");
  80. if (versionRx.indexIn(get(QtVersionString).toString()) != -1)
  81. {
  82. version = versionRx.cap(1).toInt() * 100 +
  83. versionRx.cap(2).toInt() * 10 +
  84. versionRx.cap(3).toInt();
  85. }
  86. return version;
  87. }
  88. else
  89. {
  90. // Normal settings, saved in QSettings
  91. if (instance().getSettingObject()->contains(settingKey))
  92. {
  93. // Already saved in settings, just return
  94. return instance().getSettingObject()->value(getKey(key, app));
  95. }
  96. else
  97. {
  98. // Provide appropriate default values
  99. switch (key) {
  100. case UID3:
  101. case LastWidgetPath:
  102. case LastDirectoryPath:
  103. value = "";
  104. break;
  105. case FullS60SDK:
  106. value = false;
  107. break;
  108. case PanningEnabled:
  109. value = false;
  110. break;
  111. case TextSelectionEnabled:
  112. value = true;
  113. break;
  114. case WidgetLayout:
  115. value = 0;
  116. break;
  117. case LogFilePath:
  118. value = QDir::toNativeSeparators(QDir::currentPath() + "/" + GENERATOR_LOG_FILE);
  119. break;
  120. case QtEnvBatPath:
  121. value = instance().guessQtEnvPath();
  122. break;
  123. case MaddePath:
  124. value = "";
  125. break;
  126. case HybridAuthor:
  127. value = "";
  128. break;
  129. case HybridEmail:
  130. value = "";
  131. break;
  132. case Licence:
  133. value = "lgpl";
  134. break;
  135. case HybridShortDesc:
  136. value = "Hybrid App (short desc)";
  137. break;
  138. case HybridLongDesc:
  139. value = "Hybrid App (long desc)";
  140. break;
  141. case ForumNokiaUserName:
  142. value = "";
  143. break;
  144. case ProxyConfig:
  145. value = PCK_UndefinedProxyConfig;
  146. break;
  147. case ProxyUrl:
  148. value = "";
  149. break;
  150. case ProxyPort:
  151. value = DEFAULT_PROXY_PORT;
  152. break;
  153. case ProxyUserName:
  154. value = "";
  155. break;
  156. case ProxyPassword:
  157. value = "";
  158. break;
  159. case RcProperties:
  160. value = "";
  161. break;
  162. default:
  163. value = false;
  164. break;
  165. }
  166. instance().set(key, value, app);
  167. return value;
  168. }
  169. }
  170. }
  171. /**
  172. * Saves the given parameter with the provided value.
  173. */
  174. void Settings::set(Parameter key,
  175. QVariant value,
  176. QString app)
  177. {
  178. if ((key == HybridShortDesc || key == HybridLongDesc)
  179. && app.length() == 0)
  180. {
  181. // these settings are supported to be app specific,
  182. // not saving them
  183. return;
  184. }
  185. instance().getSettingObject()->setValue(getKey(key, app), value);
  186. }
  187. /**
  188. * Saves the given parameter with the provided value as a temporary
  189. * setting -- i.e. the setting will not be saved to the permanent
  190. * QSetting configuration.
  191. */
  192. void Settings::setTemporary(Parameter key, QVariant value)
  193. {
  194. if (instance().m_overridingSettings->contains(key))
  195. {
  196. instance().m_overridingSettings->remove(key);
  197. }
  198. instance().m_overridingSettings->insert(key, value);
  199. }
  200. /**
  201. * Returns QSettings object that is used to actually store
  202. * the configuration.
  203. */
  204. QSettings* Settings::getSettingObject()
  205. {
  206. return m_settings;
  207. }
  208. /**
  209. * Return a QString associated with the given key. The QString
  210. * will be used as a key to access the QSettings value.
  211. */
  212. QString Settings::getKey(Parameter key,
  213. const QString & app)
  214. {
  215. switch (key) {
  216. case QtEnvBatPath:
  217. return "Environment/QtEnvBatPath";
  218. case FullS60SDK:
  219. return "Environment/FullS60SDK";
  220. case LogFilePath:
  221. return "Environment/LogFilePath";
  222. case LastWidgetPath:
  223. return "Environment/LastWidgetPath";
  224. case LastDirectoryPath:
  225. return "Environment/LastDirectoryPath";
  226. case UID3:
  227. return "Project/UID3";
  228. case PanningEnabled:
  229. return "Project/PanningEnabled";
  230. case TextSelectionEnabled:
  231. return "Project/TextSelectionEnabled";
  232. case WidgetLayout:
  233. return "Project/WidgetLayout";
  234. case MaddePath:
  235. return "Environment/MaddePath";
  236. case HybridAuthor:
  237. return "Project/HybridAuthor";
  238. case HybridEmail:
  239. return "Project/HybridEmail";
  240. case Licence:
  241. return "Project/Licence";
  242. case HybridShortDesc:
  243. return QString("Project/%1/HybridShortDesc").arg(app);
  244. case HybridLongDesc:
  245. return QString("Project/%1/HybridLongDesc").arg(app);
  246. case ForumNokiaUserName:
  247. return "Environment/Network/ForumNokiaUserName";
  248. case ProxyConfig:
  249. return "Environment/Network/ProxyConfig";
  250. case ProxyUrl:
  251. return "Environment/Network/ProxyUrl";
  252. case ProxyPort:
  253. return "Environment/Network/ProxyPort";
  254. case ProxyUserName:
  255. return "Environment/Network/ProxyUserName";
  256. case ProxyPassword:
  257. return "Environment/Network/ProxyPassword";
  258. case RcProperties:
  259. return "RemoteCompiler/Properties";
  260. default:
  261. return "Default";
  262. }
  263. }
  264. /**
  265. * Tries to guess the path to qtenv.bat. First checks the QT_ENV_BAT environmental
  266. * variable, and if the file given by it exists, the value will be used. If no such
  267. * env variable exists, tries to use Qt path from PATH to find the bat file. Gives
  268. * up if still no hit.
  269. */
  270. QString Settings::guessQtEnvPath() const
  271. {
  272. #ifdef Q_OS_WIN32
  273. // First, guess Nokia Qt SDK path from registry if we're on Windows
  274. // This would be the preferred development environment
  275. QSettings installation("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Nokia Qt SDK", QSettings::NativeFormat);
  276. QString location = installation.value("InstallLocation").toString();
  277. if (!location.isEmpty())
  278. {
  279. location += "\\Symbian\\SDK\\bin\\qtenv2.bat";
  280. if (QFile::exists(location))
  281. {
  282. // Found Nokia Qt SDK's qtenv2.bat!
  283. return QDir::toNativeSeparators(location);
  284. }
  285. }
  286. #endif
  287. QString path = "";
  288. // Settings do not contain env bat. Check the env for QT_ENV_BAT, and
  289. // then for Qt path, and use either if found. Leave empty otherwise.
  290. QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
  291. if (environment.contains("QT_ENV_BAT") && QFile::exists(environment.value("QT_ENV_BAT")))
  292. {
  293. // Nice, the qtenv.bat was found straight from the env
  294. path = environment.value("QT_ENV_BAT");
  295. }
  296. else
  297. {
  298. // Look for Qt directories in PATH, check them for .bat
  299. QRegExp rx("([^;]*Qt[^;]*bin)");
  300. int pos = 0;
  301. while ((pos = rx.indexIn(environment.value("PATH"), pos)) != -1)
  302. {
  303. QString envTest = rx.cap(1) + "\\qtenv.bat";
  304. if (QFile::exists(envTest))
  305. {
  306. path = QDir::toNativeSeparators(envTest);
  307. break;
  308. }
  309. pos += rx.matchedLength();
  310. }
  311. }
  312. return path;
  313. }
  314. namespace
  315. {
  316. QScriptValue GetRcpScriptValue(QScriptEngine & scriptEngine)
  317. {
  318. QScriptValue
  319. rv;
  320. QString
  321. rcp(Settings::get(Settings::RcProperties).toString());
  322. if (rcp.length() > 0)
  323. {
  324. rcp.prepend("(");
  325. rcp.append(")");
  326. rv = scriptEngine.evaluate(rcp);
  327. if (scriptEngine.hasUncaughtException()) {
  328. rv = QScriptValue();
  329. }
  330. }
  331. return rv;
  332. }
  333. }
  334. Settings::OsDescMap Settings::RcOSes(QScriptEngine & scriptEngine)
  335. {
  336. OsDescMap
  337. rv;
  338. QScriptValue
  339. rcpsv(GetRcpScriptValue(scriptEngine));
  340. if (!rcpsv.isUndefined())
  341. {
  342. QScriptValue
  343. titles = rcpsv.property("titles").property("os");
  344. QScriptValueIterator
  345. it(titles);
  346. while (it.hasNext())
  347. {
  348. it.next();
  349. /* OBS
  350. QPair<QString, QString>
  351. ent;
  352. */
  353. QString
  354. os = it.name();
  355. QString
  356. desc = it.value().toString();
  357. /*
  358. ent.first = os;
  359. ent.second = desc;
  360. rv.append(ent);
  361. */
  362. rv.insert(os,
  363. desc);
  364. }
  365. }
  366. return rv;
  367. }
  368. Settings::OsQtPairList Settings::RcOsQtPairs(QScriptEngine & scriptEngine)
  369. {
  370. OsQtPairList
  371. rv;
  372. QScriptValue
  373. rcpsv(GetRcpScriptValue(scriptEngine));
  374. if (!rcpsv.isUndefined())
  375. {
  376. QScriptValue
  377. titles = rcpsv.property("titles").property("os");
  378. QScriptValue
  379. maps = rcpsv.property("map").property("os");
  380. QScriptValueIterator
  381. it(maps);
  382. while (it.hasNext())
  383. {
  384. it.next();
  385. QString
  386. os = it.name();
  387. QScriptValueIterator
  388. qtit(it.value().property("qt"));
  389. while (qtit.hasNext()) {
  390. qtit.next();
  391. QString
  392. qt = qtit.name();
  393. /*
  394. ent.first = os;
  395. ent.second = QString("pSet[os]=%1&pSet[qt]=%2").arg(os, qt);
  396. rv.append(ent);
  397. */
  398. if (os == "maemo5" || qt != "4_6_2")
  399. rv.append(OsQtPair(os, qt));
  400. }
  401. }
  402. }
  403. return rv;
  404. }
  405. Settings::OsQtPair::OsQtPair(QString os,
  406. QString qt)
  407. : m_os(os)
  408. , m_qt(qt)
  409. {
  410. ;
  411. }
  412. QString Settings::OsQtPair::wccParams() const
  413. {
  414. return QString("pSet[os]=%1&pSet[qt]=%2").arg(m_os, m_qt);
  415. }
  416. bool Settings::IsRcConfigured()
  417. {
  418. bool
  419. rv = false;
  420. QString
  421. forumNokiaUserName = get(ForumNokiaUserName).toString();
  422. if (forumNokiaUserName.length() > 0)
  423. {
  424. ProxyConfigKind
  425. proxyConfig = static_cast<ProxyConfigKind>(get(ProxyConfig).toInt());
  426. if (proxyConfig == PCK_ManualProxy)
  427. {
  428. QString
  429. url = get(ProxyUrl).toString(),
  430. userName = get(ProxyUserName).toString(),
  431. password = get(ProxyPassword).toString();
  432. int
  433. port = get(ProxyPort).toInt();
  434. rv = url.length() > 0
  435. && userName.length() > 0
  436. && password.length() > 0
  437. && port != DEFAULT_PROXY_PORT;
  438. }
  439. else
  440. {
  441. rv = proxyConfig != Settings::PCK_UndefinedProxyConfig;
  442. }
  443. }
  444. return rv;
  445. }