43#if defined(H2CORE_HAVE_OSC) || _DOXYGEN_
51 m_bUnderSessionManagement( false ),
53 m_sSessionFolderPath(
"" ),
54 m_bIsNewSession( false )
71 const char *displayName,
78 auto pController = pHydrogen->getCoreActionController();
87 std::cout << std::endl;
94 QDir sessionFolder( name );
95 if ( !sessionFolder.exists() ) {
96 if ( !sessionFolder.mkpath( name ) ) {
105 const QFileInfo sessionPath( name );
106 const QString sSongPath = QString(
"%1/%2%3" )
108 .arg( sessionPath.fileName() )
111 const QFileInfo songFileInfo = QFileInfo( sSongPath );
116 if ( pPref !=
nullptr ){
119 pPref->setNsmClientId( QString( clientID ) );
129 bool bEmptySongOpened =
false;
130 std::shared_ptr<H2Core::Song> pSong =
nullptr;
131 if ( songFileInfo.exists() ) {
139 if ( pSong ==
nullptr ) {
148 if ( pSong ==
nullptr ) {
152 pSong->setFilename( sSongPath );
153 bEmptySongOpened =
true;
158 pSong->setIsModified(
true );
163 pHydrogen->setSessionDrumkitNeedsRelinking(
true );
166 if ( ! pController->openSong( pSong,
false ) ) {
180 auto pCoreActionController = pHydrogen->getCoreActionController();
183 if ( !preferences.exists() ) {
187 const QString sNewPreferencesPath = QString(
"%1/%2" )
197 const QFileInfo newPreferencesFileInfo( sNewPreferencesPath );
198 if ( newPreferencesFileInfo.exists() ){
201 pPref->loadPreferences(
false );
204 if ( !preferences.copy( sNewPreferencesPath ) ) {
206 .arg( sNewPreferencesPath ) );
209 .arg( sNewPreferencesPath ) );
214 pCoreActionController->updatePreferences();
223 const QString sLinkedDrumkitPath = QString(
"%1/%2" )
224 .arg( sSessionFolder ).arg(
"drumkit" );
225 const QFileInfo linkedDrumkitPathInfo( sLinkedDrumkitPath );
228 if ( linkedDrumkitPathInfo.isSymLink() ||
229 linkedDrumkitPathInfo.isDir() ) {
232 pHydrogen->getSoundLibraryDatabase()->getDrumkit( sLinkedDrumkitPath );
233 if ( pDrumkit ==
nullptr ) {
234 ERRORLOG(
"Unable to load drumkit from session folder" );
238 ERRORLOG(
"No valid drumkit found in session folder" );
246 bool bRelinkDrumkit =
true;
248 const QString sDrumkitName = pSong->getLastLoadedDrumkitName();
249 const QString sDrumkitAbsPath = pSong->getLastLoadedDrumkitPath();
254 if ( sDrumkitAbsPath.contains( sSessionFolder, Qt::CaseInsensitive ) ) {
255 NsmClient::printError( QString(
"Last loaded drumkit [%1] with absolute path [%2] is located within the session folder [%3]. Linking skipped." )
257 .arg( sDrumkitAbsPath )
258 .arg( sSessionFolder ) );
262 const QString sLinkedDrumkitPath = QString(
"%1/%2" )
263 .arg( sSessionFolder ).arg(
"drumkit" );
264 const QFileInfo linkedDrumkitPathInfo( sLinkedDrumkitPath );
267 if ( linkedDrumkitPathInfo.isSymLink() ||
268 linkedDrumkitPathInfo.isDir() ) {
273 QString sLinkedDrumkitPath;
274 if ( linkedDrumkitPathInfo.isSymLink() ) {
275 sLinkedDrumkitPath = QString(
"%1" )
276 .arg( linkedDrumkitPathInfo.symLinkTarget() );
278 sLinkedDrumkitPath = QString(
"%1" )
279 .arg( sLinkedDrumkitPath );
284 QString sLinkedDrumkitName(
"seemsLikeTheKitCouldNotBeRetrievedFromTheDatabase" );
285 auto pSoundLibraryDatabase = pHydrogen->getSoundLibraryDatabase();
286 if ( pSoundLibraryDatabase !=
nullptr ) {
287 auto pDrumkit = pSoundLibraryDatabase->getDrumkit( sLinkedDrumkitPath );
288 if ( pDrumkit !=
nullptr ) {
289 sLinkedDrumkitName = pDrumkit->get_name();
293 if ( sLinkedDrumkitName == sDrumkitName ) {
294 bRelinkDrumkit =
false;
304 if ( bRelinkDrumkit ){
306 QFile linkedDrumkitFile( sLinkedDrumkitPath );
308 if ( linkedDrumkitFile.exists() ) {
309 if ( linkedDrumkitPathInfo.isDir() &&
310 ! linkedDrumkitPathInfo.isSymLink() ) {
314 QDir oldDrumkitFolder( sLinkedDrumkitPath );
315 if ( ! oldDrumkitFolder.rename( sLinkedDrumkitPath,
316 QString(
"%1/drumkit_old" )
317 .arg( sSessionFolder ) ) ) {
319 .arg( sLinkedDrumkitPath ) );
323 if ( ! linkedDrumkitFile.remove() ) {
325 .arg( sLinkedDrumkitPath ) );
331 if ( sDrumkitAbsPath.isEmpty() ) {
334 .arg( sDrumkitName ) );
338 QFile targetPath( sDrumkitAbsPath );
339 if ( !targetPath.link( sLinkedDrumkitPath ) ) {
341 .arg( sLinkedDrumkitPath )
342 .arg( sDrumkitAbsPath ) );
351 pHydrogen->setSessionDrumkitNeedsRelinking(
false );
357 if ( pSong ==
nullptr ) {
362 const QString sLastLoadedDrumkitPath = pSong->getLastLoadedDrumkitPath();
363 const QString sLastLoadedDrumkitName = pSong->getLastLoadedDrumkitName();
367 Qt::CaseInsensitive ) ) {
372 const QFileInfo lastLoadedDrumkitInfo( sLastLoadedDrumkitPath );
373 if ( lastLoadedDrumkitInfo.isSymLink() ) {
375 QString sDeferencedDrumkit = lastLoadedDrumkitInfo.symLinkTarget();
378 .arg( sDeferencedDrumkit ) );
381 else if ( lastLoadedDrumkitInfo.isDir() ) {
395 bool bDrumkitFound =
false;
396 for (
const auto& pDrumkitEntry :
397 pHydrogen->getSoundLibraryDatabase()->getDrumkitDatabase() ) {
399 auto pDrumkit = pDrumkitEntry.second;
400 if ( pDrumkit !=
nullptr ) {
401 if ( pDrumkit->get_name() == sLastLoadedDrumkitName ) {
403 bDrumkitFound =
true;
410 if ( ! bDrumkitFound ) {
411 ERRORLOG( QString(
"Drumkit used in session folder [%1] is not present on the current system. It has to be installed first in order to use the exported song" )
412 .arg( sLastLoadedDrumkitName ) );
417 INFOLOG( QString(
"Drumkit used in session folder [%1] was dereferenced to [%2]" )
418 .arg( sLastLoadedDrumkitName )
419 .arg( pSong->getLastLoadedDrumkitPath() ) );
423 ERRORLOG(
"This should not happen" );
435 const QString sDrumkitToBeReplaced = pSong->getLastLoadedDrumkitPath();
437 pSong->setLastLoadedDrumkitPath( sDrumkitPath );
439 for (
auto pInstrument : *pSong->getInstrumentList() ) {
440 if ( pInstrument !=
nullptr &&
441 pInstrument->get_drumkit_path() == sDrumkitToBeReplaced ) {
443 pInstrument->set_drumkit_path( sDrumkitPath );
449 for (
auto pComponent : *pInstrument->get_components() ) {
450 if ( pComponent !=
nullptr ) {
451 for (
auto pInstrumentLayer : *pComponent ) {
452 if ( pInstrumentLayer !=
nullptr ) {
453 auto pSample = pInstrumentLayer->get_sample();
454 if ( pSample !=
nullptr ) {
455 QString sNewPath = QString(
"%1/%2" )
457 .arg( pSample->get_filename() );
470 std::cerr <<
"[\033[30mHydrogen\033[0m]\033[31m "
471 <<
"Error: " << msg.toLocal8Bit().data() <<
"\033[0m" << std::endl;
474 std::cerr <<
"[\033[30mHydrogen\033[0m]\033[32m "
475 << msg.toLocal8Bit().data() <<
"\033[0m" << std::endl;
482 if ( ! pController->saveSong() ) {
486 if ( ! pController->savePreferences() ) {
521 QByteArray byteArray = H2ProcessName.toLatin1();
523 const char *nsm_url = getenv(
"NSM_URL" );
538 if (
nsm_init( pNsm, nsm_url ) == 0 )
562 const int nNumberOfChecks = 10;
566 if ( pHydrogen->
getSong() !=
nullptr ) {
570 if ( nCheck > nNumberOfChecks ) {
593 if (
m_pNsm !=
nullptr ) {
NSM_EXPORT nsm_client_t * nsm_new(void)
NSM_EXPORT void nsm_check_wait(nsm_client_t *nsm, int timeout)
NSM_EXPORT void nsm_set_save_callback(nsm_client_t *nsm, nsm_save_callback *save_callback, void *userdata)
NSM_EXPORT int nsm_init(nsm_client_t *nsm, const char *nsm_url)
NSM_EXPORT void nsm_send_is_clean(nsm_client_t *nsm)
NSM_EXPORT void nsm_send_announce(nsm_client_t *nsm, const char *app_name, const char *capabilities, const char *process_name)
NSM_EXPORT void nsm_set_open_callback(nsm_client_t *nsm, nsm_open_callback *open_callback, void *userdata)
NSM_EXPORT void nsm_send_is_dirty(nsm_client_t *nsm)
NSM_EXPORT void nsm_free(nsm_client_t *nsm)
static bool drumkit_valid(const QString &dk_path)
returns true if the path contains a usable drumkit
static void setPreferencesOverwritePath(const QString &sPath)
static QString usr_config_path()
returns user config path
static QString prepare_sample_path(const QString &fname)
Returns the basename if the given path is under an existing user or system drumkit path,...
static QString sys_config_path()
returns system config path
static const QString songs_ext
std::shared_ptr< Song > getSong() const
Get the current song.
static Hydrogen * get_instance()
Returns the current Hydrogen instance __instance.
CoreActionController * getCoreActionController() const
Manager for User Preferences File (singleton)
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
QString getH2ProcessName()
static std::shared_ptr< Song > load(const QString &sFilename, bool bSilent=false)
Load a song from file.
static std::shared_ptr< Song > getEmptySong()
Non session manager client implementation.
void setIsNewSession(bool bNew)
void setSessionFolderPath(const QString &sPath)
static int dereferenceDrumkit(std::shared_ptr< H2Core::Song > pSong)
Replaces a path in Song::m_sLastLoadedDrumkitPath pointing to the session folder with one pointing to...
void sendDirtyState(const bool isDirty)
Informs the NSM server whether the current H2Core::Song is modified or not.
static int OpenCallback(const char *name, const char *displayName, const char *clientID, char **outMsg, void *userData)
Callback function for the NSM server to tell Hydrogen to open a H2Core::Song.
static void printError(const QString &msg)
Custom function to print a colored error message.
static NsmClient * get_instance()
static void loadDrumkit()
Checks whether there is a drumkit present in the session folder and loads it into the H2Core::SoundLi...
static NsmClient * __instance
Object holding the current NsmClient singleton.
static void create_instance()
If __instance equals nullptr, a new NsmClient singleton will be created and stored in it.
void shutdown()
Causes the NSM client to not process events anymore.
static void copyPreferences(const char *name)
Part of OpenCallback() responsible for copying and loading the preferences.
QString getSessionFolderPath() const
nsm_client_t * m_pNsm
Stores the current instance of the NSM client.
static void linkDrumkit(std::shared_ptr< H2Core::Song > pSong)
Responsible for linking a drumkit on user or system level into the session folder and updating all co...
static void replaceDrumkitPath(std::shared_ptr< H2Core::Song > pSong, const QString &sDrumkitPath)
Replaces @H2Core::Song::m_sLastLoadedDrumkitPath as well as all @H2Core::Instrument::__drumkit_path b...
static bool bNsmShutdown
Indicates whether the NsmClient::NsmProcessEvent() function should continue processing events.
void createInitialClient()
Actual setup, initialization, and registration of the NSM client.
static int SaveCallback(char **outMsg, void *userData)
Callback function for the NSM server to tell Hydrogen to save the current session.
static void printMessage(const QString &msg)
Custom function to print a colored message.
bool m_bUnderSessionManagement
To determine whether Hydrogen is under NON session management, it is not sufficient to check whether ...
pthread_t m_NsmThread
Thread the NSM client will run in.
NsmClient()
Private constructor to allow construction only via create_instance().
static void * ProcessEvent(void *data)
Event handling function of the NSM client.