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.

1004 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. props.add (new TextPropertyComponent (getCompanyWebsite(), "Company Website", 256, false),
  295. "Your company website, which will be added to the properties of the binary where possible");
  296. props.add (new TextPropertyComponent (getCompanyEmail(), "Company E-mail", 256, false),
  297. "Your company e-mail, which will be added to the properties of the binary where possible");
  298. {
  299. StringArray projectTypeNames;
  300. Array<var> projectTypeCodes;
  301. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  302. for (int i = 0; i < types.size(); ++i)
  303. {
  304. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  305. projectTypeCodes.add (types.getUnchecked(i)->getType());
  306. }
  307. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  308. }
  309. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  310. "A unique identifier for this product, mainly for use in OSX/iOS builds. It should be something like 'com.yourcompanyname.yourproductname'");
  311. getProjectType().createPropertyEditors (*this, props);
  312. {
  313. const int maxSizes[] = { 20480, 10240, 6144, 2048, 1024, 512, 256, 128, 64 };
  314. StringArray maxSizeNames;
  315. Array<var> maxSizeCodes;
  316. maxSizeNames.add (TRANS("Default"));
  317. maxSizeCodes.add (var::null);
  318. maxSizeNames.add (String::empty);
  319. maxSizeCodes.add (var::null);
  320. for (int i = 0; i < numElementsInArray (maxSizes); ++i)
  321. {
  322. const int sizeInBytes = maxSizes[i] * 1024;
  323. maxSizeNames.add (File::descriptionOfSizeInBytes (sizeInBytes));
  324. maxSizeCodes.add (sizeInBytes);
  325. }
  326. props.add (new ChoicePropertyComponent (getMaxBinaryFileSize(), "BinaryData.cpp size limit", maxSizeNames, maxSizeCodes),
  327. "When splitting binary data into multiple cpp files, the Introjucer attempts to keep the file sizes below this threshold. "
  328. "(Note that individual resource files which are larger than this size cannot be split across multiple cpp files).");
  329. }
  330. props.add (new BooleanPropertyComponent (shouldIncludeBinaryInAppConfig(), "Include Binary",
  331. "Include BinaryData.h in the AppConfig.h file"));
  332. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, true),
  333. "Global preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, or "
  334. "new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  335. props.add (new TextPropertyComponent (getProjectUserNotes(), "Notes", 32768, true),
  336. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  337. }
  338. //==============================================================================
  339. static StringArray getVersionSegments (const Project& p)
  340. {
  341. StringArray segments;
  342. segments.addTokens (p.getVersionString(), ",.", "");
  343. segments.trim();
  344. segments.removeEmptyStrings();
  345. return segments;
  346. }
  347. int Project::getVersionAsHexInteger() const
  348. {
  349. const StringArray segments (getVersionSegments (*this));
  350. int value = (segments[0].getIntValue() << 16)
  351. + (segments[1].getIntValue() << 8)
  352. + segments[2].getIntValue();
  353. if (segments.size() >= 4)
  354. value = (value << 8) + segments[3].getIntValue();
  355. return value;
  356. }
  357. String Project::getVersionAsHex() const
  358. {
  359. return "0x" + String::toHexString (getVersionAsHexInteger());
  360. }
  361. StringPairArray Project::getPreprocessorDefs() const
  362. {
  363. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  364. }
  365. File Project::getBinaryDataCppFile (int index) const
  366. {
  367. const File cpp (getGeneratedCodeFolder().getChildFile ("BinaryData.cpp"));
  368. if (index > 0)
  369. return cpp.getSiblingFile (cpp.getFileNameWithoutExtension() + String (index + 1))
  370. .withFileExtension (cpp.getFileExtension());
  371. return cpp;
  372. }
  373. Project::Item Project::getMainGroup()
  374. {
  375. return Item (*this, projectRoot.getChildWithName (Ids::MAINGROUP));
  376. }
  377. PropertiesFile& Project::getStoredProperties() const
  378. {
  379. return getAppSettings().getProjectProperties (getProjectUID());
  380. }
  381. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  382. {
  383. if (item.isImageFile())
  384. {
  385. found.add (new Project::Item (item));
  386. }
  387. else if (item.isGroup())
  388. {
  389. for (int i = 0; i < item.getNumChildren(); ++i)
  390. findImages (item.getChild (i), found);
  391. }
  392. }
  393. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  394. {
  395. findImages (getMainGroup(), items);
  396. }
  397. //==============================================================================
  398. Project::Item::Item (Project& p, const ValueTree& s)
  399. : project (p), state (s)
  400. {
  401. }
  402. Project::Item::Item (const Item& other)
  403. : project (other.project), state (other.state)
  404. {
  405. }
  406. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  407. String Project::Item::getID() const { return state [Ids::ID]; }
  408. void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); }
  409. Drawable* Project::Item::loadAsImageFile() const
  410. {
  411. return isValid() ? Drawable::createFromImageFile (getFile())
  412. : nullptr;
  413. }
  414. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  415. {
  416. Item group (project, ValueTree (Ids::GROUP));
  417. group.setID (uid);
  418. group.initialiseMissingProperties();
  419. group.getNameValue() = name;
  420. return group;
  421. }
  422. bool Project::Item::isFile() const { return state.hasType (Ids::FILE); }
  423. bool Project::Item::isGroup() const { return state.hasType (Ids::GROUP) || isMainGroup(); }
  424. bool Project::Item::isMainGroup() const { return state.hasType (Ids::MAINGROUP); }
  425. bool Project::Item::isImageFile() const
  426. {
  427. return isFile() && (ImageFileFormat::findImageFormatForFileExtension (getFile()) != nullptr
  428. || getFile().hasFileExtension ("svg"));
  429. }
  430. Project::Item Project::Item::findItemWithID (const String& targetId) const
  431. {
  432. if (state [Ids::ID] == targetId)
  433. return *this;
  434. if (isGroup())
  435. {
  436. for (int i = getNumChildren(); --i >= 0;)
  437. {
  438. Item found (getChild(i).findItemWithID (targetId));
  439. if (found.isValid())
  440. return found;
  441. }
  442. }
  443. return Item (project, ValueTree::invalid);
  444. }
  445. bool Project::Item::canContain (const Item& child) const
  446. {
  447. if (isFile())
  448. return false;
  449. if (isGroup())
  450. return child.isFile() || child.isGroup();
  451. jassertfalse;
  452. return false;
  453. }
  454. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  455. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  456. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  457. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  458. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  459. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  460. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  461. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  462. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  463. String Project::Item::getFilePath() const
  464. {
  465. if (isFile())
  466. return state [Ids::file].toString();
  467. return String::empty;
  468. }
  469. File Project::Item::getFile() const
  470. {
  471. if (isFile())
  472. return project.resolveFilename (state [Ids::file].toString());
  473. return File::nonexistent;
  474. }
  475. void Project::Item::setFile (const File& file)
  476. {
  477. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  478. jassert (getFile() == file);
  479. }
  480. void Project::Item::setFile (const RelativePath& file)
  481. {
  482. jassert (isFile());
  483. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  484. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  485. }
  486. bool Project::Item::renameFile (const File& newFile)
  487. {
  488. const File oldFile (getFile());
  489. if (oldFile.moveFileTo (newFile)
  490. || (newFile.exists() && ! oldFile.exists()))
  491. {
  492. setFile (newFile);
  493. IntrojucerApp::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  494. return true;
  495. }
  496. return false;
  497. }
  498. bool Project::Item::containsChildForFile (const RelativePath& file) const
  499. {
  500. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  501. }
  502. Project::Item Project::Item::findItemForFile (const File& file) const
  503. {
  504. if (getFile() == file)
  505. return *this;
  506. if (isGroup())
  507. {
  508. for (int i = getNumChildren(); --i >= 0;)
  509. {
  510. Item found (getChild(i).findItemForFile (file));
  511. if (found.isValid())
  512. return found;
  513. }
  514. }
  515. return Item (project, ValueTree::invalid);
  516. }
  517. File Project::Item::determineGroupFolder() const
  518. {
  519. jassert (isGroup());
  520. File f;
  521. for (int i = 0; i < getNumChildren(); ++i)
  522. {
  523. f = getChild(i).getFile();
  524. if (f.exists())
  525. return f.getParentDirectory();
  526. }
  527. Item parent (getParent());
  528. if (parent != *this)
  529. {
  530. f = parent.determineGroupFolder();
  531. if (f.getChildFile (getName()).isDirectory())
  532. f = f.getChildFile (getName());
  533. }
  534. else
  535. {
  536. f = project.getProjectFolder();
  537. if (f.getChildFile ("Source").isDirectory())
  538. f = f.getChildFile ("Source");
  539. }
  540. return f;
  541. }
  542. void Project::Item::initialiseMissingProperties()
  543. {
  544. if (! state.hasProperty (Ids::ID))
  545. setID (createAlphaNumericUID());
  546. if (isFile())
  547. {
  548. state.setProperty (Ids::name, getFile().getFileName(), nullptr);
  549. }
  550. else if (isGroup())
  551. {
  552. for (int i = getNumChildren(); --i >= 0;)
  553. getChild(i).initialiseMissingProperties();
  554. }
  555. }
  556. Value Project::Item::getNameValue()
  557. {
  558. return state.getPropertyAsValue (Ids::name, getUndoManager());
  559. }
  560. String Project::Item::getName() const
  561. {
  562. return state [Ids::name];
  563. }
  564. void Project::Item::addChild (const Item& newChild, int insertIndex)
  565. {
  566. state.addChild (newChild.state, insertIndex, getUndoManager());
  567. }
  568. void Project::Item::removeItemFromProject()
  569. {
  570. state.getParent().removeChild (state, getUndoManager());
  571. }
  572. Project::Item Project::Item::getParent() const
  573. {
  574. if (isMainGroup() || ! isGroup())
  575. return *this;
  576. return Item (project, state.getParent());
  577. }
  578. struct ItemSorter
  579. {
  580. static int compareElements (const ValueTree& first, const ValueTree& second)
  581. {
  582. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  583. }
  584. };
  585. struct ItemSorterWithGroupsAtStart
  586. {
  587. static int compareElements (const ValueTree& first, const ValueTree& second)
  588. {
  589. const bool firstIsGroup = first.hasType (Ids::GROUP);
  590. const bool secondIsGroup = second.hasType (Ids::GROUP);
  591. if (firstIsGroup == secondIsGroup)
  592. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  593. return firstIsGroup ? -1 : 1;
  594. }
  595. };
  596. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  597. {
  598. if (keepGroupsAtStart)
  599. {
  600. ItemSorterWithGroupsAtStart sorter;
  601. state.sort (sorter, getUndoManager(), true);
  602. }
  603. else
  604. {
  605. ItemSorter sorter;
  606. state.sort (sorter, getUndoManager(), true);
  607. }
  608. }
  609. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  610. {
  611. for (int i = state.getNumChildren(); --i >= 0;)
  612. {
  613. const ValueTree child (state.getChild (i));
  614. if (child.getProperty (Ids::name) == name && child.hasType (Ids::GROUP))
  615. return Item (project, child);
  616. }
  617. return addNewSubGroup (name, -1);
  618. }
  619. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  620. {
  621. String newID (createGUID (getID() + name + String (getNumChildren())));
  622. int n = 0;
  623. while (project.getMainGroup().findItemWithID (newID).isValid())
  624. newID = createGUID (newID + String (++n));
  625. Item group (createGroup (project, name, newID));
  626. jassert (canContain (group));
  627. addChild (group, insertIndex);
  628. return group;
  629. }
  630. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  631. {
  632. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  633. return false;
  634. if (file.isDirectory())
  635. {
  636. Item group (addNewSubGroup (file.getFileName(), insertIndex));
  637. for (DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories); iter.next();)
  638. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  639. group.addFile (iter.getFile(), -1, shouldCompile);
  640. group.sortAlphabetically (false);
  641. }
  642. else if (file.existsAsFile())
  643. {
  644. if (! project.getMainGroup().findItemForFile (file).isValid())
  645. addFileUnchecked (file, insertIndex, shouldCompile);
  646. }
  647. else
  648. {
  649. jassertfalse;
  650. }
  651. return true;
  652. }
  653. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  654. {
  655. Item item (project, ValueTree (Ids::FILE));
  656. item.initialiseMissingProperties();
  657. item.getNameValue() = file.getFileName();
  658. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  659. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  660. if (canContain (item))
  661. {
  662. item.setFile (file);
  663. addChild (item, insertIndex);
  664. }
  665. }
  666. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  667. {
  668. Item item (project, ValueTree (Ids::FILE));
  669. item.initialiseMissingProperties();
  670. item.getNameValue() = file.getFileName();
  671. item.getShouldCompileValue() = shouldCompile;
  672. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  673. if (canContain (item))
  674. {
  675. item.setFile (file);
  676. addChild (item, insertIndex);
  677. return true;
  678. }
  679. return false;
  680. }
  681. Icon Project::Item::getIcon() const
  682. {
  683. const Icons& icons = getIcons();
  684. if (isFile())
  685. {
  686. if (isImageFile())
  687. return Icon (icons.imageDoc, Colours::blue);
  688. return Icon (icons.document, Colours::yellow);
  689. }
  690. if (isMainGroup())
  691. return Icon (icons.juceLogo, Colours::orange);
  692. return Icon (icons.folder, Colours::darkgrey);
  693. }
  694. bool Project::Item::isIconCrossedOut() const
  695. {
  696. return isFile()
  697. && ! (shouldBeCompiled()
  698. || shouldBeAddedToBinaryResources()
  699. || getFile().hasFileExtension (headerFileExtensions));
  700. }
  701. //==============================================================================
  702. ValueTree Project::getConfigNode()
  703. {
  704. return projectRoot.getOrCreateChildWithName (Ids::JUCEOPTIONS, nullptr);
  705. }
  706. const char* const Project::configFlagDefault = "default";
  707. const char* const Project::configFlagEnabled = "enabled";
  708. const char* const Project::configFlagDisabled = "disabled";
  709. Value Project::getConfigFlag (const String& name)
  710. {
  711. ValueTree configNode (getConfigNode());
  712. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  713. if (v.getValue().toString().isEmpty())
  714. v = configFlagDefault;
  715. return v;
  716. }
  717. bool Project::isConfigFlagEnabled (const String& name) const
  718. {
  719. return projectRoot.getChildWithName (Ids::JUCEOPTIONS).getProperty (name) == configFlagEnabled;
  720. }
  721. void Project::sanitiseConfigFlags()
  722. {
  723. ValueTree configNode (getConfigNode());
  724. for (int i = configNode.getNumProperties(); --i >= 0;)
  725. {
  726. const var value (configNode [configNode.getPropertyName(i)]);
  727. if (value != configFlagEnabled && value != configFlagDisabled)
  728. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  729. }
  730. }
  731. //==============================================================================
  732. EnabledModuleList& Project::getModules()
  733. {
  734. if (enabledModulesList == nullptr)
  735. enabledModulesList = new EnabledModuleList (*this, projectRoot.getOrCreateChildWithName (Ids::MODULES, nullptr));
  736. return *enabledModulesList;
  737. }
  738. //==============================================================================
  739. ValueTree Project::getExporters()
  740. {
  741. return projectRoot.getOrCreateChildWithName (Ids::EXPORTFORMATS, nullptr);
  742. }
  743. int Project::getNumExporters()
  744. {
  745. return getExporters().getNumChildren();
  746. }
  747. ProjectExporter* Project::createExporter (int index)
  748. {
  749. jassert (index >= 0 && index < getNumExporters());
  750. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  751. }
  752. void Project::addNewExporter (const String& exporterName)
  753. {
  754. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  755. ValueTree exporters (getExporters());
  756. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  757. }
  758. void Project::createExporterForCurrentPlatform()
  759. {
  760. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  761. }
  762. //==============================================================================
  763. String Project::getFileTemplate (const String& templateName)
  764. {
  765. int dataSize;
  766. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  767. if (data == nullptr)
  768. {
  769. jassertfalse;
  770. return String::empty;
  771. }
  772. return String::fromUTF8 (data, dataSize);
  773. }
  774. //==============================================================================
  775. Project::ExporterIterator::ExporterIterator (Project& p) : index (-1), project (p) {}
  776. Project::ExporterIterator::~ExporterIterator() {}
  777. bool Project::ExporterIterator::next()
  778. {
  779. if (++index >= project.getNumExporters())
  780. return false;
  781. exporter = project.createExporter (index);
  782. if (exporter == nullptr)
  783. {
  784. jassertfalse; // corrupted project file?
  785. return next();
  786. }
  787. return true;
  788. }