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.

347 lines
7.5KB

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