-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathDataFile.cpp
More file actions
2245 lines (1977 loc) · 61.1 KB
/
DataFile.cpp
File metadata and controls
2245 lines (1977 loc) · 61.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DataFile.cpp - implementation of class DataFile
*
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2012-2013 Paul Giblock <p/at/pgiblock.net>
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "DataFile.h"
#include <algorithm>
#include <cmath>
#include <map>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QMessageBox>
#include <QRegularExpression>
#include <QSaveFile>
#include "base64.h"
#include "ConfigManager.h"
#include "DeprecationHelper.h"
#include "Effect.h"
#include "embed.h"
#include "GuiApplication.h"
#include "LocaleHelper.h"
#include "Note.h"
#include "PluginFactory.h"
#include "ProjectVersion.h"
#include "SongEditor.h"
#include "TextFloat.h"
#include "Track.h"
#include "PathUtil.h"
#include "UpgradeExtendedNoteRange.h"
#include "lmmsversion.h"
namespace lmms
{
static void findIds(const QDomElement& elem, QList<jo_id_t>& idList);
// QMap with the DOM elements that access file resources
const DataFile::ResourcesMap DataFile::ELEMENTS_WITH_RESOURCES = {
{ "sampleclip", {"src"} },
{ "audiofileprocessor", {"src"} },
};
// Vector with all the upgrade methods
const std::vector<DataFile::UpgradeMethod> DataFile::UPGRADE_METHODS = {
&DataFile::upgrade_0_2_1_20070501 , &DataFile::upgrade_0_2_1_20070508,
&DataFile::upgrade_0_3_0_rc2 , &DataFile::upgrade_0_3_0,
&DataFile::upgrade_0_4_0_20080104 , &DataFile::upgrade_0_4_0_20080118,
&DataFile::upgrade_0_4_0_20080129 , &DataFile::upgrade_0_4_0_20080409,
&DataFile::upgrade_0_4_0_20080607 , &DataFile::upgrade_0_4_0_20080622,
&DataFile::upgrade_0_4_0_beta1 , &DataFile::upgrade_0_4_0_rc2,
&DataFile::upgrade_1_0_99 , &DataFile::upgrade_1_1_0,
&DataFile::upgrade_1_1_91 , &DataFile::upgrade_1_2_0_rc3,
&DataFile::upgrade_1_3_0 , &DataFile::upgrade_noHiddenClipNames,
&DataFile::upgrade_automationNodes , &DataFile::upgrade_extendedNoteRange,
&DataFile::upgrade_defaultTripleOscillatorHQ,
&DataFile::upgrade_mixerRename , &DataFile::upgrade_bbTcoRename,
&DataFile::upgrade_sampleAndHold , &DataFile::upgrade_midiCCIndexing,
&DataFile::upgrade_loopsRename , &DataFile::upgrade_noteTypes,
&DataFile::upgrade_fixCMTDelays , &DataFile::upgrade_fixBassLoopsTypo,
&DataFile::findProblematicLadspaPlugins,
&DataFile::upgrade_noHiddenAutomationTracks
};
// Vector of all versions that have upgrade routines.
const std::vector<ProjectVersion> DataFile::UPGRADE_VERSIONS = {
"0.2.1-20070501" , "0.2.1-20070508" , "0.3.0-rc2",
"0.3.0" , "0.4.0-20080104" , "0.4.0-20080118",
"0.4.0-20080129" , "0.4.0-20080409" , "0.4.0-20080607",
"0.4.0-20080622" , "0.4.0-beta1" , "0.4.0-rc2",
"1.0.99-0" , "1.1.0-0" , "1.1.91-0",
"1.2.0-rc3" , "1.3.0"
};
namespace
{
struct TypeDescStruct
{
DataFile::Type m_type;
QString m_name;
};
const auto s_types = std::array{
TypeDescStruct{ DataFile::Type::Unknown, "unknown" },
TypeDescStruct{ DataFile::Type::SongProject, "song" },
TypeDescStruct{ DataFile::Type::SongProjectTemplate, "songtemplate" },
TypeDescStruct{ DataFile::Type::InstrumentTrackSettings, "instrumenttracksettings" },
TypeDescStruct{ DataFile::Type::DragNDropData, "dnddata" },
TypeDescStruct{ DataFile::Type::ClipboardData, "clipboard-data" },
TypeDescStruct{ DataFile::Type::JournalData, "journaldata" },
TypeDescStruct{ DataFile::Type::EffectSettings, "effectsettings" },
TypeDescStruct{ DataFile::Type::MidiClip, "midiclip" }
};
}
DataFile::DataFile( Type type ) :
QDomDocument( "lmms-project" ),
m_fileName(""),
m_content(),
m_head(),
m_type( type ),
m_fileVersion( UPGRADE_METHODS.size() )
{
appendChild( createProcessingInstruction("xml", "version=\"1.0\""));
QDomElement root = createElement( "lmms-project" );
root.setAttribute( "version", m_fileVersion );
root.setAttribute( "type", typeName( type ) );
root.setAttribute( "creator", "LMMS" );
root.setAttribute( "creatorversion", LMMS_VERSION );
root.setAttribute("creatorplatform", QSysInfo::kernelType());
root.setAttribute("creatorplatformtype", QSysInfo::productType());
appendChild( root );
m_head = createElement( "head" );
root.appendChild( m_head );
m_content = createElement( typeName( type ) );
root.appendChild( m_content );
}
DataFile::DataFile( const QString & _fileName ) :
QDomDocument(),
m_fileName(_fileName),
m_content(),
m_head(),
m_fileVersion( UPGRADE_METHODS.size() )
{
QFile inFile( _fileName );
if( !inFile.open( QIODevice::ReadOnly ) )
{
if (gui::getGUI() != nullptr)
{
QMessageBox::critical( nullptr,
gui::SongEditor::tr( "Could not open file" ),
gui::SongEditor::tr( "Could not open file %1. You probably "
"have no permissions to read this "
"file.\n Please make sure to have at "
"least read permissions to the file "
"and try again." ).arg( _fileName ) );
}
return;
}
loadData( inFile.readAll(), _fileName );
}
DataFile::DataFile( const QByteArray & _data ) :
QDomDocument(),
m_fileName(""),
m_content(),
m_head(),
m_fileVersion( UPGRADE_METHODS.size() )
{
loadData( _data, "<internal data>" );
}
bool DataFile::validate( QString extension )
{
switch (m_type)
{
case Type::SongProject:
if (extension == "mmp" || extension == "mmpz")
{
return true;
}
break;
case Type::SongProjectTemplate:
if (extension == "mpt")
{
return true;
}
break;
case Type::InstrumentTrackSettings:
if (extension == "xpf" || extension == "xml")
{
return true;
}
break;
case Type::EffectSettings:
if (extension == "fxp")
{
return true;
}
break;
case Type::MidiClip:
if (extension == "xpt" || extension == "xptz")
{
return true;
}
break;
case Type::Unknown:
if (! ( extension == "mmp" || extension == "mpt" || extension == "mmpz" ||
extension == "xpf" || extension == "xml" ||
( extension == "xiz" && ! getPluginFactory()->pluginSupportingExtension(extension).isNull()) ||
extension == "sf2" || extension == "sf3" || extension == "pat" || extension == "mid" ||
extension == "dll"
#ifdef LMMS_BUILD_LINUX
|| extension == "so"
#endif
#ifdef LMMS_HAVE_LV2
|| extension == "lv2"
#endif
) )
{
return true;
}
if( extension == "wav" || extension == "ogg" || extension == "ds"
#ifdef LMMS_HAVE_SNDFILE_MP3
|| extension == "mp3"
#endif
)
{
return true;
}
break;
default:
return false;
}
return false;
}
QString DataFile::nameWithExtension( const QString & _fn ) const
{
const QString extension = _fn.section( '.', -1 );
switch (type())
{
case Type::SongProject:
if (extension != "mmp" &&
extension != "mpt" &&
extension != "mmpz")
{
if (ConfigManager::inst()->value("app",
"nommpz" ).toInt() == 0)
{
return _fn + ".mmpz";
}
return _fn + ".mmp";
}
break;
case Type::SongProjectTemplate:
if (extension != "mpt")
{
return _fn + ".mpt";
}
break;
case Type::InstrumentTrackSettings:
if (extension != "xpf")
{
return _fn + ".xpf";
}
break;
default: ;
}
return _fn;
}
void DataFile::write( QTextStream & _strm )
{
if( type() == Type::SongProject || type() == Type::SongProjectTemplate
|| type() == Type::InstrumentTrackSettings )
{
cleanMetaNodes( documentElement() );
}
save(_strm, 2);
}
bool DataFile::writeFile(const QString& filename, bool withResources)
{
// Small lambda function for displaying errors
auto showError = [](QString title, QString body){
if (gui::getGUI() != nullptr)
{
QMessageBox mb;
mb.setWindowTitle(title);
mb.setText(body);
mb.setIcon(QMessageBox::Warning);
mb.setStandardButtons(QMessageBox::Ok);
mb.exec();
}
else
{
qWarning() << body;
}
};
// If we are saving without resources, filename is just the file we are
// saving to. If we are saving with resources (project bundle), filename
// will be used (discarding extensions) to create a folder where the
// bundle will be saved in
QFileInfo fInfo(filename);
const QString bundleDir = fInfo.path() + "/" + fInfo.fileName().section('.', 0, 0);
const QString resourcesDir = bundleDir + "/resources";
const QString fullName = withResources
? nameWithExtension(bundleDir + "/" + fInfo.fileName())
: nameWithExtension(filename);
const QString fullNameTemp = fullName + ".new";
const QString fullNameBak = fullName + ".bak";
using gui::SongEditor;
// If we are saving with resources, setup the bundle folder first
if (withResources)
{
// First check if there's a bundle folder with the same name in
// the path already. If so, warns user that we can't overwrite a
// project bundle.
if (QDir(bundleDir).exists())
{
showError(SongEditor::tr("Operation denied"),
SongEditor::tr("A bundle folder with that name already exists on the "
"selected path. Can't overwrite a project bundle. Please select a different "
"name."));
return false;
}
// Create bundle folder
if (!QDir().mkdir(bundleDir))
{
showError(SongEditor::tr("Error"),
SongEditor::tr("Couldn't create bundle folder."));
return false;
}
// Create resources folder
if (!QDir().mkdir(resourcesDir))
{
showError(SongEditor::tr("Error"),
SongEditor::tr("Couldn't create resources folder."));
return false;
}
// Copy resources to folder and update paths
if (!copyResources(resourcesDir))
{
showError(SongEditor::tr("Error"),
SongEditor::tr("Failed to copy resources."));
return false;
}
}
QSaveFile outfile(fullNameTemp);
if (!outfile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
showError(SongEditor::tr("Could not write file"),
SongEditor::tr("Could not open %1 for writing. You probably are not permitted to "
"write to this file. Please make sure you have write-access to "
"the file and try again.").arg(fullName));
return false;
}
const QString extension = fullName.section('.', -1);
if (extension == "mmpz" || extension == "xptz")
{
QString xml;
QTextStream ts( &xml );
write( ts );
outfile.write( qCompress( xml.toUtf8() ) );
}
else
{
QTextStream ts( &outfile );
write( ts );
}
if (!outfile.commit())
{
showError(SongEditor::tr("Could not write file"),
SongEditor::tr("An unknown error has occurred and the file could not be saved."));
return false;
}
if (ConfigManager::inst()->value("app", "disablebackup").toInt())
{
// remove current file
QFile::remove(fullName);
}
else
{
// remove old backup file
QFile::remove(fullNameBak);
// move current file to backup file
QFile::rename(fullName, fullNameBak);
}
// move temporary file to current file
QFile::rename(fullNameTemp, fullName);
return true;
}
bool DataFile::copyResources(const QString& resourcesDir)
{
// List of filenames used so we can append a counter to any
// repeating filenames
std::list<QString> namesList;
auto it = ELEMENTS_WITH_RESOURCES.begin();
// Copy resources and manipulate the DataFile to have local paths to them
while (it != ELEMENTS_WITH_RESOURCES.end())
{
QDomNodeList list = elementsByTagName(it->first);
// Go through all elements with the tagname from our map
for (int i = 0; !list.item(i).isNull(); ++i)
{
QDomElement el = list.item(i).toElement();
auto res = it->second.begin();
// Search for attributes that point to resources
while (res != it->second.end())
{
// If the element has that attribute
if (el.hasAttribute(*res))
{
// Get absolute path to resource
bool error;
QString resPath = PathUtil::toAbsolute(el.attribute(*res), &error);
// If we are running without the project loaded (from CLI), "local:" base
// prefixes aren't converted, so we need to convert it ourselves
if (error)
{
resPath = QFileInfo(m_fileName).path() + "/" + resPath.remove(0,
PathUtil::basePrefix(PathUtil::Base::LocalDir).length());
}
// Check if we need to add a counter to the filename
QString finalFileName = QFileInfo(resPath).fileName();
QString extension = resPath.section('.', -1);
int repeatedNames = 0;
for (QString name : namesList)
{
if (finalFileName == name)
{
++repeatedNames;
}
}
// Add the name to the list before modifying it
namesList.push_back(finalFileName);
if (repeatedNames)
{
// Remove the extension, add the counter and add the
// extension again to get the final file name
finalFileName.truncate(finalFileName.lastIndexOf('.'));
finalFileName = finalFileName + "-" + QString::number(repeatedNames) + "." + extension;
}
// Final path is our resources dir + the new file name
QString finalPath = resourcesDir + "/" + finalFileName;
// Copy resource file to the resources folder
if(!QFile::copy(resPath, finalPath))
{
qWarning("ERROR: Failed to copy resource");
return false;
}
// Update attribute path to point to the bundle file
QString newAtt = PathUtil::basePrefix(PathUtil::Base::LocalDir) + "resources/" + finalFileName;
el.setAttribute(*res, newAtt);
}
++res;
}
}
++it;
}
return true;
}
/**
* @brief This recursive method will go through all XML nodes of the DataFile
* and check whether any of them have local paths. If they are not on
* our list of elements that can have local paths we return true,
* indicating that we potentially have plugins with local paths that
* would be a security issue. The Song class can then abort loading
* this project.
* @param parent The parent node being iterated. When called
* without arguments, this will be an empty element that will be
* ignored (since the second parameter will be true).
* @param firstCall Defaults to true, and indicates to this recursive
* method whether this is the first call. If it is it will use the
* root element as the parent.
*/
bool DataFile::hasLocalPlugins(QDomElement parent /* = QDomElement()*/, bool firstCall /* = true*/) const
{
// If this is the first iteration of the recursion we use the root element
if (firstCall) { parent = documentElement(); }
auto children = parent.childNodes();
for (int i = 0; i < children.size(); ++i)
{
QDomNode child = children.at(i);
QDomElement childElement = child.toElement();
bool skipNode = false;
// Skip the nodes allowed to have "local:" attributes, but
// still check its children
for (const auto& element : ELEMENTS_WITH_RESOURCES)
{
if (childElement.tagName() == element.first)
{
skipNode = true;
break;
}
}
// Check if they have "local:" attribute (unless they are allowed to
// and skipNode is true)
if (!skipNode)
{
auto attributes = childElement.attributes();
for (int i = 0; i < attributes.size(); ++i)
{
QDomNode attribute = attributes.item(i);
QDomAttr attr = attribute.toAttr();
if (attr.value().startsWith(PathUtil::basePrefix(PathUtil::Base::LocalDir),
Qt::CaseInsensitive))
{
return true;
}
}
}
// Now we check the children of this node (recursively)
// and if any return true we return true.
if (hasLocalPlugins(childElement, false))
{
return true;
}
}
// If we got here none of the nodes had the "local:" path.
return false;
}
DataFile::Type DataFile::type( const QString& typeName )
{
const auto it = std::find_if(s_types.begin(), s_types.end(),
[&typeName](const TypeDescStruct& type) { return type.m_name == typeName; });
if (it != s_types.end()) { return it->m_type; }
// compat code
if( typeName == "channelsettings" )
{
return Type::InstrumentTrackSettings;
}
if (typeName == "pattern")
{
return Type::MidiClip;
}
return Type::Unknown;
}
QString DataFile::typeName( Type type )
{
return s_types[static_cast<std::size_t>(type)].m_name;
}
void DataFile::cleanMetaNodes( QDomElement _de )
{
QDomNode node = _de.firstChild();
while( !node.isNull() )
{
if( node.isElement() )
{
if( node.toElement().attribute( "metadata" ).toInt() )
{
QDomNode ns = node.nextSibling();
_de.removeChild( node );
node = ns;
continue;
}
if( node.hasChildNodes() )
{
cleanMetaNodes( node.toElement() );
}
}
node = node.nextSibling();
}
}
void DataFile::mapSrcAttributeInElementsWithResources(const QMap<QString, QString>& map)
{
for (const auto& [elem, srcAttrs] : ELEMENTS_WITH_RESOURCES)
{
auto elements = elementsByTagName(elem);
for (const auto& srcAttr : srcAttrs)
{
for (int i = 0; i < elements.length(); ++i)
{
auto item = elements.item(i).toElement();
if (item.isNull() || !item.hasAttribute(srcAttr)) { continue; }
const QString srcVal = item.attribute(srcAttr);
const auto it = map.constFind(srcVal);
if (it != map.constEnd())
{
item.setAttribute(srcAttr, *it);
}
}
}
}
}
void DataFile::upgrade_0_2_1_20070501()
{
// Upgrade to version 0.2.1-20070501
QDomNodeList list = elementsByTagName( "arpandchords" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
if( el.hasAttribute( "arpdir" ) )
{
int arpdir = el.attribute( "arpdir" ).toInt();
if( arpdir > 0 )
{
el.setAttribute( "arpdir", arpdir - 1 );
}
else
{
el.setAttribute( "arpdisabled", "1" );
}
}
}
list = elementsByTagName( "sampletrack" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
if( el.attribute( "vol" ) != "" )
{
el.setAttribute( "vol", LocaleHelper::toFloat(
el.attribute( "vol" ) ) * 100.0f );
}
else
{
QDomNode node = el.namedItem(
"automation-pattern" );
if( !node.isElement() ||
!node.namedItem( "vol" ).isElement() )
{
el.setAttribute( "vol", 100.0f );
}
}
}
list = elementsByTagName( "ladspacontrols" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
QDomNode anode = el.namedItem( "automation-pattern" );
QDomNode node = anode.firstChild();
while( !node.isNull() )
{
if( node.isElement() )
{
QString name = node.nodeName();
if( name.endsWith( "link" ) )
{
el.setAttribute( name,
node.namedItem( "time" )
.toElement()
.attribute( "value" ) );
QDomNode oldNode = node;
node = node.nextSibling();
anode.removeChild( oldNode );
continue;
}
}
node = node.nextSibling();
}
}
QDomNode node = m_head.firstChild();
while( !node.isNull() )
{
if( node.isElement() )
{
if( node.nodeName() == "bpm" )
{
int value = node.toElement().attribute(
"value" ).toInt();
if( value > 0 )
{
m_head.setAttribute( "bpm",
value );
QDomNode oldNode = node;
node = node.nextSibling();
m_head.removeChild( oldNode );
continue;
}
}
else if( node.nodeName() == "mastervol" )
{
int value = node.toElement().attribute(
"value" ).toInt();
if( value > 0 )
{
m_head.setAttribute(
"mastervol", value );
QDomNode oldNode = node;
node = node.nextSibling();
m_head.removeChild( oldNode );
continue;
}
}
else if( node.nodeName() == "masterpitch" )
{
m_head.setAttribute( "masterpitch",
-node.toElement().attribute(
"value" ).toInt() );
QDomNode oldNode = node;
node = node.nextSibling();
m_head.removeChild( oldNode );
continue;
}
}
node = node.nextSibling();
}
}
void DataFile::upgrade_0_2_1_20070508()
{
// Upgrade to version 0.2.1-20070508 from some version greater than or equal to 0.2.1-20070501
QDomNodeList list = elementsByTagName( "arpandchords" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
if( el.hasAttribute( "chorddisabled" ) )
{
el.setAttribute( "chord-enabled",
!el.attribute( "chorddisabled" )
.toInt() );
el.setAttribute( "arp-enabled",
!el.attribute( "arpdisabled" )
.toInt() );
}
else if( !el.hasAttribute( "chord-enabled" ) )
{
el.setAttribute( "chord-enabled", true );
el.setAttribute( "arp-enabled",
el.attribute( "arpdir" ).toInt() != 0 );
}
}
while( !( list = elementsByTagName( "channeltrack" ) ).isEmpty() )
{
QDomElement el = list.item( 0 ).toElement();
el.setTagName( "instrumenttrack" );
}
list = elementsByTagName( "instrumenttrack" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
if( el.hasAttribute( "vol" ) )
{
float value = LocaleHelper::toFloat( el.attribute( "vol" ) );
value = roundf( value * 0.585786438f );
el.setAttribute( "vol", value );
}
else
{
QDomNodeList vol_list = el.namedItem(
"automation-pattern" )
.namedItem( "vol" ).toElement()
.elementsByTagName( "time" );
for( int j = 0; !vol_list.item( j ).isNull();
++j )
{
QDomElement timeEl = list.item( j )
.toElement();
int value = timeEl.attribute( "value" )
.toInt();
value = (int)roundf( value *
0.585786438f );
timeEl.setAttribute( "value", value );
}
}
}
}
void DataFile::upgrade_0_3_0_rc2()
{
// Upgrade to version 0.3.0-rc2 from some version greater than or equal to 0.2.1-20070508
QDomNodeList list = elementsByTagName( "arpandchords" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
if( el.attribute( "arpdir" ).toInt() > 0 )
{
el.setAttribute( "arpdir",
el.attribute( "arpdir" ).toInt() - 1 );
}
}
}
void DataFile::upgrade_0_3_0()
{
// Upgrade to version 0.3.0 (final) from some version greater than or equal to 0.3.0-rc2
QDomNodeList list;
while( !( list = elementsByTagName(
"pluckedstringsynth" ) ).isEmpty() )
{
QDomElement el = list.item( 0 ).toElement();
el.setTagName( "vibedstrings" );
el.setAttribute( "active0", 1 );
}
while( !( list = elementsByTagName( "lb303" ) ).isEmpty() )
{
QDomElement el = list.item( 0 ).toElement();
el.setTagName( "lb302" );
}
while( !( list = elementsByTagName( "channelsettings" ) ).
isEmpty() )
{
QDomElement el = list.item( 0 ).toElement();
el.setTagName( "instrumenttracksettings" );
}
}
void DataFile::upgrade_0_4_0_20080104()
{
// Upgrade to version 0.4.0-20080104 from some version greater than or equal to 0.3.0 (final)
QDomNodeList list = elementsByTagName( "fx" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
if( el.hasAttribute( "fxdisabled" ) &&
el.attribute( "fxdisabled" ).toInt() == 0 )
{
el.setAttribute( "enabled", 1 );
}
}
}
void DataFile::upgrade_0_4_0_20080118()
{
// Upgrade to version 0.4.0-20080118 from some version greater than or equal to 0.4.0-20080104
QDomNodeList list;
while( !( list = elementsByTagName( "fx" ) ).isEmpty() )
{
QDomElement fxchain = list.item( 0 ).toElement();
fxchain.setTagName( "fxchain" );
QDomNode rack = list.item( 0 ).firstChild();
QDomNodeList effects = rack.childNodes();
// move items one level up
while( effects.count() )
{
fxchain.appendChild( effects.at( 0 ) );
}
fxchain.setAttribute( "numofeffects",
rack.toElement().attribute( "numofeffects" ) );
fxchain.removeChild( rack );
}
}
void DataFile::upgrade_0_4_0_20080129()
{
// Upgrade to version 0.4.0-20080129 from some version greater than or equal to 0.4.0-20080118
QDomNodeList list;
while( !( list =
elementsByTagName( "arpandchords" ) ).isEmpty() )
{
QDomElement aac = list.item( 0 ).toElement();
aac.setTagName( "arpeggiator" );
QDomNode cloned = aac.cloneNode();
cloned.toElement().setTagName( "chordcreator" );
aac.parentNode().appendChild( cloned );
}
}
void DataFile::upgrade_0_4_0_20080409()
{
// Upgrade to version 0.4.0-20080409 from some version greater than or equal to 0.4.0-20080129
QStringList s;
s << "note" << "pattern" << "bbtco" << "sampletco" << "time";
for( QStringList::iterator it = s.begin(); it < s.end(); ++it )
{
QDomNodeList list = elementsByTagName( *it );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
el.setAttribute( "pos",
el.attribute( "pos" ).toInt()*3 );
el.setAttribute( "len",
el.attribute( "len" ).toInt()*3 );
}
}
QDomNodeList list = elementsByTagName( "timeline" );
for( int i = 0; !list.item( i ).isNull(); ++i )
{
QDomElement el = list.item( i ).toElement();
el.setAttribute( "lp0pos",
el.attribute( "lp0pos" ).toInt()*3 );
el.setAttribute( "lp1pos",
el.attribute( "lp1pos" ).toInt()*3 );
}
}
void DataFile::upgrade_0_4_0_20080607()
{
// Upgrade to version 0.4.0-20080607 from some version greater than or equal to 0.3.0-20080409
QDomNodeList list;
while( !( list = elementsByTagName( "midi" ) ).isEmpty() )
{
QDomElement el = list.item( 0 ).toElement();
el.setTagName( "midiport" );
}