-
Notifications
You must be signed in to change notification settings - Fork 3
/
m6_main.cpp
153 lines (121 loc) · 3.12 KB
/
m6_main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <QtCore/QCoreApplication>
#include <QVariant>
#include "m6_main.h"
#include "helper.h"
#include <signal.h>
#include <MafwShared.h>
#include <MafwRegistry.h>
#include <MafwTrackerModelFactory.h>
#include <MafwTrackerModelConnection.h>
Listener::Listener(QObject *parent)
{
setParent(parent);
}
bool Listener::init(void)
{
registry = MafwRegistry::instance();
shared = MafwShared::instance();
shared->initTracking(registry);
renderer = registry->renderer("mafw_gst_renderer");
if (!connect(renderer, SIGNAL(mediaChanged(int, const MafwContent&)),
this, SLOT(next(void))))
goto fail;
if (!connect(renderer, SIGNAL(stateChanged(MafwRenderer::State)),
this, SLOT(state_changed(MafwRenderer::State))))
goto fail;
if (!connect(renderer, SIGNAL(metadataChanged(const QString&, const QList<QVariant>&)),
this, SLOT(metadata_changed(const QString&, const QList<QVariant>&))))
goto fail;
tk_factory = new MafwTrackerModelFactory();
tk_factory->init();
tk_conn = tk_factory->trackerConnection();
if (!connect(tk_conn, SIGNAL(musicFavorited(const QSet<int>&)),
this, SLOT(favorited(const QSet<int>&))))
goto tk_fail;
if (!connect(tk_conn, SIGNAL(musicUnfavorited(const QSet<int>&)),
this, SLOT(unfavorited(const QSet<int>&))))
goto tk_fail;
return true;
fail:
QCoreApplication::exit(-1);
return false;
tk_fail:
return false;
}
void Listener::next(void)
{
hp_next();
}
void Listener::state_changed(MafwRenderer::State state)
{
switch (state) {
case MafwRenderer::Playing:
hp_next();
break;
case MafwRenderer::Stopped:
case MafwRenderer::Paused:
hp_stop();
break;
default:
break;
}
}
void Listener::metadata_changed(const QString& name, const QList<QVariant>& values)
{
QVariant value = values[0];
if (name == "artist")
hp_set_artist(value.toString().toUtf8());
else if (name == "title")
hp_set_title(value.toString().toUtf8());
else if (name == "duration")
hp_set_length(value.toLongLong());
else if (name == "album")
hp_set_album(value.toString().toUtf8());
}
static const QString ID_QUERY =
"SELECT nmm:artistName(nmm:performer(?song)) nie:title(?song) %1 "
"WHERE { ?song a nmm:MusicPiece . FILTER( tracker:id(?song) IN (%2) ) }";
void Listener::set_favorite(const QSet<int>& ids, bool on)
{
QStringList list;
foreach(int id, ids) {
list << QString::number(id);
}
tk_conn->queueQuery(ID_QUERY.arg(QString::number(on), list.join(",")),
3, this, SLOT(got_info(QList<QStringList>,bool)), NULL);
}
void Listener::favorited(const QSet<int>& ids)
{
set_favorite(ids, true);
}
void Listener::unfavorited(const QSet<int>& ids)
{
set_favorite(ids, false);
}
void Listener::got_info(QList<QStringList> rows, bool foo)
{
foreach(QStringList row, rows) {
hp_love(row[0].toUtf8(), row[1].toUtf8(), row[2].toInt());
}
}
static void signal_handler(int signal)
{
QCoreApplication::exit(0);
}
int main(int argc, char *argv[])
{
int r;
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
hp_init();
QCoreApplication a(argc, argv);
Listener listener(&a);
if (!listener.init()) {
r = -1;
goto leave;
}
r = a.exec();
leave:
hp_deinit();
return r;
};