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.

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