hydrogen 1.2.3
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-2024 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 = Filesystem::pattern_path( drumkitName, fileName );
42 break;
43 case SAVE_PATH:
44 fileInfo = fileName;
45 break;
46 case SAVE_TMP:
47 fileInfo = Filesystem::tmp_file_path( fileName );
48 break;
49 default:
50 ERRORLOG( QString( "unknown mode : %1" ).arg( mode ) );
51 return nullptr;
52 break;
53 }
54
55 if ( mode == SAVE_NEW && Filesystem::file_exists( fileInfo.absoluteFilePath(), true ) ) {
56 return nullptr;
57 }
58
59 if ( !Filesystem::path_usable( fileInfo.path(), true, false ) ) {
60 return nullptr;
61 }
62
63 if ( !pPattern->save_file( drumkitName, pSong->getAuthor(), pSong->getLicense(), fileInfo.absoluteFilePath(), true ) ) {
64 return nullptr;
65 }
66
67 return fileInfo.absoluteFilePath();
68 }
69
70 QString Files::savePlaylist( SaveMode mode, const QString& fileName, Playlist* playlist, bool relativePaths )
71 {
72 QFileInfo fileInfo;
73
74 switch ( mode ) {
75 case SAVE_NEW:
76 case SAVE_OVERWRITE:
77 fileInfo = Filesystem::playlist_path( fileName );
78 break;
79 case SAVE_PATH:
80 fileInfo = fileName;
81 break;
82 case SAVE_TMP:
83 fileInfo = Filesystem::tmp_file_path( fileName );
84 break;
85 default:
86 ERRORLOG( QString( "unknown mode : %1" ).arg( mode ) );
87 return nullptr;
88 break;
89 }
90
91 if ( mode == SAVE_NEW && Filesystem::file_exists( fileInfo.absoluteFilePath(), false ) ) {
92 return nullptr;
93 }
94
95 if ( !Filesystem::path_usable( fileInfo.path(), true, false ) ) {
96 return nullptr;
97 }
98
99 if ( !playlist->save_file( fileInfo.absoluteFilePath(), fileInfo.fileName(), true, relativePaths) ) {
100 return nullptr;
101 }
102
103 return fileInfo.absoluteFilePath();
104 }
105};
106
107/* vim: set softtabstop=4 noexpandtab: */
#define ERRORLOG(x)
Definition Object.h:239
@ SAVE_OVERWRITE
Definition Files.h:46
static QString savePlaylist(SaveMode mode, const QString &fileName, Playlist *playlist, bool relativePaths)
Definition Files.cpp:70
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:152
Drumkit info.
Definition Playlist.h:37
bool save_file(const QString &pl_path, const QString &name, bool overwrite, bool useRelativePaths)
Definition Playlist.cpp:123