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.

988 lines
31KB

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