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.

1031 lines
33KB

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