-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.cpp
131 lines (103 loc) · 4.31 KB
/
sqlite.cpp
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
#include <mainwindow.h>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlDatabase>
#include <QDebug>
#include <QStandardPaths>
#include <QDir>
#include <QFile>
bool MainWindow::savePlaylist()
{
return savePlaylist(_playlistTabs->currentIndex());
}
bool MainWindow::savePlaylist(const int & index)
{
QString * defaultMusicDirectory = new QString(QStandardPaths::locate(QStandardPaths::MusicLocation, "", QStandardPaths::LocateDirectory));
QDir * playlistDirectory = new QDir((*defaultMusicDirectory + "Playlists"));
if(!playlistDirectory->exists()) playlistDirectory->mkdir(playlistDirectory->absolutePath());
QStringList returnedFileNames = _openFileDialog(new QString("SQLite Database (*.db)"),new QString(playlistDirectory->absolutePath()));
QFile * databaseFile = new QFile;
if(returnedFileNames.count())
{
if(returnedFileNames.at(0).endsWith(".db")) databaseFile->setFileName(returnedFileNames.at(0));
else databaseFile->setFileName(returnedFileNames.at(0) + ".db");
}
else return false;
if(databaseFile->exists())
{
qDebug() << "File already exists!";
//FILE ALREADY EXISTS
return false;
}
else return writePlaylist(databaseFile, index);
}
bool MainWindow::writePlaylist(const QFile * playlistLocation, const int & index)
{
QSqlDatabase * playlistDatabase = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE",playlistLocation->fileName()));
playlistDatabase->setDatabaseName(playlistLocation->fileName());
bool ok = playlistDatabase->open();
if(ok)
{
QSqlQuery * dbquery = new QSqlQuery(*playlistDatabase);
dbquery->exec("CREATE TABLE audiofiles(TITLE TEXT UNIQUE)");
for(int i = 0; i < _playlistViews->at(index)->count(); i++)
{
QString audioFilePath = _players->at(index)->playlist()->media(i).canonicalUrl().url();
if(audioFilePath.contains("file:"))
audioFilePath.remove(0,7);
QString query = (
"INSERT INTO audiofiles VALUES (\"" +
audioFilePath +
"\")");
dbquery->exec(query);
}
playlistDatabase->close();
return true;
}
else return false;
}
const QStringList * MainWindow::readPlaylist(const QFile * playlistLocation)
{
QStringList * playlistFiles = new QStringList;
QSqlDatabase * playlistDatabase = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE",playlistLocation->fileName()));
playlistDatabase->setDatabaseName(playlistLocation->fileName());
bool ok = playlistDatabase->open();
if(ok)
{
QSqlQuery * dbquery = new QSqlQuery(*playlistDatabase);
QString query = "SELECT * FROM audiofiles";
dbquery->exec(query);
while(dbquery->next()) playlistFiles->push_back(dbquery->value(0).toString());
playlistDatabase->close();
}
return playlistFiles;
}
bool MainWindow::loadPlaylist()
{
return loadPlaylist(_playlistTabs->currentIndex());
}
bool MainWindow::loadPlaylist(const int & index)
{
if(_playlistViews->at(index)->count())
{
//There's already stuff in the playlist!
return false;
}
QString * defaultMusicDirectory = new QString(QStandardPaths::locate(QStandardPaths::MusicLocation, "", QStandardPaths::LocateDirectory));
QDir * playlistDirectory = new QDir((*defaultMusicDirectory + "Playlists"));
if(!playlistDirectory->exists()) playlistDirectory->mkdir(playlistDirectory->absolutePath());
QStringList returnedFileNames = _openFileDialog(new QString("SQLite Database (*.db)"),new QString(playlistDirectory->absolutePath()));
QFile * databaseFile = new QFile;
if(returnedFileNames.count())
databaseFile->setFileName(returnedFileNames.at(0));
else return false;
const QStringList * playlistFiles = readPlaylist(databaseFile);
QList<QMediaContent> playlistMediaFiles;
for(QStringList::const_iterator file = playlistFiles->constBegin(); file < playlistFiles->constEnd(); file++)
playlistMediaFiles.append(QMediaContent(QUrl::fromLocalFile(*file)));
_players->at(index)->stop();
_players->at(index)->playlist()->clear();
_players->at(index)->playlist()->addMedia(playlistMediaFiles);
_players->at(index)->playlist()->setCurrentIndex(0);
refreshPlaylistView();
return true;
}