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.

1028 lines
32KB

  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. File Project::getBinaryDataCppFile (int index) const
  176. {
  177. const File cpp (getGeneratedCodeFolder().getChildFile ("BinaryData.cpp"));
  178. if (index > 0)
  179. return cpp.getSiblingFile (cpp.getFileNameWithoutExtension() + String (index + 1))
  180. .withFileExtension (cpp.getFileExtension());
  181. return cpp;
  182. }
  183. //==============================================================================
  184. static void registerRecentFile (const File& file)
  185. {
  186. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  187. getAppSettings().recentFiles.addFile (file);
  188. getAppSettings().flush();
  189. }
  190. Result Project::loadDocument (const File& file)
  191. {
  192. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  193. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  194. return Result::fail ("Not a valid Jucer project!");
  195. ValueTree newTree (ValueTree::fromXml (*xml));
  196. if (! newTree.hasType (Tags::projectRoot))
  197. return Result::fail ("The document contains errors and couldn't be parsed!");
  198. registerRecentFile (file);
  199. projectRoot = newTree;
  200. removeDefunctExporters();
  201. setMissingDefaultValues();
  202. setChangedFlag (false);
  203. return Result::ok();
  204. }
  205. Result Project::saveDocument (const File& file)
  206. {
  207. return saveProject (file, false);
  208. }
  209. Result Project::saveProject (const File& file, bool isCommandLineApp)
  210. {
  211. updateProjectSettings();
  212. sanitiseConfigFlags();
  213. if (! isCommandLineApp)
  214. registerRecentFile (file);
  215. ProjectSaver saver (*this, file);
  216. return saver.save (! isCommandLineApp);
  217. }
  218. Result Project::saveResourcesOnly (const File& file)
  219. {
  220. ProjectSaver saver (*this, file);
  221. return saver.saveResourcesOnly();
  222. }
  223. //==============================================================================
  224. static File lastDocumentOpened;
  225. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  226. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  227. //==============================================================================
  228. void Project::valueTreePropertyChanged (ValueTree&, const Identifier& property)
  229. {
  230. if (property == Ids::projectType)
  231. setMissingDefaultValues();
  232. changed();
  233. }
  234. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  235. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&) { changed(); }
  236. void Project::valueTreeChildOrderChanged (ValueTree&) { changed(); }
  237. void Project::valueTreeParentChanged (ValueTree&) {}
  238. //==============================================================================
  239. File Project::resolveFilename (String filename) const
  240. {
  241. if (filename.isEmpty())
  242. return File::nonexistent;
  243. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename);
  244. if (FileHelpers::isAbsolutePath (filename))
  245. return File::createFileWithoutCheckingPath (FileHelpers::currentOSStylePath (filename)); // (avoid assertions for windows-style paths)
  246. return getFile().getSiblingFile (FileHelpers::currentOSStylePath (filename));
  247. }
  248. String Project::getRelativePathForFile (const File& file) const
  249. {
  250. String filename (file.getFullPathName());
  251. File relativePathBase (getFile().getParentDirectory());
  252. String p1 (relativePathBase.getFullPathName());
  253. String p2 (file.getFullPathName());
  254. while (p1.startsWithChar (File::separator))
  255. p1 = p1.substring (1);
  256. while (p2.startsWithChar (File::separator))
  257. p2 = p2.substring (1);
  258. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  259. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  260. {
  261. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  262. }
  263. return filename;
  264. }
  265. //==============================================================================
  266. const ProjectType& Project::getProjectType() const
  267. {
  268. if (const ProjectType* type = ProjectType::findType (getProjectTypeString()))
  269. return *type;
  270. const ProjectType* guiType = ProjectType::findType (ProjectType::getGUIAppTypeName());
  271. jassert (guiType != nullptr);
  272. return *guiType;
  273. }
  274. //==============================================================================
  275. void Project::createPropertyEditors (PropertyListBuilder& props)
  276. {
  277. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  278. "The name of the project.");
  279. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  280. "The project's version number, This should be in the format major.minor.point[.point]");
  281. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  282. "Your company name, which will be added to the properties of the binary where possible");
  283. {
  284. StringArray projectTypeNames;
  285. Array<var> projectTypeCodes;
  286. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  287. for (int i = 0; i < types.size(); ++i)
  288. {
  289. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  290. projectTypeCodes.add (types.getUnchecked(i)->getType());
  291. }
  292. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  293. }
  294. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  295. "A unique identifier for this product, mainly for use in OSX/iOS builds. It should be something like 'com.yourcompanyname.yourproductname'");
  296. getProjectType().createPropertyEditors (*this, props);
  297. {
  298. const int maxSizes[] = { 20480, 10240, 6144, 2048, 1024, 512, 256, 128, 64 };
  299. StringArray maxSizeNames;
  300. Array<var> maxSizeCodes;
  301. maxSizeNames.add (TRANS("Default"));
  302. maxSizeCodes.add (var::null);
  303. maxSizeNames.add (String::empty);
  304. maxSizeCodes.add (var::null);
  305. for (int i = 0; i < numElementsInArray (maxSizes); ++i)
  306. {
  307. const int sizeInBytes = maxSizes[i] * 1024;
  308. maxSizeNames.add (File::descriptionOfSizeInBytes (sizeInBytes));
  309. maxSizeCodes.add (sizeInBytes);
  310. }
  311. props.add (new ChoicePropertyComponent (getMaxBinaryFileSize(), "BinaryData.cpp size limit", maxSizeNames, maxSizeCodes),
  312. "When splitting binary data into multiple cpp files, the Introjucer attempts to keep the file sizes below this threshold. "
  313. "(Note that individual resource files which are larger than this size cannot be split across multiple cpp files).");
  314. }
  315. props.add (new TextPropertyComponent (getProjectUserNotes(), "Notes", 32768, true),
  316. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  317. }
  318. static StringArray getConfigs (const Project& p)
  319. {
  320. StringArray configs;
  321. configs.addTokens (p.getVersionString(), ",.", String::empty);
  322. configs.trim();
  323. configs.removeEmptyStrings();
  324. return configs;
  325. }
  326. int Project::getVersionAsHexInteger() const
  327. {
  328. const StringArray configs (getConfigs (*this));
  329. int value = (configs[0].getIntValue() << 16)
  330. + (configs[1].getIntValue() << 8)
  331. + configs[2].getIntValue();
  332. if (configs.size() >= 4)
  333. value = (value << 8) + configs[3].getIntValue();
  334. return value;
  335. }
  336. String Project::getVersionAsHex() const
  337. {
  338. return "0x" + String::toHexString (getVersionAsHexInteger());
  339. }
  340. StringPairArray Project::getPreprocessorDefs() const
  341. {
  342. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  343. }
  344. //==============================================================================
  345. Project::Item Project::getMainGroup()
  346. {
  347. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  348. }
  349. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  350. {
  351. if (item.isImageFile())
  352. {
  353. found.add (new Project::Item (item));
  354. }
  355. else if (item.isGroup())
  356. {
  357. for (int i = 0; i < item.getNumChildren(); ++i)
  358. findImages (item.getChild (i), found);
  359. }
  360. }
  361. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  362. {
  363. findImages (getMainGroup(), items);
  364. }
  365. //==============================================================================
  366. Project::Item::Item (Project& project_, const ValueTree& state_)
  367. : project (project_), state (state_)
  368. {
  369. }
  370. Project::Item::Item (const Item& other)
  371. : project (other.project), state (other.state)
  372. {
  373. }
  374. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  375. String Project::Item::getID() const { return state [Ids::ID]; }
  376. void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); }
  377. Image Project::Item::loadAsImageFile() const
  378. {
  379. return isValid() ? ImageCache::getFromFile (getFile())
  380. : Image::null;
  381. }
  382. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  383. {
  384. Item group (project, ValueTree (Tags::group));
  385. group.setID (uid);
  386. group.initialiseMissingProperties();
  387. group.getNameValue() = name;
  388. return group;
  389. }
  390. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  391. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  392. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  393. bool Project::Item::isImageFile() const { return isFile() && ImageFileFormat::findImageFormatForFileExtension (getFile()) != nullptr; }
  394. Project::Item Project::Item::findItemWithID (const String& targetId) const
  395. {
  396. if (state [Ids::ID] == targetId)
  397. return *this;
  398. if (isGroup())
  399. {
  400. for (int i = getNumChildren(); --i >= 0;)
  401. {
  402. Item found (getChild(i).findItemWithID (targetId));
  403. if (found.isValid())
  404. return found;
  405. }
  406. }
  407. return Item (project, ValueTree::invalid);
  408. }
  409. bool Project::Item::canContain (const Item& child) const
  410. {
  411. if (isFile())
  412. return false;
  413. if (isGroup())
  414. return child.isFile() || child.isGroup();
  415. jassertfalse;
  416. return false;
  417. }
  418. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  419. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  420. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  421. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  422. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  423. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  424. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  425. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  426. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  427. String Project::Item::getFilePath() const
  428. {
  429. if (isFile())
  430. return state [Ids::file].toString();
  431. return String::empty;
  432. }
  433. File Project::Item::getFile() const
  434. {
  435. if (isFile())
  436. return project.resolveFilename (state [Ids::file].toString());
  437. return File::nonexistent;
  438. }
  439. void Project::Item::setFile (const File& file)
  440. {
  441. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  442. jassert (getFile() == file);
  443. }
  444. void Project::Item::setFile (const RelativePath& file)
  445. {
  446. jassert (file.getRoot() == RelativePath::projectFolder);
  447. jassert (isFile());
  448. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  449. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  450. }
  451. bool Project::Item::renameFile (const File& newFile)
  452. {
  453. const File oldFile (getFile());
  454. if (oldFile.moveFileTo (newFile)
  455. || (newFile.exists() && ! oldFile.exists()))
  456. {
  457. setFile (newFile);
  458. IntrojucerApp::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  459. return true;
  460. }
  461. return false;
  462. }
  463. bool Project::Item::containsChildForFile (const RelativePath& file) const
  464. {
  465. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  466. }
  467. Project::Item Project::Item::findItemForFile (const File& file) const
  468. {
  469. if (getFile() == file)
  470. return *this;
  471. if (isGroup())
  472. {
  473. for (int i = getNumChildren(); --i >= 0;)
  474. {
  475. Item found (getChild(i).findItemForFile (file));
  476. if (found.isValid())
  477. return found;
  478. }
  479. }
  480. return Item (project, ValueTree::invalid);
  481. }
  482. File Project::Item::determineGroupFolder() const
  483. {
  484. jassert (isGroup());
  485. File f;
  486. for (int i = 0; i < getNumChildren(); ++i)
  487. {
  488. f = getChild(i).getFile();
  489. if (f.exists())
  490. return f.getParentDirectory();
  491. }
  492. Item parent (getParent());
  493. if (parent != *this)
  494. {
  495. f = parent.determineGroupFolder();
  496. if (f.getChildFile (getName()).isDirectory())
  497. f = f.getChildFile (getName());
  498. }
  499. else
  500. {
  501. f = project.getFile().getParentDirectory();
  502. if (f.getChildFile ("Source").isDirectory())
  503. f = f.getChildFile ("Source");
  504. }
  505. return f;
  506. }
  507. void Project::Item::initialiseMissingProperties()
  508. {
  509. if (! state.hasProperty (Ids::ID))
  510. setID (createAlphaNumericUID());
  511. if (isFile())
  512. {
  513. state.setProperty (Ids::name, getFile().getFileName(), nullptr);
  514. }
  515. else if (isGroup())
  516. {
  517. for (int i = getNumChildren(); --i >= 0;)
  518. getChild(i).initialiseMissingProperties();
  519. }
  520. }
  521. Value Project::Item::getNameValue()
  522. {
  523. return state.getPropertyAsValue (Ids::name, getUndoManager());
  524. }
  525. String Project::Item::getName() const
  526. {
  527. return state [Ids::name];
  528. }
  529. void Project::Item::addChild (const Item& newChild, int insertIndex)
  530. {
  531. state.addChild (newChild.state, insertIndex, getUndoManager());
  532. }
  533. void Project::Item::removeItemFromProject()
  534. {
  535. state.getParent().removeChild (state, getUndoManager());
  536. }
  537. Project::Item Project::Item::getParent() const
  538. {
  539. if (isMainGroup() || ! isGroup())
  540. return *this;
  541. return Item (project, state.getParent());
  542. }
  543. struct ItemSorter
  544. {
  545. static int compareElements (const ValueTree& first, const ValueTree& second)
  546. {
  547. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  548. }
  549. };
  550. struct ItemSorterWithGroupsAtStart
  551. {
  552. static int compareElements (const ValueTree& first, const ValueTree& second)
  553. {
  554. const bool firstIsGroup = first.hasType (Tags::group);
  555. const bool secondIsGroup = second.hasType (Tags::group);
  556. if (firstIsGroup == secondIsGroup)
  557. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  558. return firstIsGroup ? -1 : 1;
  559. }
  560. };
  561. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  562. {
  563. if (keepGroupsAtStart)
  564. {
  565. ItemSorterWithGroupsAtStart sorter;
  566. state.sort (sorter, getUndoManager(), true);
  567. }
  568. else
  569. {
  570. ItemSorter sorter;
  571. state.sort (sorter, getUndoManager(), true);
  572. }
  573. }
  574. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  575. {
  576. for (int i = state.getNumChildren(); --i >= 0;)
  577. {
  578. const ValueTree child (state.getChild (i));
  579. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  580. return Item (project, child);
  581. }
  582. return addNewSubGroup (name, -1);
  583. }
  584. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  585. {
  586. String newID (createGUID (getID() + name + String (getNumChildren())));
  587. int n = 0;
  588. while (findItemWithID (newID).isValid())
  589. newID = createGUID (newID + String (++n));
  590. Item group (createGroup (project, name, newID));
  591. jassert (canContain (group));
  592. addChild (group, insertIndex);
  593. return group;
  594. }
  595. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  596. {
  597. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  598. return false;
  599. if (file.isDirectory())
  600. {
  601. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  602. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  603. while (iter.next())
  604. {
  605. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  606. group.addFile (iter.getFile(), -1, shouldCompile);
  607. }
  608. group.sortAlphabetically (false);
  609. }
  610. else if (file.existsAsFile())
  611. {
  612. if (! project.getMainGroup().findItemForFile (file).isValid())
  613. addFileUnchecked (file, insertIndex, shouldCompile);
  614. }
  615. else
  616. {
  617. jassertfalse;
  618. }
  619. return true;
  620. }
  621. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  622. {
  623. Item item (project, ValueTree (Tags::file));
  624. item.initialiseMissingProperties();
  625. item.getNameValue() = file.getFileName();
  626. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  627. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  628. if (canContain (item))
  629. {
  630. item.setFile (file);
  631. addChild (item, insertIndex);
  632. }
  633. }
  634. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  635. {
  636. Item item (project, ValueTree (Tags::file));
  637. item.initialiseMissingProperties();
  638. item.getNameValue() = file.getFileName();
  639. item.getShouldCompileValue() = shouldCompile;
  640. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  641. if (canContain (item))
  642. {
  643. item.setFile (file);
  644. addChild (item, insertIndex);
  645. return true;
  646. }
  647. return false;
  648. }
  649. Icon Project::Item::getIcon() const
  650. {
  651. const Icons& icons = getIcons();
  652. if (isFile())
  653. {
  654. if (isImageFile())
  655. return Icon (icons.imageDoc, Colours::blue);
  656. return Icon (icons.document, Colours::yellow);
  657. }
  658. if (isMainGroup())
  659. return Icon (icons.juceLogo, Colours::orange);
  660. return Icon (icons.folder, Colours::darkgrey);
  661. }
  662. bool Project::Item::isIconCrossedOut() const
  663. {
  664. return isFile()
  665. && ! (shouldBeCompiled()
  666. || shouldBeAddedToBinaryResources()
  667. || getFile().hasFileExtension (headerFileExtensions));
  668. }
  669. //==============================================================================
  670. ValueTree Project::getConfigNode()
  671. {
  672. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  673. }
  674. const char* const Project::configFlagDefault = "default";
  675. const char* const Project::configFlagEnabled = "enabled";
  676. const char* const Project::configFlagDisabled = "disabled";
  677. Value Project::getConfigFlag (const String& name)
  678. {
  679. ValueTree configNode (getConfigNode());
  680. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  681. if (v.getValue().toString().isEmpty())
  682. v = configFlagDefault;
  683. return v;
  684. }
  685. bool Project::isConfigFlagEnabled (const String& name) const
  686. {
  687. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  688. }
  689. void Project::sanitiseConfigFlags()
  690. {
  691. ValueTree configNode (getConfigNode());
  692. for (int i = configNode.getNumProperties(); --i >= 0;)
  693. {
  694. const var value (configNode [configNode.getPropertyName(i)]);
  695. if (value != configFlagEnabled && value != configFlagDisabled)
  696. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  697. }
  698. }
  699. //==============================================================================
  700. ValueTree Project::getModulesNode()
  701. {
  702. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  703. }
  704. bool Project::isModuleEnabled (const String& moduleID) const
  705. {
  706. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  707. for (int i = 0; i < modules.getNumChildren(); ++i)
  708. if (modules.getChild(i) [Ids::ID] == moduleID)
  709. return true;
  710. return false;
  711. }
  712. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  713. {
  714. return getModulesNode().getChildWithProperty (Ids::ID, moduleID)
  715. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  716. }
  717. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  718. {
  719. return getModulesNode().getChildWithProperty (Ids::ID, moduleID)
  720. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  721. }
  722. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  723. {
  724. if (! isModuleEnabled (moduleID))
  725. {
  726. ValueTree module (Tags::module);
  727. module.setProperty (Ids::ID, moduleID, nullptr);
  728. ValueTree modules (getModulesNode());
  729. modules.addChild (module, -1, getUndoManagerFor (modules));
  730. shouldShowAllModuleFilesInProject (moduleID) = true;
  731. }
  732. if (shouldCopyFilesLocally)
  733. shouldCopyModuleFilesLocally (moduleID) = true;
  734. }
  735. void Project::removeModule (const String& moduleID)
  736. {
  737. ValueTree modules (getModulesNode());
  738. for (int i = 0; i < modules.getNumChildren(); ++i)
  739. if (modules.getChild(i) [Ids::ID] == moduleID)
  740. modules.removeChild (i, getUndoManagerFor (modules));
  741. }
  742. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  743. {
  744. for (int i = 0; i < availableModules.modules.size(); ++i)
  745. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  746. modules.add (availableModules.modules.getUnchecked(i)->create());
  747. }
  748. int Project::getNumModules() const
  749. {
  750. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  751. }
  752. String Project::getModuleID (int index) const
  753. {
  754. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [Ids::ID].toString();
  755. }
  756. //==============================================================================
  757. ValueTree Project::getExporters()
  758. {
  759. return projectRoot.getOrCreateChildWithName (Tags::exporters, nullptr);
  760. }
  761. int Project::getNumExporters()
  762. {
  763. return getExporters().getNumChildren();
  764. }
  765. ProjectExporter* Project::createExporter (int index)
  766. {
  767. jassert (index >= 0 && index < getNumExporters());
  768. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  769. }
  770. void Project::addNewExporter (const String& exporterName)
  771. {
  772. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  773. ValueTree exporters (getExporters());
  774. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  775. }
  776. void Project::createExporterForCurrentPlatform()
  777. {
  778. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  779. }
  780. //==============================================================================
  781. String Project::getFileTemplate (const String& templateName)
  782. {
  783. int dataSize;
  784. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  785. if (data == nullptr)
  786. {
  787. jassertfalse;
  788. return String::empty;
  789. }
  790. return String::fromUTF8 (data, dataSize);
  791. }
  792. //==============================================================================
  793. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  794. Project::ExporterIterator::~ExporterIterator() {}
  795. bool Project::ExporterIterator::next()
  796. {
  797. if (++index >= project.getNumExporters())
  798. return false;
  799. exporter = project.createExporter (index);
  800. if (exporter == nullptr)
  801. {
  802. jassertfalse; // corrupted project file?
  803. return next();
  804. }
  805. return true;
  806. }
  807. PropertiesFile& Project::getStoredProperties() const
  808. {
  809. return getAppSettings().getProjectProperties (getProjectUID());
  810. }