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.

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