Just Intonation  Version 1.3.0 (18)
Explore scale-independent dynamically adapting tuning in just intonation
startupsound.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2016-2017 Karolin Stange, Christoph Wick, and Haye Hinrichsen
3  *
4  * This file is part of JustIntonation.
5  *
6  * JustIntonation is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * JustIntonation is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with JustIntonation. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Class for playing a startup sound
22 //=============================================================================
23 
24 #include "startupsound.h"
25 
26 #include <QMediaPlayer>
27 #include <QSettings>
28 #include <QStandardPaths>
29 #include <QFile>
30 #include <QDir>
31 #include <QResource>
32 
33 #define STATUS if (mVerbosity >= 4) qDebug() << "StartupSound:"
34 #define MESSAGE if (mVerbosity >= 3) qDebug() << "StartupSound:"
35 #define WARNING if (mVerbosity >= 2) qWarning()<<"StartupSound: WARNING:"
36 #define ERROR if (mVerbosity >= 1) qWarning() << "StartupSound: ERROR:"
37 
38 //-----------------------------------------------------------------------------
39 // Constructor
40 //-----------------------------------------------------------------------------
46 
47 StartupSound::StartupSound(const QString &resourcefile)
48  : mResourceFile(resourcefile)
49  , mVerbosity(2)
50 {}
51 
52 
53 //-----------------------------------------------------------------------------
54 // Ask whether startup sound is enabled
55 //-----------------------------------------------------------------------------
60 
62 {
63  QSettings settings;
64  return settings.value("app/startupsound",true).toBool();
65 }
66 
67 
68 //-----------------------------------------------------------------------------
69 // Public slot: Enable / disable startup sound
70 //-----------------------------------------------------------------------------
77 
78 void StartupSound::enable (bool enabled)
79 {
80  QSettings settings;
81  STATUS << "Setting enabled =" << (enabled ? "true" : "false");
82  settings.setValue("app/startupsound",enabled);
83 }
84 
85 
86 //-----------------------------------------------------------------------------
87 // Set verbosity level
88 //-----------------------------------------------------------------------------
98 
100 {
101  qDebug() << "StartupSound: Setting verbosity of messages to level" << level;
102  mVerbosity = level;
103 }
104 
105 //-----------------------------------------------------------------------------
106 // Play startup sound
107 //-----------------------------------------------------------------------------
115 
117 {
118  if (not isEnabled())
119  {
120  MESSAGE << "Startup sound is disabled in the settings";
121  return;
122  }
123  MESSAGE << "Play startup sound" << mResourceFile;
124 
125  // Identify the extension of the file (mp3,ogg,...)
126  if (mResourceFile.size()==0) return;
127  int lastPoint = mResourceFile.lastIndexOf(".");
128  const QString extension =
129  mResourceFile.right(mResourceFile.size()-lastPoint-1);
130  if (extension.size() != 3)
131  {
132  WARNING << "Could not identify extension of resource file"
133  << mResourceFile << ":" << extension;
134  return;
135  }
136 
137  // MAC-OS cannot play the startupsound directly from the resource file
138  // For this reason the mp3/ogg file is first copied to local disk
139  // First make sure that the local storage is writable
140  const QString localpath =
141  QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
142  QDir directory;
143  if (not directory.mkpath(localpath))
144  {
145  WARNING << "Cannot create local path" << localpath;
146  return;
147  }
148 
149  // Then copy the file
150  const QString filename = "startupsound." + extension;
151  QFile localfile (localpath + "/" + filename);
152  if (localfile.exists())
153  {
154  if (localfile.size() != QResource(mResourceFile).size())
155  {
156  // If file size changed delete and reload
157  localfile.remove();
158  QFile::copy(mResourceFile, localfile.fileName());
159  }
160  }
161  else QFile::copy(mResourceFile, localfile.fileName());
162  if (not localfile.exists()) WARNING<< "Startupsound could not be copied.";
163  STATUS << mResourceFile << "was copied to" << localfile.fileName();
164 
165  QMediaPlayer *player = new QMediaPlayer(this);
166  player->setMedia(QUrl::fromLocalFile(localfile.fileName()));
167  player->setVolume(50);
168  player->play();
169  if (player->error() != QMediaPlayer::NoError)
170  WARNING << "Startupsound:" << player->errorString().toStdString().c_str();
171  STATUS << "Leaving Startupsound::play()";
172 }
173 
int mVerbosity
Verbosity of qDebug() messages.
Definition: startupsound.h:58
QString mResourceFile
Resource file object.
Definition: startupsound.h:57
void play()
Play startup sound.
void enable(bool enabled)
Public slot: Enable / disable startup sound.
StartupSound(const QString &resourcefile)
Constructor.
bool isEnabled()
StartupSound::isEnabled.
#define WARNING
#define STATUS
void setVerbosity(int verbosity)
Set verbosity level of qDebug() messages.
#define MESSAGE