File: Settings.cpp

package info (click to toggle)
pianobooster 0.6.7~svn156-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,796 kB
  • ctags: 1,930
  • sloc: cpp: 11,738; makefile: 66; ansic: 26
file content (544 lines) | stat: -rwxr-xr-x 15,959 bytes parent folder | download
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
/*!
    @file           Settings.cpp

    @brief          Save all the settings for the programme in the right place.

    @author         L. J. Barman

    Copyright (c)   2008-2009, L. J. Barman, all rights reserved

    This file is part of the PianoBooster application

    PianoBooster 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 3 of the License, or
    (at your option) any later version.

    PianoBooster 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 PianoBooster.  If not, see <http://www.gnu.org/licenses/>.

*/

/*!
 * The following attributes are save in the pb.cfg xml file
 * BOOK:
 *   name       the name of the book
 *   lastsong   the name of the last song played so it can be restored
 *
 * SONG:
 *   name
 *   hand
 *   leftHandMidiChannel
 *   rightHandMidiChannel
 *
 * HAND:
 *   speed
 *
*/

#include <QTextStream>
#include <QFile>
#include "Settings.h"
#include "GuiTopBar.h"
#include "GuiSidePanel.h"
#include "QtWindow.h"
#include "ReleaseNote.txt"


#define OPTION_DEBUG_SETTINGS     0
#if OPTION_DEBUG_SETTINGS
#define debugSettings(args)     qDebug args
#else
#define debugSettings(args)
#endif


CSettings::CSettings(QtWindow *mainWindow) : QSettings(CSettings::IniFormat, CSettings::UserScope, "PianoBooster", "Piano Booster"),
                                 m_mainWindow(mainWindow)
{
    // It is all done in the initialisation list

    m_advancedMode = false;
    m_pianistActive = false;
    m_noteNamesEnabled = value("Score/NoteNames", true ).toBool();
    m_tutorPagesEnabled = value("Tutor/TutorPages", true ).toBool();
    CNotation::setCourtesyAccidentals(value("Score/CourtesyAccidentals", false ).toBool());
}

void CSettings::setDefaultValue(const QString & key, const QVariant & value )
{
    if (contains(key)) // Do not change the value if it already set
        return;

    setValue(key, value);
}


void CSettings::init(CSong* song, GuiSidePanel* sidePanel, GuiTopBar* topBar)
{
    m_song = song;
    m_guiSidePanel = sidePanel;
    m_guiTopBar = topBar;
}


void CSettings::setNoteNamesEnabled(bool value) {
    m_noteNamesEnabled = value;
    setValue("Score/NoteNames", value );
}

void CSettings::setTutorPagesEnabled(bool value) {
    m_tutorPagesEnabled = value;
    setValue("Tutor/TutorPages", value );
    updateTutorPage();
}


void CSettings::setCourtesyAccidentals(bool value) {
    CNotation::setCourtesyAccidentals(value);
    setValue("Score/CourtesyAccidentals", value );
}

// Open a document if it exists or else create it (also delete an duplicates
QDomElement CSettings::openDomElement(QDomElement parent, const QString & elementName, const QString & attributeName)
{
    QDomElement wantedElement;

    debugSettings(("openDomElement1 %s %s %s", qPrintable(parent.tagName()), qPrintable(elementName), qPrintable(elementName)));
    // There should be only a single element without a name
    // there should be lots of elemens but only one with this tag name

    QDomNode n = parent.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        debugSettings(("openDomElement2 tagName %s %s", qPrintable(e.tagName()), qPrintable(elementName)));
        if(!e.isNull() && e.tagName() == elementName)
        {
            if (attributeName.isEmpty() || e.attribute("name") == attributeName)
            {
                debugSettings(("openDomElement3 ragName %s %s", qPrintable(e.attribute("name")), qPrintable(attributeName)));

                if (wantedElement.isNull())
                    wantedElement = e;   // we have found a match
                else
                    parent.removeChild(e); // remove unwanted duplicates
            }
        }
        n = n.nextSibling();
    }

    if (wantedElement.isNull())
    {
        // Create the element because it does not exsist
        wantedElement = m_domDocument.createElement(elementName);
        if (!attributeName.isEmpty() )
            wantedElement.setAttribute("name", attributeName);
        parent.appendChild(wantedElement);
    }

    return wantedElement;
}

void CSettings::loadHandSettings()
{
    if (m_domSong.isNull())
        return;
    m_domHand = openDomElement(m_domSong, "hand", partToHandString(m_song->getActiveHand()));
    //m_guiTopBar->setSpeed(m_domHand.attribute("speed", "100" ).toInt());
}

void CSettings::saveHandSettings()
{
    //m_domHand.setAttribute("speed", m_guiTopBar->getSpeed());
}

void CSettings::loadSongSettings()
{
    m_domSong = openDomElement(m_domBook, "song", m_currentSongName);
    m_guiSidePanel->setCurrentHand(m_domSong.attribute("hand", "both" ));
    m_guiTopBar->setSpeed(m_domSong.attribute("speed", "100" ).toInt());


    // -1 means none and -2 means not set
    int left = m_domSong.attribute("leftHandMidiChannel", "-2").toInt();
    int right = m_domSong.attribute("rightHandMidiChannel", "-2").toInt();
    CNote::setChannelHands(left, right);

    loadHandSettings();
}


void CSettings::saveSongSettings()
{
    m_domSong.setAttribute("hand", partToHandString(m_song->getActiveHand()));
    m_domSong.setAttribute("speed", m_guiTopBar->getSpeed());

    saveHandSettings();
}

void CSettings::loadBookSettings()
{
    QDomElement root = m_domDocument.documentElement();
    m_domBook = openDomElement(root, "book");
    QString lastSong = m_domBook.attribute("lastsong");
    if (!lastSong.isEmpty() && m_currentSongName.isEmpty())
        m_currentSongName = lastSong;
}


void CSettings::saveBookSettings()
{
    if (!m_currentBookName.isEmpty())
        m_domBook.setAttribute("name", m_currentBookName);
    if (!m_currentSongName.isEmpty())
        m_domBook.setAttribute("lastsong", m_currentSongName);

    saveSongSettings();
}


void CSettings::loadXmlFile()
{
    m_domDocument.documentElement().clear();
    m_domDocument.clear();
    m_domBook.clear();
    m_domSong.clear();
    m_domHand.clear();

    QFile file(m_bookPath + getCurrentBookName() + '/' + "pb.cfg");
    if (file.open(QIODevice::ReadOnly))
    {
        if (!m_domDocument.setContent(&file)) {
            ppLogError("Cannot setContent on XLM file");
        }
        file.close();
    }

    QDomElement root = m_domDocument.documentElement();
    if (root.tagName() != "pianobooster")
    {
        m_domDocument.clear();
        QDomComment comment =    m_domDocument.createComment("Piano Booster Configuration file");
        m_domDocument.appendChild(comment);
        root = m_domDocument.createElement("pianobooster");
        m_domDocument.appendChild(root);
    }

    loadBookSettings();
}

// save the xml
void CSettings::saveXmlFile()
{
    saveBookSettings();

    const int IndentSize = 4;

    QFile file(m_bookPath + getCurrentBookName() + '/' + "pb.cfg");

    // don't save the config file unless the user really is using the system
    if (m_pianistActive == false && file.exists() == false)
        return;

    m_pianistActive = false;

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        ppLogError("Cannot save xml file %s", qPrintable(file.fileName()));
        return;
    }

    QTextStream out(&file);
    m_domDocument.save(out, IndentSize);
    file.close();
}

void CSettings::updateTutorPage()
{
    QFileInfo fileInfo(getCurrentSongLongFileName());
    const char* EXTN = ".html";

    QString fileBase = fileInfo.absolutePath() + "/InfoPages/" + fileInfo.completeBaseName() + "_";

    QString locale = QLocale::system().name();

    if (m_tutorPagesEnabled)
    {
		QFileInfo tutorFile;
 		tutorFile.setFile(fileBase + locale + EXTN);
        if (tutorFile.exists())
        {
            m_mainWindow->loadTutorHtml(tutorFile.absoluteFilePath());
            return;
        }
        int n = locale.indexOf("_");

        if (n > 0)
        {
            locale = locale.left(n);
 			tutorFile.setFile(fileBase + locale + EXTN);
            if (tutorFile.exists())
            {
                m_mainWindow->loadTutorHtml(tutorFile.absoluteFilePath());
                return;
            }
        }

        locale = "en";
 		tutorFile.setFile(fileBase + locale + EXTN);
        if (tutorFile.exists())
        {
            m_mainWindow->loadTutorHtml(tutorFile.absoluteFilePath());
            return;
        }
    }
    m_mainWindow->loadTutorHtml(QString());

}

void CSettings::openSongFile(const QString & filename)
{
    if (!QFile::exists(filename))
    {
        ppLogError( "File does not exists %s", qPrintable(filename));
        return;
    }
    QDir dirBooks;
    QString  currentSongName;
    if (filename.isEmpty())
    {
        dirBooks.setPath( QDir::homePath());
    }
    else
    {
        dirBooks.setPath(filename);
        currentSongName = dirBooks.dirName();
        dirBooks.cdUp();
        m_currentBookName = dirBooks.dirName();
        dirBooks.cdUp();
    }
    m_bookPath =  dirBooks.path() + '/';

    m_currentSongName = currentSongName;
    m_guiSidePanel->loadBookList();
    updateWarningMessages();
}

void CSettings::setActiveHand(whichPart_t hand)
{
    saveHandSettings();
    m_song->setActiveHand(hand);
    loadHandSettings();
}

QStringList CSettings::getSongList()
{
    debugSettings(("getSongList %s + %s", qPrintable(getCurrentBookName()), qPrintable(m_bookPath)));
    QDir dirSongs = QDir(m_bookPath + getCurrentBookName());
    dirSongs.setFilter(QDir::Files);
    QStringList fileNames = dirSongs.entryList();


    QStringList songNames;
    for (int i = 0; i < fileNames.size(); i++)
    {
        if ( fileNames.at(i).endsWith(".mid", Qt::CaseInsensitive ) ||
             fileNames.at(i).endsWith(".midi", Qt::CaseInsensitive ) ||
             fileNames.at(i).endsWith(".kar", Qt::CaseInsensitive ) )
        {
            songNames  +=  fileNames.at(i);
        }
    }

    return songNames;
}

QStringList CSettings::getBookList()
{
    QDir dirBooks( m_bookPath);
    dirBooks.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    return dirBooks.entryList();
}


void CSettings::writeSettings()
{

    if (QFile::exists(getCurrentSongLongFileName() ))
        setValue("CurrentSong", getCurrentSongLongFileName());
    saveXmlFile();
}


void CSettings::loadSettings()
{
    unzipBootserMusicBooks();
    // Set default values
    setValue("PianoBooster/Version", PB_VERSION);
    setDefaultValue("ShortCuts/LeftHand", "F2");
    setDefaultValue("ShortCuts/BothHands","F3");
    setDefaultValue("ShortCuts/RightHand","F4");
    setDefaultValue("ShortCuts/PlayFromStart",tr("space"));
    setDefaultValue("ShortCuts/PlayPause","P");
    setDefaultValue("ShortCuts/Faster","=");
    setDefaultValue("ShortCuts/Slower","-");
    setDefaultValue("ShortCuts/NextSong","]");
    setDefaultValue("ShortCuts/PreviousSong","[");
    setDefaultValue("ShortCuts/NextBook","{");
    setDefaultValue("ShortCuts/PreviousBook","}");
    QString songName = value("CurrentSong").toString();
    if (!songName.isEmpty())
        openSongFile( songName );

    updateWarningMessages();
    updateTutorPage();

}

void CSettings::unzipBootserMusicBooks()
{
    // Set default values

    const int MUSIC_RELEASE = 1;
    const QString ZIPFILENAME("BoosterMusicBooks.zip");


    if (value("PianoBooster/MusicRelease", 0).toInt() < MUSIC_RELEASE)
    {
        QString resourceDir = QApplication::applicationDirPath() + "/../music/";

        ppLogTrace("resourceDir1 %s", qPrintable(resourceDir));

        if (!QFile::exists(resourceDir + ZIPFILENAME))
            resourceDir = QApplication::applicationDirPath() + "/../../music/";
        ppLogTrace("resourceDir2 %s", qPrintable(resourceDir));

        if (!QFile::exists(resourceDir + ZIPFILENAME))
        {
#ifdef Q_OS_LINUX
            resourceDir = QApplication::applicationDirPath() + "/../share/games/" + QSTR_APPNAME + "/music/";
#endif
#ifdef Q_OS_DARWIN
            resourceDir = QApplication::applicationDirPath() + "/../Resources/music/";
#endif
        }

        ppLogInfo(qPrintable("applicationDirPath=" + QApplication::applicationDirPath()));
        ppLogTrace("resourceDir3 %s", qPrintable(resourceDir));


        QFileInfo zipFile(resourceDir +  ZIPFILENAME);
        ppLogTrace("xx %s", qPrintable(zipFile.filePath()));
        
		QDir destMusicDir;
		
#ifdef _WIN32
        const QString MUSIC_DIR_NAME("My Music");
		QSettings settings(QSettings::UserScope, "Microsoft", "Windows");
		settings.beginGroup("CurrentVersion/Explorer/Shell Folders");
		destMusicDir.setPath(QDir::fromNativeSeparators(settings.value("Personal").toString()));
#else
        const QString MUSIC_DIR_NAME("Music");
		destMusicDir.setPath(QDir::homePath() );
#endif
         
        if (!QDir(destMusicDir.absolutePath() + "/" + MUSIC_DIR_NAME).exists())
        {
            destMusicDir.mkdir(MUSIC_DIR_NAME);
        }
        destMusicDir.setPath(destMusicDir.absolutePath() + "/" + MUSIC_DIR_NAME);

#ifndef _WIN32

		// on windows the the installer does the unzipping
        if (!zipFile.exists() )
        {
            ppLogError(qPrintable("Cannot find " + ZIPFILENAME));
            return;
        }



        QProcess unzip;
        unzip.start("unzip", QStringList() << "-o" << zipFile.filePath() << "-d" << destMusicDir.path() );
        ppLogInfo(qPrintable("running unzip -o " + zipFile.filePath() + " -d " + destMusicDir.path()) );
        char buf[1024];
        while (true)
        {
            qint64 lineLength = unzip.readLine(buf, sizeof(buf));
            if (lineLength <= 0)
                break;
            ppLogInfo(buf);
                 // the line is available in buf
        }


        if (!unzip.waitForFinished())
        {
             ppLogError("unzip failed");
             return;
        }
#endif       
		QString fileName(destMusicDir.absolutePath() + "/BoosterMusicBooks" + QString::number(MUSIC_RELEASE) + "/Booster Music/01-ClairDeLaLune.mid");
		openSongFile(fileName);
		m_mainWindow->setCurrentFile(fileName);
		setValue("PianoBooster/MusicRelease", MUSIC_RELEASE);
    }
}

void CSettings::setCurrentSongName(const QString & name)
{
    if (name.isEmpty())
        return;
    saveSongSettings();
    m_currentSongName = name;
    debugSettings(("setCurrentSongName %s -- %s", qPrintable(name), qPrintable(getCurrentSongLongFileName())));
    setValue("CurrentSong", getCurrentSongLongFileName());

    m_song->loadSong(getCurrentSongLongFileName());
    loadSongSettings();

    m_guiSidePanel->refresh();
    m_guiTopBar->refresh(true);
    m_mainWindow->setWindowTitle("Piano Booster - " + m_song->getSongTitle());
    updateTutorPage();
}

void CSettings::setCurrentBookName(const QString & name, bool clearSongName)
{
    if (name.isEmpty())
        return;
    if (!m_currentBookName.isEmpty() && m_currentBookName != name)
        saveXmlFile();

    m_currentBookName = name;
    if (clearSongName)
        m_currentSongName.clear();
    debugSettings(("setCurrentBookName %s --- %s ", qPrintable(name), qPrintable(getCurrentSongLongFileName())));
    loadXmlFile();
}

void CSettings::setChannelHands(int left, int right)
{
    CNote::setChannelHands(left, right);
    m_domSong.setAttribute("leftHandMidiChannel", left);
    m_domSong.setAttribute("rightHandMidiChannel", right);
    m_guiSidePanel->refresh();
}

void CSettings::updateWarningMessages()
{
    if (!m_song->validMidiOutput())
        m_warningMessage = tr("ERROR NO SOUND: To fix this use menu Setup/Midi Setup ...");
    else if (m_currentSongName.isEmpty())
        m_warningMessage = tr("ERROR NO MIDI FILE: To fix this use menu File/Open ...");
    else
        m_warningMessage.clear();
}