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.

189 lines
4.2KB

  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. return true;
  58. }
  59. bool
  60. Project::write_info ( void )
  61. {
  62. if ( ! open() )
  63. return true;
  64. FILE *fp;
  65. if ( ! ( fp = fopen( "info", "w" ) ) )
  66. {
  67. WARNING( "could not open project info file for writing." );
  68. return false;
  69. }
  70. fprintf( fp, "created by\n\t%s\nversion\n\t%s\nsample rate\n\t%lu\n",
  71. APP_TITLE " " VERSION,
  72. PROJECT_VERSION,
  73. (unsigned long)timeline->sample_rate() );
  74. fclose( fp );
  75. return true;
  76. }
  77. bool
  78. Project::read_info ( void )
  79. {
  80. FILE *fp;
  81. if ( ! ( fp = fopen( "info", "r" ) ) )
  82. {
  83. WARNING( "could not open project info file for reading." );
  84. return false;
  85. }
  86. /* TODO: something */
  87. fclose( fp );
  88. return true;
  89. }
  90. bool
  91. Project::open ( const char *name )
  92. {
  93. if ( chdir( name ) )
  94. {
  95. WARNING( "Cannot change to project dir \"%s\"", name );
  96. return false;
  97. }
  98. if ( ! exists( "history" ) ||
  99. ! exists( "sources" ) )
  100. // ! exists( "options" ) )
  101. {
  102. WARNING( "Not a Non-DAW project: \"%s\"", name );
  103. return false;
  104. }
  105. if ( ! Loggable::open( "history" ) )
  106. FATAL( "error opening journal" );
  107. set_name( name );
  108. read_info();
  109. _is_open = true;
  110. return true;
  111. }
  112. bool
  113. Project::create ( const char *name, const char *template_name )
  114. {
  115. close();
  116. if ( exists( name ) )
  117. {
  118. WARNING( "Project already exists" );
  119. return false;
  120. }
  121. if ( mkdir( name, 0777 ) )
  122. {
  123. WARNING( "Cannot create project directory" );
  124. return false;
  125. }
  126. if ( chdir( name ) )
  127. FATAL( "WTF? Cannot change to new project directory" );
  128. mkdir( "sources", 0777 );
  129. creat( "history", 0666 );
  130. /* TODO: copy template */
  131. if ( open( name ) )
  132. {
  133. /* add the bare essentials */
  134. timeline->beats_per_minute( 0, 120 );
  135. timeline->time( 0, 4, 4 );
  136. return true;
  137. }
  138. else
  139. return false;
  140. }