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.

334 lines
7.3KB

  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. /* Project management routines. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/fcntl.h>
  27. #include <errno.h>
  28. #include "Loggable.H"
  29. #include "Project.H"
  30. #include <FL/filename.H>
  31. #include "const.h"
  32. #include "util/debug.h"
  33. #include "util/file.h"
  34. #include "Mixer.H"
  35. const int PROJECT_VERSION = 1;
  36. const char *Project::_errstr[] =
  37. {
  38. "Not a Non-Mixer project",
  39. "Locked by another process",
  40. "Access denied",
  41. "Incompatible project version"
  42. };
  43. char Project::_name[256];
  44. char Project::_created_on[40];
  45. char Project::_path[512];
  46. bool Project::_is_open = false;
  47. int Project::_lockfd = 0;
  48. /***********/
  49. /* Private */
  50. /***********/
  51. void
  52. Project::set_name ( const char *name )
  53. {
  54. strcpy( Project::_name, name );
  55. if ( Project::_name[ strlen( Project::_name ) - 1 ] == '/' )
  56. Project::_name[ strlen( Project::_name ) - 1 ] = '\0';
  57. char *s = rindex( Project::_name, '/' );
  58. s = s ? s + 1 : Project::_name;
  59. memmove( Project::_name, s, strlen( s ) + 1 );
  60. for ( s = Project::_name; *s; ++s )
  61. if ( *s == '_' || *s == '-' )
  62. *s = ' ';
  63. }
  64. bool
  65. Project::write_info ( void )
  66. {
  67. FILE *fp;
  68. if ( ! ( fp = fopen( "info", "w" ) ) )
  69. {
  70. WARNING( "could not open project info file for writing." );
  71. return false;
  72. }
  73. char s[40];
  74. if ( ! *_created_on )
  75. {
  76. time_t t = time( NULL );
  77. ctime_r( &t, s );
  78. s[ strlen( s ) - 1 ] = '\0';
  79. }
  80. else
  81. strcpy( s, _created_on );
  82. fprintf( fp, "created by\n\t%s\ncreated on\n\t%s\nversion\n\t%d\n",
  83. APP_TITLE " " VERSION,
  84. s,
  85. PROJECT_VERSION );
  86. fclose( fp );
  87. return true;
  88. }
  89. bool
  90. Project::read_info ( int *version, char **creation_date, char **created_by )
  91. {
  92. FILE *fp;
  93. if ( ! ( fp = fopen( "info", "r" ) ) )
  94. {
  95. WARNING( "could not open project info file for reading." );
  96. return false;
  97. }
  98. *version = 0;
  99. *creation_date = 0;
  100. *created_by = 0;
  101. char *name, *value;
  102. while ( fscanf( fp, "%a[^\n]\n\t%a[^\n]\n", &name, &value ) == 2 )
  103. {
  104. MESSAGE( "Info: %s = %s", name, value );
  105. if ( ! strcmp( name, "version" ) )
  106. *version = atoi( value );
  107. else if ( ! strcmp( name, "created on" ) )
  108. *creation_date = strdup( value );
  109. else if ( ! strcmp( name, "created by" ) )
  110. *created_by = strdup( value );
  111. free( name );
  112. free( value );
  113. }
  114. fclose( fp );
  115. return true;
  116. }
  117. /**********/
  118. /* Public */
  119. /**********/
  120. /** Save out any settings and unjournaled state... */
  121. bool
  122. Project::save ( void )
  123. {
  124. if ( ! open() )
  125. return true;
  126. // tle->save_timeline_settings();
  127. return mixer->save();
  128. // return Loggable::save_unjournaled_state();
  129. }
  130. /** Close the project (reclaiming all memory) */
  131. bool
  132. Project::close ( void )
  133. {
  134. if ( ! open() )
  135. return true;
  136. if ( ! save() )
  137. return false;
  138. Loggable::close();
  139. /* // write_info(); */
  140. _is_open = false;
  141. *Project::_name = '\0';
  142. *Project::_created_on = '\0';
  143. release_lock( &_lockfd, ".lock" );
  144. return true;
  145. }
  146. /** Ensure a project is valid before opening it... */
  147. bool
  148. Project::validate ( const char *name )
  149. {
  150. bool r = true;
  151. char pwd[512];
  152. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  153. if ( chdir( name ) )
  154. {
  155. WARNING( "Cannot change to project dir \"%s\"", name );
  156. return false;
  157. }
  158. if ( ! exists( "info" ) ||
  159. ! exists( "snapshot" ))
  160. {
  161. WARNING( "Not a Non-Mixer project: \"%s\"", name );
  162. r = false;
  163. }
  164. chdir( pwd );
  165. return r;
  166. }
  167. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  168. * otherwise */
  169. int
  170. Project::open ( const char *name )
  171. {
  172. if ( ! validate( name ) )
  173. return E_INVALID;
  174. close();
  175. chdir( name );
  176. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  177. return E_LOCKED;
  178. int version;
  179. char *creation_date;
  180. char *created_by;
  181. if ( ! read_info( &version, &creation_date, &created_by ) )
  182. return E_INVALID;
  183. if ( strncmp( created_by, APP_TITLE, strlen( APP_TITLE ) ) )
  184. return E_INVALID;
  185. if ( version != PROJECT_VERSION )
  186. return E_VERSION;
  187. if ( ! Loggable::replay( "snapshot" ) )
  188. return E_INVALID;
  189. if ( creation_date )
  190. {
  191. strcpy( _created_on, creation_date );
  192. free( creation_date );
  193. }
  194. else
  195. *_created_on = 0;
  196. set_name( name );
  197. *_path = '\0';
  198. fl_filename_absolute( _path, sizeof( _path ), "." );
  199. _is_open = true;
  200. // tle->load_timeline_settings();
  201. // timeline->zoom_fit();
  202. MESSAGE( "Loaded project \"%s\"", name );
  203. return 0;
  204. }
  205. /** Create a new project /name/ from existing template
  206. * /template_name/ */
  207. bool
  208. Project::create ( const char *name, const char *template_name )
  209. {
  210. if ( exists( name ) )
  211. {
  212. WARNING( "Project already exists" );
  213. return false;
  214. }
  215. close();
  216. if ( mkdir( name, 0777 ) )
  217. {
  218. WARNING( "Cannot create project directory" );
  219. return false;
  220. }
  221. if ( chdir( name ) )
  222. FATAL( "WTF? Cannot change to new project directory" );
  223. // mkdir( "sources", 0777 );
  224. creat( "snapshot", 0666 );
  225. /* TODO: copy template */
  226. write_info();
  227. if ( open( name ) == 0 )
  228. {
  229. // /* add the bare essentials */
  230. // timeline->beats_per_minute( 0, 120 );
  231. // timeline->time( 0, 4, 4 );
  232. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  233. return true;
  234. }
  235. else
  236. {
  237. WARNING( "Failed to open newly created project" );
  238. return false;
  239. }
  240. }
  241. /** Replace the journal with a snapshot of the current state */
  242. void
  243. Project::compact ( void )
  244. {
  245. Loggable::compact();
  246. }