summaryrefslogtreecommitdiffstats
path: root/beetplayer.cpp
blob: c846c8323dcfcfeb63744957a865b0b4dd2f55d1 (plain)
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
#include <QSettings>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QApplication>
#include <QHBoxLayout>
#include <QAction>
#include <QMenuBar>

#include "beetplayer.h"
#include "configurationdialog.h"
#include "indexerwidget.h"
#include "playerwidget.h"
#include "globals.h"

BeetPlayer::BeetPlayer(QWidget *parent, Qt::WindowFlags f) : QMainWindow(parent, f) {
    //general setup
    qApp->setWindowIcon(QIcon(":/beetplayer32.png"));
    setMinimumWidth(1024);
    setMinimumHeight(768);
    openDatabase();
    createGlobalActions();

    //tabs
    mTab = new QTabWidget;
    IndexerWidget *indexer = new IndexerWidget;
    PlayerWidget *player = new PlayerWidget;
    mTab->addTab(player, tr("Player"));
    mTab->addTab(indexer, tr("Indexer"));
    connect(mTab, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
    menuBar()->clear();
    foreach(QMenu* m, indexer->menus()){
        menuBar()->addMenu(m);
    }

    //layout
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(mTab);
    setCentralWidget(mTab);
}

void BeetPlayer::tabChanged(int tab){
    if(tab == 0){ //replace with indexer
        //do something;;
    }
}

void BeetPlayer::openDatabase(){
    QSettings s;
    QString dbhost = s.value("dbhost").toString();
    QString dbuser = s.value("dbuser").toString();
    QString dbpass = s.value("dbpass").toString();
    QString dbname = s.value("dbname").toString();
    if(!QSqlDatabase::contains("beetplayerdb")){
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "beetplayerdb");
        db.setHostName(dbhost);
        db.setUserName(dbuser);
        db.setPassword(dbpass);
        db.setDatabaseName(dbname);
        if(!db.open()){
            int res = ConfigurationDialog(this).exec();
            if(res == QDialog::Accepted){
                openDatabase();
            }else{
                QMessageBox::critical(this, tr("Error"), tr("Could not open database. Giving up!"));
                qApp->closeAllWindows();
            }
        }
    }
}

void BeetPlayer::createGlobalActions(){
    QAction *quitA = new QAction(tr("Quit"), this);
    quitA->setShortcut(tr("CTRL+Q"));
    connect(quitA, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
    quitA->setData(Globals::QuitAction);
    Globals::instance()->addAction(quitA);
    QAction *configA = new QAction(QIcon(":/chastity_belt.png"), tr("Configure..."), this);
    connect(configA, SIGNAL(triggered()), this, SLOT(configure()));
    configA->setData(Globals::ConfigAction);
    Globals::instance()->addAction(configA);
}

void BeetPlayer::configure(){
    ConfigurationDialog w(this);
    int res = w.exec();
    if(res == QDialog::Accepted){
        openDatabase();
    }
}