The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

984 lines
31KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_Project.h"
  19. #include "jucer_ProjectType.h"
  20. #include "../Project Saving/jucer_ProjectExporter.h"
  21. #include "../Project Saving/jucer_ProjectSaver.h"
  22. #include "../Application/jucer_OpenDocumentManager.h"
  23. //==============================================================================
  24. namespace Tags
  25. {
  26. const Identifier projectRoot ("JUCERPROJECT");
  27. const Identifier projectMainGroup ("MAINGROUP");
  28. const Identifier group ("GROUP");
  29. const Identifier file ("FILE");
  30. const Identifier exporters ("EXPORTFORMATS");
  31. const Identifier configGroup ("JUCEOPTIONS");
  32. const Identifier modulesGroup ("MODULES");
  33. const Identifier module ("MODULE");
  34. }
  35. const char* Project::projectFileExtension = ".jucer";
  36. //==============================================================================
  37. Project::Project (const File& file_)
  38. : FileBasedDocument (projectFileExtension,
  39. String ("*") + projectFileExtension,
  40. "Choose a Jucer project to load",
  41. "Save Jucer project"),
  42. projectRoot (Tags::projectRoot)
  43. {
  44. setFile (file_);
  45. removeDefunctExporters();
  46. setMissingDefaultValues();
  47. setChangedFlag (false);
  48. mainProjectIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  49. projectRoot.addListener (this);
  50. }
  51. Project::~Project()
  52. {
  53. projectRoot.removeListener (this);
  54. OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false);
  55. }
  56. //==============================================================================
  57. void Project::setTitle (const String& newTitle)
  58. {
  59. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  60. getMainGroup().getNameValue() = newTitle;
  61. }
  62. String Project::getDocumentTitle()
  63. {
  64. return getProjectName().toString();
  65. }
  66. void Project::updateProjectSettings()
  67. {
  68. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  69. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  70. }
  71. void Project::setMissingDefaultValues()
  72. {
  73. if (! projectRoot.hasProperty (ComponentBuilder::idProperty))
  74. projectRoot.setProperty (ComponentBuilder::idProperty, createAlphaNumericUID(), nullptr);
  75. // Create main file group if missing
  76. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  77. {
  78. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  79. projectRoot.addChild (mainGroup.state, 0, 0);
  80. }
  81. getMainGroup().initialiseMissingProperties();
  82. if (getDocumentTitle().isEmpty())
  83. setTitle ("JUCE Project");
  84. if (! projectRoot.hasProperty (Ids::projectType))
  85. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  86. if (! projectRoot.hasProperty (Ids::version))
  87. getVersionValue() = "1.0.0";
  88. updateOldStyleConfigList();
  89. moveOldPropertyFromProjectToAllExporters (Ids::bigIcon);
  90. moveOldPropertyFromProjectToAllExporters (Ids::smallIcon);
  91. getProjectType().setMissingProjectProperties (*this);
  92. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  93. addDefaultModules (false);
  94. }
  95. void Project::updateOldStyleConfigList()
  96. {
  97. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (ProjectExporter::configurations));
  98. if (deprecatedConfigsList.isValid())
  99. {
  100. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  101. for (Project::ExporterIterator exporter (*this); exporter.next();)
  102. {
  103. if (exporter->getNumConfigurations() == 0)
  104. {
  105. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  106. if (! exporter->isXcode())
  107. {
  108. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  109. {
  110. ValueTree config (newConfigs.getChild(j));
  111. config.removeProperty (Ids::osxSDK, nullptr);
  112. config.removeProperty (Ids::osxCompatibility, nullptr);
  113. config.removeProperty (Ids::osxArchitecture, nullptr);
  114. }
  115. }
  116. exporter->settings.addChild (newConfigs, 0, nullptr);
  117. }
  118. }
  119. }
  120. }
  121. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  122. {
  123. if (projectRoot.hasProperty (name))
  124. {
  125. for (Project::ExporterIterator exporter (*this); exporter.next();)
  126. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  127. projectRoot.removeProperty (name, nullptr);
  128. }
  129. }
  130. void Project::removeDefunctExporters()
  131. {
  132. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  133. for (;;)
  134. {
  135. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  136. if (oldVC6Exporter.isValid())
  137. exporters.removeChild (oldVC6Exporter, nullptr);
  138. else
  139. break;
  140. }
  141. }
  142. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  143. {
  144. addModule ("juce_core", shouldCopyFilesLocally);
  145. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  146. {
  147. addModule ("juce_events", shouldCopyFilesLocally);
  148. addModule ("juce_graphics", shouldCopyFilesLocally);
  149. addModule ("juce_data_structures", shouldCopyFilesLocally);
  150. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  151. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  152. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  153. addModule ("juce_cryptography", shouldCopyFilesLocally);
  154. addModule ("juce_video", shouldCopyFilesLocally);
  155. addModule ("juce_opengl", shouldCopyFilesLocally);
  156. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  157. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  158. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  159. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  160. }
  161. }
  162. bool Project::isAudioPluginModuleMissing() const
  163. {
  164. return getProjectType().isAudioPlugin()
  165. && ! isModuleEnabled ("juce_audio_plugin_client");
  166. }
  167. //==============================================================================
  168. static void registerRecentFile (const File& file)
  169. {
  170. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  171. StoredSettings::getInstance()->recentFiles.addFile (file);
  172. StoredSettings::getInstance()->flush();
  173. }
  174. Result Project::loadDocument (const File& file)
  175. {
  176. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  177. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  178. return Result::fail ("Not a valid Jucer project!");
  179. ValueTree newTree (ValueTree::fromXml (*xml));
  180. if (! newTree.hasType (Tags::projectRoot))
  181. return Result::fail ("The document contains errors and couldn't be parsed!");
  182. registerRecentFile (file);
  183. projectRoot = newTree;
  184. removeDefunctExporters();
  185. setMissingDefaultValues();
  186. return Result::ok();
  187. }
  188. Result Project::saveDocument (const File& file)
  189. {
  190. return saveProject (file, false);
  191. }
  192. Result Project::saveProject (const File& file, bool isCommandLineApp)
  193. {
  194. updateProjectSettings();
  195. sanitiseConfigFlags();
  196. if (! isCommandLineApp)
  197. registerRecentFile (file);
  198. ProjectSaver saver (*this, file);
  199. return saver.save (! isCommandLineApp);
  200. }
  201. Result Project::saveResourcesOnly (const File& file)
  202. {
  203. ProjectSaver saver (*this, file);
  204. return saver.saveResourcesOnly();
  205. }
  206. //==============================================================================
  207. static File lastDocumentOpened;
  208. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  209. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  210. //==============================================================================
  211. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  212. {
  213. if (property == Ids::projectType)
  214. setMissingDefaultValues();
  215. changed();
  216. }
  217. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  218. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&) { changed(); }
  219. void Project::valueTreeChildOrderChanged (ValueTree&) { changed(); }
  220. void Project::valueTreeParentChanged (ValueTree&) {}
  221. //==============================================================================
  222. File Project::resolveFilename (String filename) const
  223. {
  224. if (filename.isEmpty())
  225. return File::nonexistent;
  226. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  227. .replaceCharacter ('\\', '/');
  228. if (FileHelpers::isAbsolutePath (filename))
  229. return File::createFileWithoutCheckingPath (filename); // (avoid assertions for windows-style paths)
  230. return getFile().getSiblingFile (filename);
  231. }
  232. String Project::getRelativePathForFile (const File& file) const
  233. {
  234. String filename (file.getFullPathName());
  235. File relativePathBase (getFile().getParentDirectory());
  236. String p1 (relativePathBase.getFullPathName());
  237. String p2 (file.getFullPathName());
  238. while (p1.startsWithChar (File::separator))
  239. p1 = p1.substring (1);
  240. while (p2.startsWithChar (File::separator))
  241. p2 = p2.substring (1);
  242. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  243. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  244. {
  245. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  246. }
  247. return filename;
  248. }
  249. //==============================================================================
  250. const ProjectType& Project::getProjectType() const
  251. {
  252. const ProjectType* type = ProjectType::findType (getProjectTypeString());
  253. jassert (type != nullptr);
  254. if (type == nullptr)
  255. {
  256. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  257. jassert (type != nullptr);
  258. }
  259. return *type;
  260. }
  261. //==============================================================================
  262. void Project::createPropertyEditors (PropertyListBuilder& props)
  263. {
  264. props.add (new TextPropertyComponent (getProjectName(), "Project Name", 256, false),
  265. "The name of the project.");
  266. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  267. "The project's version number, This should be in the format major.minor.point");
  268. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  269. "Your company name, which will be added to the properties of the binary where possible");
  270. {
  271. StringArray projectTypeNames;
  272. Array<var> projectTypeCodes;
  273. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  274. for (int i = 0; i < types.size(); ++i)
  275. {
  276. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  277. projectTypeCodes.add (types.getUnchecked(i)->getType());
  278. }
  279. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  280. }
  281. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  282. "A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  283. getProjectType().createPropertyEditors (*this, props);
  284. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false),
  285. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  286. props.setPreferredHeight (22);
  287. }
  288. String Project::getVersionAsHex() const
  289. {
  290. StringArray configs;
  291. configs.addTokens (getVersionString(), ",.", String::empty);
  292. configs.trim();
  293. configs.removeEmptyStrings();
  294. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  295. if (configs.size() >= 4)
  296. value = (value << 8) + configs[3].getIntValue();
  297. return "0x" + String::toHexString (value);
  298. }
  299. StringPairArray Project::getPreprocessorDefs() const
  300. {
  301. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  302. }
  303. //==============================================================================
  304. Project::Item Project::getMainGroup()
  305. {
  306. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  307. }
  308. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  309. {
  310. if (item.isImageFile())
  311. {
  312. found.add (new Project::Item (item));
  313. }
  314. else if (item.isGroup())
  315. {
  316. for (int i = 0; i < item.getNumChildren(); ++i)
  317. findImages (item.getChild (i), found);
  318. }
  319. }
  320. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  321. {
  322. findImages (getMainGroup(), items);
  323. }
  324. //==============================================================================
  325. Project::Item::Item (Project& project_, const ValueTree& state_)
  326. : project (project_), state (state_)
  327. {
  328. }
  329. Project::Item::Item (const Item& other)
  330. : project (other.project), state (other.state)
  331. {
  332. }
  333. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  334. String Project::Item::getID() const { return state [ComponentBuilder::idProperty]; }
  335. void Project::Item::setID (const String& newID) { state.setProperty (ComponentBuilder::idProperty, newID, nullptr); }
  336. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  337. Image Project::Item::loadAsImageFile() const
  338. {
  339. return isValid() ? ImageCache::getFromFile (getFile())
  340. : Image::null;
  341. }
  342. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  343. {
  344. Item group (project, ValueTree (Tags::group));
  345. group.setID (uid);
  346. group.initialiseMissingProperties();
  347. group.getNameValue() = name;
  348. return group;
  349. }
  350. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  351. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  352. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  353. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  354. Project::Item Project::Item::findItemWithID (const String& targetId) const
  355. {
  356. if (state [ComponentBuilder::idProperty] == targetId)
  357. return *this;
  358. if (isGroup())
  359. {
  360. for (int i = getNumChildren(); --i >= 0;)
  361. {
  362. Item found (getChild(i).findItemWithID (targetId));
  363. if (found.isValid())
  364. return found;
  365. }
  366. }
  367. return Item (project, ValueTree::invalid);
  368. }
  369. bool Project::Item::canContain (const Item& child) const
  370. {
  371. if (isFile())
  372. return false;
  373. if (isGroup())
  374. return child.isFile() || child.isGroup();
  375. jassertfalse
  376. return false;
  377. }
  378. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  379. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  380. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  381. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  382. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  383. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  384. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  385. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  386. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  387. String Project::Item::getFilePath() const
  388. {
  389. if (isFile())
  390. return state [Ids::file].toString();
  391. else
  392. return String::empty;
  393. }
  394. File Project::Item::getFile() const
  395. {
  396. if (isFile())
  397. return project.resolveFilename (state [Ids::file].toString());
  398. else
  399. return File::nonexistent;
  400. }
  401. void Project::Item::setFile (const File& file)
  402. {
  403. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  404. jassert (getFile() == file);
  405. }
  406. void Project::Item::setFile (const RelativePath& file)
  407. {
  408. jassert (file.getRoot() == RelativePath::projectFolder);
  409. jassert (isFile());
  410. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  411. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  412. }
  413. bool Project::Item::renameFile (const File& newFile)
  414. {
  415. const File oldFile (getFile());
  416. if (oldFile.moveFileTo (newFile)
  417. || (newFile.exists() && ! oldFile.exists()))
  418. {
  419. setFile (newFile);
  420. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  421. return true;
  422. }
  423. return false;
  424. }
  425. bool Project::Item::containsChildForFile (const RelativePath& file) const
  426. {
  427. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  428. }
  429. Project::Item Project::Item::findItemForFile (const File& file) const
  430. {
  431. if (getFile() == file)
  432. return *this;
  433. if (isGroup())
  434. {
  435. for (int i = getNumChildren(); --i >= 0;)
  436. {
  437. Item found (getChild(i).findItemForFile (file));
  438. if (found.isValid())
  439. return found;
  440. }
  441. }
  442. return Item (project, ValueTree::invalid);
  443. }
  444. File Project::Item::determineGroupFolder() const
  445. {
  446. jassert (isGroup());
  447. File f;
  448. for (int i = 0; i < getNumChildren(); ++i)
  449. {
  450. f = getChild(i).getFile();
  451. if (f.exists())
  452. return f.getParentDirectory();
  453. }
  454. Item parent (getParent());
  455. if (parent != *this)
  456. {
  457. f = parent.determineGroupFolder();
  458. if (f.getChildFile (getName()).isDirectory())
  459. f = f.getChildFile (getName());
  460. }
  461. else
  462. {
  463. f = project.getFile().getParentDirectory();
  464. if (f.getChildFile ("Source").isDirectory())
  465. f = f.getChildFile ("Source");
  466. }
  467. return f;
  468. }
  469. void Project::Item::initialiseMissingProperties()
  470. {
  471. if (! state.hasProperty (ComponentBuilder::idProperty))
  472. setID (createAlphaNumericUID());
  473. if (isFile())
  474. {
  475. state.setProperty (Ids::name, getFile().getFileName(), 0);
  476. }
  477. else if (isGroup())
  478. {
  479. for (int i = getNumChildren(); --i >= 0;)
  480. getChild(i).initialiseMissingProperties();
  481. }
  482. }
  483. Value Project::Item::getNameValue()
  484. {
  485. return state.getPropertyAsValue (Ids::name, getUndoManager());
  486. }
  487. String Project::Item::getName() const
  488. {
  489. return state [Ids::name];
  490. }
  491. void Project::Item::addChild (const Item& newChild, int insertIndex)
  492. {
  493. state.addChild (newChild.state, insertIndex, getUndoManager());
  494. }
  495. void Project::Item::removeItemFromProject()
  496. {
  497. state.getParent().removeChild (state, getUndoManager());
  498. }
  499. Project::Item Project::Item::getParent() const
  500. {
  501. if (isMainGroup() || ! isGroup())
  502. return *this;
  503. return Item (project, state.getParent());
  504. }
  505. struct ItemSorter
  506. {
  507. static int compareElements (const ValueTree& first, const ValueTree& second)
  508. {
  509. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  510. }
  511. };
  512. struct ItemSorterWithGroupsAtStart
  513. {
  514. static int compareElements (const ValueTree& first, const ValueTree& second)
  515. {
  516. const bool firstIsGroup = first.hasType (Tags::group);
  517. const bool secondIsGroup = second.hasType (Tags::group);
  518. if (firstIsGroup == secondIsGroup)
  519. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  520. else
  521. return firstIsGroup ? -1 : 1;
  522. }
  523. };
  524. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  525. {
  526. if (keepGroupsAtStart)
  527. {
  528. ItemSorterWithGroupsAtStart sorter;
  529. state.sort (sorter, getUndoManager(), true);
  530. }
  531. else
  532. {
  533. ItemSorter sorter;
  534. state.sort (sorter, getUndoManager(), true);
  535. }
  536. }
  537. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  538. {
  539. for (int i = state.getNumChildren(); --i >= 0;)
  540. {
  541. const ValueTree child (state.getChild (i));
  542. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  543. return Item (project, child);
  544. }
  545. return addNewSubGroup (name, -1);
  546. }
  547. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  548. {
  549. String newID (createGUID (getID() + name + String (getNumChildren())));
  550. int n = 0;
  551. while (findItemWithID (newID).isValid())
  552. newID = createGUID (newID + String (++n));
  553. Item group (createGroup (project, name, newID));
  554. jassert (canContain (group));
  555. addChild (group, insertIndex);
  556. return group;
  557. }
  558. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  559. {
  560. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  561. return false;
  562. if (file.isDirectory())
  563. {
  564. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  565. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  566. while (iter.next())
  567. {
  568. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  569. group.addFile (iter.getFile(), -1, shouldCompile);
  570. }
  571. group.sortAlphabetically (false);
  572. }
  573. else if (file.existsAsFile())
  574. {
  575. if (! project.getMainGroup().findItemForFile (file).isValid())
  576. addFileUnchecked (file, insertIndex, shouldCompile);
  577. }
  578. else
  579. {
  580. jassertfalse;
  581. }
  582. return true;
  583. }
  584. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  585. {
  586. Item item (project, ValueTree (Tags::file));
  587. item.initialiseMissingProperties();
  588. item.getNameValue() = file.getFileName();
  589. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  590. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  591. if (canContain (item))
  592. {
  593. item.setFile (file);
  594. addChild (item, insertIndex);
  595. }
  596. }
  597. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  598. {
  599. Item item (project, ValueTree (Tags::file));
  600. item.initialiseMissingProperties();
  601. item.getNameValue() = file.getFileName();
  602. item.getShouldCompileValue() = shouldCompile;
  603. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  604. if (canContain (item))
  605. {
  606. item.setFile (file);
  607. addChild (item, insertIndex);
  608. return true;
  609. }
  610. return false;
  611. }
  612. const Drawable* Project::Item::getIcon() const
  613. {
  614. if (isFile())
  615. {
  616. if (isImageFile())
  617. return StoredSettings::getInstance()->getImageFileIcon();
  618. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  619. }
  620. else if (isMainGroup())
  621. {
  622. return &(project.mainProjectIcon);
  623. }
  624. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  625. }
  626. //==============================================================================
  627. ValueTree Project::getConfigNode()
  628. {
  629. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  630. }
  631. const char* const Project::configFlagDefault = "default";
  632. const char* const Project::configFlagEnabled = "enabled";
  633. const char* const Project::configFlagDisabled = "disabled";
  634. Value Project::getConfigFlag (const String& name)
  635. {
  636. ValueTree configNode (getConfigNode());
  637. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  638. if (v.getValue().toString().isEmpty())
  639. v = configFlagDefault;
  640. return v;
  641. }
  642. bool Project::isConfigFlagEnabled (const String& name) const
  643. {
  644. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  645. }
  646. void Project::sanitiseConfigFlags()
  647. {
  648. ValueTree configNode (getConfigNode());
  649. for (int i = configNode.getNumProperties(); --i >= 0;)
  650. {
  651. const var value (configNode [configNode.getPropertyName(i)]);
  652. if (value != configFlagEnabled && value != configFlagDisabled)
  653. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  654. }
  655. }
  656. //==============================================================================
  657. ValueTree Project::getModulesNode()
  658. {
  659. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  660. }
  661. bool Project::isModuleEnabled (const String& moduleID) const
  662. {
  663. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  664. for (int i = 0; i < modules.getNumChildren(); ++i)
  665. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  666. return true;
  667. return false;
  668. }
  669. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  670. {
  671. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  672. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  673. }
  674. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  675. {
  676. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  677. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  678. }
  679. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  680. {
  681. if (! isModuleEnabled (moduleID))
  682. {
  683. ValueTree module (Tags::module);
  684. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  685. ValueTree modules (getModulesNode());
  686. modules.addChild (module, -1, getUndoManagerFor (modules));
  687. shouldShowAllModuleFilesInProject (moduleID) = true;
  688. }
  689. if (shouldCopyFilesLocally)
  690. shouldCopyModuleFilesLocally (moduleID) = true;
  691. }
  692. void Project::removeModule (const String& moduleID)
  693. {
  694. ValueTree modules (getModulesNode());
  695. for (int i = 0; i < modules.getNumChildren(); ++i)
  696. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  697. modules.removeChild (i, getUndoManagerFor (modules));
  698. }
  699. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  700. {
  701. for (int i = 0; i < availableModules.modules.size(); ++i)
  702. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  703. modules.add (availableModules.modules.getUnchecked(i)->create());
  704. }
  705. int Project::getNumModules() const
  706. {
  707. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  708. }
  709. String Project::getModuleID (int index) const
  710. {
  711. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  712. }
  713. //==============================================================================
  714. ValueTree Project::getExporters()
  715. {
  716. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  717. if (! exporters.isValid())
  718. {
  719. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  720. exporters = getExporters();
  721. }
  722. return exporters;
  723. }
  724. int Project::getNumExporters()
  725. {
  726. return getExporters().getNumChildren();
  727. }
  728. ProjectExporter* Project::createExporter (int index)
  729. {
  730. jassert (index >= 0 && index < getNumExporters());
  731. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  732. }
  733. void Project::addNewExporter (const String& exporterName)
  734. {
  735. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  736. ValueTree exporters (getExporters());
  737. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  738. }
  739. void Project::deleteExporter (int index)
  740. {
  741. ValueTree exporters (getExporters());
  742. exporters.removeChild (index, getUndoManagerFor (exporters));
  743. }
  744. void Project::createDefaultExporters()
  745. {
  746. ValueTree exporters (getExporters());
  747. exporters.removeAllChildren (getUndoManagerFor (exporters));
  748. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  749. for (int i = 0; i < exporterNames.size(); ++i)
  750. addNewExporter (exporterNames[i]);
  751. }
  752. //==============================================================================
  753. String Project::getFileTemplate (const String& templateName)
  754. {
  755. int dataSize;
  756. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  757. if (data == nullptr)
  758. {
  759. jassertfalse;
  760. return String::empty;
  761. }
  762. return String::fromUTF8 (data, dataSize);
  763. }
  764. //==============================================================================
  765. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  766. Project::ExporterIterator::~ExporterIterator() {}
  767. bool Project::ExporterIterator::next()
  768. {
  769. if (++index >= project.getNumExporters())
  770. return false;
  771. exporter = project.createExporter (index);
  772. if (exporter == nullptr)
  773. {
  774. jassertfalse; // corrupted project file?
  775. return next();
  776. }
  777. return true;
  778. }