forked from glKarin/cloudmusicqt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqmlapi.cpp
More file actions
328 lines (275 loc) · 8.26 KB
/
qmlapi.cpp
File metadata and controls
328 lines (275 loc) · 8.26 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "qmlapi.h"
#include <QDateTime>
#include <QApplication>
#include <QPixmap>
#include <QDesktopServices>
#include <QFile>
#include <QFileDialog>
#include <QDebug>
#include <QClipboard>
#ifdef Q_OS_SYMBIAN
#ifndef Q_OS_S60V5
#include <akndiscreetpopup.h>
#endif
#include <avkon.hrh>
#include <gslauncher.h>
#include <gsfwviewuids.h>
#endif
#include "networkaccessmanagerfactory.h"
#include "userconfig.h"
#include "musicfetcher.h"
#include "qjson/json_parser.hh"
#include "qrcodegen/qrcodegen.h"
#ifdef PIGLER_API
#include <QtSvg/qsvgrenderer>
QmlApi::QmlApi(QObject *parent) : QObject(parent),
mParser(new QJson::Parser),
api(new QPiglerAPI(this)),
uid(0)
{
qint32 response = api->init("cloudmusicqt");
// 如果软件闪退,那么已有的Notification就会残留
// 再次启动的时候就会跑出来
// 怎么办?只有殺!
api->removeAllNotifications();
if (response < 0) {
// api actually unavailable, but invoke related function won't cause any exception
} else {
// api successfully initialized
}
}
#else
QmlApi::QmlApi(QObject *parent) : QObject(parent),
mParser(new QJson::Parser)
{
}
#endif
QmlApi::~QmlApi()
{
delete mParser;
}
QString QmlApi::getCookieToken()
{
QList<QNetworkCookie> cookies = NetworkCookieJar::Instance()->cookiesForUrl(QUrl("http://music.163.com"));
foreach (const QNetworkCookie& cookie, cookies) {
if (cookie.name() == "MUSIC_U" && cookie.expirationDate() > QDateTime::currentDateTime()) {
return cookie.value();
}
}
return QString();
}
QString QmlApi::getUserId()
{
return UserConfig::Instance()->getSetting(UserConfig::KeyUserId).toString();
}
void QmlApi::saveUserId(const QString &id)
{
UserConfig::Instance()->setSetting(UserConfig::KeyUserId, id);
}
void QmlApi::logout()
{
saveUserId(QString());
NetworkCookieJar::Instance()->clearCookies();
}
int QmlApi::getVolume()
{
return qBound(0, UserConfig::Instance()->getSetting(UserConfig::KeyVolume, 30).toInt(), 100);
}
void QmlApi::saveVolume(const int &volume)
{
UserConfig::Instance()->setSetting(UserConfig::KeyVolume, qBound(0, volume, 100));
}
QString QmlApi::getPlayMode()
{
return UserConfig::Instance()->getSetting(UserConfig::KeyPlayMode, "Normal").toString();
}
void QmlApi::savePlayMode(const QString &playMode)
{
UserConfig::Instance()->setSetting(UserConfig::KeyPlayMode, playMode);
}
void QmlApi::takeScreenShot()
{
QPixmap p = QPixmap::grabWidget(QApplication::activeWindow());
QString fileName = QString("%1/%2_%3.png")
.arg(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation),
qApp->applicationName(),
QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"));
p.save(fileName);
}
void QmlApi::showNotification(const QString &title, const QString &text, const int &commandId)
{
#ifdef Q_OS_SYMBIAN
#ifndef Q_OS_S60V5
TPtrC16 sTitle(static_cast<const TUint16 *>(title.utf16()), title.length());
TPtrC16 sText(static_cast<const TUint16 *>(text.utf16()), text.length());
TUid uid = TUid::Uid(0x2006DFF5);
TRAP_IGNORE(CAknDiscreetPopup::ShowGlobalPopupL(
sTitle,
sText,
KAknsIIDNone,
KNullDesC,
0,
0,
KAknDiscreetPopupDurationLong,
commandId,
this,
uid ));
#endif
#else
qDebug() << "showNotification:" << title << text << commandId;
#endif
}
static const uint MaxAccurateNumberInQML = 65535;
static QVariant fixVariant(const QVariant& variant)
{
QVariant result(variant);
if (result.type() == QVariant::ULongLong) {
quint64 value = result.toULongLong();
if (value > MaxAccurateNumberInQML)
result.convert(QVariant::String);
else
result.convert(QVariant::Int);
}
else if (result.type() == QVariant::Map) {
QVariantMap map = result.toMap();
for (QVariantMap::iterator i = map.begin(); i != map.end(); i++) {
i.value() = fixVariant(i.value());
}
result = map;
}
else if (result.type() == QVariant::List) {
QVariantList list = result.toList();
for (QVariantList::iterator i = list.begin(); i != list.end(); i++) {
*i = fixVariant(*i);
}
result = list;
}
return result;
}
QVariant QmlApi::jsonParse(const QString &text)
{
return fixVariant(mParser->parse(text.toUtf8()));
}
bool QmlApi::compareVariant(const QVariant &left, const QVariant &right)
{
return left == right;
}
QString QmlApi::getNetEaseImageUrl(const QString &imgId)
{
return MusicInfo::getPictureUrl(imgId.toAscii());
}
bool QmlApi::isFileExists(const QString &fileName)
{
return QFile::exists(fileName);
}
bool QmlApi::removeFile(const QString &fileName)
{
return QFile::remove(fileName);
}
QString QmlApi::cleanPath(const QString &path)
{
return QDir::cleanPath(path);
}
QString QmlApi::getHomePath() const
{
return QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
}
QString QmlApi::selectFolder(const QString &title, const QString &defaultDir)
{
return QFileDialog::getExistingDirectory(0, title, defaultDir);
}
bool QmlApi::showAccessPointTip()
{
return UserConfig::Instance()->getSetting(UserConfig::KeyShowAccessPointTip, true).toBool();
}
void QmlApi::clearAccessPointTip()
{
UserConfig::Instance()->setSetting(UserConfig::KeyShowAccessPointTip, false);
}
void QmlApi::launchSettingApp()
{
#ifdef Q_OS_SYMBIAN
CGSLauncher* l = CGSLauncher::NewLC();
l->LaunchGSViewL ( KGSAppsPluginUid, TUid::Uid(0), KNullDesC8 );
CleanupStack::PopAndDestroy(l);
#endif
}
#ifdef Q_OS_SYMBIAN
void QmlApi::ProcessCommandL(TInt aCommandId)
{
emit processCommand(aCommandId);
}
#endif
//karin
//other
void QmlApi::CopyToClipboard(const QString &text)
{
QApplication::clipboard()->setText(text);
}
QString QmlApi::generateSvgQrCode(QString string)
{
const int border = 4;
const QString lightColor = "#FFFFFF";
const QString darkColor = "#000000";
uint8_t qr0[qrcodegen_BUFFER_LEN_MAX];
uint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];
const char* source = string.toLatin1().data();
bool ok = qrcodegen_encodeText(source,
tempBuffer, qr0, qrcodegen_Ecc_MEDIUM,
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX,
qrcodegen_Mask_AUTO, true);
if (!ok)
{
return "";
}
QString temp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"><svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 %1 %1\" stroke=\"none\"><rect width=\"100%\" height=\"100%\" fill=\"%2\"/><path d=\"%3\" fill=\"%4\"/></svg>";
QString part = "";
int size = qrcodegen_getSize(qr0);
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if(qrcodegen_getModule(qr0, x, y))
{
part.append(QString("M%1,%2h1v1h-1z ").arg(QString::number(x + border)).arg(QString::number(y + border)));
}
}
}
return "data:image/svg+xml;utf8," + temp.arg(QString::number(size + border * 2), lightColor, part, darkColor);
}
QString QmlApi::getChineseWeekday()
{
// 星期七
char* weekdayChar[] = {"一", "二", "三", "四", "五", "六", "日"};
QDate date = QDate::currentDate();
QString weekday = QString::fromUtf8(weekdayChar[date.dayOfWeek()-1]);
return QString::fromUtf8("星期").append(weekday);
}
#ifdef PIGLER_API
void QmlApi::showStatusPanelNotification(QString text)
{
if (!api) return;
if (uid != 0) {
api->updateNotification(uid, QString::fromUtf8("网易云音乐"), text);
} else {
uid = api->createNotification(QString::fromUtf8("网易云音乐"), text);
api->setRemoveOnTap(uid, false);
api->setNotificationIcon(uid, QImage("qml/cloudmusicqt/gfx/notification.png"));
}
}
void QmlApi::hideStatusPanelNotification()
{
if (!api) return;
if (uid == 0) return;
api->removeNotification(uid);
}
bool QmlApi::isPiglerAPIAvailable()
{
return true;
}
#else
bool QmlApi::isPiglerAPIAvailable()
{
return false;
}
#endif