hydrogen 1.2.6
Files.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#include <core/config.h>
24#include <core/Helpers/Files.h>
26#include <core/Basics/Pattern.h>
28#include <core/Basics/Song.h>
29
30namespace H2Core
31{
32
33
34 QString Files::savePattern( SaveMode mode, const QString& fileName, const Pattern* pPattern, std::shared_ptr<Song> pSong, const QString& drumkitName )
35 {
36 QFileInfo fileInfo;
37
38 switch ( mode ) {
39 case SAVE_NEW:
40 case SAVE_OVERWRITE:
41 fileInfo = QFileInfo( Filesystem::pattern_path( drumkitName,
42 fileName ) );
43 break;
44 case SAVE_PATH:
45 fileInfo = QFileInfo( fileName );
46 break;
47 case SAVE_TMP:
48 fileInfo = QFileInfo( Filesystem::tmp_file_path( fileName ) );
49 break;
50 default:
51 ERRORLOG( QString( "unknown mode : %1" ).arg( mode ) );
52 return nullptr;
53 break;
54 }
55
56 if ( mode == SAVE_NEW && Filesystem::file_exists( fileInfo.absoluteFilePath(), true ) ) {
57 return nullptr;
58 }
59
60 if ( !Filesystem::path_usable( fileInfo.path(), true, false ) ) {
61 return nullptr;
62 }
63
64 if ( !pPattern->save_file( drumkitName, pSong->getAuthor(), pSong->getLicense(), fileInfo.absoluteFilePath(), true ) ) {
65 return nullptr;
66 }
67
68 return fileInfo.absoluteFilePath();
69 }
70
71 QString Files::savePlaylist( SaveMode mode, const QString& fileName, Playlist* playlist, bool relativePaths )
72 {
73 QFileInfo fileInfo;
74
75 switch ( mode ) {
76 case SAVE_NEW:
77 case SAVE_OVERWRITE:
78 fileInfo = QFileInfo( Filesystem::playlist_path( fileName ) );
79 break;
80 case SAVE_PATH:
81 fileInfo = QFileInfo( fileName );
82 break;
83 case SAVE_TMP:
84 fileInfo = QFileInfo( Filesystem::tmp_file_path( fileName ) );
85 break;
86 default:
87 ERRORLOG( QString( "unknown mode : %1" ).arg( mode ) );
88 return nullptr;
89 break;
90 }
91
92 if ( mode == SAVE_NEW && Filesystem::file_exists( fileInfo.absoluteFilePath(), false ) ) {
93 return nullptr;
94 }
95
96 if ( !Filesystem::path_usable( fileInfo.path(), true, false ) ) {
97 return nullptr;
98 }
99
100 if ( !playlist->save_file( fileInfo.absoluteFilePath(), fileInfo.fileName(), true, relativePaths) ) {
101 return nullptr;
102 }
103
104 return fileInfo.absoluteFilePath();
105 }
106};
107
108/* vim: set softtabstop=4 noexpandtab: */
#define ERRORLOG(x)
Definition Object.h:242
@ SAVE_OVERWRITE
Definition Files.h:46
static QString savePlaylist(SaveMode mode, const QString &fileName, Playlist *playlist, bool relativePaths)
Definition Files.cpp:71
static QString savePattern(SaveMode mode, const QString &fileName, const Pattern *pattern, std::shared_ptr< Song > song, const QString &drumkitName)
Definition Files.cpp:34
static QString playlist_path(const QString &pl_name)
returns user playlist path, add file extension
static bool file_exists(const QString &path, bool silent=false)
returns true if the given path is an existing regular file
static QString pattern_path(const QString &dk_name, const QString &p_name)
returns user patterns path, add file extension
static bool path_usable(const QString &path, bool create=true, bool silent=false)
returns true if the path is a readable and writable regular directory, create if it not exists
static QString tmp_file_path(const QString &base)
touch a temporary file under tmp_dir() and return it's path.
Pattern class is a Note container.
Definition Pattern.h:46
bool save_file(const QString &drumkit_name, const QString &author, const License &license, const QString &pattern_path, bool overwrite=false) const
save a pattern into an xml file
Definition Pattern.cpp:147
Drumkit info.
Definition Playlist.h:37
bool save_file(const QString &pl_path, const QString &name, bool overwrite, bool useRelativePaths)
Definition Playlist.cpp:131