Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
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.

178 lines
4.0KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. /* Routings for opening/closing/creation of projects. All the actual
  19. project state belongs to Timeline and other classes. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <fcntl.h>
  26. #include "Loggable.H"
  27. #include "Project.H"
  28. #include "Timeline.H" // for sample_rate();
  29. /* FIXME: wrong place for this */
  30. #define APP_TITLE "Non-DAW"
  31. #define PROJECT_VERSION "0.28.0"
  32. #include "debug.h"
  33. char Project::_name[256];
  34. bool Project::_is_open = false;
  35. void
  36. Project::set_name ( const char *name )
  37. {
  38. char *s = rindex( name, '/' );
  39. strcpy( Project::_name, s ? s + 1 : name );
  40. for ( s = Project::_name; *s; ++s )
  41. if ( *s == '_' || *s == '-' )
  42. *s = ' ';
  43. }
  44. static int
  45. exists ( const char *name )
  46. {
  47. struct stat st;
  48. return 0 == stat( name, &st );
  49. }
  50. #include <errno.h>
  51. bool
  52. Project::close ( void )
  53. {
  54. Loggable::close();
  55. write_info();
  56. _is_open = false;
  57. }
  58. bool
  59. Project::write_info ( void )
  60. {
  61. if ( ! open() )
  62. return true;
  63. FILE *fp;
  64. if ( ! ( fp = fopen( "info", "w" ) ) )
  65. {
  66. WARNING( "could not open project info file for writing." );
  67. return false;
  68. }
  69. fprintf( fp, "created by\n\t%s\nversion\n\t%s\nsample rate\n\t%lu\n",
  70. APP_TITLE " " VERSION,
  71. PROJECT_VERSION,
  72. timeline->sample_rate() );
  73. fclose( fp );
  74. return true;
  75. }
  76. bool
  77. Project::read_info ( void )
  78. {
  79. FILE *fp;
  80. if ( ! ( fp = fopen( "info", "r" ) ) )
  81. {
  82. WARNING( "could not open project info file for reading." );
  83. return false;
  84. }
  85. /* TODO: something */
  86. fclose( fp );
  87. return true;
  88. }
  89. bool
  90. Project::open ( const char *name )
  91. {
  92. if ( chdir( name ) )
  93. {
  94. WARNING( "Cannot change to project dir \"%s\"", name );
  95. return false;
  96. }
  97. if ( ! exists( "history" ) ||
  98. ! exists( "sources" ) )
  99. // ! exists( "options" ) )
  100. {
  101. WARNING( "Not a Non-DAW project: \"%s\"", name );
  102. return false;
  103. }
  104. if ( ! Loggable::open( "history" ) )
  105. FATAL( "error opening journal" );
  106. set_name( name );
  107. read_info();
  108. _is_open = true;
  109. return true;
  110. }
  111. bool
  112. Project::create ( const char *name, const char *template_name )
  113. {
  114. close();
  115. if ( exists( name ) )
  116. {
  117. WARNING( "Project already exists" );
  118. return false;
  119. }
  120. if ( mkdir( name, 0777 ) )
  121. {
  122. WARNING( "Cannot create project directory" );
  123. return false;
  124. }
  125. if ( chdir( name ) )
  126. FATAL( "WTF? Cannot change to new project directory" );
  127. mkdir( "sources", 0777 );
  128. creat( "history", 0666 );
  129. /* TODO: copy template */
  130. return open( name );
  131. }