Skip to content

Commit

Permalink
Merge pull request mixxxdj#22 from uklotzde/jmigual-library-redesign-…
Browse files Browse the repository at this point in the history
…merge

Merge from 'master' (again)
  • Loading branch information
daschuer committed Jun 18, 2017
2 parents 6e1f42d + 6fafb09 commit 40c27aa
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 119 deletions.
70 changes: 28 additions & 42 deletions src/library/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,43 +62,27 @@ const ConfigKey Library::kConfigKeyRepairDatabaseOnNextRestart(kConfigGroup, "Re
// The default row height of the library.
const int Library::kDefaultRowHeightPx = 20;

Library::Library(UserSettingsPointer pConfig,
PlayerManagerInterface* pPlayerManager,
RecordingManager* pRecordingManager) :
m_pConfig(pConfig),
m_mixxxDb(pConfig),
m_dbConnectionPooler(m_mixxxDb.connectionPool()),
m_pTrackCollection(new TrackCollection(pConfig)),
m_pLibraryControl(new LibraryControl(this)),
m_pRecordingManager(pRecordingManager),
m_scanner(m_mixxxDb.connectionPool(), m_pTrackCollection, pConfig),
m_pSidebarExpanded(nullptr),
m_hoveredFeature(nullptr),
m_focusedFeature(nullptr),
m_focusedPaneId(-1),
m_preselectedPane(-1),
m_previewPreselectedPane(-1) {
kLogger.info() << "Opening datbase connection";

const mixxx::DbConnectionPooled dbConnectionPooled(m_mixxxDb.connectionPool());
if (!dbConnectionPooled) {
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"Mixxx requires QT with SQLite support. Please read "
"the Qt SQL driver documentation for information on how "
"to build it.\n\n"
"Click OK to exit."), QMessageBox::Ok);
// TODO(XXX) something a little more elegant
exit(-1);
}
QSqlDatabase dbConnection(dbConnectionPooled);
DEBUG_ASSERT(dbConnection.isOpen());

kLogger.info() << "Initializing or upgrading database schema";
if (!MixxxDb::initDatabaseSchema(dbConnection)) {
// TODO(XXX) something a little more elegant
exit(-1);
}
Library::Library(
UserSettingsPointer pConfig,
mixxx::DbConnectionPoolPtr pDbConnectionPool,
PlayerManagerInterface* pPlayerManager,
RecordingManager* pRecordingManager)
: m_pConfig(pConfig),
m_pDbConnectionPool(pDbConnectionPool),
m_pTrackCollection(new TrackCollection(m_pConfig)),
m_pMixxxLibraryFeature(nullptr),
m_pPlaylistFeature(nullptr),
m_pCrateFeature(nullptr),
m_pAnalysisFeature(nullptr),
m_pLibraryControl(new LibraryControl(this)),
m_scanner(m_pDbConnectionPool, m_pTrackCollection, m_pConfig),
m_pSidebarExpanded(nullptr),
m_hoveredFeature(nullptr),
m_focusedFeature(nullptr),
m_focusedPaneId(-1),
m_preselectedPane(-1),
m_previewPreselectedPane(-1) {
QSqlDatabase dbConnection = mixxx::DbConnectionPooled(m_pDbConnectionPool);

// TODO(XXX): Add a checkbox in the library preferences for checking
// and repairing the database on the next restart of the application.
Expand All @@ -125,7 +109,7 @@ Library::Library(UserSettingsPointer pConfig,
this, SLOT(slotRefreshLibraryModels()));

createTrackCache();
createFeatures(pConfig, pPlayerManager);
createFeatures(pConfig, pPlayerManager, pRecordingManager);

// On startup we need to check if all of the user's library folders are
// accessible to us. If the user is using a database from <1.12.0 with
Expand Down Expand Up @@ -748,8 +732,10 @@ void Library::createTrackCache() {



void Library::createFeatures(UserSettingsPointer pConfig,
PlayerManagerInterface* pPlayerManager) {
void Library::createFeatures(
UserSettingsPointer pConfig,
PlayerManagerInterface* pPlayerManager,
RecordingManager* pRecordingManager) {
m_pMixxxLibraryFeature = new MixxxLibraryFeature(
pConfig, this, this, m_pTrackCollection);
addFeature(m_pMixxxLibraryFeature);
Expand All @@ -766,7 +752,7 @@ void Library::createFeatures(UserSettingsPointer pConfig,
addFeature(m_pCrateFeature);

BrowseFeature* browseFeature = new BrowseFeature(
pConfig, this, this, m_pTrackCollection, m_pRecordingManager);
pConfig, this, this, m_pTrackCollection, pRecordingManager);
connect(browseFeature, SIGNAL(scanLibrary()),
&m_scanner, SLOT(scan()));
connect(&m_scanner, SIGNAL(scanStarted()),
Expand All @@ -776,7 +762,7 @@ void Library::createFeatures(UserSettingsPointer pConfig,
addFeature(browseFeature);

addFeature(new RecordingFeature(
pConfig, this, this, m_pTrackCollection, m_pRecordingManager));
pConfig, this, this, m_pTrackCollection, pRecordingManager));

addFeature(new HistoryFeature(pConfig, this, this, m_pTrackCollection));

Expand Down
27 changes: 10 additions & 17 deletions src/library/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@
#include "track/track.h"
#include "util/parented_ptr.h"
#include "util/memory.h"
#include "database/mixxxdb.h"
#include "util/db/dbconnectionpooler.h"
#include "util/db/dbconnectionpool.h"

class AnalysisFeature;
class CrateFeature;
class KeyboardEventFilter;
class LibraryPaneManager;
class LibraryControl;
class LibraryFeature;
class LibraryTableModel;
class LibrarySidebarExpandedManager;
class LibraryView;
class MixxxLibraryFeature;
class PlaylistFeature;
class PlayerManagerInterface;
class SidebarModel;
class TrackModel;
class TrackCollection;
class WBaseLibrary;
class WLibraryPane;
Expand All @@ -61,12 +57,13 @@ class Library : public QObject {
static const ConfigKey kConfigKeyRepairDatabaseOnNextRestart;

Library(UserSettingsPointer pConfig,
mixxx::DbConnectionPoolPtr pDbConnectionPool,
PlayerManagerInterface* pPlayerManager,
RecordingManager* pRecordingManager);
virtual ~Library();
~Library() override;

mixxx::DbConnectionPoolPtr dbConnectionPool() const {
return m_mixxxDb.connectionPool();
return m_pDbConnectionPool;
}

void bindSearchBar(WSearchLineEdit* searchLine, int id);
Expand Down Expand Up @@ -158,29 +155,25 @@ class Library : public QObject {
LibraryPaneManager* getPreselectedPane();

void createTrackCache();
void createFeatures(UserSettingsPointer pConfig, PlayerManagerInterface *pPlayerManager);
void createFeatures(
UserSettingsPointer pConfig,
PlayerManagerInterface *pPlayerManager,
RecordingManager* pRecordingManager);

void handleFocus();
void handlePreselection();

const UserSettingsPointer m_pConfig;

// The Mixxx SQLite3 database
const MixxxDb m_mixxxDb;

// The Mixxx database connection for the thread that creates
// and owns this library instance. TODO(XXX): Move database
// related code out of the GUI into multiple, dedicated
// worker threads.
const mixxx::DbConnectionPooler m_dbConnectionPooler;
// The Mixxx database connection pool
const mixxx::DbConnectionPoolPtr m_pDbConnectionPool;

TrackCollection* m_pTrackCollection;
MixxxLibraryFeature* m_pMixxxLibraryFeature;
PlaylistFeature* m_pPlaylistFeature;
CrateFeature* m_pCrateFeature;
AnalysisFeature* m_pAnalysisFeature;
LibraryControl* m_pLibraryControl;
RecordingManager* m_pRecordingManager;
LibraryScanner m_scanner;
QFont m_trackTableFont;
int m_iTrackTableRowHeight;
Expand Down
9 changes: 3 additions & 6 deletions src/library/scanner/libraryscanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ void LibraryScanner::run() {
Trace trace("LibraryScanner");

const mixxx::DbConnectionPooler dbConnectionPooler(m_pDbConnectionPool);
const mixxx::DbConnectionPooled dbConnectionPooled(m_pDbConnectionPool);
if (!dbConnectionPooled) {
QSqlDatabase dbConnection = mixxx::DbConnectionPooled(m_pDbConnectionPool);
if (!dbConnection.isOpen()) {
kLogger.warning()
<< "Failed to open database connection for library scanner";
kLogger.debug() << "Exiting thread";
return;
}
QSqlDatabase dbConnection(dbConnectionPooled);
DEBUG_ASSERT(dbConnection.isOpen());

m_libraryHashDao.initialize(dbConnection);
m_cueDao.initialize(dbConnection);
Expand Down Expand Up @@ -242,8 +240,7 @@ void LibraryScanner::cleanUpScan() {

// Start a transaction for all the library hashing (moved file
// detection) stuff.
const mixxx::DbConnectionPooled dbConnectionPooled(m_pDbConnectionPool);
QSqlDatabase dbConnection(dbConnectionPooled);
QSqlDatabase dbConnection = mixxx::DbConnectionPooled(m_pDbConnectionPool);
ScopedTransaction transaction(dbConnection);

kLogger.debug() << "Marking tracks in changed directories as verified";
Expand Down
43 changes: 24 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@
#include <X11/Xlib.h>
#endif

namespace {

int runMixxx(MixxxApplication* app, const CmdlineArgs& args) {
int result = -1;
MixxxMainWindow mainWindow(app, args);
// If startup produced a fatal error, then don't even start the
// Qt event loop.
if (ErrorDialogHandler::instance()->checkError()) {
mainWindow.finalize();
} else {
qDebug() << "Displaying main window";
mainWindow.show();

qDebug() << "Running Mixxx";
result = app->exec();
}
return result;
}

} // anonymous namespace

int main(int argc, char * argv[]) {
Console console;

Expand Down Expand Up @@ -69,7 +90,7 @@ int main(int argc, char * argv[]) {
mixxx::Logging::initialize(args.getSettingsPath(),
args.getLogLevel(), args.getDebugAssertBreak());

MixxxApplication a(argc, argv);
MixxxApplication app(argc, argv);

// Support utf-8 for all translation strings. Not supported in Qt 5.
// TODO(rryan): Is this needed when we switch to qt5? Some sources claim it
Expand Down Expand Up @@ -98,26 +119,10 @@ int main(int argc, char * argv[]) {
}
#endif

MixxxMainWindow* mixxx = new MixxxMainWindow(&a, args);

// When the last window is closed, terminate the Qt event loop.
QObject::connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));

int result = -1;

// If startup produced a fatal error, then don't even start the Qt event
// loop.
if (ErrorDialogHandler::instance()->checkError()) {
mixxx->finalize();
} else {
qDebug() << "Displaying mixxx";
mixxx->show();

qDebug() << "Running Mixxx";
result = a.exec();
}
QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));

delete mixxx;
int result = runMixxx(&app, args);

qDebug() << "Mixxx shutdown complete with code" << result;

Expand Down
57 changes: 51 additions & 6 deletions src/mixxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "track/track.h"
#include "waveform/waveformwidgetfactory.h"
#include "waveform/sharedglcontext.h"
#include "database/mixxxdb.h"
#include "util/debug.h"
#include "util/statsmanager.h"
#include "util/timer.h"
Expand All @@ -67,6 +68,8 @@
#include "preferences/settingsmanager.h"
#include "widget/wmainmenubar.h"
#include "util/screensaver.h"
#include "util/logger.h"
#include "util/db/dbconnectionpooled.h"

#ifdef __VINYLCONTROL__
#include "vinylcontrol/vinylcontrolmanager.h"
Expand All @@ -76,6 +79,12 @@
#include "preferences/dialog/dlgprefmodplug.h"
#endif

namespace {

const mixxx::Logger kLogger("MixxxMainWindow");

} // anonymous namespace

// static
const int MixxxMainWindow::kMicrophoneCount = 4;
// static
Expand Down Expand Up @@ -258,17 +267,32 @@ void MixxxMainWindow::initialize(QApplication* pApp, const CmdlineArgs& args) {

CoverArtCache::create();

// (long)
m_pLibrary = new Library(pConfig,
m_pPlayerManager,
m_pRecordingManager);
m_pDbConnectionPool = MixxxDb(pConfig).connectionPool();
if (!m_pDbConnectionPool) {
// TODO(XXX) something a little more elegant
exit(-1);
}
// Create a connection for the main thread
m_pDbConnectionPool->createThreadLocalConnection();
if (!initializeDatabase()) {
// TODO(XXX) something a little more elegant
exit(-1);
}

launchProgress(35);

m_pLibrary = new Library(
pConfig,
m_pDbConnectionPool,
m_pPlayerManager,
m_pRecordingManager);
m_pPlayerManager->bindToLibrary(m_pLibrary);

new QShortcut(
QKeySequence(tr("Ctrl+F", "Search|Focus")),
this, SLOT(slotFocusSearch()));

launchProgress(35);
launchProgress(40);

// Get Music dir
bool hasChanged_MusicDir = false;
Expand Down Expand Up @@ -547,6 +571,10 @@ void MixxxMainWindow::finalize() {
qDebug() << t.elapsed(false).debugMillisWithUnit() << "deleting Library";
delete m_pLibrary;

qDebug() << t.elapsed(false).debugMillisWithUnit() << "closing database connection(s)";
m_pDbConnectionPool->destroyThreadLocalConnection();
m_pDbConnectionPool.reset(); // should drop the last reference

// PlayerManager depends on Engine, SoundManager, VinylControlManager, and Config
qDebug() << t.elapsed(false).debugMillisWithUnit() << "deleting PlayerManager";
delete m_pPlayerManager;
Expand Down Expand Up @@ -636,6 +664,23 @@ void MixxxMainWindow::finalize() {
StatsManager::destroy();
}

bool MixxxMainWindow::initializeDatabase() {
kLogger.info() << "Connecting to database";
QSqlDatabase dbConnection = mixxx::DbConnectionPooled(m_pDbConnectionPool);
if (!dbConnection.isOpen()) {
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"Mixxx requires QT with SQLite support. Please read "
"the Qt SQL driver documentation for information on how "
"to build it.\n\n"
"Click OK to exit."), QMessageBox::Ok);
return false;
}

kLogger.info() << "Initializing or upgrading database schema";
return MixxxDb::initDatabaseSchema(dbConnection);
}

void MixxxMainWindow::initializeWindow() {
// be sure createMenuBar() is called first
DEBUG_ASSERT(m_pMenuBar != nullptr);
Expand Down

0 comments on commit 40c27aa

Please sign in to comment.