]> git.sur5r.net Git - minitube/blob - src/appwidget.cpp
New upstream version 3.1
[minitube] / src / appwidget.cpp
1 #include "appwidget.h"
2 #include "constants.h"
3 #include "http.h"
4 #ifdef APP_EXTRA
5 #include "updatedialog.h"
6 #endif
7
8 AppsWidget::AppsWidget(QWidget *parent) : QWidget(parent) {
9     const int padding = 30;
10
11     QBoxLayout *layout = new QHBoxLayout(this);
12     layout->setMargin(padding);
13     layout->setSpacing(padding*2);
14     layout->setAlignment(Qt::AlignCenter);
15
16 #ifdef APP_MAC
17     const QString ext = "dmg";
18 #elif defined APP_WIN
19     const QString ext = "exe";
20 #else
21     const QString ext = "deb";
22 #endif
23
24     setupApp("Finetune", "finetune." + ext);
25     setupApp("Musictube", "musictube." + ext);
26     setupApp("Musique", "musique." + ext);
27 }
28
29 void AppsWidget::setupApp(const QString &name, const QString &code) {
30     AppWidget *w = new AppWidget(name, code);
31     layout()->addWidget(w);
32 }
33
34 void AppsWidget::paintEvent(QPaintEvent *e) {
35     Q_UNUSED(e);
36     QStyleOption o;
37     o.initFrom(this);
38     QPainter p(this);
39     style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
40 }
41
42 AppWidget::AppWidget(const QString &name, const QString &code, QWidget *parent) : QWidget(parent), icon(0), name(name), downloadButton(0) {
43     const QString unixName = code.left(code.lastIndexOf('.'));
44     const QString baseUrl = QLatin1String("https://") + Constants::ORG_DOMAIN;
45     const QString filesUrl = baseUrl + QLatin1String("/files/");
46     url = filesUrl + unixName + QLatin1String("/") + code;
47     webPage = baseUrl + QLatin1String("/") +  unixName;
48
49     QBoxLayout *layout = new QVBoxLayout(this);
50     layout->setMargin(0);
51     layout->setAlignment(Qt::AlignHCenter);
52
53     icon = new QLabel();
54     icon->setMinimumHeight(128);
55     layout->addWidget(icon);
56     const QString iconUrl = filesUrl + QLatin1String("products/") + unixName + QLatin1String(".png");
57     QObject *reply = Http::instance().get(iconUrl);
58     connect(reply, SIGNAL(data(QByteArray)), SLOT(iconDownloaded(QByteArray)));
59
60     QLabel *appTitle = new QLabel(name);
61     appTitle->setAlignment(Qt::AlignHCenter);
62     layout->addWidget(appTitle);
63
64 #ifdef APP_EXTRA
65 #if !defined(APP_UBUNTU) && !defined(APP_MAC_STORE)
66     downloadButton = new QPushButton(tr("Download"));
67     downloadButton->setAttribute(Qt::WA_MacSmallSize);
68     downloadButton->setCursor(Qt::ArrowCursor);
69     QSizePolicy sp = downloadButton->sizePolicy();
70     sp.setHorizontalPolicy(QSizePolicy::Fixed);
71     sp.setRetainSizeWhenHidden(true);
72     downloadButton->setSizePolicy(sp);
73     connect(downloadButton, SIGNAL(clicked(bool)), SLOT(downloadApp()));
74     layout->addWidget(downloadButton, Qt::AlignHCenter);
75     layout->setAlignment(downloadButton, Qt::AlignHCenter);
76     downloadButton->hide();
77 #endif
78 #endif
79
80     setCursor(Qt::PointingHandCursor);
81 }
82
83 void AppWidget::enterEvent(QEvent *e) {
84     Q_UNUSED(e);
85     if (downloadButton) downloadButton->show();
86 }
87
88 void AppWidget::leaveEvent(QEvent *e) {
89     Q_UNUSED(e);
90     if (downloadButton) downloadButton->hide();
91 }
92
93 void AppWidget::mouseReleaseEvent(QMouseEvent *e) {
94     if (e->button() == Qt::LeftButton) {
95         QDesktopServices::openUrl(webPage);
96     }
97 }
98
99 void AppWidget::iconDownloaded(const QByteArray &bytes) {
100     QPixmap pixmap;
101     pixmap.loadFromData(bytes, "PNG");
102     icon->setPixmap(pixmap);
103 }
104
105 void AppWidget::downloadApp() {
106 #ifdef APP_EXTRA
107     if (!icon) return;
108     UpdateDialog *dialog = new UpdateDialog(icon->pixmap(), name, QString(), url, this);
109     dialog->downloadUpdate();
110     dialog->show();
111 #endif
112 }